public void ParseExistingFiles() { var start = DateTime.Now; var files = new List <IFileInfo>(); LoadFiles(_configuration["PostsLocalPath"], ref files); List <BlogPost> posts = new List <BlogPost>(); foreach (var fileInfo in files) { var fileName = fileInfo.Name; using (var reader = new StreamReader(fileInfo.CreateReadStream())) { try { var post = ParseFile(fileName, reader); posts.Add(post); } catch (FileParserServiceException ex) { _logger.LogError($"FileParserService.ParseExistingFiles Failed to parse file '{fileName}'. Error: {ex.Message}"); } } } _blogPostRepository.RemoveAll(); _blogPostRepository.Add(posts); _dbContext.SaveChanges(); var diff = (DateTime.Now - start).ToString(); var count = _dbContext.BlogPosts.Count(); _logger.LogInformation($"Parsing {count} posts, took {diff} time"); }
public HttpResponseMessage SubmitPost(HttpRequestMessage request, [FromBody] BlogPost blogPost) { HttpResponseMessage response = null; try { blogPost.Timestamp = DateTime.Now; PreSubmissionPostingEventArgs preArgs = new PreSubmissionPostingEventArgs(blogPost); _ExtensibilityManager.InvokeCancelableModuleEvent <PreSubmissionPostingEventArgs>( _ModuleEvents.PreSubmissionPosting, preArgs); if (!preArgs.Cancel) { IBlogPostRepository blogPostRepository = _componentLocator.ResolveComponent <IBlogPostRepository>(); blogPost = blogPostRepository.Add(blogPost); PostSubmissionPostingEventArgs postArgs = new PostSubmissionPostingEventArgs(blogPost); _ExtensibilityManager.InvokeModuleEvent <PostSubmissionPostingEventArgs>( _ModuleEvents.PostSubmissionPosting, postArgs); response = request.CreateResponse <BlogPost>(HttpStatusCode.OK, blogPost); } } catch (Exception ex) { response = request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex); } return(response); }
public async Task <IActionResult> Create([Bind("BlogPostID,Title,Body,PostedOn,EditedOn,Categories,UserId")] BlogPost blogPost) { if (ModelState.IsValid) { _repo.Add(blogPost); _uow.Complete(); return(RedirectToAction(nameof(Index))); } return(View(blogPost)); }
public HttpResponseMessage PostBlogPost(BlogPost item) { item = repository.Add(item); var response = Request.CreateResponse <BlogPost>(HttpStatusCode.Created, item); string uri = Url.Link("DefaultApi", new { id = item.Id }); response.Headers.Location = new Uri(uri); return(response); }
public IHttpActionResult Post(BlogPost newBlog) { newBlog.Id = 0; newBlog.PostDate = DateTime.Now; if (!ModelState.IsValid) //if model title/content are empty OR title/content are too long { return(BadRequest()); } newBlog.Id = _blogPostRepository.Add(newBlog); return(Created(new Uri(Request.RequestUri + "/" + newBlog.Id), newBlog)); }
public bool CreatePost(BlogPost post) { bool flag = false; try { _blogRepository.Add(post); _unitOfWork.Commit(); flag = true; } catch { } return(flag); }
public AddBlogPostResponse AddBlogPost(AddBlogPostRequest request) { var response = new AddBlogPostResponse(); var newPost = CreateNewPost(request); response.Post = newPost; try { _blogPostRepo.Add(newPost); response.AddSuccessful = true; } catch (Exception) { response.AddSuccessful = false; } return(response); }
public void Add(BlogPost blogPost) { repo.Add(blogPost); repo.Save(); }