Example #1
0
        public void Add(APIKey key, Order order, OrderInsertResult result)
        {
            if (order.Vendor == null)
            {
                order.Vendor = key.Vendor;
            }

            // Check for duplicates if override not allowed
            if (key.Vendor.AllowDuplicateOrderNumbers == false)
            {
                var existing =
                    _context.Orders.FirstOrDefault(o => o.OrderNumber == order.OrderNumber && o.Vendor == key.Vendor);
                if (existing != null)
                {
                    result.Success = false;
                    result.Message =
                        $"Duplication Error: Order number {order.OrderNumber} already exists in the system";
                    return;
                }
            }


            // Adding trhe crud and logging information to the record
            order.CreatedDate  = DateTime.Now;
            order.ModifiedDate = DateTime.Now;
            var vendorName = order.Vendor.VendorName;

            order.CreatedBy  = vendorName;
            order.ModifiedBy = vendorName;

            _context.Orders.Add(order);
            _context.SaveChanges();

            result.Success = true;
            result.ItemId  = order.ItemId;
        }
        public InsertOrderResult Put(
            [FromHeader] string vendorKey,
            [FromBody] Order[] orders)
        {
            var key = _vendorService.ValidateAPIKey(vendorKey);

            if (key == null || !key.Enabled)
            {
                return(new InsertOrderResult()
                {
                    Success = false, Message = "API Key Not Valid"
                });
            }

            if (orders == null)
            {
                return(new InsertOrderResult()
                {
                    Success = false, Message = "No Orders were provided"
                });
            }

            var results = new List <OrderInsertResult>();

            foreach (var order in orders)
            {
                var result = new OrderInsertResult
                {
                    OrderNumber = order.OrderNumber
                };

                if (TryValidateModel(order))
                {
                    _repository.Add(key, order, result);

                    if (result.Success)
                    {
                        result.OrderStatus = order.OrderStatus;
                        result.ItemId      = order.ItemId;
                    }
                }
                else
                {
                    var messages = new List <string>();
                    foreach (var item in ModelState.Keys)
                    {
                        var stateItem = ModelState[item];
                        foreach (var subItem in stateItem.Errors)
                        {
                            messages.Add(subItem.ErrorMessage);
                        }
                    }

                    result.Success = false;
                    result.Message = $"Validation Error: {string.Join("|", messages)}";
                }

                results.Add(result);
            }


            return(new InsertOrderResult {
                Success = true, Results = results
            });
        }