aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Services/Services
diff options
context:
space:
mode:
Diffstat (limited to 'src/DevHive.Services/Services')
-rw-r--r--src/DevHive.Services/Services/FeedService.cs47
-rw-r--r--src/DevHive.Services/Services/RoleService.cs4
-rw-r--r--src/DevHive.Services/Services/UserService.cs53
3 files changed, 95 insertions, 9 deletions
diff --git a/src/DevHive.Services/Services/FeedService.cs b/src/DevHive.Services/Services/FeedService.cs
new file mode 100644
index 0000000..cae986f
--- /dev/null
+++ b/src/DevHive.Services/Services/FeedService.cs
@@ -0,0 +1,47 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using AutoMapper;
+using DevHive.Data.Interfaces.Repositories;
+using DevHive.Data.Models;
+using DevHive.Services.Interfaces;
+using DevHive.Services.Models;
+using DevHive.Services.Models.Post.Post;
+
+namespace DevHive.Services.Services
+{
+ public class FeedService : IFeedService
+ {
+ private readonly IMapper _mapper;
+ private readonly IFeedRepository _feedRepository;
+ private readonly IUserRepository _userRepository;
+
+ public FeedService(IFeedRepository feedRepository, IUserRepository userRepository, IMapper mapper)
+ {
+ this._feedRepository = feedRepository;
+ this._userRepository = userRepository;
+ this._mapper = mapper;
+ }
+
+ public async Task<ReadPageServiceModel> GetPage(GetPageServiceModel model)
+ {
+ User user = await this._userRepository.GetByIdAsync(model.UserId) ??
+ throw new ArgumentException("User doesn't exist!");
+
+ List<User> friendsList = user.Friends.ToList();
+ // if(friendsList.Count == 0)
+ // throw new ArgumentException("This user does not have any friends!");
+
+ List<Post> posts = await this._feedRepository
+ .GetFriendsPosts(friendsList, model.FirstRequestIssued, model.PageNumber, model.PageSize) ??
+ throw new ArgumentException("No posts to query.");
+
+ ReadPageServiceModel readPageServiceModel = new();
+ foreach (Post post in posts)
+ readPageServiceModel.Posts.Add(this._mapper.Map<ReadPostServiceModel>(post));
+
+ return readPageServiceModel;
+ }
+ }
+}
diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs
index a8b8e17..9f7a5ac 100644
--- a/src/DevHive.Services/Services/RoleService.cs
+++ b/src/DevHive.Services/Services/RoleService.cs
@@ -38,12 +38,12 @@ namespace DevHive.Services.Services
}
- public async Task<RoleServiceModel> GetRoleById(Guid id)
+ public async Task<ReadRoleServiceModel> GetRoleById(Guid id)
{
Role role = await this._roleRepository.GetByIdAsync(id)
?? throw new ArgumentException("Role does not exist!");
- return this._roleMapper.Map<RoleServiceModel>(role);
+ return this._roleMapper.Map<ReadRoleServiceModel>(role);
}
public async Task<bool> UpdateRole(UpdateRoleServiceModel updateRoleServiceModel)
diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs
index d7013e1..1beb07f 100644
--- a/src/DevHive.Services/Services/UserService.cs
+++ b/src/DevHive.Services/Services/UserService.cs
@@ -111,7 +111,7 @@ namespace DevHive.Services.Services
await this.ValidateUserCollections(updateUserServiceModel);
- //Preserve roles
+ /* Roles */
int roleCount = updateUserServiceModel.Roles.Count;
for (int i = 0; i < roleCount; i++)
{
@@ -123,6 +123,7 @@ namespace DevHive.Services.Services
updateUserServiceModel.Roles.Add(updateRoleServiceModel);
}
+ /* Languages */
int langCount = updateUserServiceModel.Languages.Count;
for (int i = 0; i < langCount; i++)
{
@@ -133,10 +134,10 @@ namespace DevHive.Services.Services
updateUserServiceModel.Languages.Add(updateLanguageServiceModel);
}
-
//Clean the already replaced languages
updateUserServiceModel.Languages.RemoveWhere(x => x.Id == Guid.Empty);
+ /* Technologies */
int techCount = updateUserServiceModel.Technologies.Count;
for (int i = 0; i < techCount; i++)
{
@@ -147,11 +148,25 @@ namespace DevHive.Services.Services
updateUserServiceModel.Technologies.Add(updateTechnologyServiceModel);
}
-
//Clean the already replaced technologies
updateUserServiceModel.Technologies.RemoveWhere(x => x.Id == Guid.Empty);
+ /* Friends */
+ HashSet<User> friends = new();
+ int friendsCount = updateUserServiceModel.Friends.Count;
+ for (int i = 0; i < friendsCount; i++)
+ {
+ User friend = await this._userRepository.GetByUsernameAsync(updateUserServiceModel.Friends.ElementAt(i).Name) ??
+ throw new ArgumentException("Invalid friend's username!");
+
+ friends.Add(friend);
+ }
+ //Clean the already replaced technologies
+ updateUserServiceModel.Friends.RemoveWhere(x => x.Id == Guid.Empty);
+
User user = this._userMapper.Map<User>(updateUserServiceModel);
+ user.Friends = friends;
+
bool successful = await this._userRepository.EditAsync(updateUserServiceModel.Id, user);
if (!successful)
@@ -189,14 +204,14 @@ namespace DevHive.Services.Services
/* Check if user is trying to do something to himself, unless he's an admin */
- if (!jwtRoleNames.Contains(Role.AdminRole))
- if (user.Id != id)
- return false;
-
/* Check roles */
if (jwtRoleNames.Contains(Role.AdminRole))
return true;
+ if (!jwtRoleNames.Contains(Role.AdminRole))
+ if (user.Id != id)
+ return false;
+
// Check if jwt contains all user roles (if it doesn't, jwt is either old or tampered with)
foreach (var role in user.Roles)
{
@@ -290,5 +305,29 @@ namespace DevHive.Services.Services
return tokenHandler.WriteToken(token);
}
#endregion
+
+ public async Task<Guid> SuperSecretPromotionToAdmin(Guid userId)
+ {
+ User user = await this._userRepository.GetByIdAsync(userId) ??
+ throw new ArgumentException("User does not exist! Can't promote shit in this country...");
+
+ if(!await this._roleRepository.DoesNameExist("Admin"))
+ {
+ Role adminRole = new()
+ {
+ Name = Role.AdminRole
+ };
+ adminRole.Users.Add(user);
+
+ await this._roleRepository.AddAsync(adminRole);
+ }
+
+ Role admin = await this._roleRepository.GetByNameAsync(Role.AdminRole);
+
+ user.Roles.Add(admin);
+ await this._userRepository.EditAsync(user.Id, user);
+
+ return admin.Id;
+ }
}
}