Exemple #1
0
        public async Task <IActionResult> UploadRating([FromBody] Rating rating)
        {
            if (string.IsNullOrWhiteSpace(rating.Source))
            {
                return(BadRequest("Source is missing."));
            }
            if (string.IsNullOrWhiteSpace(rating.Id))
            {
                return(BadRequest("Id is missing."));
            }
            var ratingFromDatabase = await _elasticSearchGateway.GetRating(rating.Id, rating.Source);

            if (ratingFromDatabase.Raters.FirstOrDefault(r => r.Id == User.Identity.Name) != null)
            {
                return(BadRequest("User already rated this item."));
            }
            var rater = rating.Raters.FirstOrDefault(r => r.Id == User.Identity.Name);

            if (rater == null)
            {
                return(BadRequest("Invalid rating, new rating's raters should contain logged in user"));
            }
            ratingFromDatabase.Raters.Add(rater);
            await _elasticSearchGateway.UpdateRating(ratingFromDatabase);

            return(Ok(ratingFromDatabase));
        }