public async Task BuilderResponseWhenRepositoryReturnsValidResponse()
        {
            var pharmacyInputModelMock = new PharmacyInputModel
            {
                Latitude  = 23.4,
                Longitude = -3.43
            };

            var pharmacyDataModelMock = new PharmacyDataModel
            {
                Name     = "Store1",
                Address  = "Address1",
                Distance = 23M
            };

            var pharmacyModelMock = new PharmacyModel
            {
                Name     = "Store1",
                Address  = "Address1",
                Distance = 23M
            };

            _pharmacyRepositoryMock.Setup(x => x.FindClosestPharmacyAsync(pharmacyInputModelMock.Latitude, pharmacyInputModelMock.Longitude)).ReturnsAsync(pharmacyDataModelMock);
            var pharmacyBuilder = new PharmacyBuilder(_pharmacyRepositoryMock.Object, _loggerMock.Object);

            Assert.True(pharmacyModelMock.Equals(await pharmacyBuilder.FindClosestAsync(pharmacyInputModelMock)));
        }
Example #2
0
        public async Task <ActionResult <PharmacyModel> > FindClosestPharmacy([FromBody] PharmacyInputModel pharmacyInput)
        {
            _logger.LogDebug("Invoked FindClosestPharmacy");
            try
            {
                var validateResponse = _validationService.ValidatePharmacyInput(pharmacyInput).ToList();
                if (validateResponse.Any())
                {
                    return(StatusCode(400, JsonConvert.SerializeObject(validateResponse)));
                }

                _logger.LogDebug("FindClosestPharmacy Success");
                return(new OkObjectResult(await _pharmacyService.FindClosestAsync(pharmacyInput)));
            }
            catch (SqlNullValueException e)
            {
                _logger.LogError("SqlNullValueException", e);
                return(StatusCode(400, "Null value in database"));
            }
            catch (SqlValueOutOfRangeException e)
            {
                _logger.LogError("SqlValueOutOfRangeException", e);
                return(StatusCode(400, "Invalid value in database"));
            }
            catch (Exception e)
            {
                _logger.LogError("Exception", e);
                return(StatusCode(500, e.Message));
            }
        }
 public IEnumerable <string> ValidatePharmacyInput(PharmacyInputModel pharmacyInput)
 {
     if (pharmacyInput == null)
     {
         yield return("Latitude and Longtitude fields are required");
     }
 }
Example #4
0
        public async Task <IActionResult> Add(PharmacyInputModel inputModel)
        {
            ApplicationUser user = this.usersService.GerUserByName(this.User.Identity.Name);

            await this.pharmaciesService.AddAsync(inputModel.Name, inputModel.Address, inputModel.ContactNumber, inputModel.ProfileImage, user.Id, user);

            return(this.Redirect("/"));
        }
        public void ValidatePharmacyInputWhenPharmacyInputIsValid()
        {
            var pharmacyInputModel = new PharmacyInputModel
            {
                Latitude  = 23.3,
                Longitude = -42.3
            };

            Assert.Empty(_apiValidationService.ValidatePharmacyInput(pharmacyInputModel));
        }
        public async Task BuilderResponseWhenRepositoryReturnsNull()
        {
            var pharmacyInputModelMock = new PharmacyInputModel
            {
                Latitude  = 23.4,
                Longitude = -3.43
            };

            _pharmacyRepositoryMock.Setup(x => x.FindClosestPharmacyAsync(pharmacyInputModelMock.Latitude, pharmacyInputModelMock.Longitude)).ReturnsAsync(() => null);
            var pharmacyBuilder = new PharmacyBuilder(_pharmacyRepositoryMock.Object, _loggerMock.Object);

            Assert.Null(await pharmacyBuilder.FindClosestAsync(pharmacyInputModelMock));
        }
Example #7
0
        public async Task <PharmacyModel> FindClosestAsync(PharmacyInputModel pharmacyInput)
        {
            _logger.LogDebug("Invoked Service FindClosestAsync");

            if (pharmacyInput == null)
            {
                throw new ArgumentNullException();
            }

            var key = $"pharmacy:{pharmacyInput.Latitude}:{pharmacyInput.Longitude}";

            _logger.LogDebug($"Findind closest pharmacy to lat [{pharmacyInput.Latitude}] and long [{pharmacyInput.Longitude}]");
            var firstCheck = await _cacheRepository.GetByKeyAsync <PharmacyModel>(Payload, key);

            if (firstCheck != null)
            {
                _logger.LogDebug("Service first cache successful");
                return(firstCheck);
            }

            _logger.LogDebug("Service first cache miss");
            using (await _lock.LockAsync())
            {
                var secondCheck = await _cacheRepository.GetByKeyAsync <PharmacyModel>(Payload, key);

                if (secondCheck != null)
                {
                    _logger.LogDebug("Service second cache successful");
                    return(secondCheck);
                }

                _logger.LogDebug("Service second cache miss");
                var pharmacy = await _pharmacyBuilder.FindClosestAsync(pharmacyInput);

                if (pharmacy == null)
                {
                    _logger.LogWarning("Service pharmacy closest null from builder, returning empty object success");
                    _logger.LogDebug("Service FindClosestAsync empty successful");
                    return(new PharmacyModel());
                }

                await _cacheRepository.UpsertKeyAsync(key, pharmacy);

                await _cacheRepository.SetKeyExpirationAsync(key, TimeSpan.FromHours(2));

                _logger.LogDebug("Service FindClosestAsync successful");
                return(pharmacy);
            }
        }
Example #8
0
        public IActionResult Edit(string id)
        {
            var pharmacy = this.pharmaciesService.GetPharmacyById(id);

            var inputModel = new PharmacyInputModel
            {
                Id            = id,
                Name          = pharmacy.Name,
                Address       = pharmacy.Address,
                ContactNumber = pharmacy.ContactNumber,
                ProfileImage  = pharmacy.ProfileImage,
                OwnerId       = pharmacy.OwnerId,
            };

            ViewBag.Data = inputModel;
            return(View());
        }
        public async Task FindClosestWhenServiceThrowsUncaughtException()
        {
            var testPharmacyInput = new PharmacyInputModel
            {
                Latitude  = 23.3,
                Longitude = -42.9
            };

            _apiValidationServiceMock.Setup(x => x.ValidatePharmacyInput(It.IsAny <PharmacyInputModel>())).Returns(() => new List <string>());
            _pharmacyServicemock.Setup(x => x.FindClosestAsync(It.IsAny <PharmacyInputModel>())).Throws(new Exception());

            var pharmacyController = new PharmacyController(_apiValidationServiceMock.Object,
                                                            _pharmacyServicemock.Object, _loggerMock.Object);

            var actionResult = await pharmacyController.FindClosestPharmacy(testPharmacyInput);

            Assert.IsAssignableFrom <ActionResult <PharmacyModel> >(actionResult);
            Assert.Equal(500, ((ObjectResult)actionResult.Result).StatusCode);
        }
Example #10
0
        public async Task <PharmacyModel> FindClosestAsync(PharmacyInputModel pharmacyInput)
        {
            _logger.LogDebug("Invoked Builder FindClosestAsync");
            var data = await _pharmacyRepository.FindClosestPharmacyAsync(pharmacyInput.Latitude, pharmacyInput.Longitude);

            if (data == null)
            {
                _logger.LogDebug("Builder null FindClosestAsync successful");
                return(null);
            }

            _logger.LogDebug("Builder FindClosestAsync successful");
            return(new PharmacyModel
            {
                Name = data.Name,
                Address = data.Address,
                Distance = data.Distance
            });
        }
        public async Task <string> CreatePharmacy(PharmacyInputModel pharmacyInputModel)
        {
            if (pharmacyInputModel.BrandexId != 0 &&
                pharmacyInputModel.Name != null &&
                pharmacyInputModel.PharmacyChainId != 0 &&
                pharmacyInputModel.RegionId != 0 &&
                pharmacyInputModel.CityId != 0 &&
                pharmacyInputModel.CompanyId != 0)
            {
                var pharmacyModel = new Pharmacy
                {
                    BrandexId       = pharmacyInputModel.BrandexId,
                    Name            = pharmacyInputModel.Name,
                    Address         = pharmacyInputModel.Address,
                    PharmacyChainId = pharmacyInputModel.PharmacyChainId,
                    Active          = pharmacyInputModel.Active,
                    CityId          = pharmacyInputModel.CityId,
                    SopharmaId      = pharmacyInputModel.SopharmaId,
                    StingId         = pharmacyInputModel.StingId,
                    PhoenixId       = pharmacyInputModel.PhoenixId,
                    PharmnetId      = pharmacyInputModel.PharmnetId,
                    CompanyId       = pharmacyInputModel.CompanyId,
                    PharmacyClass   = pharmacyInputModel.PharmacyClass,
                    RegionId        = pharmacyInputModel.RegionId
                };

                await this.db.Pharmacies.AddAsync(pharmacyModel);

                await this.db.SaveChangesAsync();

                return(pharmacyModel.Name);
            }
            else
            {
                return("");
            }
        }
