aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Services/Services
diff options
context:
space:
mode:
authortranstrike <transtrike@gmail.com>2021-02-03 02:59:21 +0200
committertranstrike <transtrike@gmail.com>2021-02-03 02:59:21 +0200
commit81b3fc43a900857443c3c763f52e0a54719c2fae (patch)
tree1f79f61296cc91002233a6de533698172f2e5fd7 /src/DevHive.Services/Services
parentf99d14ff45ec4e03ce261f7f5a8cccf876a94307 (diff)
downloadDevHive-81b3fc43a900857443c3c763f52e0a54719c2fae.tar
DevHive-81b3fc43a900857443c3c763f52e0a54719c2fae.tar.gz
DevHive-81b3fc43a900857443c3c763f52e0a54719c2fae.zip
User-Friends Relation Fixed
Diffstat (limited to 'src/DevHive.Services/Services')
-rw-r--r--src/DevHive.Services/Services/FeedService.cs14
-rw-r--r--src/DevHive.Services/Services/UserService.cs53
2 files changed, 41 insertions, 26 deletions
diff --git a/src/DevHive.Services/Services/FeedService.cs b/src/DevHive.Services/Services/FeedService.cs
index 92ba3ea..b9d1922 100644
--- a/src/DevHive.Services/Services/FeedService.cs
+++ b/src/DevHive.Services/Services/FeedService.cs
@@ -38,12 +38,13 @@ namespace DevHive.Services.Services
if (user == null)
throw new ArgumentException("User doesn't exist!");
- List<User> friendsList = user.Friends.Select(x => x.Friend).ToList();
- if (friendsList.Count == 0)
- throw new ArgumentException("User has no friends to get feed from!");
+ // List<User> friendsList = user.Friends.Select(x => x.Friend).ToList();
+ // if (friendsList.Count == 0)
+ // throw new ArgumentException("User has no friends to get feed from!");
- List<Post> posts = await this._feedRepository
- .GetFriendsPosts(friendsList, model.FirstRequestIssued, model.PageNumber, model.PageSize);
+ List<Post> posts = new();
+ // await this._feedRepository
+ // .GetFriendsPosts(friendsList, model.FirstRequestIssued, model.PageNumber, model.PageSize);
ReadPageServiceModel readPageServiceModel = new();
foreach (Post post in posts)
@@ -52,7 +53,8 @@ namespace DevHive.Services.Services
return readPageServiceModel;
}
- public async Task<ReadPageServiceModel> GetUserPage(GetPageServiceModel model) {
+ public async Task<ReadPageServiceModel> GetUserPage(GetPageServiceModel model)
+ {
User user = null;
if (!string.IsNullOrEmpty(model.Username))
diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs
index c8624ee..09b56c1 100644
--- a/src/DevHive.Services/Services/UserService.cs
+++ b/src/DevHive.Services/Services/UserService.cs
@@ -109,12 +109,15 @@ namespace DevHive.Services.Services
User user = await this.PopulateModel(updateUserServiceModel);
+ await this.CreateRelationToFriends(user, updateUserServiceModel.Friends.ToList());
+
bool successful = await this._userRepository.EditAsync(updateUserServiceModel.Id, user);
if (!successful)
throw new InvalidOperationException("Unable to edit user!");
- return this._userMapper.Map<UserServiceModel>(user);
+ return this._userMapper.Map<UserServiceModel>(
+ await this._userRepository.GetByIdAsync(user.Id));
}
#endregion
@@ -269,25 +272,6 @@ namespace DevHive.Services.Services
else
user.Roles = (await this._userRepository.GetByIdAsync(updateUserServiceModel.Id)).Roles;
- /* Fetch Friends and replace model's*/
- HashSet<UserFriends> friends = new();
- int friendsCount = updateUserServiceModel.Friends.Count;
- for (int i = 0; i < friendsCount; i++)
- {
- User friend = await this._userRepository.GetByUsernameAsync(updateUserServiceModel.Friends.ElementAt(i).UserName) ??
- throw new ArgumentException("Invalid friend's username!");
-
- UserFriends relation = new()
- {
- UserId = user.Id,
- User = user,
- FriendId = friend.Id,
- Friend = friend
- };
- friends.Add(relation);
- }
- user.Friends = friends;
-
/* Fetch Languages and replace model's*/
HashSet<Language> languages = new();
int languagesCount = updateUserServiceModel.Languages.Count;
@@ -314,6 +298,35 @@ namespace DevHive.Services.Services
return user;
}
+
+ private async Task<bool> CreateRelationToFriends(User user, List<UpdateFriendServiceModel> friends)
+ {
+ foreach (var friend in friends)
+ {
+ User amigo = await this._userRepository.GetByUsernameAsync(friend.UserName) ??
+ throw new ArgumentException("No amigo, bro!");
+
+ UserFriend relation = new()
+ {
+ UserId = user.Id,
+ User = user,
+ FriendId = amigo.Id,
+ Friend = amigo
+ };
+
+ UserFriend theOtherRelation = new()
+ {
+ UserId = amigo.Id,
+ User = amigo,
+ FriendId = user.Id,
+ Friend = user
+ };
+
+ user.MyFriends.Add(relation);
+ user.FriendsOf.Add(theOtherRelation);
+ }
+ return true;
+ }
#endregion
}
}