public async Task <ResultVm <Unit> > Handle(Command request, CancellationToken cancellationToken) { var product = await _context.Products.Include(p => p.Pictures) .FirstOrDefaultAsync(x => x.Id == request.proId); if (product == null) { return(null); } var photo = product.Pictures.FirstOrDefault(x => x.Id == request.Id); if (photo == null) { return(null); } var currentMain = product.Pictures.FirstOrDefault(x => x.IsMain); if (currentMain != null) { currentMain.IsMain = false; } photo.IsMain = true; var success = await _context.SaveChangesAsync() > 0; if (success) { return(ResultVm <Unit> .Success(Unit.Value)); } return(ResultVm <Unit> .Failure("Problem setting main photo")); }
public async Task <ResultVm <Unit> > Handle(Command request, CancellationToken cancellationToken) { var product = await _context.Products.FindAsync(request.Product.Id); if (product == null) { return(null); } product.Name = request.Product.Name; product.Price = request.Product.Price; product.Description = request.Product.Description; product.BrandId = request.Product.BrandId; product.UpdatedDate = DateTime.Now; var result = await _context.SaveChangesAsync() > 0; if (!result) { return(ResultVm <Unit> .Failure("Failed to update Product")); } return(ResultVm <Unit> .Success(Unit.Value)); }
public async Task <ResultVm <Unit> > Handle(Command request, CancellationToken cancellationToken) { var product = await _context.Products.Include(p => p.Pictures) .FirstOrDefaultAsync(x => x.Id == request.proId); if (product.Pictures == null || product.Pictures.Count < 1) { return(ResultVm <Unit> .Success(Unit.Value)); } foreach (var item in product.Pictures) { var result = await _photoAccessor.DeletePhoto(item.Id); if (result == null) { return(ResultVm <Unit> .Failure("Problem deleting picture from Cloudinary")); } product.Pictures.Remove(item); _context.Remove(item); } var success = await _context.SaveChangesAsync() > 0; if (success) { return(ResultVm <Unit> .Success(Unit.Value)); } return(ResultVm <Unit> .Failure("Problem deleting all picture from API")); }
public async Task <ResultVm <Unit> > Handle(Command request, CancellationToken cancellationToken) { var product = await _context.Products.FindAsync(request.productId); if (product == null) { return(null); } var user = await _context.Users .FirstOrDefaultAsync(x => x.UserName == _userAccessor.GetUsername()); var cartItem = await _context.CartItems .FirstOrDefaultAsync(x => x.productId == request.productId && x.userId == user.Id); if (cartItem == null) { return(null); } _context.CartItems.Remove(cartItem); var result = await _context.SaveChangesAsync() > 0; if (result) { return(ResultVm <Unit> .Success(Unit.Value)); } return(ResultVm <Unit> .Failure("Problem removing item from cart")); }
public async Task <ResultVm <Picture> > Handle(Command request, CancellationToken cancellationToken) { var product = await _context.Products.Include(p => p.Pictures) .FirstOrDefaultAsync(x => x.Id == request.productId); if (product == null) { return(null); } var photoUploadResult = await _photoAccessor.AddPhoto(request.File); var photo = new Picture { Url = photoUploadResult.Url, Id = photoUploadResult.PublicId }; if (!product.Pictures.Any(x => x.IsMain)) { photo.IsMain = true; } product.Pictures.Add(photo); var result = await _context.SaveChangesAsync() > 0; if (result) { return(ResultVm <Picture> .Success(photo)); } return(ResultVm <Picture> .Failure("Problem with adding Picture")); }
public async Task <ResultVm <Unit> > Handle(Command request, CancellationToken cancellationToken) { var user = await _context.Users.FirstOrDefaultAsync(x => x.UserName == _userAccessor.GetUsername()); // Add order Order order = new Order { User = user, }; _context.Orders.Add(order); var result1 = await _context.SaveChangesAsync() > 0; if (!result1) { return(ResultVm <Unit> .Failure("#Checkout: Problem with result 1 -> adding new Order")); } // Add list of order detail foreach (var proID in request.productIds) { var product = await _context.Products.FindAsync(proID); if (product == null) { return(null); } var cart = await _context.CartItems.FirstOrDefaultAsync(x => x.productId == product.Id && x.userId == user.Id); if (cart == null) { return(null); } OrderDetail orderDetail = new OrderDetail { orderId = 1, Order = order, Product = product, price = product.Price, quantity = cart.quantity, totalPrice = product.Price * cart.quantity }; order.orders.Add(orderDetail); _context.CartItems.Remove(cart); } var result2 = await _context.SaveChangesAsync() > 0; if (result2) { return(ResultVm <Unit> .Success(Unit.Value)); } return(ResultVm <Unit> .Failure("Problem with checkout")); }
public async Task <ResultVm <CategoryVm> > Handle(Query request, CancellationToken cancellationToken) { var category = await _context.Categories .ProjectTo <CategoryVm>(_mapper.ConfigurationProvider) .FirstOrDefaultAsync(x => x.Id == request.Id); if (category == null) { return(ResultVm <CategoryVm> .Failure("Category not found!")); } return(ResultVm <CategoryVm> .Success(category)); }
public async Task <ResultVm <ProfileVm> > getProfile() { var client = _httpClientFactory.CreateClient(); var token = _httpContextAccessor.HttpContext.Request.Cookies["jwt"]; client.SetBearerToken(token); var response = await client.GetAsync(_config["API:Default"] + "/Profile"); if (response.IsSuccessStatusCode) { return(ResultVm <ProfileVm> .Success(await response.Content.ReadAsAsync <ProfileVm>())); } return(ResultVm <ProfileVm> .Failure(response.Content.ReadAsStringAsync().Result.ToString())); }
public async Task <ResultVm <UserVm> > postRegister(RegisterVm register) { //Send Json body var content = new StringContent(JsonConvert.SerializeObject(register) , Encoding.UTF8, "application/json"); var client = _httpClientFactory.CreateClient(); var response = await client.PostAsync(_config["API:Default"] + "/api/Account/register", content); if (response.IsSuccessStatusCode) { return(ResultVm <UserVm> .Success(await response.Content.ReadAsAsync <UserVm>())); } return(ResultVm <UserVm> .Failure(response.Content.ReadAsStringAsync().Result.ToString())); }
public async Task <ResultVm <string> > DeletFromCart(int productID) { var client = _httpClientFactory.CreateClient(); var token = _httpContextAccessor.HttpContext.Request.Cookies["jwt"]; client.SetBearerToken(token); var response = await client.DeleteAsync(_config["API:Default"] + $"/Profile/DeleteFromCart/{productID}"); if (response.IsSuccessStatusCode) { return(ResultVm <string> .Success("complete")); } return(ResultVm <string> .Failure("failed to add product to cart")); }
public async Task <ResultVm <string> > SetRating(int productID, double rate) { var client = _httpClientFactory.CreateClient(); var token = _httpContextAccessor.HttpContext.Request.Cookies["jwt"]; client.SetBearerToken(token); var response = await client.PostAsync(_config["API:Default"] + $"/Rate/{productID}?rate={rate}", null); if (response.IsSuccessStatusCode) { return(ResultVm <string> .Success("complete")); } return(ResultVm <string> .Failure("failed to rate a product")); }
public async Task <ResultVm <Unit> > Handle(Command request, CancellationToken cancellationToken) { var brand = new Brand { Name = request.brandFormVm.Name }; _context.Brands.Add(brand); var result = await _context.SaveChangesAsync() > 0; if (!result) { return(ResultVm <Unit> .Failure("Failed to create brand")); } return(ResultVm <Unit> .Success(Unit.Value)); }
public async Task <ResultVm <Unit> > Handle(Command request, CancellationToken cancellationToken) { var Product = await _context.Products.FindAsync(request.Id); // if (Product == null) return ResultVm<Unit>.Failure("Failed to find a product \r\nIn Deleting process"); _context.Products.Remove(Product); var result = await _context.SaveChangesAsync() > 0; if (!result) { return(ResultVm <Unit> .Failure("Failed to delete a product")); } return(ResultVm <Unit> .Success(Unit.Value)); }
public async Task <ResultVm <Unit> > Handle(Command request, CancellationToken cancellationToken) { var product = await _context.Products.FindAsync(request.productId); if (product == null) { return(null); } var user = await _context.Users .FirstOrDefaultAsync(x => x.UserName == _userAccessor.GetUsername()); var cartItem = await _context.CartItems .FirstOrDefaultAsync(x => x.productId == request.productId && x.userId == user.Id); var increase = request.quantity; if (request.quantity == 0) { increase = 1; } if (cartItem == null) { cartItem = new CartItem { Product = product, User = user, quantity = increase, }; _context.Add(cartItem); } else { cartItem.quantity = cartItem.quantity + increase; } var result = await _context.SaveChangesAsync() > 0; if (result) { return(ResultVm <Unit> .Success(Unit.Value)); } return(ResultVm <Unit> .Failure("Something wrong with your Cart")); }
public async Task <ResultVm <Unit> > Handle(Command request, CancellationToken cancellationToken) { if (request.rate > 5) { return(ResultVm <Unit> .Failure("Rate point is over 5")); } var product = await _context.Products.FindAsync(request.productID); var user = await _context.Users .FirstOrDefaultAsync(x => x.UserName == _userAccessor.GetUsername()); if (product == null) { return(null); } var rating = await _context.Rates .FirstOrDefaultAsync(x => x.productId == request.productID && x.userId == user.Id); //Toggle if rating exist or not if (rating == null) { rating = new Rate { product = product, user = user, rate = request.rate, }; _context.Rates.Add(rating); } else { rating.rate = request.rate; } var result = await _context.SaveChangesAsync() > 0; if (result) { return(ResultVm <Unit> .Success(Unit.Value)); } return(ResultVm <Unit> .Failure("Something wrong with your rating")); }
public async Task <ResultVm <Unit> > Handle(Command request, CancellationToken cancellationToken) { var Category = new Category { Name = request.categoryFormVm.Name }; _context.Categories.Add(Category); var result = await _context.SaveChangesAsync() > 0; if (!result) { return(ResultVm <Unit> .Failure("Failed to create category")); } return(ResultVm <Unit> .Success(Unit.Value)); }
public async Task <ResultVm <Unit> > Handle(Command request, CancellationToken cancellationToken) { var brand = await _context.Brands.FindAsync(request.Id); if (brand == null) { return(ResultVm <Unit> .Failure("Failed to delete Brand")); } _context.Brands.Remove(brand); var result = await _context.SaveChangesAsync() > 0; if (!result) { return(ResultVm <Unit> .Failure("Failed to delete Brand")); } return(ResultVm <Unit> .Success(Unit.Value)); }
public async Task <ResultVm <Unit> > Handle(Command request, CancellationToken cancellationToken) { var category = await _context.Categories.FindAsync(request.Id); if (category == null) { return(ResultVm <Unit> .Failure("Failed to find category \r\nIn deleting Process")); } _context.Remove(category); var result = await _context.SaveChangesAsync() > 0; if (!result) { return(ResultVm <Unit> .Failure("Failed to delete category")); } return(ResultVm <Unit> .Success(Unit.Value)); }
public async Task <ResultVm <Unit> > Handle(Command request, CancellationToken cancellationToken) { var product = await _context.Products.Include(p => p.Pictures) .FirstOrDefaultAsync(x => x.Id == request.proId); if (product == null) { return(null); } var picture = product.Pictures.FirstOrDefault(x => x.Id == request.Id); if (picture == null) { return(null); } if (picture.IsMain) { return(ResultVm <Unit> .Failure("You cannot delete your main picture")); } var result = await _photoAccessor.DeletePhoto(picture.Id); if (result == null) { return(ResultVm <Unit> .Failure("Problem deleting picture from Cloudinary")); } product.Pictures.Remove(picture); var success = await _context.SaveChangesAsync() > 0; if (success) { return(ResultVm <Unit> .Success(Unit.Value)); } return(ResultVm <Unit> .Failure("Problem deleting picture from API")); }
public async Task <ResultVm <int> > Handle(Command request, CancellationToken cancellationToken) { var product = new Product { Name = request.product.Name, Price = request.product.Price, Description = request.product.Description, BrandId = request.product.BrandId, CreatedDate = DateTime.Now }; foreach (var cate in request.product.CategoryName) { var category = await _context.Categories .FirstOrDefaultAsync(x => x.Name == cate); product.ProductCategories.Add( new CategoryProduct { Category = category, Product = product } ); } _context.Add(product); var result = await _context.SaveChangesAsync() > 0; if (!result) { return(ResultVm <int> .Failure("Failed to create Product")); } return(ResultVm <int> .Success(product.Id)); }