public async Task <Friends> InsertAsync(FriendsInput friendsInput) { var userId = _logged.GetUserLoggedId(); var friend = new Friends(userId, friendsInput.UserFriendId); var checkIfAlredyExist = await _friendsRepository .GetFriendsByFriendIdAsync(userId, friendsInput.UserFriendId) .ConfigureAwait(false); var checkIfIsPending1 = await _friendsRepository .GetFriendsByFriendIdPendingAsync(userId, friendsInput.UserFriendId) .ConfigureAwait(false); var checkIfIsPending2 = await _friendsRepository .GetFriendsByFriendIdPendingAsync(friendsInput.UserFriendId, userId) .ConfigureAwait(false); if (userId == friendsInput.UserFriendId) { throw new ArgumentException("Você está tentando enviar uma solicitação para si mesmo, isso não é permitido"); } if (checkIfAlredyExist != null) { throw new ArgumentException("Você já é amigo dessa pessoa"); } if (checkIfIsPending2 != null) { throw new ArgumentException("Espere a pessoa aceitar seu convite, pois ainda está pendente"); } if (checkIfIsPending1 != null) { throw new ArgumentException("Existe uma solicitação a ser aceita por você enviada por esta amiga, apenas aceite o convite"); } if (!friend.IsValid()) { throw new ArgumentException("Existem dados que são obrigatórios e não foram preenchidos"); } var id = await _friendsRepository .InsertAsync(friend) .ConfigureAwait(false); friend.SetId(id); return(friend); }
public async Task <IActionResult> Post([FromBody] FriendsInput friendInput) { try { var postage = await _friendAppService .InsertAsync(friendInput) .ConfigureAwait(false); return(Created("", postage)); } catch (ArgumentException arg) { return(BadRequest(arg.Message)); } }