Exemplo n.º 1
0
        public IActionResult UpdatePost([FromBody] UpdatePostViewModel model)
        {
            var user = userRepository.GetApplicationUserByUsername(User.Identity.Name, true);

            if (user == null)
            {
                return(Unauthorized());
            }
            var post = postRepository.GetPostByPostId(model.PostId, true, new string[] { "CurrentVersion", "PostTags", "ExternalFiles" });

            if (post == null)
            {
                return(NotFound());
            }
            try {
                bool errors = false;
                if (model.CurrentVersion != null)
                {
                    var postVersion = postRepository.GetPostVersionByPostVersionId((int)model.CurrentVersion);
                    if (postVersion == null)
                    {
                        errors = true;
                        ModelState.AddModelError("PostVersion", "PostVersion not found");
                    }
                    post.CurrentVersion = postVersion;
                }
                if (model.AliasSet)
                {
                    post.Alias = model.Alias;
                }
                if (model.Published != null)
                {
                    post.Published = (bool)model.Published;
                    if (post.FirstPublished == null)
                    {
                        post.FirstPublished = DateTime.Now;
                    }
                }
                if (model.List != null)
                {
                    post.List = (bool)model.List;
                }
                if (model.PostTags != null)
                {
                    var newTags = new List <PostTag>();
                    var allTags = tagRepository.GetTags().ToDictionary(x => x.TagId, x => x);
                    foreach (var tag in model.PostTags)
                    {
                        if (!allTags.ContainsKey(tag))
                        {
                            return(BadRequest());
                        }
                        newTags.Add(new PostTag()
                        {
                            PostId = post.PostId, TagId = tag
                        });
                    }
                    post.PostTags = newTags;
                }
                if (errors)
                {
                    return(BadRequest(ModelStateHelper.GetModelStateErrors(ModelState)));
                }
                postRepository.UpdatePost(post);
                return(Ok(Conversions.PostViewModelFromPost(post)));
            }
            catch (Exception) {
                return(BadRequest());
            }
        }
Exemplo n.º 2
0
        public IActionResult UpdateMenuItem([FromBody] UpdateMenuItemViewModel model)
        {
            var menuItem = menuRepository.GetMenuItemByMenuItemId(model.MenuItemId);

            if (menuItem == null)
            {
                return(NotFound());
            }
            try {
                if (model.ParentMenuItemIdSet)
                {
                    if (model.ParentMenuItemId != null)
                    {
                        var parentItem = menuRepository.GetMenuItemByMenuItemId((int)model.ParentMenuItemId);
                        if (parentItem == null)
                        {
                            ModelState.AddModelError("parent", "Could not find specified parent");
                        }
                        else if (parentItem.ParentMenuName != menuItem.ParentMenuName)
                        {
                            ModelState.AddModelError("parent", "Parent must be part of the same menu");
                        }
                    }
                    menuItem.ParentMenuItemId = model.ParentMenuItemId;
                }
                if (model.Name != null)
                {
                    if (model.Name.Length < 1)
                    {
                        ModelState.AddModelError("name", "Name must be 1 character or longer");
                    }
                    menuItem.Name = model.Name;
                }
                if (model.Priority != null)
                {
                    if (model.Priority < 0 || model.Priority > 1000)
                    {
                        ModelState.AddModelError("priority", "Priority must be between 0 and 1000");
                    }
                    menuItem.Priority = (int)model.Priority;
                }
                if (model.LinkSet)
                {
                    menuItem.Link = model.Link;
                }
                if (model.PostIdSet)
                {
                    if (model.PostId != null)
                    {
                        var post = postRepository.GetPostByPostId((int)model.PostId);
                        if (post == null)
                        {
                            ModelState.AddModelError("postid", "Could not find post");
                        }
                    }
                    menuItem.PostId = model.PostId;
                }
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelStateHelper.GetModelStateErrors(ModelState)));
                }
                menuRepository.UpdateMenuItem(menuItem);
                return(Ok(Conversions.MenuItemViewModelFromMenuItem(menuItem)));
            }
            catch (Exception) {
                return(BadRequest());
            }
        }