public IActionResult Post([FromBody] Post postTransport) { if (ModelState.IsValid) { context.Post.Add(postTransport); context.SaveChanges(); return(Ok(postTransport)); } return(BadRequest(ModelState)); }
public IActionResult Post([FromBody] User user) { if (ModelState.IsValid) { context.User.Add(user); context.SaveChanges(); log.LogInformation("User added"); return(Ok(user)); } return(BadRequest(ModelState)); }
public IActionResult AddComment([FromBody]Comment comment) { if (ModelState.IsValid) { comment.id_user = context.User.FirstOrDefault(u => u.login == HttpContext.Session.GetString("Username")).id; context.Comment.Add(comment); context.SaveChanges(); return Ok(); } return BadRequest(); }
public IActionResult Subscribe([FromBody] Id userIdToSub) { if (ModelState.IsValid) { //this user wants to subscribe another user User user = context.User.FirstOrDefault(u => u.login == HttpContext.Session.GetString("Username")); List <Following> followings = context.Following.ToList(); var userToSubscribe = context.Following.FirstOrDefault(x => x.id_who == user.id && x.id_whom == user.id); if (userToSubscribe != null) { return(BadRequest()); } Following subscription = new Following(user.id, userIdToSub.id); context.Following.Add(subscription); context.SaveChanges(); return(Ok()); } return(BadRequest()); }
public IActionResult Like() { long id_user = context.User.FirstOrDefault(u => u.login == HttpContext.Session.GetString("Username")).id; long id_post = Convert.ToInt64(Request.Headers["id_post"]); if (context.LikePost.FirstOrDefault(l => (l.id_user == id_user && l.id_post == id_post)) != null) { context.LikePost.Remove(context.LikePost.FirstOrDefault(l => (l.id_user == id_user && l.id_post == id_post))); } else { LikePost like = new LikePost(id_user, id_post, true); context.LikePost.Add(like); } context.SaveChanges(); return(Ok()); }