public async Task Update(Guid id, ProductModel model)
        {
            var query = _readOnlyRepository.Query <Product>(x => x.Id.Equals(id))
                        .ProjectTo <ProductModel>(_mapper.ConfigurationProvider);

            var result = await _readOnlyRepository.SingleAsync(query);

            if (result != null)
            {
                //result.Id = id;
                //result.ProductName = model.ProductName;
                //result.Description = model.Description;
                //result.Characteristics = model.Characteristics;
                //result.Price = model.Price;
                ////result.Category = model.Category;
                //result.CategoryId = model.CategoryId;
                var entity = _mapper.Map <Product>(model);
                entity.Id         = id;
                entity.CategoryId = model.CategoryId;
                _repository.Update(entity);

                await _repository.SaveChangesAsync();
            }
            else
            {
                throw EntityNotFoundException.For <Product>(id);
            }
        }
        public async Task <ProductsModel> Get(Guid id)
        {
            var query = _readOnlyRepository.Query <Product>(x => x.Id == id && x.Status == EnabledStatus.Enabled)
                        .ProjectTo <ProductsModel>(_mapper.ConfigurationProvider);

            var queryCategory = _readOnlyRepository.Query <ProductCat>(x => x.Status == EnabledStatus.Enabled && x.ProductId == id, y => y.Category).ToList();



            var result = await _readOnlyRepository.SingleAsync(query);

            if (result == null)
            {
                throw EntityNotFoundException.For <Product>(id);
            }


            if (result.ProductDetails != null)
            {
                result.ProductDetails = result.ProductDetails.Where(x => x.Status == EnabledStatus.Enabled).ToList();
            }


            result.Categories = new List <Category>();

            queryCategory.ForEach(x => {
                result.Categories.Add(new Category {
                    Id          = x.Category.Id,
                    Name        = x.Category.Name,
                    Description = x.Category.Description
                });
            });
            return(result);
        }
        public static async Task <Example> FindEnabledExample(this IRepositoryBase repository, Guid id)
        {
            var entity = await repository.SingleAsync <Example>(x => x.Id == id && x.Status == EnabledStatus.Enabled);

            if (entity == null)
            {
                throw EntityNotFoundException.For <Example>(id);
            }

            return(entity);
        }
示例#4
0
        public async Task Delete(Guid idcart, Guid iddetail)
        {
            var entity = await _repository.FirstAsync <CartItem>(x => x.CartID == idcart && x.ProductDetailsId == iddetail);

            if (entity == null)
            {
                throw EntityNotFoundException.For <Cart>(idcart);
            }

            _repository.Remove(entity);
            await _repository.SaveChangesAsync();
        }
        public async Task Update(Guid id, ProductDetailsModel ProductDetails)
        {
            var entity = await _repository.FirstAsync <ProductDetails>(x => x.Id == id);

            if (entity == null)
            {
                throw EntityNotFoundException.For <ProductDetails>(id);
            }

            _mapper.Map(ProductDetails, entity);
            await _repository.SaveChangesAsync();
        }
示例#6
0
        protected override async Task Handle(Edit <AddExampleModel> command, CancellationToken cancellationToken)
        {
            var entity = await _repository.FirstAsync <Example>(x => x.Id == command.Id);

            if (entity == null)
            {
                throw EntityNotFoundException.For <Example>(command.Id);
            }

            _mapper.Map(command.Model, entity);
            await _repository.SaveChangesAsync(cancellationToken);
        }
        public async Task Delete(Guid id)
        {
            var entity = await _repository.FirstAsync <Product>(x => x.Id == id);

            if (entity == null)
            {
                throw EntityNotFoundException.For <Product>(id);
            }
            entity.Status = EnabledStatus.Deleted;

            _repository.Update(entity);
            await _repository.SaveChangesAsync();
        }
        public async Task <ProductModel> Get(Guid id)
        {
            var query = _readOnlyRepository.Query <Product>(x => x.Id.Equals(id))
                        .ProjectTo <ProductModel>(_mapper.ConfigurationProvider);

            var result = await _readOnlyRepository.SingleAsync(query);

            if (result == null)
            {
                throw EntityNotFoundException.For <Product>(id);
            }

            return(result);
        }
        public async Task <OrderModel> Get(Guid orderId)
        {
            var query = _readOnlyRepository.Query <Order>(x => x.Id == orderId)
                        .ProjectTo <OrderModel>(_mapper.ConfigurationProvider);

            var result = await _readOnlyRepository.SingleAsync(query);

            if (result == null)
            {
                throw EntityNotFoundException.For <Order>(orderId);
            }

            return(result);
        }
