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 --- src/Data/DevHive.Data/ConnectionString.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/Data/DevHive.Data/ConnectionString.json') diff --git a/src/Data/DevHive.Data/ConnectionString.json b/src/Data/DevHive.Data/ConnectionString.json index 1281a1c..c48d214 100644 --- a/src/Data/DevHive.Data/ConnectionString.json +++ b/src/Data/DevHive.Data/ConnectionString.json @@ -1,5 +1,5 @@ { - "ConnectionStrings": { - "DEV": "Server=localhost;Port=5432;Database=DevHive_API;User Id=postgres;Password=password;" - } + "ConnectionStrings": { + "DEV": "Server=localhost;Port=5432;Database=DevHive_API;User Id=postgres;Password=password;" + } } -- cgit v1.2.3 From 5595bf9abe7b3d495f36ef39cfa5236dbde86039 Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 7 Apr 2021 19:56:55 +0300 Subject: Added adding & removing friends funtionality & endpoints for them --- src/Data/DevHive.Data.Models/Friendship.cs | 17 -------- src/Data/DevHive.Data/ConnectionString.json | 2 +- .../Repositories/FriendshipRepository.cs | 20 ---------- .../User/FriendshipServiceModel.cs | 11 ------ .../Configurations/Mapping/RoleMapings.cs | 4 +- .../DevHive.Services/Services/FriendsService.cs | 46 ++++++++++------------ 6 files changed, 24 insertions(+), 76 deletions(-) delete mode 100644 src/Data/DevHive.Data.Models/Friendship.cs delete mode 100644 src/Data/DevHive.Data/Repositories/FriendshipRepository.cs delete mode 100644 src/Services/DevHive.Services.Models/User/FriendshipServiceModel.cs (limited to 'src/Data/DevHive.Data/ConnectionString.json') diff --git a/src/Data/DevHive.Data.Models/Friendship.cs b/src/Data/DevHive.Data.Models/Friendship.cs deleted file mode 100644 index 91ac9c7..0000000 --- a/src/Data/DevHive.Data.Models/Friendship.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations.Schema; - -namespace DevHive.Data.Models -{ - //class for handling the many to many relationship betwen users and users for friends - public class Friendship - { - [NotMapped] - public Guid BaseUserId { get; set; } - [NotMapped] - public Guid FriendUserId { get; set; } - - public User BaseUser { get; set; } - public User FriendUser { get; set; } - } -} diff --git a/src/Data/DevHive.Data/ConnectionString.json b/src/Data/DevHive.Data/ConnectionString.json index c48d214..ec065d1 100644 --- a/src/Data/DevHive.Data/ConnectionString.json +++ b/src/Data/DevHive.Data/ConnectionString.json @@ -1,5 +1,5 @@ { "ConnectionStrings": { - "DEV": "Server=localhost;Port=5432;Database=DevHive_API;User Id=postgres;Password=password;" + "DEV": "Server=localhost;Port=5432;Database=DevHive_API;User Id=postgres;Password=;" } } diff --git a/src/Data/DevHive.Data/Repositories/FriendshipRepository.cs b/src/Data/DevHive.Data/Repositories/FriendshipRepository.cs deleted file mode 100644 index 94fdec3..0000000 --- a/src/Data/DevHive.Data/Repositories/FriendshipRepository.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using DevHive.Data.Models; - -namespace DevHive.Data.Repositories -{ - public class FriendshipRepository : BaseRepository - { - private readonly DevHiveContext _context; - - public FriendshipRepository(DevHiveContext context) - : base(context) - { - this._context = context; - } - } -} diff --git a/src/Services/DevHive.Services.Models/User/FriendshipServiceModel.cs b/src/Services/DevHive.Services.Models/User/FriendshipServiceModel.cs deleted file mode 100644 index b37daf2..0000000 --- a/src/Services/DevHive.Services.Models/User/FriendshipServiceModel.cs +++ /dev/null @@ -1,11 +0,0 @@ -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/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(); CreateMap(); + + CreateMap(); } } } diff --git a/src/Services/DevHive.Services/Services/FriendsService.cs b/src/Services/DevHive.Services/Services/FriendsService.cs index 6d4ee73..4e3e355 100644 --- a/src/Services/DevHive.Services/Services/FriendsService.cs +++ b/src/Services/DevHive.Services/Services/FriendsService.cs @@ -1,51 +1,45 @@ using System; -using System.Linq; using System.Threading.Tasks; -using AutoMapper; -using DevHive.Common.Jwt.Interfaces; +using DevHive.Common.Constants; +using DevHive.Data.Interfaces; +using DevHive.Data.Models; using DevHive.Services.Interfaces; -using DevHive.Services.Models.User; namespace DevHive.Services.Services { public class FriendsService : IFriendsService { - private readonly IUserService _userService; - private readonly IMapper _userMapper; + private readonly IUserRepository _friendRepository; - public FriendsService(IUserService userService, - IMapper mapper) + public FriendsService(IUserRepository friendRepository) { - this._userService = userService; - this._userMapper = mapper; + this._friendRepository = friendRepository; } public async Task AddFriend(Guid userId, Guid friendId) { - UserServiceModel user = await this._userService.GetUserById(userId); - UserServiceModel friendUser = await this._userService.GetUserById(friendId); + User user = await this._friendRepository.GetByIdAsync(userId) ?? + throw new ArgumentNullException(string.Format(ErrorMessages.DoesNotExist, nameof(user))); - UpdateUserServiceModel updateUser = this._userMapper.Map(user); - UpdateFriendServiceModel updatefriendUser = this._userMapper.Map(friendUser); + User friend = await this._friendRepository.GetByIdAsync(friendId) ?? + throw new ArgumentNullException(string.Format(ErrorMessages.DoesNotExist, nameof(friend))); - updateUser.Friends.Add(updatefriendUser); - - return (await this._userService.UpdateUser(updateUser)) - .Friends.Any(x => x.Id == friendId); + 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 RemoveFriend(Guid userId, Guid friendId) { - 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); + User user = await this._friendRepository.GetByIdAsync(userId) ?? + throw new ArgumentNullException(string.Format(ErrorMessages.DoesNotExist, nameof(user))); - updateUser.Friends.Remove(updatefriendUser); + User friend = await this._friendRepository.GetByIdAsync(friendId) ?? + throw new ArgumentNullException(string.Format(ErrorMessages.DoesNotExist, nameof(friend))); - return !(await this._userService.UpdateUser(updateUser)) - .Friends.Any(x => x.Id == friendId); + 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; } } } -- cgit v1.2.3