Пример #1
0
        public async Task <IActionResult> PostRatingAsync(int id, RatingArgs args)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_entityIndex.Exists(ResourceType.Route, id))
            {
                return(NotFound(ErrorMessages.ContentNotFound(ResourceType.Route, id)));
            }

            var ev = new RatingAdded()
            {
                Id        = _ratingIndex.NextId(ResourceType.Route),
                EntityId  = id,
                UserId    = User.Identity.GetUserIdentity(),
                Value     = args.Rating.GetValueOrDefault(),
                RatedType = ResourceType.Route,
                Timestamp = DateTimeOffset.Now
            };

            await _eventStore.AppendEventAsync(ev);

            return(Created($"{Request.Scheme}://{Request.Host}/api/Exhibits/Rating/{ev.Id}", ev.Id));
        }
Пример #2
0
        private void ValidateRouteArgs(RouteArgs args)
        {
            if (args == null)
            {
                return;
            }

            // ensure referenced image exists
            if (args.Image != null && !_mediaIndex.IsImage(args.Image.Value))
            {
                ModelState.AddModelError(nameof(args.Image),
                                         ErrorMessages.ImageNotFound(args.Image.Value));
            }

            // ensure referenced audio exists
            if (args.Audio != null && !_mediaIndex.IsAudio(args.Audio.Value))
            {
                ModelState.AddModelError(nameof(args.Audio),
                                         ErrorMessages.AudioNotFound(args.Audio.Value));
            }

            // ensure referenced exhibits exist
            if (args.Exhibits != null)
            {
                var invalidIds = args.Exhibits
                                 .Where(id => !_entityIndex.Exists(ResourceType.Exhibit, id))
                                 .ToList();

                foreach (var id in invalidIds)
                {
                    ModelState.AddModelError(nameof(args.Exhibits),
                                             ErrorMessages.ContentNotFound(ResourceType.Exhibit, id));
                }
            }

            // ensure referenced tags exist
            if (args.Tags != null)
            {
                var invalidIds = args.Tags
                                 .Where(id => !_entityIndex.Exists(ResourceType.Tag, id))
                                 .ToList();

                foreach (var id in invalidIds)
                {
                    ModelState.AddModelError(nameof(args.Tags),
                                             ErrorMessages.ContentNotFound(ResourceType.Tag, id));
                }
            }
        }
Пример #3
0
        public IActionResult GetRating(int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_entityIndex.Exists(ResourceType.Route, id))
            {
                return(NotFound(ErrorMessages.ContentNotFound(ResourceType.Route, id)));
            }

            var result = new RatingResult()
            {
                Id          = id,
                Average     = _ratingIndex.Average(ResourceType.Route, id),
                Count       = _ratingIndex.Count(ResourceType.Route, id),
                RatingTable = _ratingIndex.Table(ResourceType.Route, id)
            };

            return(Ok(result));
        }