public async Task <ActionResult <CustomerBasket> > AddItemToBasket(int customerId, [FromBody] BasketItem item)
        {
            await _context.Database.MigrateAsync();

            var customerBasket = await _context.Baskets.Include(b => b.Items).FirstOrDefaultAsync(b => b.CustomerId == customerId);

            //if there is no basket for the desired customer, create it
            if (customerBasket == null)
            {
                customerBasket = new CustomerBasket {
                    CustomerId = customerId, Items = new List <BasketItem>()
                };
                _context.Baskets.Add(customerBasket);
                await _context.SaveChangesAsync();
            }

            //check if the item to be added already got added to the basket
            if (customerBasket.Items.Any(basketItem => basketItem.ProductId == item.ProductId))
            {
                //get the item and increase the count
                var product = customerBasket.Items.FirstOrDefault(basketItem => basketItem.ProductId == item.ProductId);
                product.Count++;
                await _context.SaveChangesAsync();

                return(customerBasket);
            }

            //then add it to the basket
            customerBasket.Items.Add(item);
            await _context.SaveChangesAsync();

            return(customerBasket);
        }
        /// <summary>
        /// Adds a new record to the database
        /// </summary>
        /// <param name="entity">Entity instance of the record to be added</param>
        /// <returns>Entity instance of the added record</returns>
        public async Task <T> AddAsync(T entity)
        {
            _dbContext.Set <T>().Add(entity);
            await _dbContext.SaveChangesAsync();

            return(entity);
        }
        public async Task <ActionResult <Wrapper <BasketEntity> > > PostBasketEntity(BasketEntity basketEntity)
        {
            _context.Baskets.Add(basketEntity);
            await _context.SaveChangesAsync();

            return(new Wrapper <BasketEntity> {
                Data = basketEntity, Success = true
            });
        }
Beispiel #4
0
        public async Task When <TMessage>(
            TMessage message,
            Func <TMessage, BasketDbContext, CancellationToken, Task> when,
            Action onCompleted                  = null,
            Action <Exception> onFailed         = null,
            CancellationToken cancellationToken = default)

        {
            Func <Task> internalOnCompleted;

            try
            {
                await when(message, _basketDbContext, cancellationToken);

                internalOnCompleted = PrepareCompleteAction(_basketDbContext);

                await _basketDbContext.SaveChangesAsync(cancellationToken);
            }
            catch (Exception ex)
            {
                onFailed?.Invoke(ex);

                throw;
            }

            await internalOnCompleted();

            onCompleted?.Invoke();
        }
Beispiel #5
0
 public async Task <int> SaveChangesasync(System.Threading.CancellationToken cancellationToken = default)
 {
     try
     {
         return(await context.SaveChangesAsync(cancellationToken));
     }
     catch (Exception ex)
     {
         return(0);
     }
 }
        private async Task <int> SaveChangesAsync()
        {
            try
            {
                return(await _context.SaveChangesAsync());
            }

            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        public async Task <bool> SaveAsync()
        {
            try
            {
                await _context.SaveChangesAsync();

                return(true);
            }
            catch
            {
                return(false);
            }
        }
        public async Task Handle(ProductChangedIntegrationEvent @event)
        {
            _logger.LogInformation("EVERNT => ProductChangedIntegrationEventHandler ");
            _logger.LogInformation($"EVERNT => ProductChangedIntegrationEventHandler => evernt object: {Newtonsoft.Json.JsonConvert.SerializeObject(@event)}");
            var basketItems = await _context.Baskets.Where(x => x.ProductId == @event.ProductId).ToArrayAsync();

            foreach (var item in basketItems)
            {
                item.ProductName = @event.Name;
                item.UnitPrice   = @event.Price;
                item.PictureUrl  = @event.PictureUrl;
            }
            _context.UpdateRange(basketItems);
            await _context.SaveChangesAsync();
        }
        public async Task <bool> Handle(DeleteItemCommand command, CancellationToken cancellationToken)
        {
            var items      = _dbContext.Set <Item>();
            var itemFromDb = await items.SingleAsync(x => x.ItemId == command.ItemId, cancellationToken : cancellationToken);

            _dbContext.Items.Remove(itemFromDb);

            var result = await _dbContext.SaveChangesAsync(cancellationToken);

            await _itemCreatedNotificationHandler.Handle(new ItemDeletedMessage
            {
                ItemId = command.ItemId
            },
                                                         cancellationToken);

            return(result > 0);
        }
        public async Task <ItemDto> Handle(AddItemCommand command, CancellationToken cancellationToken)
        {
            var itemFromCommand = _mapper.Map <Item>(command);

            var items      = _dbContext.Set <Item>();
            var itemFromDb = await items.SingleOrDefaultAsync(x => x.ItemId == command.ItemId, cancellationToken : cancellationToken);

            if (itemFromDb != null)
            {
                _mapper.Map(itemFromCommand, itemFromDb);
                items.Update(itemFromDb);
            }
            else
            {
                await _dbContext.Items.AddAsync(itemFromCommand, cancellationToken);
            }

            await _dbContext.SaveChangesAsync(cancellationToken);

            await _itemCreatedNotificationHandler.Handle(new ItemCreatedMessage(), cancellationToken);

            return(_mapper.Map <ItemDto>(itemFromCommand));
        }
 public async Task <bool> SaveChanges()
 {
     return(await _basketDbContext.SaveChangesAsync() > 0);
 }
Beispiel #12
0
        public async Task AddBasketEvent(BasketChangeEvent basketChangeEvent)
        {
            await _basketDbContext.BasketChangeEvents.AddAsync(basketChangeEvent);

            await _basketDbContext.SaveChangesAsync();
        }
Beispiel #13
0
        public async Task <ResponseModel> AddItem(int customerId, int productId, int amount)
        {
            try
            {
                var checkCustomer = await _customerService.CustomerExist(customerId);

                var checkProduct = await _productService.ProductExist(productId);

                if (checkCustomer.Data.Equals(false) || checkProduct.Data.Equals(false))
                {
                    return(checkCustomer.Data.Equals(false) ? checkCustomer : checkProduct);
                }

                var customer = await _customerService.GetCustomers(customerId);

                var product = (Product)(await _productService.GetProducts(productId)).Data;

                var checkStock = CheckStock(product.ProductStock, amount);
                if (checkStock.Data.Equals(false))
                {
                    return(checkStock);
                }

                if (await _db.Baskets.AnyAsync(b =>
                                               b.Customer.CustomerId == customerId && b.BasketProducts.ProductId == productId))
                {
                    var updateResponse = await UpdateBasket(customerId, productId, amount);

                    return(updateResponse);
                }

                await _db.Baskets.AnyAsync(b =>
                                           b.Customer.CustomerId == customerId && b.BasketProducts.ProductId == productId);

                var basketProducts = new BasketProduct
                {
                    ProductId = product.Id, ProductName = product.ProductName, Amount = amount
                };

                var basket = new Data.Domain.Basket
                {
                    Customer = (Customer)customer.Data, BasketProducts = basketProducts
                };

                await _db.Baskets.AddAsync(basket);

                await _db.SaveChangesAsync();

                return(new ResponseModel
                {
                    Message = "Başarılı",
                    Status = ResponseStatus.Success,
                    Data = basket
                });
            }
            catch (Exception e)
            {
                _logger.Error("AddItem Error: " + e.Message);
                return(new ResponseModel
                {
                    Message = e.Message,
                    Status = ResponseStatus.Error,
                    Data = null
                });
            }
        }