aboutsummaryrefslogtreecommitdiff
path: root/src/Services/DevHive.Services
diff options
context:
space:
mode:
Diffstat (limited to 'src/Services/DevHive.Services')
-rw-r--r--src/Services/DevHive.Services/Configurations/Mapping/RoleMapings.cs4
-rw-r--r--src/Services/DevHive.Services/Configurations/Mapping/UserMappings.cs3
-rw-r--r--src/Services/DevHive.Services/Interfaces/IFriendsService.cs11
-rw-r--r--src/Services/DevHive.Services/Services/FriendsService.cs45
4 files changed, 62 insertions, 1 deletions
diff --git a/src/Services/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/Services/DevHive.Services/Configurations/Mapping/RoleMapings.cs
index 37f259a..e870ab1 100644
--- a/src/Services/DevHive.Services/Configurations/Mapping/RoleMapings.cs
+++ b/src/Services/DevHive.Services/Configurations/Mapping/RoleMapings.cs
@@ -1,5 +1,5 @@
-using DevHive.Data.Models;
using AutoMapper;
+using DevHive.Data.Models;
using DevHive.Services.Models.Role;
namespace DevHive.Services.Configurations.Mapping
@@ -14,6 +14,8 @@ namespace DevHive.Services.Configurations.Mapping
CreateMap<Role, RoleServiceModel>();
CreateMap<Role, UpdateRoleServiceModel>();
+
+ CreateMap<RoleServiceModel, UpdateRoleServiceModel>();
}
}
}
diff --git a/src/Services/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/Services/DevHive.Services/Configurations/Mapping/UserMappings.cs
index 99b41a8..5a39f73 100644
--- a/src/Services/DevHive.Services/Configurations/Mapping/UserMappings.cs
+++ b/src/Services/DevHive.Services/Configurations/Mapping/UserMappings.cs
@@ -23,6 +23,9 @@ namespace DevHive.Services.Configurations.Mapping
CreateMap<User, UpdateUserServiceModel>()
.ForMember(dest => dest.ProfilePictureURL, src => src.MapFrom(p => p.ProfilePicture.PictureURL));
CreateMap<User, FriendServiceModel>();
+
+ CreateMap<UserServiceModel, UpdateUserServiceModel>();
+ CreateMap<UserServiceModel, UpdateFriendServiceModel>();
}
}
}
diff --git a/src/Services/DevHive.Services/Interfaces/IFriendsService.cs b/src/Services/DevHive.Services/Interfaces/IFriendsService.cs
new file mode 100644
index 0000000..e337793
--- /dev/null
+++ b/src/Services/DevHive.Services/Interfaces/IFriendsService.cs
@@ -0,0 +1,11 @@
+using System;
+using System.Threading.Tasks;
+
+namespace DevHive.Services.Interfaces
+{
+ public interface IFriendsService
+ {
+ Task<bool> AddFriend(Guid userId, Guid friendId);
+ Task<bool> RemoveFriend(Guid userId, Guid friendId);
+ }
+}
diff --git a/src/Services/DevHive.Services/Services/FriendsService.cs b/src/Services/DevHive.Services/Services/FriendsService.cs
new file mode 100644
index 0000000..4e3e355
--- /dev/null
+++ b/src/Services/DevHive.Services/Services/FriendsService.cs
@@ -0,0 +1,45 @@
+using System;
+using System.Threading.Tasks;
+using DevHive.Common.Constants;
+using DevHive.Data.Interfaces;
+using DevHive.Data.Models;
+using DevHive.Services.Interfaces;
+
+namespace DevHive.Services.Services
+{
+ public class FriendsService : IFriendsService
+ {
+ private readonly IUserRepository _friendRepository;
+
+ public FriendsService(IUserRepository friendRepository)
+ {
+ this._friendRepository = friendRepository;
+ }
+
+ public async Task<bool> AddFriend(Guid userId, Guid friendId)
+ {
+ User user = await this._friendRepository.GetByIdAsync(userId) ??
+ throw new ArgumentNullException(string.Format(ErrorMessages.DoesNotExist, nameof(user)));
+
+ User friend = await this._friendRepository.GetByIdAsync(friendId) ??
+ throw new ArgumentNullException(string.Format(ErrorMessages.DoesNotExist, nameof(friend)));
+
+ bool addedToUser = user.Friends.Add(friend) && await this._friendRepository.EditAsync(userId, user);
+ bool addedToFriend = friend.Friends.Add(user) && await this._friendRepository.EditAsync(friendId, friend);
+ return addedToUser && addedToFriend;
+ }
+
+ public async Task<bool> RemoveFriend(Guid userId, Guid friendId)
+ {
+ User user = await this._friendRepository.GetByIdAsync(userId) ??
+ throw new ArgumentNullException(string.Format(ErrorMessages.DoesNotExist, nameof(user)));
+
+ User friend = await this._friendRepository.GetByIdAsync(friendId) ??
+ throw new ArgumentNullException(string.Format(ErrorMessages.DoesNotExist, nameof(friend)));
+
+ bool addedToUser = user.Friends.Remove(friend) && await this._friendRepository.EditAsync(userId, user);
+ bool addedToFriend = friend.Friends.Remove(user) && await this._friendRepository.EditAsync(friendId, friend);
+ return addedToUser && addedToFriend;
+ }
+ }
+}