public async Task <Result <DishView> > UpdateAsync(DishToUpdate dish, CancellationToken cancellationToken = default)
        {
            DishDB dishToUpdate = await _context.Dishes.IgnoreQueryFilters().Include(c => c.DishTags).ThenInclude(sc => sc.Tag).AsNoTracking().FirstOrDefaultAsync(_ => _.Id == dish.Id);

            if (dishToUpdate is null)
            {
                return(Result <DishView> .Quite <DishView>(ExceptionConstants.DISH_WAS_NOT_FOUND));
            }
            var dishTagList = await _context.DishTags.Where(_ => _.DishId == dish.Id).AsNoTracking().ToListAsync(cancellationToken);

            _context.DishTags.RemoveRange(dishTagList);
            dishToUpdate.DishTags.Clear();
            await _context.SaveChangesAsync(cancellationToken);

            try
            {
                dishToUpdate          = _mapper.Map <DishDB>(dish);
                dishToUpdate.Modified = DateTime.Now;

                _context.Entry(dishToUpdate).Property(c => c.Name).IsModified        = true;
                _context.Entry(dishToUpdate).Property(c => c.Composition).IsModified = true;
                _context.Entry(dishToUpdate).Property(c => c.Description).IsModified = true;
                _context.Entry(dishToUpdate).Property(c => c.Price).IsModified       = true;
                _context.Entry(dishToUpdate).Property(c => c.Weight).IsModified      = true;
                _context.Entry(dishToUpdate).Property(c => c.Sale).IsModified        = true;
                _context.Entry(dishToUpdate).Property(c => c.Modified).IsModified    = true;

                await UpdateTagLinks(dish.TagNames, dishToUpdate);

                await _context.SaveChangesAsync(cancellationToken);

                DishDB dishAfterAdding = await _context.Dishes.Where(_ => _.Id == dishToUpdate.Id).Include(c => c.DishTags).ThenInclude(sc => sc.Tag).Select(_ => _).AsNoTracking().FirstOrDefaultAsync(cancellationToken);

                DishView view = _mapper.Map <DishView>(dishAfterAdding);
                view.TagList = CollectTagList(dishAfterAdding.DishTags, cancellationToken).Result;
                return(Result <DishView> .Ok(_mapper.Map <DishView>(view)));
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return(Result <DishView> .Fail <DishView>(ExceptionConstants.CANNOT_UPDATE_MODEL + ex.Message));
            }
            catch (DbUpdateException ex)
            {
                return(Result <DishView> .Fail <DishView>(ExceptionConstants.CANNOT_UPDATE_MODEL + ex.Message));
            }
        }