示例#10
0
        public async Task <ExampleModel> Get(Guid id)
        {
            var query = _readOnlyRepository.Query <Example>(x => x.Id == id && x.Status == EnabledStatus.Enabled)
                        .ProjectTo <ExampleModel>(_mapper.ConfigurationProvider);

            var result = await _readOnlyRepository.SingleAsync(query);

            if (result == null)
            {
                throw EntityNotFoundException.For <Example>(id);
            }

            return(result);
        }
        public async Task <ExampleModel> Handle(GetExampleQuery request, CancellationToken cancellationToken)
        {
            var query = _readOnlyRepository.Query <Example>(x => x.Id == request.Id && x.Status == EnabledStatus.Enabled)
                        .ProjectTo <ExampleModel>(_mapper.ConfigurationProvider);

            var result = await _readOnlyRepository.SingleAsync(query, cancellationToken);

            if (result == null)
            {
                throw EntityNotFoundException.For <Example>(request.Id);
            }

            return(result);
        }
示例#12
0
        //Get cart by UserId
        public async Task <CartModel> Get(Guid id)
        {
            var query = _readOnlyRepository.Query <Cart>(x => x.UserId == id && x.Status == EnabledStatus.Enabled)
                        .ProjectTo <CartModel>(_mapper.ConfigurationProvider);

            var result = await _readOnlyRepository.SingleAsync(query);

            var CartId = result.Id;

            result.CartItems = _readOnlyRepository.Query <CartItem>(x => x.CartID == CartId && x.Status == EnabledStatus.Enabled, i => i.ProductDetail.Product).ToList();

            if (result == null)
            {
                throw EntityNotFoundException.For <Cart>(id);
            }

            return(result);
        }
        public async Task Delete(Guid id)
        {
            var query = _readOnlyRepository.Query <Product>(x => x.Id.Equals(id))
                        .ProjectTo <ProductModel>(_mapper.ConfigurationProvider);

            var result = await _readOnlyRepository.SingleAsync(query);

            if (result != null)
            {
                product.Id              = result.Id;
                product.ProductName     = result.ProductName;
                product.Description     = result.Description;
                product.Characteristics = result.Characteristics;
                product.Price           = result.Price;
                //product.Category = result.Category;
                product.CategoryId = result.CategoryId;
                _repository.Remove(product);
                await _repository.SaveChangesAsync();
            }
            else
            {
                throw EntityNotFoundException.For <Product>(id);
            }
        }
