public async Task <int> NewPost(NewPostModel model) { if (model == null) { throw new ModelException("Model is null"); } var author = await _unitOfWork.TRepository .GetAsync(model.UserId).ConfigureAwait(false); if (author == null) { throw new DataException("Author does not exist!"); } if (author.SilencedTo > DateTime.Now) { throw new AccessException($"This user is in ReadOnly mode up to {author.SilencedTo}"); } var post = _mapper.Map <Post>(model); post.DateCreated = DateTime.Now; await _unitOfWork.URepository .InsertAsync(post).ConfigureAwait(false); await _unitOfWork.URepository .SaveChangesAsync().ConfigureAwait(false); return(post.Id); }
public async Task <ActionResult> NewPost(NewPostModel model) { if (!ModelState.IsValid) { return(View(model)); } // var blogContext = new BlogContext(); // Not neede here any more // XXX WORK HERE // Insert the post into the posts collection var newPost = new Post { Title = model.Title, Content = model.Content, Author = this.User.Identity.Name, CreatedAtUtc = DateTime.UtcNow, Tags = model.Tags.Split(',').ToList(), Comments = new List <Comment>() // Need to initialise a new list of comments here, as it comes empty from the database when we're adding a new post }; /* * var tagList = new List<string>(); * tagList.AddRange(model.Tags.Split(',').ToList()); * newPost.Tags = tagList; */ var post = await _postService.InsertNewPost(newPost); return(RedirectToAction("Post", new { id = post.Id })); }
/// <summary> /// Adds a new "Post" object to the ID-d parent "Thread" object. /// </summary> /// <param name="id">Integer: the ID of the parent "Thread" object.</param> /// <returns>View model data: for showing the post-creation page.</returns> public async Task <IActionResult> Create(int id) { var thread = _threadEntityService.GetById(id); var user = await _userManager.FindByNameAsync(User.Identity.Name); var model = new NewPostModel { ThreadContent = thread.Content, ThreadTitle = thread.Title, ThreadId = thread.Id, AuthorId = user.Id, AuthorName = User.Identity.Name, AuthorImageUrl = user.ProfileImageUrl, AuthorRating = user.Rating, IsAuthorAdmin = User.IsInRole("Admin"), ForumId = thread.Forum.Id, ForumTitle = thread.Forum.Title, CreatedAt = DateTime.Now }; return(View(model)); }
public async Task <ActionResult> NewPost(NewPostModel model) { if (!ModelState.IsValid) { return(View(model)); } var content = model.Content?.Trim(); if (string.IsNullOrEmpty(content)) { ModelState.AddModelError("Content", "Post should not be empty"); return(View(model)); } var blogContext = new BlogContext(); var p = new Post() { Author = User.Identity.Name, Content = content, Title = model.Title, Tags = model.Tags.Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries), CreatedAtUtc = DateTime.UtcNow }; await blogContext.Posts.InsertOneAsync(p); return(RedirectToAction("Post", new { id = p.PostId.ToString() })); }
public async Task <ActionResult> NewPost(NewPostModel model) { if (!ModelState.IsValid) { return(View(model)); } var blogContext = new BlogContext(); // XXX WORK HERE // Insert the post into the posts collection var post = new Post { Author = User.Identity.Name, Content = model.Content, Title = model.Title, CreatedAtUtc = DateTime.Now }; if (string.IsNullOrWhiteSpace(model.Tags) == false) { post.Tags.AddRange(model.Tags.Split(',').Select(x => x.Trim())); } await blogContext.Posts.InsertOneAsync(post); return(RedirectToAction("Post", new { id = post.Id })); }
public async Task <ActionResult> NewPost(NewPostModel model) { if (!ModelState.IsValid) { return(View(model)); } var claims = this.Request.GetOwinContext().Authentication.User.Claims.ToList(); //string email = claims.Where(claim => claim.Type == ClaimTypes.Email) // .Select(claim => claim.Value) // .FirstOrDefault(); string name = claims.Where(claim => claim.Type == ClaimTypes.Name || claim.Type == ClaimTypes.GivenName) .Select(claim => claim.Value) .FirstOrDefault(); var blogContext = new BlogContext(); User user = await blogContext.FindUserByName(name); var post = (Post)model; post.Author = user.Name; await blogContext.Posts.InsertOneAsync(post); // XXX WORK HERE // Insert the post into the posts collection return(RedirectToAction("Post", new { id = post.Id })); }
public async Task<PostDetail> AddPost(NewPostModel postModel, ClaimsPrincipal user) { var imageUploadTasks = Task.WhenAll(postModel.Base64Images.Select(i => imageUploadService.Upload(i))); var postAuthor = !postModel.IsAnonymous ? new PostAuthor(user.GetUserId(), user.GetUserName()) : null; var exchangeDetails = postModel.Type == PostType.Exchange ? new ExchangeDetails(postModel.Price ?? 0, postModel.ExchangeType, postModel.Unit) : null; var imageUploads = await imageUploadTasks; var post = new Post( postModel.Title, postAuthor, postModel.Type, exchangeDetails, imageUploads.Any() ? imageUploads.First().Thumb.Url : null); post = await dbContext.Posts.Upsert(post); var postDetail = new PostDetail( post.Id ?? throw new KeyNotFoundException("post.Id was null"), postModel.Description, imageUploads ); return await dbContext.PostDetails.Upsert(postDetail); }
public async Task <IActionResult> NewPost(NewPostModel model) { if (ModelState.IsValid) { var user = await _userManager.GetUserAsync(HttpContext.User); var category = _categories.GetById(model.CategoryId); Post post = new Post { Title = model.Title, CategoryId = model.CategoryId, Category = category, Content = model.Content, Summary = model.Summary, Slug = model.Slug, User = user, UserId = user.Id }; _repository.Add(post); _repository.Save(); return(RedirectToAction("Index", "Post")); } return(View()); }
public async Task <ActionResult> NewPost(NewPostModel model) { if (!ModelState.IsValid) { return(View(model)); } var blogContext = new BlogContext(); // XXX WORK HERE // Insert the post into the posts collection var post = new Post { Author = this.User.Identity.Name, Title = model.Title, Content = model.Content, Tags = model.Tags.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(), CreatedAtUtc = DateTime.Now, Comments = new List <Comment>() }; await blogContext.Posts.InsertOneAsync(post); return(RedirectToAction("Post", new { id = post.Id })); }
public IActionResult Delete(int id) { var post = _postService.GetById(id); var forum = _forumService.GetById(post.Forum.Id); var model = new NewPostModel { ForumId = forum.Id, ForumName = forum.Title, ForumImageUrl = forum.ImageUrl, AuthorId = post.User.Id, Email = post.User.Email, AutorRating = post.User.Rating, AuthorName = post.User.UserName, MemberSince = post.User.MemberSince, IsAuthorAdmin = IsAuthorAdmin(post.User), AuthorImageUrl = post.User.ProfileImageUrl, PostId = post.Id, Title = post.Title, Content = post.Content, PostImageUrl = post.ImageUrl }; return(View(model)); }
public ActionResult NewPost(NewPostModel model) { if (!User.IsInRole("admin")) { return(RedirectToAction("Index")); } if (ModelState.IsValid) { using (var db = new BlogModels()) { Post new_post = new Post() { Label = model.Label, Text = model.Text, ImageUrl = model.ImageUrl, CreationTime = DateTime.Now, Author = User.Identity.GetUserId(), Likes = 0, Views = 0 }; db.Posts.Add(new_post); db.SaveChanges(); } return(RedirectToAction("Index", "Blog")); } ModelState.AddModelError("", "Что то не правильно"); return(View(model)); }
public async Task <ActionResult> NewPost(NewPostModel model) { if (!ModelState.IsValid) { return(View(model)); } var blogContext = new BlogContext(); // XXX WORK HERE // Insert the post into the posts collection Post _post = new Post { Author = User.Identity.Name, Title = model.Title, Content = model.Content, Tags = model.Tags.Split(' ', ','), CreatedAtUtc = DateTime.UtcNow, Comments = new List <Comment>() }; blogContext.Posts.InsertOneAsync(_post); return(RedirectToAction("Post", new { id = _post.Id })); }
public async Task <IActionResult> AddPost(NewPostModel model) { var userId = _userManager.GetUserId(User); var user = _userManager.FindByIdAsync(userId).Result; var post = BuildPost(model, user); _postService.Add(post).Wait(); return(RedirectToAction("Index", "Post", new { id = post.Id })); }
public ActionResult NewPost() { if (!User.IsInRole("admin")) { return(RedirectToAction("Index")); } var model = new NewPostModel(); return(View(model)); }
private Post BuildPost(NewPostModel model, ApplicationUser user) { return(new Post { Title = model.Title, Content = model.Content, Created = DateTime.Now, User = user }); }
public async Task <IActionResult> AddPost(NewPostModel model) { var userId = _userManager.GetUserId(User); var user = _userManager.FindByIdAsync(userId).Result; var post = BuildPost(model, user); await _servicePost.Add(post); return(RedirectToAction(nameof(Index), new { id = post.Id })); }
public async Task <IActionResult> EditPost(NewPostModel model) { if (ModelState.IsValid) { await _postService.EditPost(model.Id, model.Title, model.Content); return(RedirectToAction("Index", "Post", new { id = model.Id })); } return(Edit(model.Id)); }
public async Task <IActionResult> AddPost(NewPostModel model) { var userId = _userManager.GetUserId(User); var user = _userManager.FindByIdAsync(userId).Result; var post = BuildPost(model, user); await _postService.Add(post); return(RedirectToAction("Forum", "Post")); }
public async Task <IActionResult> addPost(NewPostModel model) { var userId = _userManager.GetUserId(User); var user = _userManager.FindByIdAsync(userId).Result; var post = BuildPost(model, user); _postService.add(post).Wait(); //Block the current thread until task is done //maybe user rating addition return(RedirectToAction("Index", "Post", new { id = post.id })); }
public async Task <IActionResult> AddPost(NewPostModel model) { string userId = _userManager.GetUserId(User); ApplicationUser user = _userManager.FindByIdAsync(userId).Result; //Async task requires await Post post = BuildPost(model, user); await _postService.Add(post); //Adds to db Wait blocks the current thread until the task is complete return(RedirectToAction("Index", "Post", new { id = post.Id })); }
public async Task <IActionResult> AddPost(NewPostModel model) { var userId = _userManager.GetUserId(User); var user = await _userManager.FindByIdAsync(userId); // returns app user var post = BuildPost(model, user); await _postService.Add(post); return(RedirectToAction("Index", "Post", post.Id)); }
public async Task <IActionResult> Add(NewPostModel newPostModel) { var result = await postService.AddPost(newPostModel, User); await profileService.AddBookmark( User.GetUserId(), result.PostId, newPostModel.EnableNotifications); return(Ok(result.PostId)); // todo?: return Created() }
public async Task <IActionResult> AddPost(NewPostModel model) { var userId = _userManager.GetUserId(User); var user = _userManager.FindByIdAsync(userId).Result; var post = BuildPostReplies(model, user); await _postService.Add(post); //TODO : Implement user rating system return(RedirectToAction("Index", "Post", new { id = post.Id })); }
public async Task <ActionResult> NewPost(NewPostModel model) { if (!ModelState.IsValid) { return(View(model)); } var postId = BlogServices.AddPost(User.Identity.Name, model.Title, model.Content, model.Tags); return(RedirectToAction("Post", new { id = postId })); }
private static Post BuildPost(NewPostModel model, AppUser user) { return(new Post { Title = model.Title, Content = model.Content, Created = DateTime.Now, User = user, ForumId = model.ForumId }); }
public async Task <IActionResult> AddPost(NewPostModel model) { var userId = _userManager.GetUserId(User); var user = await _userManager.FindByIdAsync(userId); var post = BuildPost(model, user); await _postService.Add(post); await _userService.UpdateUserRating(userId, typeof(Post)); return(RedirectToAction("Index", "Post", new { id = post.Id })); }
private Post BuildPost(NewPostModel model, ApplicationUser user) { var forum = _forumService.GetById(model.ForumId); return(new Post { Title = model.Title, Content = model.Content, Created = DateTime.Now, User = user, Forum = forum }); }
public async Task <ActionResult> AddPost(NewPostModel model) { var userId = User.Identity.GetUserId(); var user = _userManager.FindByIdAsync(userId).Result; var post = BuildPost(model, user); await _postRepositories.Add(post); await _applicationUserRepositories.IncrementUserRating(user.Id, typeof(Post)); return(RedirectToAction("Index", "Post", new { id = post.Id })); }
public ActionResult Create(int id) { var forum = _forumRepositories.GetById(id); var model = new NewPostModel { ForumId = forum.Id, ForumName = forum.Title, AuthorName = User.Identity.Name }; return(View(model)); }
public async Task <IActionResult> AddPost(NewPostModel model) { var userId = _userManager.GetUserId(User); var user = _userManager.FindByIdAsync(userId).Result; var post = BuildPost(model, user); _postService.Add(post).Wait(); // Block the current thread until the task is complete // TO DO: Implement user rating management return(RedirectToAction("Index", "Post", new { id = post.Id })); }
public void new_post(NewPostModel new_post) { //Create a new post using data from the page and add the new post int cid = 0; DateTime time = DateTime.Now; string time_s = string.Format("{0:D4}-{1:D2}-{2:D2} {3:D2}:{4:D2}:{5:D2}", time.Year, time.Month, time.Day, time.Hour, time.Minute, time.Second); string SQL_Query = "INSERT INTO content(ctype, ctext, poster, ptime) VALUES("; if (new_post.ctype) SQL_Query += "1, '"; else SQL_Query += "0, '"; SQL_Query += new_post.ctext + "', " + new_post.pid + ", '" + time_s + "');"; try { using (MySqlConnection connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["MySqlConnString"].ConnectionString)) { if (connection.State != System.Data.ConnectionState.Open) connection.Open(); MySqlCommand command = new MySqlCommand(SQL_Query, connection); command.ExecuteNonQuery(); SQL_Query = "SELECT MAX(cid) FROM content;"; command = new MySqlCommand(SQL_Query, connection); MySqlDataReader dr = command.ExecuteReader(); if (dr.Read()) cid = dr.GetInt32("MAX(cid)"); dr.Close(); if (new_post.ctype) { SQL_Query = "INSERT INTO visible(cid, poster, ftype) VALUES(" + cid + "," + new_post.pid + ",'" + new_post.ftype + "');"; command = new MySqlCommand(SQL_Query, connection); command.ExecuteNonQuery(); } if (new_post.topic != null) //We have a topic to add {//First get the latest post we added to get the CID //Make Query to add new topic SQL_Query = "INSERT INTO contopic(cid, topic) VALUES(" + cid + ",'" + new_post.topic + "');"; command = new MySqlCommand(SQL_Query, connection); command.ExecuteNonQuery(); } connection.Close(); } } catch (Exception ex) { } }