From 43336a1e977f33ad49ad8b4851ca449936f93ce6 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Wed, 7 Apr 2021 19:30:04 +0300 Subject: adding functionality to add and remove friends --- .../User/FriendshipServiceModel.cs | 11 ++++++++ .../Configurations/Mapping/UserMappings.cs | 3 ++ .../DevHive.Services/Interfaces/IFriendsService.cs | 4 +-- .../DevHive.Services/Services/FriendsService.cs | 33 ++++++++++++++++------ 4 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 src/Services/DevHive.Services.Models/User/FriendshipServiceModel.cs (limited to 'src/Services') diff --git a/src/Services/DevHive.Services.Models/User/FriendshipServiceModel.cs b/src/Services/DevHive.Services.Models/User/FriendshipServiceModel.cs new file mode 100644 index 0000000..b37daf2 --- /dev/null +++ b/src/Services/DevHive.Services.Models/User/FriendshipServiceModel.cs @@ -0,0 +1,11 @@ +using System; + +namespace DevHive.Services.Models.User +{ + public class FriendshipServiceModel + { + public Guid BaseUserId { get; set; } + + public Guid FriendUserId { get; set; } + } +} 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() .ForMember(dest => dest.ProfilePictureURL, src => src.MapFrom(p => p.ProfilePicture.PictureURL)); CreateMap(); + + CreateMap(); + CreateMap(); } } } diff --git a/src/Services/DevHive.Services/Interfaces/IFriendsService.cs b/src/Services/DevHive.Services/Interfaces/IFriendsService.cs index 6ab7b90..e337793 100644 --- a/src/Services/DevHive.Services/Interfaces/IFriendsService.cs +++ b/src/Services/DevHive.Services/Interfaces/IFriendsService.cs @@ -5,7 +5,7 @@ namespace DevHive.Services.Interfaces { public interface IFriendsService { - Task AddFriend(Guid userId, Guid friendId); - Task RemoveFriend(Guid userId, Guid friendId); + Task AddFriend(Guid userId, Guid friendId); + Task RemoveFriend(Guid userId, Guid friendId); } } diff --git a/src/Services/DevHive.Services/Services/FriendsService.cs b/src/Services/DevHive.Services/Services/FriendsService.cs index 7577572..6d4ee73 100644 --- a/src/Services/DevHive.Services/Services/FriendsService.cs +++ b/src/Services/DevHive.Services/Services/FriendsService.cs @@ -1,8 +1,10 @@ using System; +using System.Linq; using System.Threading.Tasks; using AutoMapper; using DevHive.Common.Jwt.Interfaces; using DevHive.Services.Interfaces; +using DevHive.Services.Models.User; namespace DevHive.Services.Services { @@ -10,25 +12,40 @@ namespace DevHive.Services.Services { private readonly IUserService _userService; private readonly IMapper _userMapper; - private readonly IJwtService _jwtService; public FriendsService(IUserService userService, - IMapper mapper, - IJwtService jwtService) + IMapper mapper) { this._userService = userService; this._userMapper = mapper; - this._jwtService = jwtService; } - public async Task AddFriend(Guid userId, Guid friendId) + public async Task AddFriend(Guid userId, Guid friendId) { - return new { Message = "FUCK YOU" }; + UserServiceModel user = await this._userService.GetUserById(userId); + UserServiceModel friendUser = await this._userService.GetUserById(friendId); + + UpdateUserServiceModel updateUser = this._userMapper.Map(user); + UpdateFriendServiceModel updatefriendUser = this._userMapper.Map(friendUser); + + updateUser.Friends.Add(updatefriendUser); + + return (await this._userService.UpdateUser(updateUser)) + .Friends.Any(x => x.Id == friendId); } - public async Task RemoveFriend(Guid userId, Guid friendId) + public async Task RemoveFriend(Guid userId, Guid friendId) { - return new { Message = "FUCK YOU" }; + UserServiceModel user = await this._userService.GetUserById(userId); + UserServiceModel friendUser = await this._userService.GetUserById(friendId); + + UpdateUserServiceModel updateUser = this._userMapper.Map(user); + UpdateFriendServiceModel updatefriendUser = this._userMapper.Map(friendUser); + + updateUser.Friends.Remove(updatefriendUser); + + return !(await this._userService.UpdateUser(updateUser)) + .Friends.Any(x => x.Id == friendId); } } } -- cgit v1.2.3