Created
July 3, 2021 18:43
-
-
Save ouzdev/0e98fbe56ea3a43ce5c3524e865dabd4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.EntityFrameworkCore; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
var builder = WebApplication.CreateBuilder(args); | |
builder.Services.AddDbContext<ProductDbContext>(options => options.UseInMemoryDatabase("Products")); | |
await using var app = builder.Build(); | |
if (app.Environment.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
} | |
app.MapGet("/", (Func<string>)(() => "Hello World!")); | |
#region Ürün Getirme End-Point | |
app.MapGet("/products", async (http) => { | |
var dbContext = http.RequestServices.GetService<ProductDbContext>(); | |
var productItems = await dbContext.Products.ToListAsync(); | |
await http.Response.WriteAsJsonAsync(productItems); | |
}); | |
#endregion | |
#region Ürün Ekleme End-Point | |
app.MapPost("/products", async(http) => { | |
var productItem = await http.Request.ReadFromJsonAsync<Product>(); | |
var dbContext = http.RequestServices.GetService<ProductDbContext>(); | |
await dbContext.Products.AddAsync(productItem); | |
await dbContext.SaveChangesAsync(); | |
await http.Response.WriteAsJsonAsync("Ürün Eklendi"); | |
}); | |
#endregion | |
#region Ürün Güncelleştirme End-Point | |
app.MapPut("/products/{id}", async(http) => { | |
if(!http.Request.RouteValues.TryGetValue("id",out var id)){ | |
http.Response.StatusCode=400; | |
return; | |
} | |
var dbContext = http.RequestServices.GetService<ProductDbContext>(); | |
var productItem = await dbContext.Products.FindAsync(int.Parse(id.ToString())); | |
if(productItem ==null){ | |
http.Response.StatusCode = 404; | |
return; | |
} | |
var inputProductItem = await http.Request.ReadFromJsonAsync<Product>(); | |
productItem.UnitPrice = inputProductItem.UnitPrice; | |
productItem.Name = inputProductItem.Name; | |
await dbContext.SaveChangesAsync(); | |
http.Response.StatusCode = 204; | |
}); | |
#endregion | |
#region Ürün Silme End-Point | |
app.MapDelete("/products/{id}", async (http) => { | |
if(!http.Request.RouteValues.TryGetValue("id",out var id)){ | |
http.Response.StatusCode=400; | |
return; | |
} | |
var dbContext = http.RequestServices.GetService<ProductDbContext>(); | |
var productItem = await dbContext.Products.FindAsync(int.Parse(id.ToString())); | |
if(productItem == null){ | |
http.Response.StatusCode=404; | |
await http.Response.WriteAsJsonAsync("Ürün Bulunamadı."); | |
return; | |
} | |
dbContext.Products.Remove(productItem); | |
await dbContext.SaveChangesAsync(); | |
http.Response.StatusCode = 204; | |
await http.Response.WriteAsJsonAsync("Ürün Başarıyla Silindi"); | |
}); | |
#endregion | |
await app.RunAsync(); | |
public class ProductDbContext:DbContext{ | |
public ProductDbContext(DbContextOptions context):base(context) | |
{ | |
} | |
public DbSet<Product> Products{get;set;} | |
} | |
public class Product | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public double UnitPrice { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment