103 lines
2.9 KiB
C#
103 lines
2.9 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using TestAppRuna.Entities;
|
|
using TestAppRuna.Services;
|
|
|
|
namespace TestAppRuna.API
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class CartController : ControllerBase
|
|
{
|
|
public CartService CartSvc { get; }
|
|
|
|
public CartController(CartService cartSvc)
|
|
{
|
|
this.CartSvc = cartSvc;
|
|
}
|
|
|
|
|
|
[HttpGet]
|
|
public async Task<ActionResult<List<CartCustomerListItem>>> Get()
|
|
{
|
|
return await CartSvc.Get();
|
|
}
|
|
[HttpGet("{customerId}")]
|
|
public async Task<ActionResult<List<CartListItem>>> GetFromCustomer(Guid customerId)
|
|
{
|
|
return await CartSvc.GetFromCustomer(customerId);
|
|
}
|
|
[HttpPost]
|
|
public async Task<ActionResult<bool>> Post([FromBody] CartCreateUpdateModel model)
|
|
{
|
|
if (await CartSvc.HasCustomerID(model.CustomerID))
|
|
{
|
|
ModelState.AddModelError("CustomerID", "Customer ID not found");
|
|
}
|
|
if(await CartSvc.HasProductID(model.ProductID))
|
|
{
|
|
ModelState.AddModelError("ProductID", "Product ID not found");
|
|
}
|
|
|
|
if(ModelState.IsValid == false)
|
|
{
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
return await CartSvc.CreateOrUpdateCart(model);
|
|
}
|
|
|
|
[HttpPost("delete")]
|
|
public async Task<ActionResult<bool>> Delete([FromBody] CartDeleteModel model)
|
|
{
|
|
var success = await CartSvc.DeleteCart(model);
|
|
if(success == false)
|
|
{
|
|
return NotFound();
|
|
} else
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
public class CartListItem {
|
|
public Guid ProductID { get; set; }
|
|
public string ProductName { get; set; }
|
|
public decimal ProductPrice { get; set; }
|
|
public int Quantity { get; set; }
|
|
}
|
|
public class CartCustomerListItem
|
|
{
|
|
public Guid ProductID { get; set; }
|
|
public string ProductName { get; set; }
|
|
public decimal ProductPrice { get; set; }
|
|
public string CustomerName { get; set; }
|
|
public string CustomerEmail { get; set; }
|
|
public int Quantity { get; set; }
|
|
}
|
|
public class CartCreateUpdateModel
|
|
{
|
|
[Required]
|
|
public Guid CustomerID { get; set; }
|
|
[Required]
|
|
public Guid ProductID { get; set; }
|
|
[Required]
|
|
[Range(1,Double.MaxValue)]
|
|
public int Quantity { get; set; }
|
|
}
|
|
public class CartDeleteModel
|
|
{
|
|
[Required]
|
|
public Guid CustomerID { get; set; }
|
|
[Required]
|
|
public Guid ProductID { get; set; }
|
|
}
|
|
|
|
}
|