示例#14
0
        public async Task Update(Guid id, ProductsModel product)
        {
            var entity = await _repository.FirstAsync <Product>(x => x.Id == id);

            if (entity == null)
            {
                throw EntityNotFoundException.For <Product>(id);
            }
            _mapper.Map(product, entity);
            entity.ImgSource = this.ImageResize(product.ImgSource, 225, 225);

            //Want to update the product even if the categories or product details (that are optional) are not updated
            await _repository.SaveChangesAsync();

            //Updating categories.
            bool validator = true;

            if (product.ProductCatRelation == null)
            {
                product.ProductCatRelation = new List <ProductCat>();
            }

            var dbCategories  = _repository.Query <ProductCat>(x => x.ProductId == id).ToList();
            var newCategories = new List <Category>();

            try
            {
                newCategories = product.Categories.ToList();
            }
            catch (Exception e)
            {
                Console.WriteLine($"An exception have been thrown {e.StackTrace}");
                validator = false;
            }

            if (validator)
            {
                if (dbCategories.Count == newCategories.Count)
                {
                    for (int i = 0; i < dbCategories.Count; i++)
                    {
                        if (dbCategories[i].CategoryId != newCategories[i].Id)
                        {
                            validator = false;
                            break;
                        }
                    }
                }
                else
                {
                    validator = false;
                }

                if (!validator)
                {
                    for (int i = 0; i < dbCategories.Count; i++)
                    {
                        var aux = dbCategories[i];
                        _repository.Remove(aux);
                    }
                    for (int i = 0; i < newCategories.Count; i++)
                    {
                        _repository.Add(new ProductCat()
                        {
                            CategoryId = newCategories[i].Id, ProductId = id
                        });
                    }
                }
            }
            //Want to update the categories even if product details (that are optional) are not updated
            await _repository.SaveChangesAsync();



            //Updating productDetails.
            validator = true;
            bool same = true;

            var dbDetails  = _repository.Query <ProductDetails>(x => x.ProductId == id && x.Status == EnabledStatus.Enabled).ToList();
            var newDetails = new List <ProductDetails>();

            try
            {
                newDetails = product.ProductDetails.ToList();
            }
            catch (Exception e)
            {
                Console.WriteLine($"An exception have been thrown {e.StackTrace}");
                validator = false;
            }

            if (dbDetails.Count <= 0 && !validator)
            {
                return;
            }
            //Delete all product details case.
            if (dbDetails.Count > 0 && !validator)
            {
                for (int i = 0; i < dbDetails.Count; i++)
                {
                    dbDetails[i].Status = EnabledStatus.Deleted;
                    _repository.Update(dbDetails[i]);
                }
                await _repository.SaveChangesAsync();

                return;
            }

            if (dbDetails.Count == newDetails.Count)
            {
                for (int i = 0; i < dbDetails.Count; i++)
                {
                    if (!newDetails.Contains(dbDetails[i]))
                    {
                        same = false;
                        break;
                    }
                }
                //Updating if the list are with the same product details.
                if (same)
                {
                    for (int i = 0; i < dbDetails.Count; i++)
                    {
                        for (int y = 0; y < newDetails.Count; y++)
                        {
                            if (dbDetails[i].Id == newDetails[y].Id)
                            {
                                dbDetails[i].Price        = newDetails[y].Price;
                                dbDetails[i].Type         = newDetails[y].Type;
                                dbDetails[i].Availability = newDetails[y].Availability;
                                _repository.Update(dbDetails[i]);
                            }
                        }
                    }
                    await _repository.SaveChangesAsync();

                    return;
                }
            }
            else
            {
                same = false;
            }

            //Not same items in the list case.
            if (!same)
            {
                var foundSome = false;
                //Deleting in case a item in the new details is not in the db details (Deleted).
                for (int i = 0; i < dbDetails.Count; i++)
                {
                    if (!newDetails.Contains(dbDetails[i]))
                    {
                        dbDetails[i].Status = EnabledStatus.Deleted;
                        _repository.Update(dbDetails[i]);
                        foundSome = true;
                    }
                }
                if (foundSome)
                {
                    await _repository.SaveChangesAsync();
                }


                //Updating new details.
                bool changed = false;
                for (int i = 0; i < newDetails.Count; i++)
                {
                    //Searching for same items and updating them.
                    for (int y = 0; y < dbDetails.Count; y++)
                    {
                        if (dbDetails[y].Id == newDetails[i].Id)
                        {
                            dbDetails[y].Price        = newDetails[i].Price;
                            dbDetails[y].Type         = newDetails[i].Type;
                            dbDetails[y].Availability = newDetails[i].Availability;
                            _repository.Update(dbDetails[y]);
                            //await _repository.SaveChangesAsync();
                            changed = true;
                            break;
                        }
                    }

                    //In case a item is not in the data base already is added to.
                    if (!changed)
                    {
                        _repository.Add(newDetails[i]);
                        //await _repository.SaveChangesAsync();
                        changed = false;
                    }
                    else
                    {
                        changed = false;
                    }
                }
            }
            await _repository.SaveChangesAsync();
        }
