Exemple #1
0
        /// <summary>
        /// Update a shipment.
        /// </summary>
        /// <param name="id">ID of the shipment to be updated.</param>
        /// <param name="model">Details of the shipment to be updated.</param>
        /// <returns>Returns the updated shipment or an appropriate error message.</returns>
        public ShipmentDTO Update(int id, UpdateShipmentDTO model)
        {
            var shipment = FindShipment(id);

            if (model.StatusId != 0)
            {
                var status = this.dbContext.Statuses.FirstOrDefault(s => s.Id == model.StatusId)
                             ?? throw new ArgumentNullException(Exceptions.InvalidStatus);

                shipment.StatusId = model.StatusId;
            }
            if (model.WarehouseId != 0)
            {
                var warehouse = this.dbContext.Warehouses.Include(w => w.Address)
                                .FirstOrDefault(w => w.Id == model.WarehouseId)
                                ?? throw new ArgumentNullException(Exceptions.InvalidWarehouse);

                shipment.WarehouseId = model.WarehouseId;
            }
            if (model.Arrival != DateTime.MinValue)
            {
                shipment.Arrival = model.Arrival;
            }
            if (model.Departure != DateTime.MinValue)
            {
                shipment.Departure = model.Departure;
            }
            shipment.ModifiedOn = DateTime.UtcNow;
            this.dbContext.SaveChanges();

            return(new ShipmentDTO(shipment));
        }
Exemple #2
0
 public IActionResult Update([FromHeader] string authorizationUsername, int id, [FromBody] UpdateShipmentDTO model)
 {
     try
     {
         this.authHelper.TryGetEmployee(authorizationUsername);
         var shipment = this.shipmentService.Update(id, model);
         return(Ok(shipment));
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }