Esempio n. 1
0
        public async Task TestCreateMarket_Failed_Error()
        {
            //Arrange
            var marketDetailsViewModel = new MarketDetailsViewModel()
            {
                MarketId   = 1,
                MarketName = "market",
            };
            var market = new Market()
            {
                Id = 1, Name = "Market1", IsActive = true, IsDeleted = false
            };
            var baseResult = new BaseResult <List <Market> >()
            {
                Result = new List <Market>()
                {
                    market
                }
            };

            iMarketLibrary.Setup(x => x.InsertEntity(It.IsAny <Market>())).Returns(Task.FromResult(new BaseResult <long>()
            {
                IsError          = true,
                ExceptionMessage = Helper.Common.GetMockException()
            })).Verifiable();

            //Act
            Task <BaseResult <Market> > actionResult = marketRepository.CreateMarket(marketDetailsViewModel, It.IsAny <string>());

            //Assert
            Assert.IsTrue(actionResult.Result.IsError);
            Assert.IsTrue(actionResult.Result.ExceptionMessage != null);
        }
        public void TestCreateMarket_Failed_BadRequest()
        {
            MarketDetailsViewModel marketDetailsViewModel = null;
            //Act
            var result = mockMarketontroller.CreateMarket(marketDetailsViewModel);

            //Assert
            mockMarketRepository.Verify();
            Assert.IsTrue(result.Result is BadRequestObjectResult);
            Assert.AreEqual(((BadRequestObjectResult)result.Result).StatusCode, 400);
        }
 /// <summary>
 /// Method to Map Market, MarketIncludedCountryRelation and MarketExcludedCountryRelation
 /// </summary>
 /// <param name="marketModel"></param>
 /// <param name="marketIncludedCountryRelationModel"></param>
 /// <param name="marketExcludedCountryRelationModel"></param>
 /// <param name="marketDetailsViewModel"></param>
 public static void MapForCreateMarket(ref Market marketModel, MarketDetailsViewModel marketDetailsViewModel, string userName)
 {
     marketModel = new Market();
     //Map Market
     marketModel             = AutoMapper.Mapper.Map <MarketDetailsViewModel, Market>(marketDetailsViewModel);
     marketModel.IsActive    = true;
     marketModel.IsDeleted   = false;
     marketModel.CreatedBy   = userName;
     marketModel.CreatedDate = DateTime.Now.JakartaOffset();;
     marketModel.UpdatedBy   = userName;
     marketModel.UpdatedDate = DateTime.Now.JakartaOffset();;
 }
        public void TestCreateMarket_Exception_InternalServerError()
        {
            MarketDetailsViewModel marketDetailsViewModel = new MarketDetailsViewModel();

            marketDetailsViewModel.MarketId   = 12;
            marketDetailsViewModel.MarketName = "market1";
            mockMarketRepository.Setup(a => a.CreateMarket(marketDetailsViewModel, It.IsAny <string>())).Returns(Task.FromResult(new BaseResult <Market> {
                IsError = true, ExceptionMessage = Helper.Common.GetMockException()
            }));
            Task <IActionResult> actionResult = mockMarketontroller.CreateMarket(marketDetailsViewModel);

            Assert.IsTrue(actionResult != null);
            Assert.AreEqual(((Microsoft.AspNetCore.Mvc.StatusCodeResult)actionResult.Result).StatusCode, 500);
        }
