Exemplo n.º 1
0
        public ServiceResponse <ServiceOrders.SalesOrder> CreateSalesOrder(ServiceOrders.SalesOrder orderToAdd)
        {
            var response = new ServiceResponse <ServiceOrders.SalesOrder>()
            {
                Time = DateTime.Now,
                Data = orderToAdd
            };

            try
            {
                var entityCustomer = _customersReader.GetCustomerFromDbById(orderToAdd.CustomerId);
                var entityOrder    = OrderMapper.SerialiseSalesOrder(orderToAdd);
                entityOrder.Customer = entityCustomer;
                _ordersWriter.AddSalesOrderToDb(entityOrder);
                response.IsSuccessful = true;
                response.Message      = $"Order number {orderToAdd.Id} successfully created";
            }
            catch (Exception e)
            {
                response.IsSuccessful = false;
                response.Message      = $"Failed to create order number {orderToAdd.Id} placed by customer {orderToAdd.CustomerId} {orderToAdd.CustomerId}. Stack trace: {e.StackTrace}";
            }

            return(response);
        }
Exemplo n.º 2
0
        public ActionResult PlaceOrder([FromBody] ServiceOrders.SalesOrder order)
        {
            _logger.LogInformation($"Placing order {order.Id} for customer {order.CustomerId}");
            var orderResponse = _ordersService.PlaceOrder(order);

            return(Ok(orderResponse));
        }
Exemplo n.º 3
0
        // in comes a service model and out goes an entity model. Maping for the Customer inside is done outside, in e.g the OrdersService by invoking a customers reader
        public static EntityOrders.SalesOrder SerialiseSalesOrder(ServiceOrders.SalesOrder serviceOrder)
        {
            var entityOrder = new EntityOrders.SalesOrder()
            {
                Id        = serviceOrder.Id,
                Items     = SerialiseSalesOrderItems(serviceOrder.Items),
                IsPaid    = serviceOrder.IsPaid,
                CreatedOn = serviceOrder.CreatedOn,
                UpdatedOn = serviceOrder.UpdatedOn
            };

            return(entityOrder);
        }
Exemplo n.º 4
0
        // in goes an entity model and out comes a service model
        public static ServiceOrders.SalesOrder SerialiseSalesOrder(EntityOrders.SalesOrder entityOrder)
        {
            var serviceOrder = new ServiceOrders.SalesOrder()
            {
                Id         = entityOrder.Id,
                CustomerId = CustomerMapper.SerialiseCustomer(entityOrder.Customer).Id,
                Items      = SerialiseSalesOrderItems(entityOrder.Items),
                IsPaid     = entityOrder.IsPaid,
                CreatedOn  = entityOrder.CreatedOn,
                UpdatedOn  = entityOrder.UpdatedOn
            };

            return(serviceOrder);
        }
Exemplo n.º 5
0
        public ServiceResponse <bool> PlaceOrder(ServiceOrders.SalesOrder order)
        {
            _logger.LogInformation($"Getting items from new order placed by customer {order.CustomerId}");

            var boolResponse = new ServiceResponse <bool>()
            {
                Time = DateTime.Now
            };

            foreach (var item in order.Items)
            {
                var entityProductWithGivenId = _productsReader.GetProductFromDbById(item.Product.Id);
                var serviceProduct           = ProductMapper.SerialiseProduct(entityProductWithGivenId);
                item.Product = serviceProduct;

                // find the inventory record that matches given product id as opposed to find inventory record with given inventory record id. Like this we only need to inject
                // product reader and inventoies writer no need for inventories reader aswell
                try
                {
                    _inventoriesWriter.UpdateQuantityAvailableInDb(item.Product.Id, -item.QuantityOrdered);
                    item.Product.UpdatedOn = DateTime.Now;
                }
                catch (Exception e)
                {
                    boolResponse.Data         = false;
                    boolResponse.IsSuccessful = false;
                    boolResponse.Message      = $"Failed to update quanitity available in DB. stack trace: {e.StackTrace}";
                }
            }

            _logger.LogInformation("Updated quantity available of each item in this order. Now creating sales order in DB...");
            var orderResponse = CreateSalesOrder(order);

            boolResponse.Data         = orderResponse.IsSuccessful;
            boolResponse.IsSuccessful = orderResponse.IsSuccessful;
            boolResponse.Message      = $"Successfully placed order {order.Id}!";

            return(boolResponse);
        }