Example #2
0
        public async Task <Result <TagToUpdate> > UpdateAsync(TagToUpdate tag, CancellationToken cancellationToken = default)
        {
            TagDB tagForUpdate = _mapper.Map <TagDB>(tag);

            tagForUpdate.Id = Guid.Parse(tag.Id);
            _context.Entry(tagForUpdate).Property(c => c.TagName).IsModified = true;

            try
            {
                await _context.SaveChangesAsync(cancellationToken);

                return(Result <TagToUpdate> .Ok(tag));
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return(Result <TagToUpdate> .Fail <TagToUpdate>(ExceptionConstants.CANNOT_UPDATE_MODEL + ex.Message));
            }
            catch (DbUpdateException ex)
            {
                return(Result <TagToUpdate> .Fail <TagToUpdate>(ExceptionConstants.CANNOT_UPDATE_MODEL + ex.Message));
            }
        }
        public async Task <Result <BasketView> > AddUpdateDishAsync(string dishId, string userIdFromIdentity, int quantity, CancellationToken cancellationToken = default)
        {
            DishDB dishToAdd = await _context.Dishes.Where(_ => _.Id == Guid.Parse(dishId)).Select(_ => _).AsNoTracking().FirstOrDefaultAsync(cancellationToken);

            if (dishToAdd is null)
            {
                return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.DISH_WAS_NOT_FOUND));
            }
            UserDB user = await _context.Users.Where(_ => _.IdFromIdentity == userIdFromIdentity).Select(_ => _).AsNoTracking().FirstOrDefaultAsync(cancellationToken);

            AppUser userIdentity = await _userManager.FindByIdAsync(userIdFromIdentity);

            if (user is null || userIdentity is null)
            {
                return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.USER_WAS_NOT_FOUND));
            }
            BasketDB basket = await _context.Baskets.Where(_ => _.UserId == user.Id).AsNoTracking().FirstOrDefaultAsync(cancellationToken);

            if (basket is null)
            {
                return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.BASKET_WAS_NOT_FOUND));
            }

            var connection = await _context.BasketDishes.Where(_ => _.BasketId == basket.Id && _.DishId == Guid.Parse(dishId)).Select(_ => _).AsNoTracking().FirstOrDefaultAsync(cancellationToken);

            if (connection is null)
            {
                BasketDishDB basketDish = new BasketDishDB()
                {
                    BasketId = basket.Id, DishId = dishToAdd.Id, Quantity = quantity, DishPrice = dishToAdd.Price, Sale = dishToAdd.Sale
                };
                _context.BasketDishes.Add(basketDish);
            }
            else
            {
                if (quantity > 0)
                {
                    connection.Quantity = quantity;
                    _context.Entry(connection).Property(c => c.Quantity).IsModified = true;
                }
                else // quantity == 0
                {
                    _context.BasketDishes.Remove(connection);
                }
            }

            basket.ModificationTime = DateTime.Now;
            _context.Entry(basket).Property(c => c.ModificationTime).IsModified = true;

            try
            {
                await _context.SaveChangesAsync(cancellationToken);

                var dishList = await _context.BasketDishes.Where(_ => _.BasketId == basket.Id).AsNoTracking().ToListAsync(cancellationToken);

                //BasketView view = CollectBasket(basket, dishList, userIdentity).Result.Data;
                //return Result<BasketView>.Ok(view);
                return(CollectBasket(basket, dishList, userIdentity).Result);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.CANNOT_SAVE_MODEL + ex.Message));
            }
            catch (DbUpdateException ex)
            {
                return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.CANNOT_SAVE_MODEL + ex.Message));
            }
            catch (ArgumentNullException ex)
            {
                return(Result <BasketView> .Fail <BasketView>(ExceptionConstants.SOURCE_IS_NULL + ex.Message));
            }
        }
        public async Task <Result <ReviewView> > AddAsync(ReviewToAdd review, string userIdFromIdentity, CancellationToken cancellationToken = default)
        {
            UserDB userDB = await _context.Users.Where(_ => _.IdFromIdentity == userIdFromIdentity).Select(_ => _).AsNoTracking().FirstOrDefaultAsync(cancellationToken);

            var reviewToAdd = _mapper.Map <ReviewDB>(review);

            reviewToAdd.UserId  = userDB.Id;
            reviewToAdd.OrderId = Guid.Parse(review.OrderId);
            _context.Reviews.Add(reviewToAdd);
            var rating = await _context.Rating.FirstOrDefaultAsync(cancellationToken);

            if (rating  is null)
            {
                rating = new RatingDB()
                {
                    Count = 0, Sum = 0
                };
                _context.Rating.Add(rating);
            }
            rating.Sum += reviewToAdd.Rating;
            rating.Count++;
            _context.Entry(rating).Property(c => c.Sum).IsModified   = true;
            _context.Entry(rating).Property(c => c.Count).IsModified = true;
            try
            {
                await _context.SaveChangesAsync(cancellationToken);

                ReviewDB reviewAfterAdding = await _context.Reviews.Where(_ => _.Id == reviewToAdd.Id).Select(_ => _).AsNoTracking().FirstOrDefaultAsync(cancellationToken);

                return(Result <ReviewView> .Ok(_mapper.Map <ReviewView>(reviewAfterAdding)));
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return(Result <ReviewView> .Fail <ReviewView>(ExceptionConstants.CANNOT_SAVE_MODEL + ex.Message));
            }
            catch (DbUpdateException ex)
            {
                return(Result <ReviewView> .Fail <ReviewView>(ExceptionConstants.CANNOT_SAVE_MODEL + ex.Message));
            }
            catch (ArgumentNullException ex)
            {
                return(Result <ReviewView> .Fail <ReviewView>(ExceptionConstants.SOURCE_IS_NULL + ex.Message));
            }
        }
        public async Task <Result <OrderView> > AddAsync(OrderToAdd order, string userIdFromIdentity, CancellationToken cancellationToken = default)
        {
            AppUser userIdentity = await _userManager.FindByIdAsync(userIdFromIdentity);

            UserDB userDB = await _context.Users.Where(_ => _.IdFromIdentity == userIdentity.Id).Select(_ => _).AsNoTracking().FirstOrDefaultAsync(cancellationToken);

            if (userDB is null || userIdentity is null)
            {
                return(Result <OrderView> .Fail <OrderView>(ExceptionConstants.USER_WAS_NOT_FOUND));
            }

            OrderDB orderToAdd = _mapper.Map <OrderDB>(order);

            if (orderToAdd.IsInfoFromProfile)
            {
                if (userIdentity.Address is null || userIdentity.PhoneNumber is null || userIdentity.Name is null || userIdentity.Surname is null)
                {
                    return(Result <OrderView> .Quite <OrderView>(NotificationConstans.FILL_PROFILE));
                }
                orderToAdd.Address     = userIdentity.Address;
                orderToAdd.PhoneNumber = userIdentity.PhoneNumber;
                orderToAdd.Name        = userIdentity.Name;
                orderToAdd.Surname     = userIdentity.Surname;
            }
            orderToAdd.PersonalDiscount = userIdentity.PersonalDiscount;
            orderToAdd.UserId           = userDB.Id;
            orderToAdd.Status           = Enum.GetName(typeof(OrderStatuses), 0);
            orderToAdd.UpdateTime       = DateTime.Now;
            orderToAdd.TotalCost        = 0;
            orderToAdd.DeliveryCost     = 0;

            var connections = await _context.BasketDishes.Where(_ => _.BasketId == userDB.BasketId).Select(_ => _).AsNoTracking().ToListAsync(cancellationToken);

            foreach (var connection in connections)
            {
                orderToAdd.TotalCost = orderToAdd.TotalCost.Value + connection.DishPrice.Value * (1 - (connection.Sale.Value / 100)) * connection.Quantity.Value;
            }
            orderToAdd.TotalCost *= 1 - userIdentity.PersonalDiscount / 100;
            orderToAdd.TotalCost  = Math.Round(orderToAdd.TotalCost.Value, 2);
            if (orderToAdd.TotalCost < NumberСonstants.FREE_DELIVERY_BORDER)
            {
                orderToAdd.DeliveryCost = NumberСonstants.DELIVERY_PRICE;
            }
            _context.Orders.Add(orderToAdd);

            foreach (var connection in connections)
            {
                connection.OrderId  = orderToAdd.Id;
                connection.BasketId = Guid.Empty;
                _context.Entry(connection).Property(c => c.OrderId).IsModified  = true;
                _context.Entry(connection).Property(c => c.BasketId).IsModified = true;
            }
            try
            {
                await _context.SaveChangesAsync();

                OrderDB orderAfterAdding = await _context.Orders.Where(_ => _.Id == orderToAdd.Id).Select(_ => _).AsNoTracking().FirstOrDefaultAsync(cancellationToken);

                var dishList = await _context.BasketDishes.Where(_ => _.OrderId == orderAfterAdding.Id).AsNoTracking().ToListAsync(cancellationToken);

                OrderView view  = CollectOrderView(orderAfterAdding).Result.Data;
                var       users = await _userManager.GetUsersInRoleAsync(AppIdentityConstants.ADMIN);

                foreach (var user in users)
                {
                    var sendEmailBefore = await _emailSender.SendEmailAsync(user.Email, EmailConstants.NEW_ORDER_SUBJECT, EmailConstants.NEW_ORDER_MESSAGE, cancellationToken);
                }
                return(Result <OrderView> .Ok(view));
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return(Result <OrderView> .Fail <OrderView>(ExceptionConstants.CANNOT_SAVE_MODEL + ex.Message));
            }
            catch (DbUpdateException ex)
            {
                return(Result <OrderView> .Fail <OrderView>(ExceptionConstants.CANNOT_SAVE_MODEL + ex.Message));
            }
            catch (ArgumentNullException ex)
            {
                return(Result <OrderView> .Fail <OrderView>(ExceptionConstants.SOURCE_IS_NULL + ex.Message));
            }
        }