Esempio n. 5
0
        public async Task TestAddIncludedAndExcludedCountriesList_Failed_ByExcludedCountries_Error()
        {
            //Arrange
            MarketCountriesViewModel marketIncludedCountry = new MarketCountriesViewModel {
                CountryId = 7
            };
            MarketCountriesViewModel marketExcludedCountry = new MarketCountriesViewModel {
                CountryId = 45
            };
            var marketDetailsViewModel = new MarketDetailsViewModel()
            {
                MarketId   = 1,
                MarketName = "market"
            };

            marketDetailsViewModel.IncludedMarketList.Add(marketIncludedCountry);
            marketDetailsViewModel.ExcludedMarketList.Add(marketExcludedCountry);

            var market = new Market()
            {
                Id = 1, Name = "Market1", IsActive = true, IsDeleted = false
            };
            var baseResult = new BaseResult <List <Market> >()
            {
                Result = new List <Market>()
                {
                    market
                }
            };

            iMarketLibrary.Setup(x => x.InsertEntity(It.IsAny <Market>())).Returns(Task.FromResult(new BaseResult <long>()
            {
                Result = 2
            })).Verifiable();
            iMarketIncludedCountryRelationLibrary.Setup(x => x.InsertEntityList(It.IsAny <List <MarketIncludedCountryRelation> >())).Returns(Task.FromResult(new BaseResult <long>()
            {
                Result = 2
            })).Verifiable();
            iMarketExcludedCountryRelationLibrary.Setup(x => x.InsertEntityList(It.IsAny <List <MarketExcludedCountryRelation> >())).Returns(Task.FromResult(new BaseResult <long>()
            {
                IsError          = true,
                ExceptionMessage = Helper.Common.GetMockException()
            })).Verifiable();
            //Act
            Task <BaseResult <Market> > actionResult = marketRepository.CreateMarket(marketDetailsViewModel, It.IsAny <string>());

            //Assert
            Assert.IsTrue(actionResult.Result.IsError);
            Assert.IsTrue(actionResult.Result.ExceptionMessage != null);
        }
Esempio n. 6
0
        public async Task TestCreateMarket_Pass_Success()
        {
            //Arrange
            MarketCountriesViewModel marketIncludedCountry = new MarketCountriesViewModel {
                CountryId = 7
            };
            MarketCountriesViewModel marketExcludedCountry = new MarketCountriesViewModel {
                CountryId = 45
            };
            var marketDetailsViewModel = new MarketDetailsViewModel()
            {
                MarketId   = 1,
                MarketName = "market"
            };

            marketDetailsViewModel.IncludedMarketList.Add(marketIncludedCountry);
            marketDetailsViewModel.ExcludedMarketList.Add(marketExcludedCountry);

            var market = new Market()
            {
                Id = 1, Name = "Market1", IsActive = true, IsDeleted = false
            };
            var baseResult = new BaseResult <List <Market> >()
            {
                Result = new List <Market>()
                {
                    market
                }
            };

            iMarketLibrary.Setup(x => x.InsertEntity(It.IsAny <Market>())).Returns(Task.FromResult(new BaseResult <long>()
            {
                Result = 2
            })).Verifiable();
            iMarketIncludedCountryRelationLibrary.Setup(x => x.InsertEntityList(It.IsAny <List <MarketIncludedCountryRelation> >())).Returns(Task.FromResult(new BaseResult <long>()
            {
                Result = 2
            })).Verifiable();
            iMarketExcludedCountryRelationLibrary.Setup(x => x.InsertEntityList(It.IsAny <List <MarketExcludedCountryRelation> >())).Returns(Task.FromResult(new BaseResult <long>()
            {
                Result = 2
            })).Verifiable();
            //Act
            Task <BaseResult <Market> > actionResult = marketRepository.CreateMarket(marketDetailsViewModel, It.IsAny <string>());

            //Assert
            Assert.IsTrue(actionResult.Result != null);
            Assert.IsTrue(actionResult.Result is BaseResult <Market>);
        }
        public void TestCreateMarket_EmptyResult_NoContentResponse()
        {
            MarketDetailsViewModel marketDetailsViewModel = new MarketDetailsViewModel();

            marketDetailsViewModel.MarketId   = 12;
            marketDetailsViewModel.MarketName = "market1";
            mockMarketRepository.Setup(a => a.CreateMarket(marketDetailsViewModel, It.IsAny <string>())).Returns(Task.FromResult(new BaseResult <Market> {
                Result = new Market()
            }));
            mockMarketRepository.Setup(a => a.CreateMarket(marketDetailsViewModel, It.IsAny <string>())).Returns(Task.FromResult(new BaseResult <Market> {
                Result = null
            })).Verifiable();
            Task <IActionResult> actionResult = mockMarketontroller.CreateMarket(marketDetailsViewModel);

            Assert.IsTrue(actionResult != null);
            Assert.AreEqual(((Microsoft.AspNetCore.Mvc.StatusCodeResult)actionResult.Result).StatusCode, 204);
        }
