Skip to content

Instantly share code, notes, and snippets.

@ouzdev
Created July 18, 2021 12:33
Show Gist options
  • Save ouzdev/e28783c9f7f660a6a1f39917007afdf2 to your computer and use it in GitHub Desktop.
Save ouzdev/e28783c9f7f660a6a1f39917007afdf2 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
[Route("api/[controller]")]
public class ProductsController:Controller
{
private readonly ProductDbContext _context;
public ProductsController(ProductDbContext context)
{
_context = context;
}
[HttpGet]
public async Task<IEnumerable<Product>> GetProducts(){
return await _context.Products.ToListAsync();
}
[HttpPost]
public async void AddProduct(ProductDto model){
await _context.Products.AddAsync(new Product{Name = model.Name,UnitPrice = model.UnitPrice});
await _context.SaveChangesAsync();
}
[HttpPut("{id}")]
public async void UpdateProduct(int id ,ProductDto model)
{
var result = await _context.Products.FindAsync(id);
result.Name = model.Name;
result.UnitPrice = model.UnitPrice;
await _context.SaveChangesAsync();
}
[HttpDelete("{id}")]
public async void DeleteProduct(int id){
var result = await _context.Products.FindAsync(id);
_context.Remove(result);
await _context.SaveChangesAsync();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment