Esempio n. 1
0
        public async Task GetListings_ShouldReturnProductListings_WhenListingsExistAsync_Happy()
        {
            ProductListingData productListing1 = new ProductListingData();

            productListing1.ProductListingId = 1;
            productListing1.FarmerId         = 1;
            productListing1.Quantity         = 20;
            productListing1.Price            = 50;
            productListing1.ProductTypeId    = 1;

            ProductListingData productListing2 = new ProductListingData();

            productListing2.ProductListingId = 2;
            productListing2.FarmerId         = 2;
            productListing2.Quantity         = 30;
            productListing2.Price            = 40;
            productListing2.ProductTypeId    = 2;
            var testListings = new List <ProductListingData>()
            {
                productListing1,
                productListing2
            };

            //Arrange
            productDataMock.Setup(x => x.GetProductListings()).ReturnsAsync(testListings);
            //Act
            List <ProductListingData> testResult = (List <ProductListingData>) await productServices.GetProductListings();

            //Assert
            CollectionAssert.AreEqual(testListings, testResult);
        }
Esempio n. 2
0
        /// <summary>
        /// Common method to get Farmer details based on farmer id in productListing from database
        /// </summary>
        /// <param name="productListing"></param>
        /// <returns></returns>
        public async Task <FarmerDetails> GetFarmerDetailsByProductListing(ProductListingData productListing)
        {
            FarmerDetails farmer = new FarmerDetails();
            Users         user   = new Users();

            try
            {
                farmer = await(Task <FarmerDetails>)(from farmerDetails in _OrchardContext.FarmerDetails
                                                     join userData in _OrchardContext.Users on farmerDetails.UserId equals userData.UserId
                                                     where farmerDetails.FarmerId == productListing.FarmerId
                                                     select farmerDetails).FirstAsync();
                user = await(Task <Users>)(from farmerDetails in _OrchardContext.FarmerDetails
                                           join userDetails in _OrchardContext.Users on farmerDetails.UserId equals userDetails.UserId
                                           where farmerDetails.FarmerId == productListing.FarmerId
                                           select userDetails).FirstAsync();
            }
            catch (Exception)
            {
                throw new GeneralException("Something went wrong");
            }
            farmer.User = new Users()
            {
                UserFullName = user.UserFullName, Location = user.Location
            };
            return(farmer);
        }
