示例#1
0
        // [Authorize(oAppConst.AccessPolicies.LevelTwo)] /// Done
        public async Task <IActionResult> Get(
            int selectedPage,
            int maxNumberPerItemsPage,
            string searchValue       = "",
            string filterProductUnit = "",
            string filterStatus      = "")
        {
            try
            {
                bool.TryParse(filterStatus, out bool boolFilterStatus);
                ProductUnitType productUnitType = ProductUnitType.Grams;
                switch (filterProductUnit)
                {
                case nameof(ProductUnitType.Grams):
                    productUnitType = ProductUnitType.Grams;
                    break;

                case nameof(ProductUnitType.Kg):
                    productUnitType = ProductUnitType.Kg;
                    break;

                case nameof(ProductUnitType.PerItem):
                    productUnitType = ProductUnitType.PerItem;
                    break;

                default:
                    filterProductUnit = oAppConst.GetAllRecords;
                    break;
                }
                int totalCount = await DbContext.Categories
                                 .Where(c => filterProductUnit.Equals(oAppConst.GetAllRecords)?true : c.Unit == productUnitType)
                                 .Where(c => filterStatus.Equals(oAppConst.GetAllRecords) ? true : c.Status == boolFilterStatus)
                                 .CountAsync(c => searchValue.Equals(oAppConst.GetAllRecords) ? true : c.Name.Contains(searchValue))
                                 .ConfigureAwait(false);

                List <oCategory> list = await DbContext.Categories
                                        .OrderBy(c => c.Name)
                                        .Where(c => filterStatus.Equals(oAppConst.GetAllRecords) ? true : c.Status == boolFilterStatus)
                                        .Where(c => filterProductUnit.Equals(oAppConst.GetAllRecords) ? true : c.Unit == productUnitType)
                                        .Where(c => searchValue.Equals(oAppConst.GetAllRecords) ? true : c.Name.Contains(searchValue))
                                        .Skip((selectedPage - 1) * maxNumberPerItemsPage)
                                        .Take(maxNumberPerItemsPage)
                                        .ToListAsync()
                                        .ConfigureAwait(false);

                /// return the list of Categories
                return(Ok(new { list, totalCount }));
            }
            catch (Exception) //ArgumentNullException
            {
                /// in the case any exceptions return the following error
                oAppFunc.Error(ref ErrorsList, oAppConst.CommonErrors.ServerError);
                return(StatusCode(417, ErrorsList));
            }
        }
示例#2
0
        public void Update(string name, string description, ProductUnitType unit, decimal quantityPerUnit, bool isActive, decimal?defaultPrice = null)
        {
            this.Update();

            this.SetName(name);
            this.Description         = description;
            this.Unit                = unit;
            this.QuantityPerUnit     = quantityPerUnit < 0 ? throw new BadRequestException($"Quantity per unit can not be less than zero.") : quantityPerUnit;
            this.DefaultPricePerUnit = defaultPrice.HasValue ? new Money(defaultPrice.Value) : null;
            this.IsActive            = isActive;
        }
