Ejemplo n.º 1
0
        public ServiceResult <DtoOrderItem> SetOrderItem(DtoOrderItem orderItem)
        {
            using (var transaction = Context.Database.BeginTransaction())
            {
                try
                {
                    if (orderItem == null) //maybe also if(!modelState.IsValid)
                    {
                        return(new ServiceResult <DtoOrderItem>
                        {
                            Success = false,
                            Exception = new ArgumentNullException("Order item not valid.")
                        });
                    }

                    var dbOrderItem = Context.OrderItems.FirstOrDefault(oi => oi.OrderItemId == orderItem.OrderItemId);

                    if (dbOrderItem != null)
                    {
                        if (orderItem.ItemQuantity > 0)
                        {
                            if (dbOrderItem.ItemQuantity == orderItem.ItemQuantity)
                            {
                                return(new ServiceResult <DtoOrderItem>
                                {
                                    Success = true,
                                    Item = new DtoOrderItem(dbOrderItem)
                                });
                            }

                            //exists - update it
                            dbOrderItem.ItemQuantity = orderItem.ItemQuantity;
                            Context.SaveChanges();

                            return(new ServiceResult <DtoOrderItem>
                            {
                                Success = true,
                                Item = new DtoOrderItem(dbOrderItem)
                            });
                        }
                        else if (orderItem.ItemQuantity == 0)
                        {
                            //exists - delete it
                            Context.OrderItems.Remove(dbOrderItem);
                            Context.SaveChanges();

                            return(new ServiceResult <DtoOrderItem>
                            {
                                Success = true
                            });
                        }
                        else
                        {
                            //exists - throw error because quantity cannot be negative
                            return(new ServiceResult <DtoOrderItem>
                            {
                                Success = false,
                                ErrorMessage = "Item quantity cannot be negative."
                            });
                        }
                    }
                    else
                    {
                        if (orderItem.ItemQuantity < 1)
                        {
                            return(new ServiceResult <DtoOrderItem>
                            {
                                Success = false,
                                ErrorMessage = "Item quantity cannot be negative."
                            });
                        }

                        var orderExists = Context.Orders.Any(o => o.OrderId == orderItem.OrderId);
                        var itemExists  = Context.Items.Any(i => i.ItemId == orderItem.ItemId);

                        if (!orderExists)
                        {
                            return new ServiceResult <DtoOrderItem>
                                   {
                                       Success      = false,
                                       ErrorMessage = "Order not found."
                                   }
                        }
                        ;

                        if (!itemExists)
                        {
                            return new ServiceResult <DtoOrderItem>
                                   {
                                       Success      = false,
                                       ErrorMessage = "Item not found."
                                   }
                        }
                        ;

                        //create the order item
                        var newOrderItem = new OrderItem
                        {
                            //OrderItemId = orderItem.OrderItemId,
                            ItemId       = orderItem.ItemId,
                            OrderId      = orderItem.OrderId,
                            ItemQuantity = orderItem.ItemQuantity
                        };

                        Context.OrderItems.Add(newOrderItem);
                        Context.SaveChanges();

                        transaction.Commit();

                        return(new ServiceResult <DtoOrderItem>()
                        {
                            Success = true,
                            Item = new DtoOrderItem(newOrderItem)
                        });
                    }
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return(new ServiceResult <DtoOrderItem>()
                    {
                        Success = false,
                        Exception = ex,
                        ErrorMessage = "Order item not set."
                    });
                }
            }
        }
Ejemplo n.º 2
0
        public ServiceResult <DtoOrderItem> SetOrderItem(DtoOrderItem orderItemDto)
        {
            try
            {
                var dbOrderItem = DbContext.OrderItems
                                  .FirstOrDefault(oi => oi.Id == orderItemDto.Id);

                if (dbOrderItem != null)
                {
                    if (dbOrderItem.Quantity == orderItemDto.Quantity)
                    {
                        return new ServiceResult <DtoOrderItem>
                               {
                                   Success = true,
                                   Item    = new DtoOrderItem(dbOrderItem)
                               }
                    }
                    ;

                    if (orderItemDto.Quantity > 0)
                    {
                        //exists => update-it
                        dbOrderItem.Quantity = orderItemDto.Quantity;

                        DbContext.SaveChanges();
                        return(new ServiceResult <DtoOrderItem>
                        {
                            Success = true,
                            Item = new DtoOrderItem(dbOrderItem)
                        });
                    }
                    else if (orderItemDto.Quantity == 0)
                    {
                        //exists => delete-it
                        DbContext.OrderItems.Remove(dbOrderItem);
                        DbContext.SaveChanges();
                        return(new ServiceResult <DtoOrderItem>
                        {
                            Success = true
                        });
                    }
                    else
                    {
                        //exists => return error because quantity cannot be negative
                        return(new ServiceResult <DtoOrderItem>
                        {
                            Success = false,
                            ErrorMessage = "quantity cannot be negative"
                        });
                    }
                }
                else
                {
                    //TODO: Validation
                    if (orderItemDto.Quantity < 1)
                    {
                        return new ServiceResult <DtoOrderItem>
                               {
                                   Success      = false,
                                   ErrorMessage = "quantity cannot be negative or zero"
                               }
                    }
                    ;

                    var orderExists = DbContext.Orders
                                      .Any(o => o.Id == orderItemDto.OrderId);

                    var itemExists = DbContext.Items
                                     .Any(i => i.Id == orderItemDto.ItemId);

                    if (!orderExists)
                    {
                        return new ServiceResult <DtoOrderItem>
                               {
                                   Success      = false,
                                   ErrorMessage = "order not found"
                               }
                    }
                    ;

                    if (!itemExists)
                    {
                        return new ServiceResult <DtoOrderItem>
                               {
                                   Success      = false,
                                   ErrorMessage = "item not found"
                               }
                    }
                    ;

                    var newOrderItem = new OrderItem
                    {
                        ItemId   = orderItemDto.ItemId,
                        OrderId  = orderItemDto.OrderId,
                        Quantity = orderItemDto.Quantity
                    };

                    DbContext.OrderItems.Add(newOrderItem);
                    DbContext.SaveChanges();
                    return(new ServiceResult <DtoOrderItem>
                    {
                        Success = true,
                        Item = new DtoOrderItem(newOrderItem)
                    });
                }
            }
            catch (Exception ex)
            {
                return(new ServiceResult <DtoOrderItem>
                {
                    Success = false,
                    Exception = ex
                });
            }
        }