aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Services
diff options
context:
space:
mode:
Diffstat (limited to 'src/DevHive.Services')
-rw-r--r--src/DevHive.Services/Configurations/Mapping/FeedMappings.cs11
-rw-r--r--src/DevHive.Services/Configurations/Mapping/PostMappings.cs1
-rw-r--r--src/DevHive.Services/Configurations/Mapping/RoleMapings.cs4
-rw-r--r--src/DevHive.Services/Configurations/Mapping/UserMappings.cs2
-rw-r--r--src/DevHive.Services/Interfaces/IFeedService.cs10
-rw-r--r--src/DevHive.Services/Interfaces/IRoleService.cs2
-rw-r--r--src/DevHive.Services/Interfaces/IUserService.cs2
-rw-r--r--src/DevHive.Services/Models/Feed/GetPageServiceModel.cs15
-rw-r--r--src/DevHive.Services/Models/Feed/ReadPageServiceModel.cs10
-rw-r--r--src/DevHive.Services/Models/Identity/User/UserServiceModel.cs2
-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
13 files changed, 150 insertions, 13 deletions
diff --git a/src/DevHive.Services/Configurations/Mapping/FeedMappings.cs b/src/DevHive.Services/Configurations/Mapping/FeedMappings.cs
new file mode 100644
index 0000000..952e480
--- /dev/null
+++ b/src/DevHive.Services/Configurations/Mapping/FeedMappings.cs
@@ -0,0 +1,11 @@
+using AutoMapper;
+
+namespace DevHive.Services.Configurations.Mapping
+{
+ public class FeedMappings : Profile
+ {
+ public FeedMappings()
+ {
+ }
+ }
+}
diff --git a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs
index cea7b1c..d8dcc84 100644
--- a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs
+++ b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs
@@ -20,6 +20,7 @@ namespace DevHive.Services.Configurations.Mapping
.ForMember(dest => dest.CreatorFirstName, src => src.Ignore())
.ForMember(dest => dest.CreatorLastName, src => src.Ignore())
.ForMember(dest => dest.CreatorUsername, src => src.Ignore());
+ //TODO: Map those here /\
}
}
}
diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs
index e61a107..23bd46f 100644
--- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs
+++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs
@@ -9,10 +9,10 @@ namespace DevHive.Services.Configurations.Mapping
public RoleMappings()
{
CreateMap<CreateRoleServiceModel, Role>();
- CreateMap<RoleServiceModel, Role>();
+ CreateMap<ReadRoleServiceModel, Role>();
CreateMap<UpdateRoleServiceModel, Role>();
- CreateMap<Role, RoleServiceModel>();
+ CreateMap<Role, ReadRoleServiceModel>();
CreateMap<Role, UpdateRoleServiceModel>();
}
}
diff --git a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs
index 5d9e41c..6797ce1 100644
--- a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs
+++ b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs
@@ -14,6 +14,8 @@ namespace DevHive.Services.Configurations.Mapping
CreateMap<UpdateUserServiceModel, User>()
.AfterMap((src, dest) => dest.PasswordHash = PasswordModifications.GeneratePasswordHash(src.Password));
CreateMap<FriendServiceModel, User>();
+ CreateMap<UpdateFriendServiceModel, User>()
+ .ForMember(dest => dest.UserName, src => src.MapFrom(p => p.Name));
CreateMap<User, UserServiceModel>();
CreateMap<User, UpdateUserServiceModel>()
diff --git a/src/DevHive.Services/Interfaces/IFeedService.cs b/src/DevHive.Services/Interfaces/IFeedService.cs
new file mode 100644
index 0000000..1edba5a
--- /dev/null
+++ b/src/DevHive.Services/Interfaces/IFeedService.cs
@@ -0,0 +1,10 @@
+using System.Threading.Tasks;
+using DevHive.Services.Models;
+
+namespace DevHive.Services.Interfaces
+{
+ public interface IFeedService
+ {
+ Task<ReadPageServiceModel> GetPage(GetPageServiceModel getPageServiceModel);
+ }
+}
diff --git a/src/DevHive.Services/Interfaces/IRoleService.cs b/src/DevHive.Services/Interfaces/IRoleService.cs
index d47728c..d3a45e5 100644
--- a/src/DevHive.Services/Interfaces/IRoleService.cs
+++ b/src/DevHive.Services/Interfaces/IRoleService.cs
@@ -8,7 +8,7 @@ namespace DevHive.Services.Interfaces
{
Task<Guid> CreateRole(CreateRoleServiceModel roleServiceModel);
- Task<RoleServiceModel> GetRoleById(Guid id);
+ Task<ReadRoleServiceModel> GetRoleById(Guid id);
Task<bool> UpdateRole(UpdateRoleServiceModel roleServiceModel);
diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs
index 51e3cf9..9372517 100644
--- a/src/DevHive.Services/Interfaces/IUserService.cs
+++ b/src/DevHive.Services/Interfaces/IUserService.cs
@@ -18,5 +18,7 @@ namespace DevHive.Services.Interfaces
Task DeleteUser(Guid id);
Task<bool> ValidJWT(Guid id, string rawTokenData);
+
+ Task<Guid> SuperSecretPromotionToAdmin(Guid userId);
}
}
diff --git a/src/DevHive.Services/Models/Feed/GetPageServiceModel.cs b/src/DevHive.Services/Models/Feed/GetPageServiceModel.cs
new file mode 100644
index 0000000..745039f
--- /dev/null
+++ b/src/DevHive.Services/Models/Feed/GetPageServiceModel.cs
@@ -0,0 +1,15 @@
+using System;
+
+namespace DevHive.Services.Models
+{
+ public class GetPageServiceModel
+ {
+ public Guid UserId { get; set; }
+
+ public int PageNumber { get; set; }
+
+ public DateTime FirstRequestIssued { get; set; }
+
+ public int PageSize { get; set; }
+ }
+}
diff --git a/src/DevHive.Services/Models/Feed/ReadPageServiceModel.cs b/src/DevHive.Services/Models/Feed/ReadPageServiceModel.cs
new file mode 100644
index 0000000..f291de7
--- /dev/null
+++ b/src/DevHive.Services/Models/Feed/ReadPageServiceModel.cs
@@ -0,0 +1,10 @@
+using System.Collections.Generic;
+using DevHive.Services.Models.Post.Post;
+
+namespace DevHive.Services.Models
+{
+ public class ReadPageServiceModel
+ {
+ public List<ReadPostServiceModel> Posts { get; set; } = new();
+ }
+}
diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs
index 3aa0d44..3e41057 100644
--- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs
+++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs
@@ -7,7 +7,7 @@ namespace DevHive.Services.Models.Identity.User
{
public class UserServiceModel : BaseUserServiceModel
{
- public HashSet<RoleServiceModel> Roles { get; set; } = new HashSet<RoleServiceModel>();
+ public HashSet<ReadRoleServiceModel> Roles { get; set; } = new HashSet<ReadRoleServiceModel>();
public HashSet<FriendServiceModel> Friends { get; set; } = new HashSet<FriendServiceModel>();
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;
+ }
}
}