Esempio n. 3
0
        /// <summary>
        /// Method to add prdouctListings
        /// </summary>
        /// <param name="productListingData"></param>
        /// <returns></returns>
        public async Task <bool> AddProductListings(ProductListingData productListingData)
        {
            try
            {
                int           userId = productListingData.FarmerId;
                FarmerDetails farmer = await _ApnaAaharContext.FarmerDetails.SingleOrDefaultAsync(farmerFound => farmerFound.UserId == userId);

                int FramerId = farmer.FarmerId;
                productListingData.FarmerId = FramerId;
                int rowAffected = 0;
                _ApnaAaharContext.Add(productListingData);
                rowAffected = await _ApnaAaharContext.SaveChangesAsync();

                if (rowAffected == 0)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
            catch (SqlException ex)
            {
                throw new AnySqlException("Internal Error Occured!", ex);
            }
            catch (Exception ex)
            {
                throw new GeneralException("Unchecked error occured", ex);
            }
        }
Esempio n. 4
0
        public async Task AddContactRequest_ShouldReturnBool_WhenRequestAdded_BadFlow()
        {
            Users user = new Users();
            ProductListingData productListingData = new ProductListingData();

            _userRepositoryMock.Setup(u => u.AddContactRequest(productListingData, user)).ThrowsAsync(new DataNotSavedException());
            Boolean addedRequest = await _userServices.AddContactRequest(productListingData, user);

            Assert.IsFalse(addedRequest);
        }
Esempio n. 5
0
        public async Task AddContactRequest_ShouldReturnBool_WhenRequestAdded_HappyFlow()
        {
            Users user = new Users();
            ProductListingData productListingData = new ProductListingData();

            _userRepositoryMock.Setup(u => u.AddContactRequest(productListingData, user)).ReturnsAsync(true);
            Boolean addedRequest = await _userServices.AddContactRequest(productListingData, user);

            Assert.IsTrue(addedRequest);
        }
Esempio n. 6
0
 public async Task <bool> AddContactRequest(ProductListingData productListing, Users user)
 {
     try
     {
         return(await _UserData.AddContactRequest(productListing, user));
     }
     catch (DataNotSavedException)
     {
         return(await Task.FromResult(false));
     }
 }
Esempio n. 7
0
        public void AddContactRequest_ShouldReturnBool_Sad()
        {
            Users user = new Users();

            user.UserId = 1;
            ProductListingData productListingData = new ProductListingData();

            productListingData.ProductListingId = 1;
            Seed(_orchard1Context);
            var result = userData.AddContactRequest(productListingData, user);

            Assert.IsFalse(result.Result);
        }
Esempio n. 8
0
        public void AddContactRequest_ShouldReturnBool_Happy()
        {
            _orchard1Context.Database.EnsureDeleted();
            Users user = new Users();

            user.UserId = 3;
            ProductListingData productListingData = new ProductListingData();

            productListingData.ProductListingId = 1;
            Seed(_orchard1Context);
            var result = userData.AddContactRequest(productListingData, user);

            Assert.IsTrue(result.Result);
        }
Esempio n. 9
0
        public void AddProdcutData_ShouldThrowException_WhenExceptionExist_Bad()
        {
            Orchard1Context.Database.EnsureDeleted();
            ProductListingData productListing1 = new ProductListingData();

            productListing1.FarmerId      = 1;
            productListing1.Quantity      = 20;
            productListing1.Price         = 50;
            productListing1.ProductTypeId = 1;
            SeedFarmerDetails();
            orchardContext.Setup(o => o.ProductListingData).Throws(new AnySqlException());
            Assert.ThrowsExceptionAsync <AnySqlException>(async() => await mockedProductData.AddProductListings(productListing1));
            Orchard1Context.Database.EnsureDeleted();
        }
Esempio n. 10
0
        public async Task AddProductListings_ShouldThrowException_WhenExceptionExistAsync_Bad()
        {
            ProductListingData productListing1 = new ProductListingData();

            productListing1.ProductListingId = 1;
            productListing1.FarmerId         = 1;
            productListing1.Quantity         = 20;
            productListing1.Price            = 50;
            productListing1.ProductTypeId    = 1;

            productDataMock.Setup(t => t.AddProductListings(It.IsAny <ProductListingData>())).Throws(new AnySqlException("Server error occured!"));

            await Assert.ThrowsExceptionAsync <AnySqlException>(async() => await productServices.AddProductListings(productListing1));
        }
Esempio n. 11
0
        public async Task AddContactRequest_ShouldThrowException_WhenException_Bad()
        {
            Users user = new Users();

            user.UserId = 3;
            ProductListingData productListingData = new ProductListingData();

            productListingData.ProductListingId = 1;
            //Arrange
            _mockedOrchardContext.Setup(o => o.ProductListingData).Throws(new GeneralException());
            //Act
            //Assert
            await Assert.ThrowsExceptionAsync <DataNotSavedException>(async() => await _mockedUserData.AddContactRequest(productListingData, user));
        }
Esempio n. 12
0
        public async Task AddProdcutData_ShouldReturnTrue_WhenProductAdded_Happy()
        {
            Orchard1Context.Database.EnsureDeleted();
            ProductListingData productListing1 = new ProductListingData();

            productListing1.FarmerId      = 1;
            productListing1.Quantity      = 20;
            productListing1.Price         = 50;
            productListing1.ProductTypeId = 1;
            SeedFarmerDetails();
            bool result = await productData.AddProductListings(productListing1);

            Assert.IsTrue(result);
            Orchard1Context.Database.EnsureDeleted();
        }
Esempio n. 13
0
        public ProductListingData ProductListings()
        {
            ProductListingData productListingData = new ProductListingData
            {
                Price         = 100,
                FarmerId      = 1,
                Quantity      = 100,
                ProductTypeId = 2,

                Farmer      = FarmersData(),
                ProductType = ProductData(),
            };

            return(productListingData);
        }
Esempio n. 14
0
        public async Task AddProductListings_ShouldReturnTrue_WhenProductNotAddedAsync_Sad()
        {
            ProductListingData productListing1 = new ProductListingData();

            productListing1.ProductListingId = 1;
            productListing1.FarmerId         = 1;
            productListing1.Quantity         = 20;
            productListing1.Price            = 50;
            productListing1.ProductTypeId    = 1;

            productDataMock.Setup(t => t.AddProductListings(It.IsAny <ProductListingData>())).ReturnsAsync(false);

            bool result = await productServices.AddProductListings(productListing1);

            Assert.IsFalse(result);
        }
Esempio n. 15
0
        /// <summary>
        /// Common method to get ProductType details based on producttype id in productListing from database
        /// </summary>
        /// <param name="productListing"></param>
        /// <returns></returns>
        public async Task <ProductType> GetProductTypeByProductListing(ProductListingData productListing)
        {
            ProductType productType = new ProductType();

            try
            {
                productType = await(Task <ProductType>)(from productTypeData in _OrchardContext.ProductType
                                                        where productTypeData.ProductTypeId == productListing.ProductTypeId
                                                        select productTypeData).FirstAsync();
            }
            catch (Exception)
            {
                throw new GeneralException("Something went wrong");
            }

            return(productType);
        }
Esempio n. 16
0
 public async Task <ActionResult> AddProductListings(ProductListingData productListingData)
 {
     try
     {
         return(Ok(await _IProductServices.AddProductListings(productListingData)));
     }
     catch (AnySqlException ex)
     {
         return(BadRequest(ex.Message));
     }
     catch (GeneralException ex)
     {
         return(BadRequest(ex.Message));
     }
     catch (Exception)
     {
         return(BadRequest("Internal Error Occured!"));
     }
 }
Esempio n. 17
0
        /// <summary>
        /// method to update product listing data
        /// </summary>
        /// <param name="productListingDataNew"></param>
        /// <returns>int based on savechanges</returns>
        public async Task <int> UpdateProductListings(ProductListingData productListingDataNew)
        {
            try
            {
                ProductListingData productListingDataOld = await _ApnaAaharContext.ProductListingData
                                                           .FindAsync(productListingDataNew.ProductListingId);

                productListingDataOld.Price    = productListingDataNew.Price;
                productListingDataOld.Quantity = productListingDataNew.Quantity;
                return(await _ApnaAaharContext.SaveChangesAsync());
            }
            catch (SqlException ex)
            {
                throw new AnySqlException("Internal Error Occured!", ex);
            }
            catch (Exception ex)
            {
                throw new GeneralException("Unchecked error occured", ex);
            }
        }
Esempio n. 18
0
        public async Task <bool> AddContactRequest(ProductListingData productListing, Users user)
        {
            try
            {
                ContactRequest contactRequest = await(from req in _ApnaAaharContext.ContactRequest where req.ProductListingId == productListing.ProductListingId && req.BuyerId == user.UserId select req).FirstOrDefaultAsync();
                if (contactRequest == null)
                {
                    await _ApnaAaharContext.ContactRequest.AddAsync(new ContactRequest()
                    {
                        BuyerId = user.UserId, ProductListingId = productListing.ProductListingId
                    });
                }
            }
            catch (Exception)
            {
                throw new DataNotSavedException();
            }
            int results = await _ApnaAaharContext.SaveChangesAsync();

            return((results > 0) ? true : false);
        }
Esempio n. 19
0
 /// <summary>
 /// service method to update product listing
 /// </summary>
 /// <param name="productListingData"></param>
 /// <returns></returns>
 public async Task <int> UpdateProductListings(ProductListingData productListingData)
 {
     return(await _ProductData.UpdateProductListings(productListingData));
 }
Esempio n. 20
0
 /// <summary>
 /// Method to add prdouctListings
 /// </summary>
 /// <param name="productListingData"></param>
 /// <returns></returns>
 public async Task <bool> AddProductListings(ProductListingData productListingData)
 {
     return(await _ProductData.AddProductListings(productListingData));
 }