Esempio n. 8
0
        /// <summary>
        /// Method to insert included and excluded countries
        /// </summary>
        /// <param name="marketDetailsViewModel"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public async Task <BaseResult <Market> > AddIncludedAndExcludedCountriesList(MarketDetailsViewModel marketDetailsViewModel, string userName)
        {
            BaseResult <Market> result = new BaseResult <Market>();

            result.Result = new Market();
            List <MarketIncludedCountryRelation> marketIncludedCountryRelationList = null;
            List <MarketExcludedCountryRelation> marketExcludedCountryRelationList = null;

            MarketRequestMapper.MapMarketIncludedAndExcludedCountryRelation(ref marketIncludedCountryRelationList, ref marketExcludedCountryRelationList, marketDetailsViewModel, userName);

            if (marketIncludedCountryRelationList != null && marketIncludedCountryRelationList.Any())
            {
                var includedCountryInsertResult = await iMarketIncludedCountryRelationLibrary.InsertEntityList(marketIncludedCountryRelationList).ConfigureAwait(false);

                if (includedCountryInsertResult.IsError || includedCountryInsertResult.ExceptionMessage != null)
                {
                    result.IsError          = true;
                    result.ExceptionMessage = includedCountryInsertResult.ExceptionMessage;
                    return(result);
                }
                else if (includedCountryInsertResult.Result > default(long))
                {
                    var excludedCountryInsertResult = await iMarketExcludedCountryRelationLibrary.InsertEntityList(marketExcludedCountryRelationList).ConfigureAwait(false);

                    if (excludedCountryInsertResult.IsError || excludedCountryInsertResult.ExceptionMessage != null)
                    {
                        result.IsError          = true;
                        result.ExceptionMessage = excludedCountryInsertResult.ExceptionMessage;
                        return(result);
                    }
                    else if (excludedCountryInsertResult.Result > default(long))
                    {
                        result.Result.Id = marketDetailsViewModel.MarketId;
                    }
                }
            }
            else
            {
                result.IsError   = true;
                result.ErrorCode = (int)coreHelper.Constants.ErrorCodes.EmptyModel;
                result.Message   = string.Format(coreHelper.Constants.ErrorMessage.EmptyModel);
                return(result);
            }
            return(result);
        }
        public async Task TestCreateMarket_Success_OkResponse()
        {
            MarketDetailsViewModel marketDetailsViewModel = new MarketDetailsViewModel();

            marketDetailsViewModel.MarketId   = 12;
            marketDetailsViewModel.MarketName = "market1";
            mockMarketRepository.Setup(a => a.CreateMarket(marketDetailsViewModel, It.IsAny <string>())).Returns(Task.FromResult(new BaseResult <Market>()
            {
                Result = new Market {
                    Id = 12, Name = "India", IsActive = true, IsDeleted = false
                }
            }));

            Task <IActionResult> actionResult = mockMarketontroller.CreateMarket(marketDetailsViewModel);
            BaseResult <Market>  marketIncludedAndExcludedCountriesList = (actionResult.Result as Microsoft.AspNetCore.Mvc.OkObjectResult).Value as BaseResult <Market>;

            Assert.AreEqual(((Microsoft.AspNetCore.Mvc.ObjectResult)actionResult.Result).StatusCode, 200);
            Assert.IsNotNull(marketIncludedAndExcludedCountriesList);
            Assert.IsTrue(!marketIncludedAndExcludedCountriesList.IsError);
            Assert.IsTrue(marketIncludedAndExcludedCountriesList.Result != null);
        }
