public async Task <IActionResult> PostPost([FromRoute] int userId, [FromBody] PostModel content) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (_context.Post.Any(e => e.Content == content.Text)) { ModelState.AddModelError("Post with same title and content already exists", "Please edit the post or create new post"); return(BadRequest(ModelState)); } Post post = new Post() { Content = content.Text, UserId = userId, Title = content.Text.Substring(0, 5), CreationDate = DateTime.UtcNow }; _context.Post.Add(post); await _context.SaveChangesAsync(); return(CreatedAtAction("GetPost", new { id = post.PostId }, post)); }
public async Task <IActionResult> PostUser([FromBody] UseModel user) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } User newUser = new User() { Name = user.Name, Passsword = user.Passsword, Email = user.Email }; var existsUser = await _context.User.SingleOrDefaultAsync(m => m.Email == user.Email); if (existsUser != null) { ModelState.AddModelError("user exists", "user alrady registered in the system"); return(BadRequest(ModelState)); } _context.User.Add(newUser); await _context.SaveChangesAsync(); return(CreatedAtAction("GetUser", new { id = newUser.UserId }, user)); }
public async Task <IActionResult> Vote(int id, int userId, int vote) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = await _context.User.SingleOrDefaultAsync(m => m.UserId == userId); if (user == null) { ModelState.AddModelError("userId", "not found"); return(BadRequest(ModelState)); } var pp = await _context.Post.SingleOrDefaultAsync(m => m.PostId == id); if (pp == null) { ModelState.AddModelError("userId", "not found"); return(BadRequest(ModelState)); } if (vote != 0 && vote != 1) { ModelState.AddModelError("vote", "can be 0 or 1"); return(BadRequest(ModelState)); } var postvote = await this._context.PostVote.SingleOrDefaultAsync(m => m.PostId == id && m.UserId == userId); if (postvote != null) { if (postvote.VoteDown && postvote.VoteUp) { return(Json(new { errMsg = "you already voted up and down for this post" })); } if (vote == 1 && postvote.VoteUp) { return(Json(new { errMsg = "you already voted up for this post" })); } if (vote == 0 && postvote.VoteDown) { return(Json(new { errMsg = "you already voted down for this post" })); } } // update if (postvote != null) { if (vote == 0) { postvote.VoteDown = true; } else if (vote == 1) { postvote.VoteUp = true; } if (await TryUpdateModelAsync <PostVote>(postvote)) { try { await _context.SaveChangesAsync(); // return RedirectToAction(nameof(Index)); } catch (DbUpdateException ex) { //Log the error (uncomment ex variable name and write a log.) ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists, " + "contact your system administrator." + ex); } } return(NoContent()); } // insert var newVote = new PostVote() { PostId = id, UserId = userId }; if (vote == 0) { newVote.VoteDown = true; } else if (vote == 1) { newVote.VoteUp = true; } try { _context.PostVote.Add(newVote); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } return(Ok()); }