public async Task <ApiResult <bool> > RatingProduct(ProductRatingRequest request) { var rating = await _context.Ratings.Where(x => x.cus_id == request.cus_id && x.product_id == request.product_id).FirstOrDefaultAsync(); if (rating != null) { return(new ApiErrorResult <bool>("Bạn đã đánh giá sản phẩm này rồi, không thể đánh giá thêm lần nữa")); } var product = await _context.Products.FindAsync(request.product_id); if (product == null || product.isDelete == true) { return(new ApiErrorResult <bool>("Sản phẩm bạn đánh giá hiện không còn tồn tại")); } if (!product.isActive) { return(new ApiErrorResult <bool>("Sản phẩm này đang bị khóa, bạn không thể đánh giá sản phẩm này")); } var customer = await _context.Customers.FindAsync(request.cus_id); var newRating = new TechShopSolution.Data.Entities.Rating() { content = request.content, cus_id = request.cus_id, date_rating = DateTime.Now, product_id = request.product_id, score = request.score }; await _context.Ratings.AddAsync(newRating); await _context.SaveChangesAsync(); return(new ApiSuccessResult <bool>()); }
public async Task <IActionResult> RatingProduct(ProductRatingRequest request) { var result = await _customerService.RatingProduct(request); if (!result.IsSuccess) { return(BadRequest(result)); } return(Ok(result)); }
public async Task <ApiResult <bool> > RatingPoduct(ProductRatingRequest request) { var client = _httpClientFactory.CreateClient(); client.BaseAddress = new Uri(_configuration["BaseAddress"]); var json = JsonConvert.SerializeObject(request); var httpContent = new StringContent(json, Encoding.UTF8, "application/json"); var respone = await client.PostAsync($"/api/customer/rating", httpContent); var result = await respone.Content.ReadAsStringAsync(); if (respone.IsSuccessStatusCode) { return(JsonConvert.DeserializeObject <ApiSuccessResult <bool> >(result)); } else { return(JsonConvert.DeserializeObject <ApiErrorResult <bool> >(result)); } }