Пример #1
0
        public async Task <IActionResult> PostAsync([FromBody] ExhibitArgs args)
        {
            ValidateExhibitArgs(args);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!UserPermissions.IsAllowedToCreate(User.Identity, args.Status))
            {
                return(Forbid());
            }

            // validation passed, emit event
            var ev = new ExhibitCreated
            {
                Id         = _entityIndex.NextId(ResourceType.Exhibit),
                UserId     = User.Identity.GetUserIdentity(),
                Properties = args,
                Timestamp  = DateTimeOffset.Now
            };

            await _eventStore.AppendEventAsync(ev);

            return(Created($"{Request.Scheme}://{Request.Host}/api/Exhibits/{ev.Id}", ev.Id));
        }
Пример #2
0
 public Exhibit(ExhibitArgs args)
 {
     Name        = args.Name;
     Description = args.Description;
     Image.Id    = args.Image;
     Latitude    = args.Latitude;
     Longitude   = args.Longitude;
     Status      = args.Status;
     Tags.Add(args.Tags?.Select(id => (BsonValue)id));
     Pages.Add(args.Pages?.Select(id => (BsonValue)id));
     AccessRadius = args.AccessRadius;
 }
Пример #3
0
        private void ValidateExhibitArgs(ExhibitArgs 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 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));
                }
            }

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

                foreach (var id in invalidIds)
                {
                    ModelState.AddModelError(nameof(args.Pages),
                                             ErrorMessages.ExhibitPageNotFound(id));
                }
            }
        }
Пример #4
0
        public async Task <IActionResult> PutAsync(int id, [FromBody] ExhibitArgs args)
        {
            ValidateExhibitArgs(args);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_entityIndex.Exists(ResourceType.Exhibit, id))
            {
                return(NotFound());
            }

            if (!UserPermissions.IsAllowedToEdit(User.Identity, args.Status, _entityIndex.Owner(ResourceType.Exhibit, id)))
            {
                return(Forbid());
            }

            var oldStatus = _entityIndex.Status(ResourceType.Exhibit, id).GetValueOrDefault();

            if (args.Status == ContentStatus.Unpublished && oldStatus != ContentStatus.Published)
            {
                return(BadRequest(ErrorMessages.CannotBeUnpublished(ResourceType.Exhibit)));
            }

            // validation passed, emit event
            var ev = new ExhibitUpdated
            {
                Id         = id,
                UserId     = User.Identity.GetUserIdentity(),
                Properties = args,
                Timestamp  = DateTimeOffset.Now
            };

            await _eventStore.AppendEventAsync(ev);

            return(StatusCode(204));
        }