Ejemplo n.º 1
0
        /// <summary>
        /// Get a parcel by a certain ID.
        /// </summary>
        /// <param name="id">ID of the parcel to get.</param>
        /// <returns>Returns a parcel with certain ID or an appropriate error message.</returns>
        public ParcelDTO Get(int id)
        {
            var       parcel    = FindParcel(id);
            ParcelDTO parcelDTO = new ParcelDTO(parcel);

            return(parcelDTO);
        }
Ejemplo n.º 2
0
        public void Return_Correct_Parcel()
        {
            var options = Utils.GetOptions(nameof(Return_Correct_Parcel));
            var parcels = Utils.SeedParcels();

            using (var arrangeContext = new DeliverItContext(options))
            {
                arrangeContext.Customers.AddRange(Utils.SeedCustomers());
                arrangeContext.Addresses.AddRange(Utils.SeedAddresses());
                arrangeContext.Cities.AddRange(Utils.SeedCities());
                arrangeContext.Statuses.AddRange(Utils.SeedStatuses());
                arrangeContext.Categories.AddRange(Utils.SeedCategories());
                arrangeContext.Warehouses.AddRange(Utils.SeedWarehouses());
                arrangeContext.Shipments.AddRange(Utils.SeedShipments());
                arrangeContext.Parcels.AddRange(parcels);
                arrangeContext.SaveChanges();
            }

            var parcelDTO = new ParcelDTO(parcels.First());

            using (var actContext = new DeliverItContext(options))
            {
                var sut    = new ParcelService(actContext);
                var result = sut.Get(1);

                Assert.AreEqual(parcelDTO.Id, result.Id);
                Assert.AreEqual(parcelDTO.Category, result.Category);
                Assert.AreEqual(parcelDTO.CustomerFirstName, result.CustomerFirstName);
                Assert.AreEqual(parcelDTO.CustomerLastName, result.CustomerLastName);
                Assert.AreEqual(parcelDTO.ParcelArrival, result.ParcelArrival);
                Assert.AreEqual(parcelDTO.WarehouseAddress, result.WarehouseAddress);
                Assert.AreEqual(parcelDTO.WarehouseCity, result.WarehouseCity);
                Assert.AreEqual(parcelDTO.Weight, result.Weight);
            }
        }
Ejemplo n.º 3
0
 public void Delete(ParcelDTO parcel)
 {
     try
     {
         _dbContext.Parcels.Remove(parcel);
         _dbContext.SaveChanges();
     }
     catch (Exception ex)
     {
         throw new DalException("DAL error deleting parcel", ex);
     }
 }
Ejemplo n.º 4
0
 public void Update(ParcelDTO parcel)
 {
     try
     {
         _dbContext.Parcels.Update(parcel);
         _dbContext.SaveChanges();
     }
     catch (Exception ex)
     {
         throw new DalException("DAL error updating parcel", ex);
     }
 }
