Esempio n. 1
0
        public async Task <IActionResult> PutAsync(string packageId, [FromBody] RegisterPackage command)
        {
            if (ModelState.IsValid)
            {
                var package = await _packageRepo.GetPackageAsync(packageId);

                if (package != null)
                {
                    // Shipping Checklist for package and transport
                    bool packageIsIssued    = false;
                    bool transportIsChecked = false;
                    var  transports         = await _transportRepo.GetTransportsAsync(package.PackageId);

                    var transport = await _transportRepo.GetTransportAsync(command.Transport.TransportId);

                    // Update Package
                    // (NOTE: Only "ShippingStatus", "BarcodeNumber", "Delivered" and "Transport")
                    package.BarcodeNumber = command.BarcodeNumber;

                    // Check if "ShippingStatus" is "IN STOCK" and transport choice is not null
                    if (package.ShippingStatus == "IN STOCK" && command.Transport != null)
                    {
                        // (NOTE: If transport choice is in the available list of transports update the package
                        if (transports.Count() != 0 && transport != null && transports.Any(t => t == transport))
                        {
                            package.ShippingStatus = "ISSUE";
                            package.Delivered      = false;
                            package.Transport      = transport;

                            packageIsIssued    = true;
                            transportIsChecked = true;
                        }
                        else
                        {
                            return(NotFound());
                        }
                    }

                    await _packageRepo.UpdatePackageAsync();

                    if (packageIsIssued == true && transportIsChecked == true)
                    {
                        // Send Event
                        RegisterPackage   c = Mapper.Map <RegisterPackage>(package);
                        PackageRegistered e = Mapper.Map <PackageRegistered>(c);
                        await _messagePublisher.PublishMessageAsync(e.MessageType, e, "");

                        return(CreatedAtRoute("GetPackageById", new { packageId = package.PackageId }, package));
                    }
                }

                return(NotFound());
            }

            return(BadRequest());
        }
Esempio n. 2
0
        public async Task <IActionResult> GetAsync(string transportId)
        {
            var transport = await _transportRepo.GetTransportAsync(transportId);

            if (transport != null)
            {
                return(Ok(transport));
            }

            return(NotFound());
        }