public ActionResult Register(RegisterDto register) { if (register == null || register.Address == null) { return(new BadRequestResult()); } var client = new Client { Email = register.Email, FirstName = register.FirstName, LastName = register.LastName, Password = Hash.GetHash(register.Password), Address = new Address() { City = register.Address.City, StreetAndHouseNumber = register.Address.StreetAndHouseNumber, PostCode = register.Address.PostCode }, HoneysInTheCart = new List <HoneyItem>(), }; if (!_context.Clients.Any(x => x.Email == client.Email)) { _context.Clients.Add(client); _context.SaveChanges(); return(new OkResult()); } return(new ConflictResult()); }
public ActionResult NewOrder(OrderDto order) { if (order == null) { return(new BadRequestResult()); } if (!_context.Clients.Any(x => x.Id == order.ClientId)) { return(new NotFoundResult()); } List <OrderedHoney> orderedHoneys = new List <OrderedHoney>(); foreach (var honey in order.OrderedHoneys) { //check if is in the warehouse int amountInWarehouse = _context.HoneysInTheWarehouse.Where(x => x.Name == honey.Name).FirstOrDefault().Amount; if (amountInWarehouse - honey.Amount < 0) { return(new ConflictResult()); } orderedHoneys.Add(new OrderedHoney() { Name = honey.Name, Price = honey.Price, Amount = honey.Amount }); } Order newOrder = new Order() { ClientId = order.ClientId, OrderedHoneys = orderedHoneys, TotalPrice = order.TotalPrice, Delivery = order.Delivery, Payment = order.Payment, Status = Status.New, Date = order.Date }; _context.Orders.Add(newOrder); var cart = _context.HoneysInTheCart.Where(x => x.ClientId == order.ClientId); _context.HoneysInTheCart.RemoveRange(cart); _context.SaveChanges(); UpdateWarehouse(-1, newOrder.Id); return(new OkResult()); }
public IActionResult AddHoney(HoneyInTheWarehouseDto honey) { if (honey == null) { return(new BadRequestResult()); } if (_context.HoneysInTheWarehouse.Where(x => x.Name == honey.Name).Any()) { File.Delete(honey.ImgPath); return(new ConflictResult()); } _context.HoneysInTheWarehouse.Add(new HoneyInTheWarehouse() { Name = honey.Name, Price = honey.Price, Amount = honey.Amount, Description = honey.Description, ImgPath = honey.ImgPath }); _context.SaveChanges(); return(new OkResult()); }
public ActionResult AddToCart(HoneyInTheCartDto honey, int clientId) { if (honey == null || !_context.Clients.Where(x => x.Id == clientId).Any()) { return(new NotFoundResult()); } var cart = _context.HoneysInTheCart.Where(x => x.ClientId == clientId).ToList(); bool isInTheCart = false; foreach (var honeyInCart in cart) { if (honey.Name == honeyInCart.Name) { isInTheCart = true; honeyInCart.Amount++; _context.HoneysInTheCart.Update(honeyInCart); } } if (!isInTheCart) { _context.HoneysInTheCart.Add(new HoneyItem() { Name = honey.Name, Price = honey.Price, Amount = honey.Amount, ClientId = clientId }); } _context.SaveChanges(); return(new OkResult()); }