Ejemplo n.º 5
0
 public void Create(ParcelDTO parcel)
 {
     try
     {
         _dbContext.Parcels.Add(parcel);
         _dbContext.SaveChanges();
     }
     catch (Exception ex)
     {
         throw new DalException("DAL error creating parcel: " + ex.Message + ", Inner: " + ex.InnerException.Message, ex);
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Gets the parcels of a certain customer
        /// </summary>
        /// <param name="filter">The parcels the customer wants to see past and/or future</param>
        /// <returns></returns>
        public List <ParcelDTO> GetCustomerParcels(string username, string filter)
        {
            var allCustomerParcels = this.dbContext
                                     .Parcels
                                     .Include(p => p.Category)
                                     .Include(p => p.Customer)
                                     .Include(p => p.Shipment)
                                     .ThenInclude(s => s.Status)
                                     .Include(p => p.Category)
                                     .Include(p => p.Warehouse)
                                     .ThenInclude(w => w.Address)
                                     .ThenInclude(a => a.City)
                                     .Where(p => (p.Customer.FirstName + "." + p.Customer.LastName) == username);
            var filteredParcels = new List <ParcelDTO>();

            if (filter == "past")
            {
                foreach (var parcel in allCustomerParcels)
                {
                    if (parcel.Shipment.Arrival < DateTime.UtcNow)
                    {
                        var pastParcel = new ParcelDTO(parcel);
                        filteredParcels.Add(pastParcel);
                    }
                }
            }
            if (filter == "future")
            {
                foreach (var parcel in allCustomerParcels)
                {
                    if (parcel.Shipment.Arrival >= DateTime.UtcNow)
                    {
                        var futureParcel = new ParcelDTO(parcel);
                        filteredParcels.Add(futureParcel);
                    }
                }
            }
            if (filter == null)
            {
                foreach (var parcel in allCustomerParcels)
                {
                    var customerParcel = new ParcelDTO(parcel);
                    filteredParcels.Add(customerParcel);
                }
            }
            if (filteredParcels.Count == 0)
            {
                throw new ArgumentNullException(Exceptions.InvalidParcels);
            }
            return(filteredParcels);
        }
 public double?ParseTheParcel(ParcelDTO parcel)
 {
     foreach (var rule in parseRules)
     {
         if (parcel.Length <= rule.Length &&
             parcel.Breadth <= rule.Breadth &&
             parcel.Height <= rule.Height &&
             parcel.Weight <= rule.Weight
             )
         {
             return(rule.Price);
         }
     }
     return(null);
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Create a parcel.
        /// </summary>
        /// <param name="dto">Details of the parcel to be created.</param>
        /// <returns>Returns the created parcel or an appropriate error message.</returns>
        public ParcelDTO Create(NewParcelDTO dto)
        {
            var newParcel = new Parcel();

            var category = this.dbContext.Categories.FirstOrDefault(c => c.Id == dto.CategoryId)
                           ?? throw new ArgumentNullException(Exceptions.InvalidCategory);

            newParcel.CategoryId = dto.CategoryId;

            var customer = this.dbContext.Customers.FirstOrDefault(c => c.Id == dto.CustomerId)
                           ?? throw new ArgumentNullException(Exceptions.InvalidCustomer);

            newParcel.CustomerId = dto.CustomerId;

            var warehouse = this.dbContext.Warehouses.FirstOrDefault(w => w.Id == dto.WarehouseId)
                            ?? throw new ArgumentNullException(Exceptions.InvalidWarehouse);

            newParcel.WarehouseId = dto.WarehouseId;

            var shipment = this.dbContext.Shipments.FirstOrDefault(s => s.Id == dto.ShipmentId)
                           ?? throw new ArgumentNullException(Exceptions.InvalidShipment);

            newParcel.ShipmentId = dto.ShipmentId;

            if (dto.Weight < 0.1 || dto.Weight > 500)
            {
                throw new ArgumentOutOfRangeException(Exceptions.InvalidWeight);
            }
            newParcel.Weight    = dto.Weight;
            newParcel.CreatedOn = DateTime.UtcNow;

            this.dbContext.Parcels.Add(newParcel);
            warehouse.Parcels.Add(newParcel);
            category.Parcels.Add(newParcel);
            shipment.Parcels.Add(newParcel);
            customer.Parcels.Add(newParcel);

            this.dbContext.SaveChanges();

            var createdParcel = FindParcel(newParcel.Id);

            ParcelDTO parcelDTO = new ParcelDTO(createdParcel);

            return(parcelDTO);
        }
Ejemplo n.º 9
0
        public void Report_hop_should_return_true()
        {
            var hop = new HopArrivalModel
            {
                Code       = "1234",
                DateTime   = DateTime.Now,
                TrackingId = "12345ABC"
            };

            var recipient = new RecipientDTO
            {
                FirstName  = "Rudi",
                LastName   = "Recipient",
                Street     = "Poststraße",
                PostalCode = "A-1070",
                City       = "Poststadt"
            };
            var parcel = new ParcelDTO {
                Recipient = recipient, Weight = 1.0f, TrackingCode = "MOCKCODE"
            };

            var ware = new WarehouseDTO
            {
                Code        = "1234",
                Description = "root warehouse",
                Duration    = 12,
                Id          = 3
            };

            var truck = new TruckDTO
            {
                Code        = "1234",
                Description = "root truck",
                Duration    = 42,
                Id          = 4
            };

            truckMock.Setup(input => input.GetByCode(It.IsAny <string>())).Returns(truck);
            warehouseMock.Setup(input => input.GetByCode(It.IsAny <string>())).Returns(ware);
            parcelMock.Setup(input => input.GetByTrackingCode(It.IsAny <string>())).Returns(parcel);
            var result = tracking.ReportHop(hop);

            // Assert
            Assert.IsTrue(result);
        }
 public ParseTheParcelResponseModel ParseTheParcel([FromQuery] ParcelDTO parcel)
 {
     try
     {
         if (parcel.Length <= 0 ||
             parcel.Breadth <= 0 ||
             parcel.Height <= 0 ||
             parcel.Weight <= 0)
         {
             return(new ParseTheParcelResponseModel
             {
                 isSuccess = false,
                 Message = "The input value should not be negative.",
                 Price = 0
             });
         }
         double?price = _service.ParseTheParcel(parcel);
         if (price is double priceValue)
         {
             _logger.LogInformation(ParseTheParcelEventID.Done, $"The parcel {parcel.Length}x{parcel.Breadth}x{parcel.Height} @ {parcel.Weight / 1000.0} kg costs ${priceValue}");
             return(new ParseTheParcelResponseModel
             {
                 isSuccess = true,
                 Message = "",
                 Price = priceValue
             });
         }
         else
         {
             // parcel is too big or too heavy
             _logger.LogInformation(ParseTheParcelEventID.TooLarge, $"The parcel {parcel.Length}x{parcel.Breadth}x{parcel.Height} @ {parcel.Weight / 1000.0} kg is too large");
             return(new ParseTheParcelResponseModel
             {
                 isSuccess = false,
                 Message = "The parcel is either too large or too heavy.",
                 Price = 0
             });
         }
     } catch (Exception e) {
         _logger.LogInformation(ParseTheParcelEventID.InternalError, e, $"Exception when parsing parcel {parcel.Length}x{parcel.Breadth}x{parcel.Height} @ {parcel.Weight / 1000.0} kg.");
         throw e;
     }
 }
Ejemplo n.º 11
0
        public void Get_Parcel_by_code_should_return_Parcel()
        {
            // Arrange
            var recipient = new RecipientDTO()
            {
                FirstName  = "Rudi",
                LastName   = "Recipient",
                Street     = "Poststraße",
                PostalCode = "A-1070",
                City       = "Poststadt"
            };
            var parcel = new ParcelDTO()
            {
                Recipient = recipient, Weight = 1.0f, TrackingCode = "MOCKCODE"
            };

            parcelMock.Setup(input => input.GetByTrackingCode(It.IsAny <string>())).Returns(parcel);

            var result = tracking.GetParcelByCode("MOCKCODE");

            // Assert
            Assert.IsInstanceOfType(result, typeof(ParcelModel));
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Filter and/or sort parcels.
        /// </summary>
        /// <param name="filter1">First filter for parcels.</param>
        /// <param name="value1">Value of the first filter.</param>
        /// <param name="filter2">Second filter for parcels.</param>
        /// <param name="value2">Value of the second filter.</param>
        /// <param name="sortBy1">First sort option for parcels.</param>
        /// <param name="sortBy2">Second sort option for parcels.</param>
        /// <param name="order">Value of sorting order.</param>
        /// <returns></returns>
        public List <ParcelDTO> GetBy(string filter1, string value1, string filter2, string value2, string sortBy1, string sortBy2, string order)
        {
            var allParcels = this.dbContext
                             .Parcels
                             .Include(p => p.Category)
                             .Include(p => p.Customer)
                             .Include(p => p.Shipment)
                             .ThenInclude(s => s.Status)
                             .Include(p => p.Category)
                             .Include(p => p.Warehouse)
                             .ThenInclude(w => w.Address)
                             .ThenInclude(a => a.City);
            var filteredParcels = new List <ParcelDTO>();

            if (filter1 == "weight")
            {
                switch (filter2)
                {
                case null:
                    foreach (var parcel in allParcels)
                    {
                        if ((parcel.Weight == double.Parse(value1)) && parcel.IsDeleted == false)
                        {
                            var parcelDTO = new ParcelDTO(parcel);
                            filteredParcels.Add(parcelDTO);
                        }
                    }
                    break;

                case "customer":
                    foreach (var parcel in allParcels)
                    {
                        if ((parcel.Weight == double.Parse(value1) &&
                             (parcel.Customer.FirstName.Equals(value2, StringComparison.OrdinalIgnoreCase) || parcel.Customer.LastName.Equals(value2, StringComparison.OrdinalIgnoreCase) &&
                              parcel.IsDeleted == false)))
                        {
                            var parcelDTO = new ParcelDTO(parcel);
                            filteredParcels.Add(parcelDTO);
                        }
                    }
                    break;

                case "warehouse":
                    foreach (var parcel in allParcels)
                    {
                        if ((parcel.Weight == double.Parse(value1) &&
                             (parcel.WarehouseId == int.Parse(value2)) && parcel.IsDeleted == false))
                        {
                            var parcelDTO = new ParcelDTO(parcel);
                            filteredParcels.Add(parcelDTO);
                        }
                    }
                    break;

                case "category":
                    foreach (var parcel in allParcels)
                    {
                        if ((parcel.Weight == double.Parse(value1) &&
                             (parcel.Category.Name.Equals(value2, StringComparison.OrdinalIgnoreCase)) && parcel.IsDeleted == false))
                        {
                            var parcelDTO = new ParcelDTO(parcel);
                            filteredParcels.Add(parcelDTO);
                        }
                    }
                    break;
                }
            }
            if (filter1 == "customer")
            {
                switch (filter2)
                {
                case null:
                    foreach (var parcel in allParcels)
                    {
                        if ((parcel.Customer.FirstName.Equals(value1, StringComparison.OrdinalIgnoreCase) || parcel.Customer.LastName.Equals(value1, StringComparison.OrdinalIgnoreCase)) &&
                            parcel.IsDeleted == false)
                        {
                            var parcelDTO = new ParcelDTO(parcel);
                            filteredParcels.Add(parcelDTO);
                        }
                    }
                    break;

                case "weight":
                    foreach (var parcel in allParcels)
                    {
                        if ((parcel.Customer.FirstName.Equals(value1, StringComparison.OrdinalIgnoreCase) || parcel.Customer.LastName.Equals(value1, StringComparison.OrdinalIgnoreCase) &&
                             (parcel.Weight == double.Parse(value2)) && parcel.IsDeleted == false))
                        {
                            var parcelDTO = new ParcelDTO(parcel);
                            filteredParcels.Add(parcelDTO);
                        }
                    }
                    break;

                case "warehouse":
                    foreach (var parcel in allParcels)
                    {
                        if ((parcel.Customer.FirstName.Equals(value1, StringComparison.OrdinalIgnoreCase) || parcel.Customer.LastName.Equals(value1, StringComparison.OrdinalIgnoreCase) &&
                             (parcel.WarehouseId == int.Parse(value2)) && parcel.IsDeleted == false))
                        {
                            var parcelDTO = new ParcelDTO(parcel);
                            filteredParcels.Add(parcelDTO);
                        }
                    }
                    break;

                case "category":
                    foreach (var parcel in allParcels)
                    {
                        if ((parcel.Customer.FirstName.Equals(value1, StringComparison.OrdinalIgnoreCase) || parcel.Customer.LastName.Equals(value1, StringComparison.OrdinalIgnoreCase) &&
                             (parcel.Category.Name.Equals(value2, StringComparison.OrdinalIgnoreCase)) && parcel.IsDeleted == false))
                        {
                            var parcelDTO = new ParcelDTO(parcel);
                            filteredParcels.Add(parcelDTO);
                        }
                    }
                    break;
                }
            }
            if (filter1 == "warehouse")
            {
                switch (filter2)
                {
                case null:
                    foreach (var parcel in allParcels)
                    {
                        if ((parcel.WarehouseId == int.Parse(value1)) && parcel.IsDeleted == false)
                        {
                            var parcelDTO = new ParcelDTO(parcel);
                            filteredParcels.Add(parcelDTO);
                        }
                    }
                    break;

                case "weight":
                    foreach (var parcel in allParcels)
                    {
                        if ((parcel.WarehouseId == int.Parse(value1)) &&
                            (parcel.Weight == double.Parse(value2)) && parcel.IsDeleted == false)
                        {
                            var parcelDTO = new ParcelDTO(parcel);
                            filteredParcels.Add(parcelDTO);
                        }
                    }
                    break;

                case "customer":
                    foreach (var parcel in allParcels)
                    {
                        if ((parcel.WarehouseId == int.Parse(value1)) &&
                            (parcel.Customer.FirstName.Equals(value2, StringComparison.OrdinalIgnoreCase) || parcel.Customer.LastName.Equals(value2, StringComparison.OrdinalIgnoreCase) &&
                             parcel.IsDeleted == false))
                        {
                            var parcelDTO = new ParcelDTO(parcel);
                            filteredParcels.Add(parcelDTO);
                        }
                    }
                    break;

                case "category":
                    foreach (var parcel in allParcels)
                    {
                        if ((parcel.WarehouseId == int.Parse(value1)) &&
                            (parcel.Category.Name.Equals(value2, StringComparison.OrdinalIgnoreCase)) && parcel.IsDeleted == false)
                        {
                            var parcelDTO = new ParcelDTO(parcel);
                            filteredParcels.Add(parcelDTO);
                        }
                    }
                    break;
                }
            }
            if (filter1 == "category")
            {
                switch (filter2)
                {
                case null:
                    foreach (var parcel in allParcels)
                    {
                        if ((parcel.Category.Name.Equals(value1, StringComparison.OrdinalIgnoreCase)) && parcel.IsDeleted == false)
                        {
                            var parcelDTO = new ParcelDTO(parcel);
                            filteredParcels.Add(parcelDTO);
                        }
                    }
                    break;

                case "weight":
                    foreach (var parcel in allParcels)
                    {
                        if ((parcel.Category.Name.Equals(value1, StringComparison.OrdinalIgnoreCase)) &&
                            (parcel.Weight == double.Parse(value2)) && parcel.IsDeleted == false)
                        {
                            var parcelDTO = new ParcelDTO(parcel);
                            filteredParcels.Add(parcelDTO);
                        }
                    }
                    break;

                case "customer":
                    foreach (var parcel in allParcels)
                    {
                        if ((parcel.Category.Name.Equals(value1, StringComparison.OrdinalIgnoreCase)) &&
                            (parcel.Customer.FirstName.Equals(value2, StringComparison.OrdinalIgnoreCase) || parcel.Customer.LastName.Equals(value2, StringComparison.OrdinalIgnoreCase) &&
                             parcel.IsDeleted == false))
                        {
                            var parcelDTO = new ParcelDTO(parcel);
                            filteredParcels.Add(parcelDTO);
                        }
                    }
                    break;

                case "warehouse":
                    foreach (var parcel in allParcels)
                    {
                        if (parcel.Category.Name.Equals(value1, StringComparison.OrdinalIgnoreCase) &&
                            (parcel.WarehouseId == int.Parse(value2)) && parcel.IsDeleted == false)
                        {
                            var parcelDTO = new ParcelDTO(parcel);
                            filteredParcels.Add(parcelDTO);
                        }
                    }
                    break;
                }
            }
            if (filteredParcels.Count == 0 && sortBy1 == null)
            {
                throw new ArgumentNullException(Exceptions.InvalidParcels);
            }
            if (filteredParcels.Count != 0)
            {
                if (sortBy1 == "weight" && sortBy2 == null)
                {
                    switch (order)
                    {
                    case "asc":
                        filteredParcels = filteredParcels.OrderBy(p => p.Weight).ToList();
                        break;

                    case "desc":
                        filteredParcels = filteredParcels.OrderByDescending(p => p.Weight).ToList();
                        break;
                    }
                }
                if (sortBy1 == "arrival" && sortBy2 == null)
                {
                    switch (order)
                    {
                    case "asc":
                        filteredParcels = filteredParcels.OrderBy(p => p.ParcelArrival).ToList();
                        break;

                    case "desc":
                        filteredParcels = filteredParcels.OrderByDescending(p => p.ParcelArrival).ToList();
                        break;
                    }
                }
                if (sortBy1 == "weight" && sortBy2 == "arrival")
                {
                    switch (order)
                    {
                    case "asc":
                        filteredParcels = filteredParcels.OrderBy(p => p.Weight).ThenBy(p => p.ParcelArrival).ToList();
                        break;

                    case "desc":
                        filteredParcels = filteredParcels.OrderByDescending(p => p.Weight).ThenBy(p => p.ParcelArrival).ToList();
                        break;
                    }
                }
                if (sortBy1 == "arrival" && sortBy2 == "weight")
                {
                    switch (order)
                    {
                    case "asc":
                        filteredParcels = filteredParcels.OrderBy(p => p.ParcelArrival).ThenBy(p => p.Weight).ToList();
                        break;

                    case "desc":
                        filteredParcels = filteredParcels.OrderByDescending(p => p.ParcelArrival).ThenBy(p => p.Weight).ToList();
                        break;
                    }
                }
            }
            else
            {
                var allParcelsDTO = new List <ParcelDTO>();
                foreach (var parcel in allParcels)
                {
                    var parcelDTO = new ParcelDTO(parcel);
                    allParcelsDTO.Add(parcelDTO);
                }
                if (sortBy1 == "weight" && sortBy2 == null)
                {
                    switch (order)
                    {
                    case "asc":
                        filteredParcels = allParcelsDTO.OrderBy(p => p.Weight).ToList();
                        break;

                    case "desc":
                        filteredParcels = allParcelsDTO.OrderByDescending(p => p.Weight).ToList();
                        break;
                    }
                }
                if (sortBy1 == "arrival" && sortBy2 == null)
                {
                    switch (order)
                    {
                    case "asc":
                        filteredParcels = allParcelsDTO.OrderBy(p => p.ParcelArrival).ToList();
                        break;

                    case "desc":
                        filteredParcels = allParcelsDTO.OrderByDescending(p => p.ParcelArrival).ToList();
                        break;
                    }
                }
                if (sortBy1 == "weight" && sortBy2 == "arrival")
                {
                    switch (order)
                    {
                    case "asc":
                        filteredParcels = allParcelsDTO.OrderBy(p => p.Weight).ThenBy(p => p.ParcelArrival).ToList();
                        break;

                    case "desc":
                        filteredParcels = allParcelsDTO.OrderByDescending(p => p.Weight).ThenBy(p => p.ParcelArrival).ToList();
                        break;
                    }
                }
                if (sortBy1 == "arrival" && sortBy2 == "weight")
                {
                    switch (order)
                    {
                    case "asc":
                        filteredParcels = allParcelsDTO.OrderBy(p => p.ParcelArrival).ThenBy(p => p.Weight).ToList();
                        break;

                    case "desc":
                        filteredParcels = allParcelsDTO.OrderByDescending(p => p.ParcelArrival).ThenBy(p => p.Weight).ToList();
                        break;
                    }
                }
            }

            return(filteredParcels);
        }
 public void Delete(ParcelDTO t)
 {
     _parcels.Remove(t);
 }
 public void Update(ParcelDTO t)
 {
     throw new NotImplementedException();
 }
 public void Create(ParcelDTO t)
 {
     t.Id = _autoinc++;
     _logger.Info("MockParcelRepository.Create (Parcel with tracking code): " + t.TrackingCode);
     _parcels.Add(t);
 }