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 ICartService CartSvc { get; } public CartController(ICartService cartSvc) { this.CartSvc = cartSvc; } [HttpGet] public async Task>> Get() { return await CartSvc.Get(); } [HttpGet("{customerId}")] public async Task>> GetFromCustomer(Guid customerId) { return await CartSvc.GetFromCustomer(customerId); } [HttpPost] public async Task> 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> 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; } } }