public IActionResult PostRatingLog([FromBody] RatingLogForEntityDTO ratingDTO) { _requestLogService.SaveRequest(User.Identity.GetUserId(), "POST", "api/v1/ratingLogs", "PostRatingLog"); if (!ModelState.IsValid) { return(BadRequest("Invalid fields provided, please double check the parameters")); } if (ratingDTO.RestaurantId != null) { var existingRestaurantRating = _ratingLogService.GetRestaurantRatingLog(ratingDTO.RestaurantId, User.Identity.GetUserId()); if (existingRestaurantRating.Comment != null && existingRestaurantRating.Rating != null) { return(BadRequest("User has already given this restaurant a rating.")); } var newRating = _ratingLogService.AddNewRatingLog(ratingDTO, User.Identity.GetUserId()); return(CreatedAtAction(nameof(GetRatingLog), new { id = newRating.RatingLogId }, newRating)); } if (ratingDTO.DishId != null) { var existingDishRating = _ratingLogService.GetDishRatingLog(ratingDTO.DishId, User.Identity.GetUserId()); if (existingDishRating.Comment != null && existingDishRating.Rating != null) { return(BadRequest("User has already given this dish a rating.")); } var newRating = _ratingLogService.AddNewRatingLog(ratingDTO, User.Identity.GetUserId()); return(CreatedAtAction(nameof(GetRatingLog), new { id = newRating.RatingLogId }, newRating)); } return(BadRequest("No dish or restaurant id provided to rate.")); }
public RatingLog Create(RatingLogForEntityDTO ratingLogDTO) { return(new RatingLog { Rating = ratingLogDTO.Rating, Comment = ratingLogDTO.Comment, RestaurantId = ratingLogDTO.RestaurantId, DishId = ratingLogDTO.DishId }); }
public RatingLogDTO AddNewRatingLog(RatingLogForEntityDTO ratingLogDTO, string userId) { var newRatingLog = _ratingLogFactory.Create(ratingLogDTO); newRatingLog.UserId = userId; _uow.RatingLogs.Add(newRatingLog); _uow.SaveChanges(); return(_ratingLogFactory.Create(newRatingLog)); }
public RatingLogDTO UpdateRatingLog(int id, RatingLogForEntityDTO updatedRatingLogDTO) { if (_uow.RatingLogs.Exists(id)) { RatingLog ratingLog = _uow.RatingLogs.Find(id); ratingLog.Comment = updatedRatingLogDTO.Comment; ratingLog.Rating = updatedRatingLogDTO.Rating; ratingLog.RestaurantId = updatedRatingLogDTO.RestaurantId; ratingLog.DishId = updatedRatingLogDTO.DishId; _uow.RatingLogs.Update(ratingLog); _uow.SaveChanges(); } return(GetRatingLogById(id)); }
public IActionResult UpdateRestaurantRatingLog(int id, [FromBody] RatingLogForEntityDTO ratingDTO) { _requestLogService.SaveRequest(User.Identity.GetUserId(), "PUT", "api/v1/ratingLogs/{id}", "UpdateRestaurantRatingLog"); if (!ModelState.IsValid) { return(BadRequest()); } var ratingLog = _ratingLogService.GetRatingLogById(id); if (ratingLog == null) { return(NotFound("No existing rating found.")); } if (ratingLog.UserId != User.Identity.GetUserId()) { return(StatusCode(403, "Users can only update their own rating. Please provide ratingLog id that belongs to logged in user.")); } RatingLogDTO updatedRating = _ratingLogService.UpdateRatingLog(id, ratingDTO); return(Ok(updatedRating)); }