aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Data/DevHive.Data.Models/Friendship.cs17
-rw-r--r--src/Data/DevHive.Data/ConnectionString.json2
-rw-r--r--src/Data/DevHive.Data/Repositories/FriendshipRepository.cs20
-rw-r--r--src/Services/DevHive.Services.Models/User/FriendshipServiceModel.cs11
-rw-r--r--src/Services/DevHive.Services/Configurations/Mapping/RoleMapings.cs4
-rw-r--r--src/Services/DevHive.Services/Services/FriendsService.cs46
6 files changed, 24 insertions, 76 deletions
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<Friendship>
- {
- 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<Role, RoleServiceModel>();
CreateMap<Role, UpdateRoleServiceModel>();
+
+ CreateMap<RoleServiceModel, UpdateRoleServiceModel>();
}
}
}
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<bool> 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<UpdateUserServiceModel>(user);
- UpdateFriendServiceModel updatefriendUser = this._userMapper.Map<UpdateFriendServiceModel>(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<bool> RemoveFriend(Guid userId, Guid friendId)
{
- 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);
+ 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;
}
}
}