Exemplo n.º 1
0
        public async Task <bool> WishlistEdit(WishlistEditDTO dto)
        {
            var rao = _mapper.Map <WishlistEditRAO>(dto);

            if (await _repository.WishlistEdit(rao))
            {
                return(true);
            }

            throw new NotImplementedException();
        }
Exemplo n.º 2
0
        public async Task BulkEditWishlist_WishlistDoesNotExistAddWish_ReturnsFail()
        {
            //Arrange
            var wishlistEdit = new WishlistEditDTO()
            {
                Id     = 12345,
                Wishes = new List <WishCreateDTO>()
                {
                    new WishCreateDTO()
                    {
                        Name  = "Wish 1",
                        Price = 1000
                    }
                }
            };

            //Act
            var operationResult = await wishlistService.BulkEditWishlist(wishlistEdit, UserId);


            //Assert
            Assert.AreEqual(OperationStatus.FAIL, operationResult.Status);
            Assert.IsNull(operationResult.Data);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Bulk edit for Wishlist
        /// </summary>
        /// <param name="wishlistEditDTO">Id of Wishlist</param>
        /// <param name="userId">Id of wishlist owner</param>
        /// <returns>OperationResult "Fail" if wishlist not found.</returns>
        public async Task <OperationResult <WishlistBulkEditDTO> > BulkEditWishlist(WishlistEditDTO wishlistEditDTO, string userId)
        {
            Guard.IsNull(wishlistEditDTO, nameof(wishlistEditDTO));
            Guard.IsNullEmptyOrWhiteSpace(userId, nameof(userId));

            var wishlist = await dbContext.Wishlists
                           .Include(w => w.User)
                           .Include(w => w.Wishes)
                           .Include(w => w.GiftGroup)
                           .FirstOrDefaultAsync(w => w.User.Auth0Id == userId && w.Id == wishlistEditDTO.Id);

            if (wishlist == null)
            {
                return new OperationResult <WishlistBulkEditDTO>
                       {
                           Message = MessageHelper.CreateEntityNotFoundMessage(nameof(WishList), wishlistEditDTO.Id),
                           Status  = OperationStatus.FAIL,
                           Data    = null
                       }
            }
            ;

            //SET GIFTGROUP
            var giftgroup = await dbContext.GiftGroups.FirstOrDefaultAsync(g => g.Id == wishlistEditDTO.Id);

            wishlist.GiftGroup = giftgroup;

            //ADD NEW GIFTS OR UPDATE
            if (wishlist.Wishes != null)
            {
                foreach (var wish in wishlistEditDTO.Wishes)
                {
                    if (wish.IsNew)
                    {
                        wishlist.Wishes.Add(new Wish()
                        {
                            Name = wish.Name, URL = wish.Link, Price = wish.Price
                        });
                    }
                    else
                    {
                        var giftToEdit = wishlist.Wishes.FirstOrDefault(g => g.Id == wish.Id);

                        if (giftToEdit != null)
                        {
                            giftToEdit.Name  = wish.Name;
                            giftToEdit.URL   = wish.Link;
                            giftToEdit.Price = wish.Price;
                        }
                    }
                }
            }

            try
            {
                await dbContext.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                if (ex is DbUpdateException || ex is DbUpdateConcurrencyException)
                {
                    return(new OperationResult <WishlistBulkEditDTO>()
                    {
                        Status = MessageHelper.CreateOperationErrorMessage(nameof(WishList), OperationType.update),
                        Data = null,
                    });
                }
                throw;
            }

            return(new OperationResult <WishlistBulkEditDTO>()
            {
                Status = OperationStatus.SUCCESS,
                Message = $"{nameof(WishList)} updated successful.",
                Data = new WishlistBulkEditDTO()
                {
                    Id = wishlist.Id
                }
            });
        }
Exemplo n.º 4
0
        public async Task<IActionResult> Put([FromBody] WishlistEditDTO wishlistEditDTO)
        {
            var operationResult = await wishlistService.BulkEditWishlist(wishlistEditDTO, User.SubjectId());

            return operationResult.Status == OperationStatus.SUCCESS ? Ok(operationResult) : NotFound(operationResult);
        }