diff options
Diffstat (limited to 'src/Services')
4 files changed, 41 insertions, 10 deletions
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<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 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<object> AddFriend(Guid userId, Guid friendId); - Task<object> RemoveFriend(Guid userId, Guid friendId); + 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 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<object> AddFriend(Guid userId, Guid friendId) + public async Task<bool> 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<UpdateUserServiceModel>(user); + UpdateFriendServiceModel updatefriendUser = this._userMapper.Map<UpdateFriendServiceModel>(friendUser); + + updateUser.Friends.Add(updatefriendUser); + + return (await this._userService.UpdateUser(updateUser)) + .Friends.Any(x => x.Id == friendId); } - public async Task<object> RemoveFriend(Guid userId, Guid friendId) + public async Task<bool> 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<UpdateUserServiceModel>(user); + UpdateFriendServiceModel updatefriendUser = this._userMapper.Map<UpdateFriendServiceModel>(friendUser); + + updateUser.Friends.Remove(updatefriendUser); + + return !(await this._userService.UpdateUser(updateUser)) + .Friends.Any(x => x.Id == friendId); } } } |