Example #12
0
        public async Task <IActionResult> Edit(PharmacyInputModel inputModel)
        {
            await this.pharmaciesService.UpdateAsync(inputModel.Name, inputModel.Address, inputModel.ContactNumber, inputModel.ProfileImage, inputModel.Id);

            return(this.Redirect("/Pharmacy/List"));
        }
        public async Task <ActionResult> ImportAsync()
        {
            IFormFile file = Request.Form.Files[0];

            string folderName = "UploadExcel";

            string webRootPath = hostEnvironment.WebRootPath;

            string newPath = Path.Combine(webRootPath, folderName);

            var errorDictionary = new Dictionary <int, string>();

            if (!Directory.Exists(newPath))

            {
                Directory.CreateDirectory(newPath);
            }

            if (file.Length > 0)

            {
                string sFileExtension = Path.GetExtension(file.FileName).ToLower();

                ISheet sheet;

                string fullPath = Path.Combine(newPath, file.FileName);

                using (var stream = new FileStream(fullPath, FileMode.Create))

                {
                    file.CopyTo(stream);

                    stream.Position = 0;

                    if (sFileExtension == ".xls")

                    {
                        HSSFWorkbook hssfwb = new HSSFWorkbook(stream); //This will read the Excel 97-2000 formats

                        sheet = hssfwb.GetSheetAt(0);                   //get first sheet from workbook
                    }

                    else

                    {
                        XSSFWorkbook hssfwb = new XSSFWorkbook(stream); //This will read 2007 Excel format

                        sheet = hssfwb.GetSheetAt(0);                   //get first sheet from workbook
                    }

                    IRow headerRow = sheet.GetRow(0); //Get Header Row

                    int cellCount = headerRow.LastCellNum;

                    for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) //Read Excel File

                    {
                        IRow row = sheet.GetRow(i);

                        if (row == null)
                        {
                            continue;
                        }

                        if (row.Cells.All(d => d.CellType == CellType.Blank))
                        {
                            continue;
                        }

                        var newPharmacy = new PharmacyInputModel();

                        for (int j = row.FirstCellNum; j < cellCount; j++)

                        {
                            string currentRow = "";

                            if (row.GetCell(j) != null)
                            {
                                currentRow = row.GetCell(j).ToString().TrimEnd();
                            }

                            switch (j)
                            {
                            case 0:
                                if (numbersChecker.WholeNumberCheck(currentRow))
                                {
                                    newPharmacy.BrandexId = int.Parse(currentRow);
                                }
                                else
                                {
                                    errorDictionary[i] = currentRow;
                                }
                                break;

                            case 1:
                                break;

                            case 2:
                                if (currentRow == "")
                                {
                                    newPharmacy.PharmacyClass = PharmacyClass.Other;
                                }
                                else
                                {
                                    newPharmacy.PharmacyClass = (PharmacyClass)Enum.Parse(typeof(PharmacyClass), currentRow, true);
                                }
                                break;

                            case 3:
                                if (currentRow == "1")
                                {
                                    newPharmacy.Active = true;
                                }
                                else
                                {
                                    newPharmacy.Active = false;
                                }
                                break;

                            case 4:
                                int companyId = await this.companiesService.IdByName(currentRow);

                                if (companyId != 0)
                                {
                                    //int companyId = await this.companiesService.IdByName(currentRow);
                                    newPharmacy.CompanyId = companyId;
                                }
                                else
                                {
                                    errorDictionary[i] = currentRow;
                                }
                                break;

                            case 5:
                                newPharmacy.Name = currentRow;
                                break;

                            case 6:
                                int chainId = await this.pharmacyChainsService.IdByName(currentRow);

                                if (chainId != 0)
                                {
                                    newPharmacy.PharmacyChainId = chainId;
                                }
                                else
                                {
                                    errorDictionary[i] = currentRow;
                                }
                                break;

                            case 7:
                                newPharmacy.Address = currentRow;
                                break;

                            case 9:
                                int regionId = await this.regionsService.IdByName(currentRow);

                                if (regionId != 0)
                                {
                                    newPharmacy.RegionId = regionId;
                                }
                                else
                                {
                                    errorDictionary[i] = currentRow;
                                }
                                break;

                            case 10:
                                break;

                            case 11:
                                break;

                            case 12:
                                break;

                            case 13:
                                break;

                            case 14:
                                break;

                            case 15:
                                if (currentRow != "")
                                {
                                    if (numbersChecker.WholeNumberCheck(currentRow))
                                    {
                                        newPharmacy.PharmnetId = int.Parse(currentRow);
                                    }
                                }
                                break;

                            case 16:
                                if (currentRow != "")
                                {
                                    if (numbersChecker.WholeNumberCheck(currentRow))
                                    {
                                        newPharmacy.PhoenixId = int.Parse(currentRow);
                                    }
                                }
                                break;

                            case 17:
                                if (currentRow != "")
                                {
                                    if (numbersChecker.WholeNumberCheck(currentRow))
                                    {
                                        newPharmacy.SopharmaId = int.Parse(currentRow);
                                    }
                                }
                                break;

                            case 18:
                                if (currentRow != "")
                                {
                                    if (numbersChecker.WholeNumberCheck(currentRow))
                                    {
                                        newPharmacy.StingId = int.Parse(currentRow);
                                    }
                                }
                                break;

                            case 19:
                                break;

                            case 20:
                                break;

                            case 21:
                                int cityId = await this.citiesService.IdByName(currentRow);

                                if (cityId != 0)
                                {
                                    //int cityId = await this.citiesService.IdByName(currentRow);
                                    newPharmacy.CityId = cityId;
                                }
                                else
                                {
                                    errorDictionary[i] = currentRow;
                                }
                                break;
                            }
                        }

                        await this.pharmaciesService.CreatePharmacy(newPharmacy);
                    }
                }
            }

            var pharmacyErrorModel = new CustomErrorDictionaryOutputModel
            {
                Errors = errorDictionary
            };

            return(this.View(pharmacyErrorModel));
        }
        public async Task <ActionResult> Upload(int brandexId,
                                                string name,
                                                PharmacyClass pharmacyClass,
                                                bool active,
                                                string companyName,
                                                string pharmacyChainName,
                                                string address,
                                                string cityName,
                                                int?pharmnetId,
                                                int?phoenixId,
                                                int?sopharmaId,
                                                int?stingId,
                                                string regionName
                                                )
        {
            if (brandexId != 0 &&
                name != null &&
                await this.companiesService.CheckCompanyByName(companyName) &&
                await this.pharmacyChainsService.CheckPharmacyChainByName(pharmacyChainName) &&
                await this.citiesService.CheckCityName(cityName) &&
                await this.regionsService.CheckRegionByName(regionName) &&
                address != null)
            {
                var pharmacyInputModel = new PharmacyInputModel
                {
                    BrandexId       = brandexId,
                    Name            = name,
                    PharmacyClass   = pharmacyClass,
                    Active          = active,
                    CompanyId       = await this.companiesService.IdByName(companyName),
                    PharmacyChainId = await this.pharmacyChainsService.IdByName(pharmacyChainName),
                    Address         = address,
                    CityId          = await this.citiesService.IdByName(cityName),
                    PharmnetId      = pharmnetId,
                    PhoenixId       = phoenixId,
                    SopharmaId      = sopharmaId,
                    StingId         = stingId,
                    RegionId        = await this.regionsService.IdByName(regionName)
                };

                if (await this.pharmaciesService.CreatePharmacy(pharmacyInputModel) != "")
                {
                    var pharmacyOutputModel = new PharmacyOutputModel
                    {
                        Name              = name,
                        PharmacyClass     = pharmacyClass.ToString(),
                        CompanyName       = companyName,
                        PharmacyChainName = pharmacyChainName,
                        Address           = address,
                        CityName          = cityName,
                        Region            = regionName,
                        BrandexId         = brandexId,
                        PharmnetId        = pharmnetId,
                        PhoenixId         = phoenixId,
                        SopharmaId        = sopharmaId,
                        StingId           = stingId
                    };

                    return(this.View(pharmacyOutputModel));
                }

                else
                {
                    return(Redirect("Index"));
                }
            }

            return(Redirect("Index"));
        }