示例#3
0
        // [Authorize(oAppConst.AccessPolicies.LevelTwo)] /// Done
        public async Task <IActionResult> Get(
            int selectedPage,
            int maxNumberPerItemsPage,
            int filterProductStoreId,
            string filterProductCategory,
            string searchValue       = "",
            string filterProductUnit = "",
            string filterStatus      = "",
            bool isAccendingSort     = true,
            SortByType sortByType    = SortByType.product)
        {
            try
            {
                bool.TryParse(filterStatus, out bool boolFilterStatus);
                int.TryParse(filterProductCategory, out int filterProductCategoryId);
                ProductUnitType productUnitType = ProductUnitType.Grams;
                switch (filterProductUnit)
                {
                case nameof(ProductUnitType.Grams):
                    productUnitType = ProductUnitType.Grams;
                    break;

                case nameof(ProductUnitType.Kg):
                    productUnitType = ProductUnitType.Kg;
                    break;

                case nameof(ProductUnitType.PerItem):
                    productUnitType = ProductUnitType.PerItem;
                    break;

                default:
                    filterProductUnit = oAppConst.GetAllRecords;
                    break;
                }
                int totalCount = await DbContext.StoreProducts
                                 .Where(sp => filterProductStoreId.Equals(oAppConst.GetAllRecords)?true : sp.Store.Id == filterProductStoreId)
                                 .Where(sp => filterProductUnit.Equals(oAppConst.GetAllRecords) ? true : sp.Product.Unit == productUnitType)
                                 .Where(sp => filterProductCategory.Equals(oAppConst.GetAllRecords) ? true : sp.Product.Category.Id == filterProductCategoryId)
                                 .CountAsync(sp => searchValue.Equals(oAppConst.GetAllRecords) ? true : (sp.Product.Name.Contains(searchValue) || sp.Product.Id.ToString().Contains(searchValue)))
                                 .ConfigureAwait(false);

                /// Include the necessary properties
                IIncludableQueryable <oStoreProduct, oCategory> product = DbContext.StoreProducts
                                                                          .Include(sp => sp.Store)
                                                                          .Include(sp => sp.Product)
                                                                          .ThenInclude(p => p.Category);

                IQueryable sortedList;
                if (isAccendingSort)
                {
                    sortedList = sortByType switch
                    {
                        SortByType.category => product.OrderBy(sp => sp.Product.Category.Name),
                        SortByType.price => product.OrderBy(sp => sp.Product.Price),
                        SortByType.unit => product.OrderBy(sp => sp.Product.Unit),
                        SortByType.unitQuantity => product.OrderBy(sp => sp.Product.UnitQuantity),
                        SortByType.status => product.OrderBy(sp => sp.Status),
                        _ => product.OrderBy(sp => sp.Product.Name),
                    }
                }
                ;
                else
                {
                    sortedList = sortByType switch
                    {
                        SortByType.category => product.OrderByDescending(sp => sp.Product.Category.Name),
                        SortByType.price => product.OrderByDescending(sp => sp.Product.Price),
                        SortByType.unit => product.OrderByDescending(sp => sp.Product.Unit),
                        SortByType.unitQuantity => product.OrderByDescending(sp => sp.Product.UnitQuantity),
                        SortByType.status => product.OrderByDescending(sp => sp.Status),
                        _ => product.OrderByDescending(sp => sp.Product.Name),
                    }
                };

                List <oStoreProduct> list = await product
                                            .Where(sp => filterProductStoreId.Equals(oAppConst.GetAllRecords)?true : sp.Store.Id == filterProductStoreId)
                                            .Where(sp => filterProductUnit.Equals(oAppConst.GetAllRecords) ? true : sp.Product.Unit == productUnitType)
                                            .Where(sp => filterProductCategory.Equals(oAppConst.GetAllRecords) ? true : sp.Product.Category.Id == filterProductCategoryId)
                                            .Where(sp => searchValue.Equals(oAppConst.GetAllRecords) ? true : (sp.Product.Name.Contains(searchValue) || sp.Product.Id.ToString().Contains(searchValue)))
                                            .Skip((selectedPage - 1) * maxNumberPerItemsPage)
                                            .Take(maxNumberPerItemsPage)
                                            .ToListAsync()
                                            .ConfigureAwait(false);


                //int totalCount = await DbContext.Products
                //    .Where(c => filterProductUnit.Equals(oAppConst.GetAllRecords) ? true : c.Unit == productUnitType)
                //    .Where(c => filterProductCategory.Equals(oAppConst.GetAllRecords) ? true : c.Category.Id == filterProductCategoryId)
                //    .CountAsync(c => searchValue.Equals(oAppConst.GetAllRecords) ? true : c.Name.Contains(searchValue))
                //    .ConfigureAwait(false);

                //List<oProduct> list = await DbContext.Products
                //    .OrderBy(c => c.Name)
                //    .Where(c => filterProductUnit.Equals(oAppConst.GetAllRecords) ? true : c.Unit == productUnitType)
                //    .Where(c => filterProductCategory.Equals(oAppConst.GetAllRecords) ? true : c.Category.Id == filterProductCategoryId)
                //    .Include(t => t.StoreProducts)
                //    .Skip((selectedPage - 1) * maxNumberPerItemsPage)
                //    .Take(maxNumberPerItemsPage)
                //    .ToListAsync()
                //    .ConfigureAwait(false);
                //foreach (oProduct product in list)
                //{
                //    product.StoreProducts = product.StoreProducts.Where(t => t.StoreId == filterProductStoreId).ToList();
                //}
                /// return the list of Categories
                return(Ok(new { list, totalCount }));
            }
 public Product(string name, ProductUnitType unitType)
 {
     this.Name     = name;
     this.UnitType = unitType;
 }