aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/DevHive.Data/Models/User.cs1
-rw-r--r--src/DevHive.Data/Repositories/UserRepository.cs12
-rw-r--r--src/DevHive.Services/Configurations/Mapping/UserMappings.cs2
-rw-r--r--src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs7
-rw-r--r--src/DevHive.Services/Models/Identity/User/UserServiceModel.cs2
-rw-r--r--src/DevHive.Services/Services/UserService.cs26
-rw-r--r--src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs16
-rw-r--r--src/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs16
-rw-r--r--src/DevHive.Web/Configurations/Mapping/UserMappings.cs3
-rw-r--r--src/DevHive.Web/Models/Identity/User/UserWebModel.cs2
-rw-r--r--src/DevHive.Web/Startup.cs4
11 files changed, 63 insertions, 28 deletions
diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs
index 31e36ac..cf779f5 100644
--- a/src/DevHive.Data/Models/User.cs
+++ b/src/DevHive.Data/Models/User.cs
@@ -18,6 +18,7 @@ namespace DevHive.Data.Models
/// <summary>
/// Languages that the user uses or is familiar with
/// </summary>
+ // [Unique]
public IList<Language> Languages { get; set; }
/// <summary>
diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs
index 81c974c..2ca8099 100644
--- a/src/DevHive.Data/Repositories/UserRepository.cs
+++ b/src/DevHive.Data/Repositories/UserRepository.cs
@@ -109,12 +109,14 @@ namespace DevHive.Data.Repositories
public async Task<bool> EditAsync(User newEntity)
{
- User user = await this.GetByIdAsync(newEntity.Id);
+ // User user = await this.GetByIdAsync(newEntity.Id);
- this._context
- .Entry(user)
- .CurrentValues
- .SetValues(newEntity);
+ // this._context
+ // .Entry(user)
+ // .CurrentValues
+ // .SetValues(newEntity);
+
+ this._context.Update(newEntity);
return await this.SaveChangesAsync(this._context);
}
diff --git a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs
index d57c6ba..541e16e 100644
--- a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs
+++ b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs
@@ -11,8 +11,10 @@ namespace DevHive.Services.Configurations.Mapping
CreateMap<UserServiceModel, User>();
CreateMap<RegisterServiceModel, User>();
CreateMap<UpdateUserServiceModel, User>();
+ CreateMap<FriendServiceModel, User>();
CreateMap<User, UserServiceModel>();
+ CreateMap<User, FriendServiceModel>();
}
}
}
diff --git a/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs b/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs
new file mode 100644
index 0000000..63d57f7
--- /dev/null
+++ b/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs
@@ -0,0 +1,7 @@
+namespace DevHive.Services.Models.Identity.User
+{
+ public class FriendServiceModel
+ {
+ public string UserName { get; set; }
+ }
+}
diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs
index aa77a0c..913b5c0 100644
--- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs
+++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs
@@ -9,7 +9,7 @@ namespace DevHive.Services.Models.Identity.User
{
public IList<RoleServiceModel> Roles { get; set; } = new List<RoleServiceModel>();
- public IList<UserServiceModel> Friends { get; set; } = new List<UserServiceModel>();
+ public IList<FriendServiceModel> Friends { get; set; } = new List<FriendServiceModel>();
public IList<LanguageServiceModel> Languages { get; set; } = new List<LanguageServiceModel>();
diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs
index a8b9ef9..ee4b24d 100644
--- a/src/DevHive.Services/Services/UserService.cs
+++ b/src/DevHive.Services/Services/UserService.cs
@@ -15,6 +15,7 @@ using DevHive.Services.Interfaces;
using DevHive.Data.Interfaces.Repositories;
using Microsoft.AspNetCore.JsonPatch;
using System.Linq;
+using Newtonsoft.Json;
namespace DevHive.Services.Services
{
@@ -171,18 +172,37 @@ namespace DevHive.Services.Services
User user = await this._userRepository.GetByIdAsync(id) ??
throw new ArgumentException("User does not exist!");
- var password = jsonPatch.Operations
+ object password = jsonPatch.Operations
.Where(x => x.path == "/password")
.Select(x => x.value)
.FirstOrDefault();
+ IEnumerable<object> friends = jsonPatch.Operations
+ .Where(x => x.path == "/friends")
+ .Select(x => x.value);
+
if(password != null)
{
string passwordHash = this.GeneratePasswordHash(password.ToString());
user.PasswordHash = passwordHash;
}
- else
- jsonPatch.ApplyTo(user);
+
+ if (friends != null)
+ {
+ foreach (object friendObj in friends)
+ {
+ FriendServiceModel friendServiceModel =
+ JsonConvert.DeserializeObject<FriendServiceModel>(friendObj.ToString());
+
+ User amigo = await this._userRepository.GetByUsernameAsync(friendServiceModel.UserName)
+ ?? throw new ArgumentException($"User {friendServiceModel.UserName} does not exist!");
+
+ user.Friends.Add(amigo);
+ }
+ }
+
+ //Remove password and friends peace from the request patch before applying the rest
+ // jsonPatch.ApplyTo(user);
bool success = await this._userRepository.EditAsync(user);
if (success)
diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs
deleted file mode 100644
index efcb8e1..0000000
--- a/src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs
+++ /dev/null
@@ -1,16 +0,0 @@
-using DevHive.Web.Middleware;
-using Microsoft.AspNetCore.Builder;
-using Microsoft.Extensions.DependencyInjection;
-
-namespace DevHive.Web.Configurations.Extensions
-{
- public static class ConfigureCustomMiddleware
- {
- public static void CustomMiddlewareConfiguration(this IServiceCollection services) { }
-
- public static void UseCustomMiddlewareConfiguration(this IApplicationBuilder app)
- {
- app.UseMiddleware<ExceptionMiddleware>();
- }
- }
-}
diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs
new file mode 100644
index 0000000..286727f
--- /dev/null
+++ b/src/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs
@@ -0,0 +1,16 @@
+using DevHive.Web.Middleware;
+using Microsoft.AspNetCore.Builder;
+using Microsoft.Extensions.DependencyInjection;
+
+namespace DevHive.Web.Configurations.Extensions
+{
+ public static class ConfigureExceptionHandlerMiddleware
+ {
+ public static void ExceptionHandlerMiddlewareConfiguration(this IServiceCollection services) { }
+
+ public static void UseExceptionHandlerMiddlewareConfiguration(this IApplicationBuilder app)
+ {
+ app.UseMiddleware<ExceptionMiddleware>();
+ }
+ }
+}
diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs
index beb9607..5faf4b5 100644
--- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs
+++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs
@@ -20,6 +20,9 @@ namespace DevHive.Web.Configurations.Mapping
CreateMap<TokenModel, TokenWebModel>();
+ CreateMap<FriendWebModel, FriendServiceModel>();
+ CreateMap<FriendServiceModel, FriendWebModel>();
+
CreateMap<FriendWebModel, UpdateUserCollectionServiceModel>()
.ForMember(f => f.Name, u => u.MapFrom(src => src.UserName));
CreateMap<UpdateLanguageWebModel, UpdateUserCollectionServiceModel>();
diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs
index 88f199d..1d2d17b 100644
--- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs
+++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs
@@ -15,7 +15,7 @@ namespace DevHive.Web.Models.Identity.User
[NotNull]
[Required]
- public IList<UserWebModel> Friends { get; set; } = new List<UserWebModel>();
+ public IList<FriendWebModel> Friends { get; set; } = new List<FriendWebModel>();
[NotNull]
[Required]
diff --git a/src/DevHive.Web/Startup.cs b/src/DevHive.Web/Startup.cs
index 8fa346a..92d4359 100644
--- a/src/DevHive.Web/Startup.cs
+++ b/src/DevHive.Web/Startup.cs
@@ -33,7 +33,7 @@ namespace DevHive.Web
services.JWTConfiguration(Configuration);
services.AutoMapperConfiguration();
services.DependencyInjectionConfiguration();
- services.CustomMiddlewareConfiguration();
+ services.ExceptionHandlerMiddlewareConfiguration();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -53,11 +53,11 @@ namespace DevHive.Web
else
{
app.UseHsts();
+ app.UseExceptionHandlerMiddlewareConfiguration();
}
app.UseDatabaseConfiguration();
app.UseAutoMapperConfiguration();
- app.UseCustomMiddlewareConfiguration();
app.UseEndpoints(endpoints =>
{