Example #1
0
        public OperationResultDTO CalculateCost(string cityName, ModuleListDTO moduleListDTO)
        {
            var city = cityService.GetCityByName(cityName);

            if (city == null)
            {
                return(new OperationErrorDTO {
                    Code = 404, Message = $"City with name: {cityName} doesn't exist"
                });
            }
            var modulesCost = city.TransportCost;

            foreach (String moduleName in moduleListDTO.ModuleList)
            {
                var module = moduleService.GetModuleByName(moduleName);

                if (module == null)
                {
                    return(new OperationErrorDTO {
                        Code = 404, Message = $"Module with name: {moduleName} doesn't exist"
                    });
                }
                modulesCost = modulesCost + module.Price + (module.AssemblyTime * city.CostOfWorkingHour);
            }

            modulesCost = modulesCost * 1.3;

            return(new OperationSuccessDTO <ResultCostDTO> {
                Message = "Success", Result = new ResultCostDTO {
                    Cost = modulesCost, InSearchHistory = false
                }
            });
        }
Example #2
0
        public ResultCostDTO GetSearchHistory(string cityName, ModuleListDTO moduleListDTO)
        {
            var city = cityService.GetCityByName(cityName);

            if (city == null)
            {
                return new ResultCostDTO {
                           InSearchHistory = false
                }
            }
            ;

            var searchHistoryList = context.SearchHistory.Where(sh => sh.CityId == city.Id);

            if (searchHistoryList == null)
            {
                return new ResultCostDTO {
                           InSearchHistory = false
                }
            }
            ;

            int counterModule = 0;

            foreach (SearchHistory searchHistory in searchHistoryList)
            {
                counterModule = 0;

                foreach (string searchHistoryPar in moduleListDTO.ModuleList)
                {
                    if (searchHistory.ModuleName1 == searchHistoryPar ||
                        searchHistory.ModuleName2 == searchHistoryPar ||
                        searchHistory.ModuleName3 == searchHistoryPar ||
                        searchHistory.ModuleName4 == searchHistoryPar)
                    {
                        counterModule++;
                    }
                    else
                    {
                        break;
                    }
                }
                if (moduleListDTO.ModuleList.Count() == ModuleHasValue(searchHistory) && moduleListDTO.ModuleList.Count() == counterModule)
                {
                    return new ResultCostDTO {
                               InSearchHistory = true, Cost = searchHistory.ProductionCost
                    }
                }
                ;
            }

            return(new ResultCostDTO {
                InSearchHistory = false
            });
        }
Example #3
0
        public ResultCostDTO PresentResult(string cityName, ModuleListDTO moduleListDTO)
        {
            var checkInHistory = searchHistoryService.GetSearchHistory(cityName, moduleListDTO);
            OperationSuccessDTO <ResultCostDTO> calculateCost = null;

            if (checkInHistory.InSearchHistory == true)
            {
                return(new ResultCostDTO
                {
                    Cost = checkInHistory.Cost,
                    InSearchHistory = checkInHistory.InSearchHistory
                });
            }
            try
            {
                calculateCost = (OperationSuccessDTO <ResultCostDTO>)
                                calculatorCostService.CalculateCost(cityName, moduleListDTO);
            }
            catch
            {
                return(new ResultCostDTO
                {
                    Cost = -1,
                    InSearchHistory = false
                });
            }
            var city = cityService.GetCityByName(cityName);

            SearchHistory searchHistory = new SearchHistory
            {
                CityId         = city.Id,
                ProductionCost = calculateCost.Result.Cost,
                ModuleName1    = moduleListDTO.ModuleList.Count > 0 ?
                                 moduleListDTO.ModuleList[0] : string.Empty,
                ModuleName2 = moduleListDTO.ModuleList.Count > 0 ?
                              moduleListDTO.ModuleList[1] : string.Empty,
                ModuleName3 = moduleListDTO.ModuleList.Count > 0 ?
                              moduleListDTO.ModuleList[2] : string.Empty,
                ModuleName4 = moduleListDTO.ModuleList.Count > 0 ?
                              moduleListDTO.ModuleList[3] : string.Empty,
            };

            searchHistoryService.AddSearchHistory(searchHistory);

            return(new ResultCostDTO
            {
                Cost = calculateCost.Result.Cost,
                InSearchHistory = false,
            });
        }