public IActionResult Create([FromHeader] string authorizationUsername, [FromBody] NewShipmentDTO model) { try { this.authHelper.TryGetEmployee(authorizationUsername); var shipment = this.shipmentService.Create(model); return(Created("post", shipment)); } catch (Exception e) { return(NotFound(e.Message)); } }
/// <summary> /// Create a shipment. /// </summary> /// <param name="dto">Details of the shipment to be created.</param> /// <returns>Returns the created shipment or an appropriate error message.</returns> public ShipmentDTO Create(NewShipmentDTO dto) { var newShipment = new Shipment(); var warehouse = this.dbContext.Warehouses.FirstOrDefault(w => w.Id == dto.WarehouseId) ?? throw new ArgumentNullException(Exceptions.InvalidWarehouse); var status = this.dbContext.Statuses.FirstOrDefault(s => s.Id == dto.StatusId) ?? throw new ArgumentNullException(Exceptions.InvalidStatus); this.dbContext.Shipments.Add(newShipment); warehouse.Shipments.Add(newShipment); status.Shipments.Add(newShipment); newShipment.CreatedOn = DateTime.UtcNow; this.dbContext.SaveChanges(); var createdShipment = FindShipment(newShipment.Id); return(new ShipmentDTO(createdShipment)); }