Exemplo n.º 1
0
        public async Task <IActionResult> Add([FromBody] AddRestroomRequest restroomRequest)
        {
            if (restroomRequest == null || restroomRequest.Restroom == null)
            {
                return(BadRequest());
            }

            var restroom = restroomRequest.Restroom;

            if (string.IsNullOrEmpty(restroom.Description) ||
                string.IsNullOrEmpty(restroom.Latitude) ||
                string.IsNullOrEmpty(restroom.Longitude))
            {
                return(Ok(new { Success = false }));
            }

            var successfulCaptcha = await GoogleController.ValidateRecaptcha(restroomRequest.RecaptchaResponse);

            if (!successfulCaptcha)
            {
                return(Ok(new { Success = false }));
            }

            await RRRepo.Add(restroom);

            return(Ok(new { Success = true }));
        }
Exemplo n.º 2
0
        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
            }));
        }
Exemplo n.º 3
0
        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,
            }));
        }
Exemplo n.º 4
0
        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));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> deleteReview(int?id)
        {
            if (id == null)
            {
                return(BadRequest());
            }

            // Validate this review is the Author's to delete
            var googleId = GetUserGoogleId();

            if (String.IsNullOrEmpty(googleId))
            {
                return(BadRequest());
            }

            var authorOwnsReview = await RRRepo.CheckAuthorOwnsReviewByGoogleId(id.Value, googleId);

            if (!authorOwnsReview)
            {
                return(BadRequest());
            }

            // we've passed all validations, proceed with deletion
            try
            {
                await RRRepo.DeleteReviewById(id.Value);

                return(Ok(new {
                    Success = true
                }));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(BadRequest());
            }
        }
Exemplo n.º 6
0
        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));
        }
Exemplo n.º 7
0
        public async Task <IActionResult> Search([FromQuery] string q)
        {
            var result = await RRRepo.Search(q);

            return(Ok(result));
        }
Exemplo n.º 8
0
        public async Task <IActionResult> Get()
        {
            var result = await RRRepo.GetAll();

            return(Ok(result));
        }