public async Task <IActionResult> AddFriend(AddFriendshipDto newFriendship) { if (!ModelState.IsValid) { return(BadRequest()); } return(Ok(await _friendshipService.AddFriend(newFriendship))); }
public async Task <ServiceResponse <User> > AddFriend(AddFriendshipDto newFriendship) //User1 is trying to add User2 { ServiceResponse <User> response = new ServiceResponse <User>(); try { #region dvapristupa_contextu //User userSendingRequest = await _context.Users // .Include(u => u.Friendships).ThenInclude(fs => fs.User2) // .FirstOrDefaultAsync(u => u.Id == newFriendship.UserId1 && u.Id == GetUserId()); //User userReceivingRequest = await _context.Users.FirstOrDefaultAsync(u => u.Id == newFriendship.UserId2); #endregion dvapristupa_contextu var usersDb = await _context.Users.ToListAsync(); //isti korisnik je ulogovan i pokusava da posalje zahtev User userSendingRequest = usersDb.FirstOrDefault(u => u.Id == GetUserId() && u.Id == newFriendship.UserId1); if (userSendingRequest == null) { response.Success = false; response.Message = "User not found"; return(response); } User userReceivingRequest = usersDb.FirstOrDefault(u => u.Id == newFriendship.UserId2); if (userReceivingRequest == null) { response.Success = false; response.Message = "Cannot find user you're trying to add."; return(response); } //TREBA DA PRETRAZIM DA LI VEC POSTOJI PRIJATELJSTVO ILI NEKAKO DA ZABRANIM REQ AKO POSTOJI Friendship friendship = new Friendship { User1 = userSendingRequest, User2 = userReceivingRequest, Status = 0 }; await _context.Friendships.AddAsync(friendship); await _context.SaveChangesAsync(); response.Data = userSendingRequest; } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }