public async Task <IActionResult> OnPostAsync(string blogAddress) { try { CurBlog = db.Blogs.Where(p => p.Address == blogAddress).First(); } catch { return(StatusCode(404)); } if (ModelState.IsValid) { var tags = Input.Tags?.Split(';'); var url = string.Join("-", new string[] { DateTime.Now.ToString("yyyy-MM-dd"), Input.Title.GetHashCode().ToString() }); //TODO: check if this url already exists var post = new Domain.Post { BlogId = CurBlog.Id, Title = Input.Title, Content = Input.Content, Tags = tags, Url = url }; db.Posts.Add(post); await db.SaveChangesAsync(); _logger.LogInformation("User created a new post."); return(RedirectToPage($"/Blog/Index", new { address = CurBlog.Address })); } return(Page()); }
public void Execute(AddPost request) { _validator.ValidateAndThrow(request); var post = new Domain.Post { UserId = request.UserId, Name = request.Name, Description = request.Description, IsDeleted = false, CreatedAt = DateTime.Now, ModifidedAt = null }; if (Context.Posts.Any(p => p.Name == request.Name)) { throw new EntityAllreadyExists("Post"); } Context.Posts.Add(post); try { Context.SaveChanges(); return; } catch (Exception) { throw; } }
public async Task <IActionResult> OnPostAsync(string address, string postUrl) { try { CurPost = db.Posts.Where(x => x.Url == postUrl).First(); } catch { return(StatusCode(404)); } if (ModelState.IsValid) { var tags = Input.Tags?.Split(';'); var post = new Domain.Post { BlogId = CurPost.BlogId, Title = Input.Title, Content = Input.Content, Tags = tags, Url = postUrl }; post.CreateTime = CurPost.CreateTime; post.Id = CurPost.Id; db.Posts.Remove(CurPost); db.Posts.Add(post); await db.SaveChangesAsync(); _logger.LogInformation("User edited post."); return(RedirectToPage("/Blog/Post", new { blogAddress = address, url = postUrl })); } return(Page()); }
public async Task <IActionResult> OnGetAsync(string address, string postUrl) { try { CurPost = db.Posts.Where(x => x.Url == postUrl).First(); } catch { return(StatusCode(404)); } Input = new InputModel { Title = CurPost.Title, Content = CurPost.Content, Tags = CurPost.TagsStr }; var user = await _userManager.GetUserAsync(_httpContextAccessor.HttpContext.User); var CurBlog = db.Blogs.Find(CurPost.BlogId); if (user.Id != CurBlog.AuthorId) { return(StatusCode(403)); } return(Page()); }
public void Execute(PostDto request) { if (Context.Posts.Any(p => p.Title == request.Title)) { throw new EntityAlreadyExistsException(); } if (request.Title == null) { throw new Exception(); } var ext = Path.GetExtension(request.Image.FileName); if (!FileUpload.AllowedExtensions.Contains(ext)) { throw new Exception("File extension is not ok"); } var newFileName = Guid.NewGuid().ToString() + "_" + request.Image.FileName; var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", newFileName); request.Image.CopyTo(new FileStream(filePath, FileMode.Create)); var image = new Domain.Image { Alt = request.Title, Path = newFileName }; Context.Images.Add(image); var post = new Domain.Post { Title = request.Title, Summary = request.Summary, Text = request.Text, CategoryId = request.CategoryId, UserId = request.UserId, Image = image }; Context.Posts.Add(post); if (request.AddTagsInPost != null) { foreach (var tag in request.AddTagsInPost) { Context.PostTags.Add(new Domain.PostTag { Post = post, TagId = tag }); } } Context.SaveChanges(); }
public static Infrastructure.Models.Post FromDomain(this Domain.Post post) { return(new Infrastructure.Models.Post { Id = post.Id, Message = post.Message, UserId = post.UserId, Timestamp = post.Timestamp }); }
public ActionResult CreatePost(HTMLContent x) { var newPost = new Domain.Post(); newPost.Content = x.HtmlContent; newPost.Title = x.Title; newPost.IsApproved = true; newPost.DatePosted = DateTime.Now; if (ModelState.IsValid) { var manager = new BlogManager(); var response = manager.AddPost(newPost); return RedirectToAction("Index"); } else { return View("OwnerAddPost"); } }
public static PostOverView Map(Domain.Post p, Optional.Option <Domain.File> f) { return(new PostOverView(p.Id, p.Created, p.Name, p.IpAddress, p.Comment, f)); }