public IHttpActionResult AcceptFriendRequest(int friendRequestId) { var user = db.Users.FirstOrDefault(u => u.UserName == User.Identity.Name); var friendRequest = db.FriendRequests.Find(friendRequestId); if (friendRequest.ToUser != user) { return Unauthorized(); } friendRequest.AcceptRequest(); var fromRequest = new FriendRequest(friendRequest.ToUser, friendRequest.FromUser); fromRequest.SentOn = friendRequest.SentOn; fromRequest.AcceptRequest(); db.FriendRequests.Add(fromRequest); db.SaveChanges(); return Ok(friendRequest.status); }
public IHttpActionResult PutFriendRequest(int id, FriendRequest friendRequest) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != friendRequest.FriendRequestId) { return BadRequest(); } db.Entry(friendRequest).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!FriendRequestExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public IHttpActionResult PostFriendRequest(ApplicationUser user) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var fromUser = db.Users.First(u => u.UserName == User.Identity.Name); var toUser = db.Users.First(u => u.UserName == user.UserName); if (db.FriendRequests.Any(fr => fr.ToName == toUser.UserName && fr.FromName == fromUser.UserName)) { return Conflict(); } var friendRequest = new FriendRequest(fromUser, toUser); db.FriendRequests.Add(friendRequest); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = friendRequest.FriendRequestId }, friendRequest); }