public HttpResponseMessage NewReview([FromBody] ReviewCreateDTO review) { if (string.IsNullOrEmpty(review.Comment) && !review.Rating.HasValue) { ModelState.AddModelError("", "Rating or comment required"); } if (ModelState.IsValid) { Review entity = new Review { AnnouncementId = review.AnnouncementId, Comment = review.Comment, Username = review.Username, Rating = review.Rating, Postdate = DateTime.Now }; using (SummerCampDbContext ctx = new SummerCampDbContext()) { ctx.Reviews.Add(entity); ctx.SaveChanges(); } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, review); return(response); } HttpError error = new HttpError(ModelState, false); return(Request.CreateResponse(HttpStatusCode.BadRequest, error)); }
public ActionResult CreateReview([FromBody] ReviewCreateDTO reviewData) { if (!ModelState.IsValid) { return(BadRequest(new { Message = "La informacion de registro del proyecto son invalidos.", ErrorsCount = ModelState.ErrorCount, Errors = ModelState.Select(x => x.Value.Errors) })); } var newReview = this._mapper.Map <Review>(reviewData); if (newReview == null) { return(BadRequest(new { Error = "No se enviaron los datos esperados." })); } var tempReview = this._appDBContext.Reviews.Add(newReview); this._appDBContext.SaveChanges(); var reviewModel = this._mapper.Map <ReviewResponseDTO>(tempReview.Entity); return(Ok(new { Message = "Ok", Result = reviewModel })); }
public async Task <ActionResult <ReviewReadDTO> > CreateReview(ReviewCreateDTO reviewCreateDTO) { try { var review = _mapper.Map <Review>(reviewCreateDTO); var eduMatID = review.EduMaterialNavPointID; var eduMatById = await _db.EduMaterialNavPoints.GetById(eduMatID); if (eduMatById == null) { _logger.LogError($"POST api/reviews - Bad Request - EduMaterialNavPoint with id:{eduMatID} doesn't exists"); return(BadRequest($"Error - EduMaterialNavPoint with id:{eduMatID} doesn't exists")); } await _db.Reviews.Create(review); await _db.Save(); var reviewDTO = _mapper.Map <ReviewReadDTO>(review); _logger.LogInformation($"POST api/reviews - Review added to database"); return(CreatedAtAction(nameof(GetReviewById), new { id = reviewDTO.ID }, reviewDTO)); } catch (Exception ex) { _logger.LogError("GET api/authors - Problem with Database"); return(StatusCode(500, "Internal Server Error. Cannot connect wiht Database!")); } }
public async Task CreateReview() { var nameDB = Guid.NewGuid().ToString(); var context = BuildContext(nameDB); CreateMovies(nameDB); var movieId = context.Movies.Select(x => x.Id).First(); var context2 = BuildContext(nameDB); var mapper = ConfigurationAutoMapper(); var controller = new ReviewsController(context2, mapper); controller.ControllerContext = BuildControllerContext(); var reviewCreateDTO = new ReviewCreateDTO() { Mark = 5 }; var response = await controller.Post(movieId, reviewCreateDTO); var value = response as NoContentResult; Assert.IsNotNull(value); var context3 = BuildContext(nameDB); var reviewDB = context3.Reviews.First(); Assert.AreEqual(defaultUserId, reviewDB.UserId); }
public async Task UserCantReviews() { var nameDB = Guid.NewGuid().ToString(); var context = BuildContext(nameDB); CreateMovies(nameDB); var movieId = context.Movies.Select(x => x.Id).First(); var review1 = new Review() { MovieId = movieId, UserId = defaultUserId, Mark = 5 }; context.Add(review1); await context.SaveChangesAsync(); var context2 = BuildContext(nameDB); var mapper = ConfigurationAutoMapper(); var controller = new ReviewsController(context2, mapper); controller.ControllerContext = BuildControllerContext(); var reviewCreateDTO = new ReviewCreateDTO { Mark = 5 }; var response = await controller.Post(movieId, reviewCreateDTO); var value = response as IStatusCodeActionResult; Assert.AreEqual(400, value.StatusCode.Value); }
public async Task <ActionResult> Put(int reviewId, [FromBody] ReviewCreateDTO createDTO) { var userId = HttpContext.GetUserId(); var review = await context.Reviews.FirstOrDefaultAsync(x => x.Id == reviewId); if (review == null) { return(NotFound()); } if (review.UserId != userId) { return(BadRequest("User has not permissions to edit this review")); } review = mapper.Map(createDTO, review); await context.SaveChangesAsync(); return(NoContent()); }
public async Task <ActionResult> Post(int movieId, [FromBody] ReviewCreateDTO reviewCreateDTO) { var userId = HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value; var reviewExist = await _context.Reviews.AnyAsync(x => x.MovieId == movieId && x.UserId == userId); if (reviewExist) { return(BadRequest("El usuario ya ha escrito un review de ésta película")); } var review = _mapper.Map <Review>(reviewCreateDTO); review.UserId = userId; review.MovieId = movieId; _context.Add(review); await _context.SaveChangesAsync(); return(NoContent()); }
public async Task <ActionResult> Post(int movieId, [FromBody] ReviewCreateDTO createDTO) { var userId = HttpContext.GetUserId(); var reviewExists = await context.Reviews.AnyAsync(x => x.MovieId == movieId && x.UserId == userId); if (reviewExists) { return(BadRequest("User has already rate this movie")); } var review = mapper.Map <Review>(createDTO); review.MovieId = movieId; review.UserId = userId; context.Add(review); await context.SaveChangesAsync(); return(NoContent()); }
public async Task <ActionResult> Put(int reviewId, [FromBody] ReviewCreateDTO reviewCreateDTO) { var review = await _context.Reviews.FirstOrDefaultAsync(r => r.Id == reviewId); if (review == null) { return(NotFound()); } var userId = HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value; if (review.UserId != userId) { return(BadRequest("No tiene permisos de editar este review")); } review = _mapper.Map(reviewCreateDTO, review); await _context.SaveChangesAsync(); return(NoContent()); }