WIP separation Web et API
This commit is contained in:
146
src/Api/Controllers/Business/ProductApiController.cs
Normal file
146
src/Api/Controllers/Business/ProductApiController.cs
Normal file
@ -0,0 +1,146 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Yavsc.Helpers;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Models.Market;
|
||||
|
||||
namespace Yavsc.Controllers
|
||||
{
|
||||
[Produces("application/json")]
|
||||
[Route("api/ProductApi")]
|
||||
public class ProductApiController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public ProductApiController(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// GET: api/ProductApi
|
||||
[HttpGet]
|
||||
public IEnumerable<Product> GetProducts()
|
||||
{
|
||||
return _context.Products;
|
||||
}
|
||||
|
||||
// GET: api/ProductApi/5
|
||||
[HttpGet("{id}", Name = "GetProduct")]
|
||||
public IActionResult GetProduct([FromRoute] long id)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
Product product = _context.Products.Single(m => m.Id == id);
|
||||
|
||||
if (product == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return Ok(product);
|
||||
}
|
||||
|
||||
// PUT: api/ProductApi/5
|
||||
[HttpPut("{id}"),Authorize(Constants.FrontOfficeGroupName)]
|
||||
public IActionResult PutProduct(long id, [FromBody] Product product)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
if (id != product.Id)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
_context.Entry(product).State = EntityState.Modified;
|
||||
|
||||
try
|
||||
{
|
||||
_context.SaveChanges(User.GetUserId());
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!ProductExists(id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return new StatusCodeResult(StatusCodes.Status204NoContent);
|
||||
}
|
||||
|
||||
// POST: api/ProductApi
|
||||
[HttpPost,Authorize(Constants.FrontOfficeGroupName)]
|
||||
public IActionResult PostProduct([FromBody] Product product)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
_context.Products.Add(product);
|
||||
try
|
||||
{
|
||||
_context.SaveChanges(User.GetUserId());
|
||||
}
|
||||
catch (DbUpdateException)
|
||||
{
|
||||
if (ProductExists(product.Id))
|
||||
{
|
||||
return new StatusCodeResult(StatusCodes.Status409Conflict);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return CreatedAtRoute("GetProduct", new { id = product.Id }, product);
|
||||
}
|
||||
|
||||
// DELETE: api/ProductApi/5
|
||||
[HttpDelete("{id}"),Authorize(Constants.FrontOfficeGroupName)]
|
||||
public IActionResult DeleteProduct(long id)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
Product product = _context.Products.Single(m => m.Id == id);
|
||||
if (product == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
_context.Products.Remove(product);
|
||||
_context.SaveChanges(User.GetUserId());
|
||||
|
||||
return Ok(product);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_context.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private bool ProductExists(long id)
|
||||
{
|
||||
return _context.Products.Count(e => e.Id == id) > 0;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user