Esempio n. 10
0
        /// <summary>
        /// Method to create new market
        /// </summary>
        /// <param name="marketDetailsViewModel"></param>
        /// <returns></returns>
        public async Task <BaseResult <Market> > CreateMarket(MarketDetailsViewModel marketDetailsViewModel, string userName)
        {
            BaseResult <Market> result = new BaseResult <Market>();

            result.Result = new Market();
            Market market = null;

            MarketRequestMapper.MapForCreateMarket(ref market, marketDetailsViewModel, userName);

            if (market != null)
            {
                //Add Market
                var insertResult = await iMarketLibrary.InsertEntity(market).ConfigureAwait(false);

                if (insertResult.IsError || insertResult.ExceptionMessage != null)
                {
                    result.IsError          = true;
                    result.ExceptionMessage = insertResult.ExceptionMessage;
                    return(result);
                }
                else if (insertResult.Result > default(long))
                {
                    //Assign Market Id
                    marketDetailsViewModel.MarketId = (int)insertResult.Result;
                    //Add Included and Excluded countries list
                    result = await AddIncludedAndExcludedCountriesList(marketDetailsViewModel, userName).ConfigureAwait(false);
                }
            }
            else
            {
                result.IsError   = true;
                result.ErrorCode = (int)coreHelper.Constants.ErrorCodes.EmptyModel;
                result.Message   = string.Format(coreHelper.Constants.ErrorMessage.EmptyModel);
                return(result);
            }
            return(result);
        }
Esempio n. 11
0
        public async Task <IActionResult> CreateMarket([FromBody] MarketDetailsViewModel marketDetailsViewModel)
        {
            BaseResult <Market> createMarketResult = new BaseResult <Market>();

            if (marketDetailsViewModel == null)
            {
                createMarketResult.IsError = true;
                createMarketResult.Message = string.Format(coreHelper.Constants.ErrorMessage.EmptyModel);
                return(BadRequest(createMarketResult)); //400
            }
            string userName = base.LoggedInUserName;

            createMarketResult = await iMarket.CreateMarket(marketDetailsViewModel, userName).ConfigureAwait(false);

            if (createMarketResult.IsError && createMarketResult.ExceptionMessage != null)
            {
                return(new StatusCodeResult(500));
            }
            else if (createMarketResult.Result == null)
            {
                return(new NoContentResult());
            }
            return(Ok(createMarketResult));
        }
Esempio n. 12
0
        /// <summary>
        /// Method to MapMarketIncludedAndExcludedCountryRelation
        /// </summary>
        /// <param name="marketIncludedCountryRelationList"></param>
        /// <param name="marketExcludedCountryRelationList"></param>
        /// <param name="marketDetailsViewModel"></param>
        /// <param name="userName"></param>
        public static void MapMarketIncludedAndExcludedCountryRelation(ref List <MarketIncludedCountryRelation> marketIncludedCountryRelationList, ref List <MarketExcludedCountryRelation> marketExcludedCountryRelationList, MarketDetailsViewModel marketDetailsViewModel, string userName)
        {
            marketIncludedCountryRelationList = new List <MarketIncludedCountryRelation>();
            marketExcludedCountryRelationList = new List <MarketExcludedCountryRelation>();

            //Map MarketIncludedCountryRelation
            foreach (var inCountry in marketDetailsViewModel.IncludedMarketList)
            {
                MarketIncludedCountryRelation marketIncludedCountryRelationModel = new MarketIncludedCountryRelation();
                //Map Market
                marketIncludedCountryRelationModel             = AutoMapper.Mapper.Map <MarketCountriesViewModel, MarketIncludedCountryRelation>(inCountry);
                marketIncludedCountryRelationModel.MarketId    = marketDetailsViewModel.MarketId;
                marketIncludedCountryRelationModel.IsActive    = true;
                marketIncludedCountryRelationModel.IsDeleted   = false;
                marketIncludedCountryRelationModel.CreatedBy   = userName;
                marketIncludedCountryRelationModel.CreatedDate = DateTime.Now.JakartaOffset();
                marketIncludedCountryRelationModel.UpdatedBy   = userName;
                marketIncludedCountryRelationModel.UpdatedDate = DateTime.Now.JakartaOffset();

                marketIncludedCountryRelationList.Add(marketIncludedCountryRelationModel);
            }

            //Map MarketExcludedCountryRelation
            foreach (var exCountry in marketDetailsViewModel.ExcludedMarketList)
            {
                MarketExcludedCountryRelation marketExcludedCountryRelationModel = new MarketExcludedCountryRelation();
                marketExcludedCountryRelationModel             = AutoMapper.Mapper.Map <MarketCountriesViewModel, MarketExcludedCountryRelation>(exCountry);
                marketExcludedCountryRelationModel.MarketId    = marketDetailsViewModel.MarketId;
                marketExcludedCountryRelationModel.IsActive    = true;
                marketExcludedCountryRelationModel.IsDeleted   = false;
                marketExcludedCountryRelationModel.CreatedBy   = userName;
                marketExcludedCountryRelationModel.CreatedDate = DateTime.Now.JakartaOffset();
                marketExcludedCountryRelationModel.UpdatedBy   = userName;
                marketExcludedCountryRelationModel.UpdatedDate = DateTime.Now.JakartaOffset();

                marketExcludedCountryRelationList.Add(marketExcludedCountryRelationModel);
            }
        }
