Пример #1
0
        public IEnumerable <ProductModel> GetInventory(OutletModel outletModel)
        {
            IEnumerable <ProductModel> response = new List <ProductModel>();

            try
            {
                List <int> outlets = new List <int>();
                // Return all outlets when no outlet selected
                if (outletModel.OutletId <= 0)
                {
                    // Get Outlet Ids of Person
                    outlets.AddRange(_outletManagerRepo.AsQueryable()
                                     .Where(x => x.UserId == outletModel.UserId && x.Status == CommonConstants.StatusTypes.Active)
                                     .Select(x => x.OutletId)
                                     .ToList());
                }
                else
                {
                    // Check if the person owns the outlet or not
                    Outlet outlet = _outletManagerRepo.Get(outletModel.OutletId);
                    if (outlet == null || outlet.UserId != outletModel.UserId)
                    {
                        return(null);
                    }

                    outlets.Add((int)outletModel.OutletId);
                }


                return(_productRepo.AsQueryable()
                       .Where(product => outlets.Contains(product.Category.OutletId) && product.Status == CommonConstants.StatusTypes.Active)
                       .Select(product => new ProductModel
                {
                    ProductId = product.ProductId,
                    ProductName = product.ProductName,
                    Quantity = product.Quantity,
                    RetailPrice = (int)product.RetailPrice,
                    OutletName = product.Category.Outlet.OutletName
                })
                       .Skip(outletModel.Skip)
                       .Take(outletModel.PageSize)
                       .ToList());
            }
            catch (Exception ex)
            {
                int pk;
                _productUnitTypeRepo.Rollback();

                if (!_crashLogRepo.AsQueryable().Any())
                {
                    pk = 0;
                }
                else
                {
                    pk = _crashLogRepo.GetMaxPK("CrashLogId") + 1;
                }

                string msg = (string.IsNullOrEmpty(ex.Message) || ex.Message.ToLower().Contains(CommonConstants.MsgInInnerException.ToLower()))
                            ? ex.InnerException.Message
                            : ex.Message;
                _crashLogRepo.Add(new Crashlog
                {
                    ClassName    = "ProductService",
                    MethodName   = "SellProduct",
                    ErrorMessage = ex.Message,
                    ErrorInner   = msg,
                    Data         = JsonSerializer.Serialize("string userId = " + outletModel.UserId + ", int? outletId = " + outletModel.OutletId),
                    TimeStamp    = DateTime.Now
                });
                return(null);
            }

            return(response);
        }