public async Task <ActionResult> Add([FromHeader] string Authentication, [FromQuery] string userId, [FromQuery] string username) { if (SessionManager.GetSessionState(Authentication) != SessionManager.SessionState.Authorized) { return(Unauthorized()); } SessionInfo sessionInfo = SessionManager.GetSessionInfo(Authentication); if (sessionInfo == null) { return(Unauthorized()); } using (UnitOfWork uow = new UnitOfWork()) { int secondUserId = 0; if (!string.IsNullOrEmpty(userId)) { int.TryParse(userId, out secondUserId); } if (secondUserId <= 0) { if (string.IsNullOrEmpty(username)) { return(NotFound()); } UsersRepository usersRepository = new UsersRepository(uow); UserDTO user = await usersRepository.GetByName(username); if (user == null) { return(NotFound("User {username} not found")); } secondUserId = user.Id; } if (secondUserId == sessionInfo.UserId) { return(Conflict("Cannot invite yourself")); } FriendshipsRepository friendshipsRepository = new FriendshipsRepository(uow); FriendshipDTO friendship = await friendshipsRepository.GetFriendship(secondUserId, sessionInfo.UserId); if (friendship == null) { friendship = new FriendshipDTO { UserOneId = sessionInfo.UserId, UserTwoId = secondUserId, Status = 0 }; await friendshipsRepository.Add(friendship); } else { if (friendship.Status == 0 && friendship.UserOneId == secondUserId) { friendship.Status = 1; await friendshipsRepository.Update(friendship); } } uow.Commit(); return(Ok(friendship)); } }