
The main server owns the migrations, it's the server part, it's simpler. It's Yavsc, not one of its lib.
148 lines
3.8 KiB
C#
148 lines
3.8 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Yavsc.Helpers;
|
|
using Yavsc.Models;
|
|
using Yavsc.Models.Market;
|
|
using Yavsc.Server.Helpers;
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|