public async Task <IActionResult> Delete([FromRoute] Guid productId) { if (ModelState.IsValid) { logger.LogDebug($"Attempting to remove product with productId {productId} from wishlist."); var userId = loginService.GetLoggedInUserId(); var wishlistEntity = new WishlistEntity { UserId = userId, ProductId = productId }; if (!await WishlistService.DoesWishlistItemExistsAsync(wishlistEntity)) { logger.LogDebug($"Failed to remove product with id {productId} from wishlist. There is no such product on the wishlist"); return(BadRequest("The item already is not on the wishlist")); } await WishlistService.DeleteAsync(wishlistEntity); logger.LogInformation($"Successfully removed product from the wishlist."); return(Ok(wishlistEntity)); } else { logger.LogWarning($"Erorr while removing product from Wishlist. Validation failed"); return(BadRequest(ModelState.ToApiV1ErrorResponseModel())); } }
public async Task <bool> DoesWishlistItemExistsAsync(WishlistEntity entity) { logger.LogDebug($"Attempting to check if a wishlist already exists."); return(await EntityFrameworkDynamicQueryableExtensions.CountAsync(dbContext.Wishlists .Where(x => x.ProductId == entity.ProductId) .Where(x => x.UserId == entity.UserId)) > 0); }
public async Task DeleteAsync(WishlistEntity entity) { logger.LogDebug($"Attempting to remove wishlist with product id {entity.ProductId}"); using (var transaction = await dbContext.Database.BeginTransactionAsync()) { dbContext.Wishlists.Remove(entity); await dbContext.SaveChangesAsync(); transaction.Commit(); } logger.LogDebug($"Successfully removed wishlist with product id {entity.ProductId}"); }
public async Task <IActionResult> AddProduct([FromRoute] Guid productId) { if (ModelState.IsValid) { logger.LogDebug($"Attempting to add new wishlist with productId {productId}."); if (!await productService.DoesProductExistByIdAsync(productId)) { logger.LogDebug($"Failed to add product with id {productId} to a wishlist. The given Product id doesn't exist at all."); return(BadRequest("The product Id doesn't exist")); } var userId = loginService.GetLoggedInUserId(); var wishlistEntity = new WishlistEntity { UserId = userId, ProductId = productId }; if (await WishlistService.DoesWishlistItemExistsAsync(wishlistEntity)) { logger.LogDebug($"Failed to add product with id {productId} to a wishlist. The item already is on the wishlist"); return(Conflict("The item already is on the wishlist")); } await WishlistService.AddAsync(wishlistEntity); logger.LogInformation($"Successfully added new product to the wishlist."); return(Ok(wishlistEntity)); } else { logger.LogWarning($"Erorr while adding product. Validation failed"); return(BadRequest(ModelState.ToApiV1ErrorResponseModel())); } }
public virtual async Task <CommerceCommand> Process(CommerceContext commerceContext, WishlistModel wishlistModel) { AddWishlistCommand addWishlistCommand = this; try { var customerEntity = await _getCustomerCommand.Process(commerceContext, wishlistModel.CustomerId); if (customerEntity != null) { if (customerEntity.HasComponent <WishlistComponent>()) { var component = customerEntity.GetComponent <WishlistComponent>(); // Checking if product is already added to list or not bool isProductAdded = false; if (component.WishlistCollection == null) { component.WishlistCollection = new List <WishlistEntity>(); } else { isProductAdded = component.WishlistCollection.Any(x => x.ProductId == wishlistModel.ProductId); } if (!isProductAdded) { WishlistEntity wishlistEntity = new WishlistEntity() { ProductId = wishlistModel.ProductId, ProductTitle = wishlistModel.ProductTitle, ProductPrice = wishlistModel.ProductPrice }; component.WishlistCollection.Add(wishlistEntity); customerEntity.SetComponent(component); commerceContext.Logger.LogInformation($"AddWishlistCommand for customer id: {wishlistModel.CustomerId} added product Id: {wishlistModel.ProductId}"); } else { commerceContext.Logger.LogInformation($"AddWishlistCommand for customer id: {wishlistModel.CustomerId} NOT ADDED product Id: {wishlistModel.ProductId} as it already exists"); } } else { WishlistComponent wishlistComponent = new WishlistComponent(); wishlistComponent.WishlistCollection = new List <WishlistEntity>(); WishlistEntity wishlistEntity = new WishlistEntity() { ProductId = wishlistModel.ProductId, ProductTitle = wishlistModel.ProductTitle, ProductPrice = wishlistModel.ProductPrice }; wishlistComponent.WishlistCollection.Add(wishlistEntity); customerEntity.SetComponent(wishlistComponent); } await this._persistEntityPipeline.Run(new PersistEntityArgument(customerEntity), commerceContext.PipelineContext); } } catch (Exception ex) { commerceContext.Logger.LogError($"Exception occured in getting customer { ex.StackTrace} and id is {ex.Message}"); } return(addWishlistCommand); }
public async Task AddAsync(WishlistEntity entity) { await dbContext.Wishlists.AddAsync(entity); await dbContext.SaveChangesAsync(); }