public async Task <ItemDto> Handle(UpdateItemCommand command, CancellationToken cancellationToken)
        {
            var productDbSet = _dbContext.Set <Item>();
            var product      = await productDbSet.SingleOrDefaultAsync(x => x.ItemId == command.ProductId, cancellationToken : cancellationToken);

            if (product == null)
            {
                throw new NotImplementedException(command.ProductId.ToString());
            }

            product.Name = command.NewProductName;

            var entity = productDbSet.Update(product);

            return(_mapper.Map <ItemDto>(entity.Entity));
        }
        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);
        }
Ejemplo n.º 3
0
        public async Task <GetItemsCommandResponse> Handle(GetItemsCommand command, CancellationToken cancellationToken)
        {
            var query = _dbContext.Set <Item>();

            var totalOfItems = await query.Where(i => i.Username == command.Username).CountAsync(cancellationToken);

            var items = await query.AsNoTracking()
                        .OrderBy(x => x.Code)
                        .Skip((command.PageIndex - 1) * command.PageSize)
                        .Take(command.PageSize)
                        .ProjectTo <ItemDto>(_mapper.ConfigurationProvider)
                        .Where(i => i.Username == command.Username)
                        .ToListAsync(cancellationToken);

            var result = new GetItemsCommandResponse
            {
                Items        = items,
                TotalOfItems = totalOfItems,
            };

            return(result);
        }
        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));
        }
Ejemplo n.º 5
0
 public EntityEntry <TEntity> Add(TEntity entity)
 {
     return(_context.Set <TEntity>().Add(entity));
 }
Ejemplo n.º 6
0
 public BaseRepository(BasketDbContext _context)
 {
     this.context = _context;
     this.dbSet   = context.Set <T>();
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Queries a specific record of a database entity
 /// </summary>
 /// <param name="id">ID of the entity required</param>
 /// <returns>The entity with the specific ID provided.
 /// If not found then returns null</returns>
 public virtual async Task <T> GetByIdAsync(int id)
 {
     return(await _dbContext.Set <T>().FindAsync(id));
 }