示例#15
0
        public async Task <CartModel> Update(Guid id, CartModel cart)
        {
            var entity = await _repository.FirstAsync <Cart>(x => x.UserId == id);

            var cartID = entity.Id;

            if (entity == null)
            {
                throw EntityNotFoundException.For <Cart>(id);
            }
            var dbCartItems    = _repository.Query <CartItem>(x => x.CartID == cartID, y => y.ProductDetail).ToList();
            var frontCartItems = cart.CartItems.ToList();

            //If there is not items in the database cart, proceed to insert the items from front end directly.
            if (dbCartItems.Count < 1)
            {
                foreach (var item in frontCartItems)
                {
                    _repository.Add(new CartItem()
                    {
                        CartID           = cartID,
                        ProductDetailsId = item.ProductDetail.Id,
                        Quantity         = item.Quantity,
                        UnitPrice        = item.UnitPrice
                    });
                    await _repository.SaveChangesAsync();
                }

                goto Finish;
            }
            //If the list is equal in lenght and items provided, proceed to update the items
            //in quantity desired
            var same = true;

            if (frontCartItems.Count == dbCartItems.Count)
            {
                frontCartItems.ForEach(fitem =>
                {
                    if (!dbCartItems.Contains(fitem))
                    {
                        same = false;
                    }
                });

                if (same)
                {
                    for (int i = 0; i < dbCartItems.Count; i++)
                    {
                        for (int y = 0; y < frontCartItems.Count; y++)
                        {
                            if (dbCartItems[i].Equals(frontCartItems[y]))
                            {
                                if ((dbCartItems[i].Quantity + frontCartItems[y].Quantity) > dbCartItems[i].ProductDetail.Availability)
                                {
                                    dbCartItems[i].Quantity = dbCartItems[i].ProductDetail.Availability;
                                    await _repository.SaveChangesAsync();
                                }
                                else
                                {
                                    dbCartItems[i].Quantity = dbCartItems[i].Quantity + frontCartItems[y].Quantity;
                                    await _repository.SaveChangesAsync();
                                }
                            }
                        }
                    }
                    goto Finish;
                }
            }

            //If the list is not equal proceed to check which items aren't in the list and which items are in the list
            //In case a item is not in the list it will be added
            //In case a item is in the list it will be updated in quantity
            bool changed = false;

            for (int i = 0; i < frontCartItems.Count; i++)
            {
                //Searching for same items and updating them.
                for (int y = 0; y < dbCartItems.Count; y++)
                {
                    if (dbCartItems[y].Equals(frontCartItems[i]))
                    {
                        if ((dbCartItems[y].Quantity + frontCartItems[i].Quantity) > dbCartItems[y].ProductDetail.Availability)
                        {
                            dbCartItems[y].Quantity = dbCartItems[y].ProductDetail.Availability;
                        }
                        else
                        {
                            dbCartItems[y].Quantity = dbCartItems[y].Quantity + frontCartItems[i].Quantity;
                        }
                        _repository.Update(dbCartItems[y]);
                        await _repository.SaveChangesAsync();

                        changed = true;
                        break;
                    }
                }

                //In case a cartitem is not in the data base already is added to.
                if (!changed)
                {
                    _repository.Add(new CartItem()
                    {
                        CartID           = cartID,
                        ProductDetailsId = frontCartItems[i].ProductDetail.Id,
                        Quantity         = frontCartItems[i].Quantity,
                        UnitPrice        = frontCartItems[i].UnitPrice
                    });
                    await _repository.SaveChangesAsync();

                    changed = false;
                }
                else
                {
                    changed = false;
                }
            }
            goto Finish;


Finish:
            var query = _readOnlyRepository.Query <Cart>(x => x.UserId == id && x.Status == EnabledStatus.Enabled)
                        .ProjectTo <CartModel>(_mapper.ConfigurationProvider);

            var result = await _readOnlyRepository.SingleAsync(query);

            var CartId = result.Id;

            result.CartItems = _readOnlyRepository.Query <CartItem>(x => x.CartID == CartId && x.Status == EnabledStatus.Enabled, i => i.ProductDetail.Product).ToList();

            if (result == null)
            {
                throw EntityNotFoundException.For <Cart>(id);
            }

            return(result);
        }