Esempio n. 13
0
        public async Task <ActionResult> GetMarketDetails(int id)
        {
            //pass the id.Get request to api to get market details
            var searchDetails = context.MarketSearch.Where(s => s.Id == id).FirstOrDefault();
            var exdeatils     = context.MarketDetail.Where(i => i.SearchId == searchDetails.Id).FirstOrDefault();
            MarketDetailsViewModel detailsModel = new MarketDetailsViewModel
            {
                MarketSearch = new MarketSearch(),
                MarketDetail = new MarketDetail()
            };

            detailsModel.MarketSearch = searchDetails;
            using (var client = new HttpClient())
            {
                var url = @"http://search.ams.usda.gov/farmersmarkets/v1/data.svc/mktDetail?id=";
                url = url + searchDetails.SearchId;
                var response = await client.GetAsync(url);

                if (response.IsSuccessStatusCode)
                {
                    var stringDetails = await response.Content.ReadAsStringAsync();

                    var json       = JObject.Parse(stringDetails);
                    var j_mAddress = json["marketdetails"]["Address"].ToObject <string>();

                    var j_mGoogleLink = json["marketdetails"]["GoogleLink"].ToObject <string>();
                    //get Lat and Long from google link
                    string   getLatAndLong = j_mGoogleLink.Substring(j_mGoogleLink.LastIndexOf('/') + 4);
                    string[] splitLink     = getLatAndLong.Split('(');
                    if (splitLink.Length > 0)
                    {
                        string val          = splitLink[0];
                        int    indexPercent = val.IndexOf("%");
                        string g_Latitude   = val.Substring(0, indexPercent);

                        int    lastPercent20 = val.LastIndexOf("%20");
                        string subLongitude  = val.Substring(0, lastPercent20);

                        int    firstPercent20 = subLongitude.IndexOf("-");
                        string g_Longitude    = subLongitude.Substring(firstPercent20);


                        var j_mProducts = json["marketdetails"]["Products"].ToObject <string>();

                        //keep schedule but remove everything to the right of AND including semicolon
                        var j_mSchedule = json["marketdetails"]["Schedule"].ToObject <string>();
                        var subSchedule = j_mSchedule.Substring(0, j_mSchedule.IndexOf(';'));

                        MarketDetail marketDetails = new MarketDetail();
                        marketDetails.SearchId  = searchDetails.Id;
                        marketDetails.Address   = j_mAddress;
                        marketDetails.Latitude  = g_Latitude;
                        marketDetails.Longitude = g_Longitude;
                        marketDetails.Products  = j_mProducts;
                        marketDetails.Schedule  = subSchedule;

                        context.MarketDetail.Add(marketDetails);
                        context.SaveChanges();

                        detailsModel.MarketDetail = marketDetails;
                    }
                }
            }
            return(View(detailsModel));
        }