public async Task <IActionResult> FindOrCreate() { var id = User.Claims.FirstOrDefault(c => c.Type == JwtRegisteredClaimNames.Sub)?.Value; var name = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value; if (id == null || name == null) { return(BadRequest()); } var author = await RRRepo.GetAuthorByGoogleId(id); if (author != null) { return(Ok(new { Success = true, Id = author.Id })); } author = new Models.Author() { GoogleId = id, Name = name }; await RRRepo.AddAuthorAsync(author); return(Ok(new { Success = true, Id = author.Id })); }
public async Task <IActionResult> Add([FromBody] AddReviewRequest reviewRequest) { if (reviewRequest == null || reviewRequest.Review == null) { return(BadRequest()); } var review = reviewRequest.Review; if (string.IsNullOrEmpty(review.Title)) { return(Ok(new { Success = false })); } var successfulCaptcha = await GoogleController.ValidateRecaptcha(reviewRequest.RecaptchaResponse); if (!successfulCaptcha) { return(Ok(new { Success = false })); } // now that we've successfully passed all validations, get the Author id // from the User object and add the review var googleId = GetUserGoogleId(); if (googleId == null) { return(BadRequest()); } var author = await RRRepo.GetAuthorByGoogleId(googleId); var authorsReviews = await RRRepo.GetReviewsByAuthorId(author.Id); if (authorsReviews?.FirstOrDefault(rev => rev.RestroomId == review.RestroomId) != null) { return(Ok(new { Success = false, Error = "ALREADY_EXISTS" })); } var restroom = await RRRepo.GetById(review.RestroomId); review.Author = author; review.Restroom = restroom; await RRRepo.AddReview(review); return(Ok(new { Success = true, Review = review, })); }
public async Task <IActionResult> GetCurrentUserReviews() { var googleId = GetUserGoogleId(); if (googleId == null) { return(BadRequest()); } var author = await RRRepo.GetAuthorByGoogleId(googleId); if (author == null) { return(BadRequest()); } var reviews = await RRRepo.GetReviewsByAuthorId(author.Id); return(Ok(reviews)); }
public async Task <IActionResult> GetById(int id) { var room = await RRRepo.GetById(id); var currentUserGoogleId = GetUserGoogleId(); Author currentAuthor = null; if (!string.IsNullOrEmpty(currentUserGoogleId)) { currentAuthor = await RRRepo.GetAuthorByGoogleId(currentUserGoogleId); } foreach (var review in room.Reviews) { // if it's their own review, don't strip the AuthorId if (review.AuthorIsAnonymous && review.AuthorId != currentAuthor?.Id) { review.Author = null; review.AuthorId = -1; } } return(Ok(room)); }