Example #1
0
        public async Task UpdateItemAsync(KudosShopItemDto dto)
        {
            var alreadyExists = await _kudosShopItemsDbSet
                                .AnyAsync(t => t.Name == dto.Name && t.OrganizationId == dto.OrganizationId && t.Id != dto.Id);

            if (alreadyExists)
            {
                throw new ValidationException(ErrorCodes.DuplicatesIntolerable, "Kudos shop item with that name already exists");
            }

            var itemToUpdate = await _kudosShopItemsDbSet
                               .FirstOrDefaultAsync(e => e.Id == dto.Id && e.OrganizationId == dto.OrganizationId);

            if (itemToUpdate == null)
            {
                throw new ValidationException(ErrorCodes.ContentDoesNotExist, "Item not found");
            }

            itemToUpdate.Name        = dto.Name;
            itemToUpdate.Price       = dto.Price;
            itemToUpdate.Description = dto.Description;
            itemToUpdate.PictureId   = dto.PictureId;

            await _uow.SaveChangesAsync(dto.UserId);
        }
Example #2
0
        public async Task CreateItemAsync(KudosShopItemDto dto)
        {
            var alreadyExists = await _kudosShopItemsDbSet
                                .AnyAsync(t => t.Name == dto.Name && t.OrganizationId == dto.OrganizationId);

            if (alreadyExists)
            {
                throw new ValidationException(PremiumErrorCodes.KudosShopItemAlreadyExist, "Kudos shop item already exists");
            }

            var modified = DateTime.UtcNow;

            var newItem = new KudosShopItem
            {
                Name           = dto.Name,
                Price          = dto.Price,
                Description    = dto.Description,
                CreatedBy      = dto.UserId,
                Created        = modified,
                Modified       = modified,
                ModifiedBy     = dto.UserId,
                OrganizationId = dto.OrganizationId,
                PictureId      = dto.PictureId
            };

            _kudosShopItemsDbSet.Add(newItem);

            await _uow.SaveChangesAsync(dto.UserId);
        }