コード例 #1
0
        /// <summary>
        /// Update a parcel by a certain ID and data.
        /// </summary>
        /// <param name="id">ID of the parcel to update.</param>
        /// <param name="model">Details of the parcel to be updated.</param>
        /// <returns>Returns the updated parcel or an appropriate error message.</returns>
        public ParcelDTO Update(int id, UpdateParcelDTO model)
        {
            var parcel = FindParcel(id);
            var result = this.dbContext.Parcels
                         .Include(p => p.Category)
                         .Include(p => p.Customer)
                         .Include(p => p.Shipment)
                         .ThenInclude(s => s.Status)
                         .Include(p => p.Warehouse)
                         .ThenInclude(w => w.Address)
                         .ThenInclude(a => a.City);

            if (model.CategoryId != 0)
            {
                var category = this.dbContext.Categories.FirstOrDefault(c => c.Id == model.CategoryId)
                               ?? throw new ArgumentNullException(Exceptions.InvalidCategory);

                parcel.CategoryId = model.CategoryId;
            }
            if (model.CustomerId != 0)
            {
                var customer = this.dbContext.Customers.FirstOrDefault(s => s.Id == model.CustomerId)
                               ?? throw new ArgumentNullException(Exceptions.InvalidCustomer);

                parcel.CustomerId = model.CustomerId;
            }
            if (model.ShipmentId != 0)
            {
                var shipment = this.dbContext.Shipments.FirstOrDefault(s => s.Id == model.ShipmentId)
                               ?? throw new ArgumentNullException(Exceptions.InvalidShipment);

                parcel.ShipmentId = model.ShipmentId;
            }
            if (model.WarehouseId != 0)
            {
                var warehouse = this.dbContext.Warehouses
                                .Include(w => w.Address)
                                .ThenInclude(a => a.City)
                                .FirstOrDefault(s => s.Id == model.WarehouseId)
                                ?? throw new ArgumentNullException(Exceptions.InvalidWarehouse);

                parcel.WarehouseId = model.WarehouseId;
            }
            if (model.Weight != 0)
            {
                parcel.Weight = model.Weight;
            }
            parcel.ModifiedOn = DateTime.UtcNow;
            this.dbContext.SaveChanges();

            return(new ParcelDTO(parcel));
        }
コード例 #2
0
 public IActionResult Update([FromHeader] string authorizationUsername, int id, [FromBody] UpdateParcelDTO model)
 {
     try
     {
         this.authHelper.TryGetEmployee(authorizationUsername);
         var parcel = this.parcelService.Update(id, model);
         return(Ok(parcel));
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }