public async Task <IActionResult> Login([FromBody] LoginModel login) { login.Username = login.Username.ToLower(); UserModel user = await _authenticationService.LoginAsync(login); UserFullInformationModel fullInfo = await _authenticationService.GetUserFullInformationAsync(user.Id); /* * if (user.Status == (int)UserStatus.NotValid) * fullInfo.Token = null;*/ if (user.Status == (int)UserStatus.NotValid) { return(StatusCode((int)HttpStatusCode.Accepted, fullInfo)); } //return StatusCode((int)HttpStatusCode.Accepted, user.Token); else { return(Ok(fullInfo)); } }
//Gets user full information public async Task <UserFullInformationModel> GetUserFullInformationAsync(int id) { Task <InformationModel> firstNameInfo = _informationRepository.GetInformationByInformationNameAsync("FirstName"); Task <InformationModel> lastNameInfo = _informationRepository.GetInformationByInformationNameAsync("LastName"); Task <InformationModel> emailInfo = _informationRepository.GetInformationByInformationNameAsync("Email"); Task <InformationModel> phoneNumberInfo = _informationRepository.GetInformationByInformationNameAsync("PhoneNumber"); UserModel user = await GetUserByIdAsync(id); Task <UserInformationModel> firstName = _userInformationRepository.GetUserInformationByIdAsync(user.Id, (await firstNameInfo).Id); Task <UserInformationModel> lastName = _userInformationRepository.GetUserInformationByIdAsync(user.Id, (await lastNameInfo).Id); Task <UserInformationModel> email = _userInformationRepository.GetUserInformationByIdAsync(user.Id, (await emailInfo).Id); Task <UserInformationModel> phoneNumber = _userInformationRepository.GetUserInformationByIdAsync(user.Id, (await phoneNumberInfo).Id); UserFullInformationModel fullInfo = new UserFullInformationModel(); fullInfo.User.Id = user.Id; fullInfo.User.Username = user.Username; fullInfo.User.Position = user.Position; fullInfo.User.Debt = 0; fullInfo.Token = user.Token; fullInfo.User.FirstName = (await firstName).Value; fullInfo.User.LastName = (await lastName).Value; fullInfo.Email = (await email).Value; fullInfo.PhoneNumber = (await phoneNumber).Value; if (user.Position == (int)UserPosition.HasNotHome) { fullInfo.NumberOfFriends = 0; fullInfo.Friends = null; fullInfo.HomeName = null; } else { user = await GetUserByIdAsync(id, true); HomeModel home = await _homeRepository.GetByIdAsync(user.Home.Id, true); fullInfo.HomeName = home.Name; fullInfo.NumberOfFriends = home.Users.Count - 1; foreach (var friend in home.Users) { if (friend.Equals(user)) { continue; } string friendFirstName = (await _userInformationRepository.GetUserInformationByIdAsync(friend.Id, (await firstNameInfo).Id)).Value; string friendLastName = (await _userInformationRepository.GetUserInformationByIdAsync(friend.Id, (await lastNameInfo).Id)).Value; double debt = 0; FriendshipModel friendship = await _friendshipRepository.GetFriendshipByIdAsync(user.Id, friend.Id); if (friendship == null) { CustomException errors = new CustomException(); errors.AddError("Unexpected Error Occured", "Friendship does not exist"); errors.Throw(); } if (friendship.User1.Id == user.Id) { debt = friendship.Debt; } else { debt = -friendship.Debt; } fullInfo.Friends.Add(new UserBaseModel(friend.Id, friend.Username, friend.Position, friendFirstName, friendLastName, debt)); } } return(fullInfo); }