public void UpdatePost(Guid oldPostId, Post @new, PostProperties propertiesToChange) { ValidatePost(@new, propertiesToChange); using (var ctx = new DB.CodingIdeasEntities()) { var p = ctx.Posts.Where(x => x.Id == oldPostId).FirstOrDefault(); if (p == null) { throw new PostNotFoundException(); } if (propertiesToChange.HasFlag(PostProperties.Content)) { p.Content = @new.Content; } if (propertiesToChange.HasFlag(PostProperties.Title)) { p.Title = @new.Title; } ctx.SaveChanges(); } }
private void ValidatePost(Post post, PostProperties props) { if (post.Content == null || props.HasFlag(PostProperties.Content) && string.IsNullOrWhiteSpace(post.Content)) { throw new InvalidContentException(); } if (post.Title == null || props.HasFlag(PostProperties.Title) && string.IsNullOrWhiteSpace(post.Title)) { throw new InvalidTitleException(); } if (props.HasFlag(PostProperties.PublishDate) && post.PublishDate > DateTime.Now) { throw new InvalidPublishDateException("The publish date cannot be in the future."); } using (var ctx = new DB.CodingIdeasEntities()) { if (props.HasFlag(PostProperties.AuthorId) && post.AuthorId != null && ctx.Users.Where(x => x.Id == post.AuthorId).FirstOrDefault() == null) { throw new UserNotFoundException(); } } }