public IActionResult Put(int addressId, [FromForm] ShippingAddressDto dto)
 {
     dto.ShippingAddressId = addressId;
     try
     {
         _editShippingAddress.Execute(dto);
         return(Ok());
     }
     catch (EntityNotFoundException e)
     {
         return(NotFound(e.Message));
     }
     catch (EntityAlreadyExistsException e)
     {
         return(Conflict(e.Message));
     }
     catch (EntityMissmatchException e)
     {
         return(UnprocessableEntity(e.Message));
     }
     catch (Exception e)
     {
         return(StatusCode(500, e));
     }
 }
        public bool Validate(ShippingAddressDto request)
        {
            if (!_context.Users.Any(x => x.Id == request.UserId))
            {
                throw new EntityNotFoundException("User with id: " + request.UserId);
            }


            if
            (
                _context.ShippingAddresses
                .Include(sa => sa.User)
                .AsQueryable()
                .Where
                (
                    x =>
                    x.Country == request.Country &&
                    x.State == request.State &&
                    x.City == request.City &&
                    x.Address == request.Address &&
                    x.PostalCode == request.PostalCode &&
                    x.User.Id == request.UserId
                )
                .Count()
                != 0
            )
            {
                throw new EntityAlreadyExistsException("Shipping address");
            }


            return(true);
        }
Example #3
0
        public void Execute(ShippingAddressDto request)
        {
            if (Validate(request))
            {
                var shippingAddress = _context.ShippingAddresses
                                      .Include(sa => sa.User)
                                      .Include(sa => sa.Orders)
                                      .ThenInclude(o => o.OrderCoupons)
                                      .Include(sa => sa.Orders)
                                      .ThenInclude(o => o.OrderProducts)
                                      .AsQueryable()
                                      .Where(x => x.Id == request.ShippingAddressId)
                                      .First();

                shippingAddress.DateUpdated = DateTime.Now;
                shippingAddress.Country     = request.Country;
                shippingAddress.State       = request.State;
                shippingAddress.City        = request.City;
                shippingAddress.Address     = request.Address;
                shippingAddress.PostalCode  = request.PostalCode;

                if (shippingAddress.Orders.Count() != 0)
                {
                    foreach (var order in shippingAddress.Orders)
                    {
                        if (order.DateShipped == null)
                        {
                            MailMessage message = new MailMessage();
                            message.From = new MailAddress("*****@*****.**", "OnlineMarketPlace");
                            message.To.Add(new MailAddress(order.User.Email, order.User.FirstName + " " + order.User.LastName));
                            message.Subject = "OnlineMarketPlace - Order cancelation";

                            string body = $"We would like to inform you that your order with id {order.Id} is cancelled. ";
                            body        += $"Reason: Shipping address for this order has changed and for safety, we have cancelled the order. ";
                            body        += "We are sorry for any inconviniences caused.";
                            message.Body = body;

                            Functions.SmtpClient.Send(message);
                        }

                        foreach (var product in order.OrderProducts)
                        {
                            product.Active = false;
                        }

                        if (order.OrderCoupons.Count() == 0)
                        {
                            foreach (var coupon in order.OrderCoupons)
                            {
                                coupon.Active = false;
                            }
                        }

                        order.Active = false;
                    }
                }

                _context.SaveChanges();
            }
        }
 public IActionResult AddShippingAddress([FromForm] ShippingAddressDto dto)
 {
     try
     {
         _createShippingAddress.Execute(dto);
         return(Ok());
     }
     catch (EntityNotFoundException e)
     {
         return(NotFound(e.Message));
     }
     catch (EntityAlreadyExistsException e)
     {
         return(Conflict(e.Message));
     }
     catch (Exception e)
     {
         return(StatusCode(500, e));
     }
 }
        public void Execute(ShippingAddressDto request)
        {
            if (Validate(request))
            {
                var newShippingAddress = new ShippingAddresses
                {
                    Active      = true,
                    DateCreated = DateTime.Now,
                    User        = _context.Users.Find(request.UserId),
                    Country     = request.Country,
                    State       = request.State,
                    City        = request.City,
                    Address     = request.Address,
                    PostalCode  = request.PostalCode
                };

                _context.ShippingAddresses.Add(newShippingAddress);

                _context.SaveChanges();
            }
        }