StoreWebApi: ProductsController.cs
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.EntityFrameworkCore; | |
using StoreWebApi.Models; | |
using AutoMapper; | |
using StoreWebApi.DTOs; | |
namespace StoreWebApi.Controllers | |
{ | |
[Produces("application/json")] | |
[Route("api/Products")] | |
public class ProductsController : Controller | |
{ | |
private readonly StoreDBContext _context; | |
public ProductsController(StoreDBContext context) | |
{ | |
_context = context; | |
} | |
// GET: api/Products | |
[HttpGet] | |
public IEnumerable<ProductDTO> GetProduct() | |
{ | |
return Mapper.Map<IEnumerable<ProductDTO>>(_context.Product.OrderBy(x => x.Name)); | |
} | |
// GET: api/Products/5 | |
[HttpGet("{id}")] | |
public async Task<IActionResult> GetProduct([FromRoute] int id) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
var product = await _context.Product.SingleOrDefaultAsync(m => m.Id == id); | |
if (product == null) | |
{ | |
return NotFound(); | |
} | |
return Ok(Mapper.Map<ProductDTO>(product)); | |
} | |
// PUT: api/Products/5 | |
[HttpPut("{id}")] | |
public async Task<IActionResult> PutProduct([FromRoute] int id, [FromBody] ProductDTO product) | |
{ | |
product.OrderDetail = null; | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
if (id != product.Id) | |
{ | |
return BadRequest(); | |
} | |
_context.Entry(Mapper.Map<Product>(product)).State = EntityState.Modified; | |
try | |
{ | |
await _context.SaveChangesAsync(); | |
} | |
catch (DbUpdateConcurrencyException) | |
{ | |
if (!ProductExists(id)) | |
{ | |
return NotFound(); | |
} | |
else | |
{ | |
throw; | |
} | |
} | |
return NoContent(); | |
} | |
// POST: api/Products | |
[HttpPost] | |
public async Task<IActionResult> PostProduct([FromBody] ProductDTO product) | |
{ | |
product.OrderDetail = null; | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
var p = Mapper.Map<Product>(product); | |
_context.Product.Add(p); | |
await _context.SaveChangesAsync(); | |
product.Id = p.Id; | |
return CreatedAtAction("GetProduct", new { id = p.Id }, product); | |
} | |
// DELETE: api/Products/5 | |
[HttpDelete("{id}")] | |
public async Task<IActionResult> DeleteProduct([FromRoute] int id) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
var product = await _context.Product.SingleOrDefaultAsync(m => m.Id == id); | |
if (product == null) | |
{ | |
return NotFound(); | |
} | |
_context.Product.Remove(product); | |
await _context.SaveChangesAsync(); | |
return Ok(Mapper.Map<ProductDTO>(product)); | |
} | |
private bool ProductExists(int id) | |
{ | |
return _context.Product.Any(e => e.Id == id); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment