public async Task Edit(int id, int blogId, string title, string content, string userName) { ServiceValidator.ServiceValidator.IsLessThanOne(id, "Id can't be less then 1."); ServiceValidator.ServiceValidator.IsLessThanOne(blogId, "Id can't be less then 1."); ServiceValidator.ServiceValidator.AreEqual(id, blogId, "Id and Blog Id are not the same."); ServiceValidator.ServiceValidator.IsStringValid(title, "Title can't be null or white space."); ServiceValidator.ServiceValidator.IsStringValid(content, "Content can't be null or white space."); ServiceValidator.ServiceValidator.IsStringValid(userName, "Editor name can't be null or white space."); try { var blogPost = await this.blogPostRepo.Get(blogId); ServiceValidator.ServiceValidator.IsNull(blogPost, "Can't find Blog to edin."); var postEditor = new BlogPostEditor() { BlogPostId = blogPost.Id, CreatedOn = this.dateTimeProvider.Now(), EditorName = userName, }; blogPost.PostEditors.Add(postEditor); blogPost.Title = title; blogPost.Content = content; await this.blogPostRepo.Update(blogPost); } catch (DbUpdateConcurrencyException ex) { throw new ArgumentException(ex.Message, ex.InnerException); } }
public async Task Create(string title, string content, string userName, string authorId) { ServiceValidator.ServiceValidator.IsStringValid(title, "Title can't be null or white space."); ServiceValidator.ServiceValidator.IsStringValid(content, "Content can't be null or white space."); ServiceValidator.ServiceValidator.IsStringValid(userName, "Editor name can't be null or white space."); ServiceValidator.ServiceValidator.IsStringValid(authorId, "Editor name can't be null or white space."); var author = await this.authorRepo.Get(authorId); ServiceValidator.ServiceValidator.IsNull(author, "Object get fails."); var editor = new BlogPostEditor() { CreatedOn = this.dateTimeProvider.Now(), EditorName = userName }; var newBlogPost = new BlogPost() { AuthorId = author.Id, CreatedBy = userName, Content = content, Title = title, CreatedOn = this.dateTimeProvider.Now(), ModifiedOn = this.dateTimeProvider.Now(), PostEditors = new List <BlogPostEditor>() { new BlogPostEditor() { CreatedOn = this.dateTimeProvider.Now(), EditorName = userName } } }; ServiceValidator.ServiceValidator.IsNull(newBlogPost, "Object creation fails."); try { await this.blogPostRepo.Add(newBlogPost); } catch (DbUpdateException ex) { throw new DbUpdateException($"Db Update Exception : {ex.Message} : {ex.InnerException.Message}", ex.InnerException); } catch (Exception ex) { throw new InvalidOperationException("Can't Create blog post."); } }
public static BlogPostEditor[] SeedData() { var blogPosts = new BlogPostEditor[] { new BlogPostEditor() { Id = 1, BlogPostId = 1, CreatedOn = DateTime.Now, EditorName = "Admin1" } }; return(blogPosts); }