aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/DevHive.Common/DevHive.Common.csproj12
-rw-r--r--src/DevHive.Common/Models/Data/RepositoryMethods.cs15
-rw-r--r--src/DevHive.Common/Models/Identity/TokenModel.cs12
-rw-r--r--src/DevHive.Data/DevHive.Data.csproj4
-rw-r--r--src/DevHive.Data/Repositories/LanguageRepository.cs14
-rw-r--r--src/DevHive.Data/Repositories/RoleRepository.cs20
-rw-r--r--src/DevHive.Data/Repositories/UserRepository.cs13
-rw-r--r--src/DevHive.Services/DevHive.Services.csproj4
-rw-r--r--src/DevHive.Services/Models/Identity/User/TokenServiceModel.cs12
-rw-r--r--src/DevHive.Services/Services/UserService.cs20
-rw-r--r--src/DevHive.Web/Configurations/Mapping/UserMappings.cs4
-rw-r--r--src/DevHive.Web/Controllers/UserController.cs10
-rw-r--r--src/DevHive.Web/DevHive.Web.csproj1
-rw-r--r--src/DevHive.code-workspace4
14 files changed, 95 insertions, 50 deletions
diff --git a/src/DevHive.Common/DevHive.Common.csproj b/src/DevHive.Common/DevHive.Common.csproj
new file mode 100644
index 0000000..1aca8a6
--- /dev/null
+++ b/src/DevHive.Common/DevHive.Common.csproj
@@ -0,0 +1,12 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <TargetFramework>net5.0</TargetFramework>
+ </PropertyGroup>
+
+<ItemGroup>
+ <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.1" />
+</ItemGroup>
+
+
+</Project>
diff --git a/src/DevHive.Common/Models/Data/RepositoryMethods.cs b/src/DevHive.Common/Models/Data/RepositoryMethods.cs
new file mode 100644
index 0000000..b56aad9
--- /dev/null
+++ b/src/DevHive.Common/Models/Data/RepositoryMethods.cs
@@ -0,0 +1,15 @@
+using System.Threading.Tasks;
+using Microsoft.EntityFrameworkCore;
+
+namespace DevHive.Common.Models.Data
+{
+ public static class RepositoryMethods
+ {
+ public static async Task<bool> SaveChangesAsync(DbContext context)
+ {
+ int result = await context.SaveChangesAsync();
+
+ return result >= 1;
+ }
+ }
+} \ No newline at end of file
diff --git a/src/DevHive.Common/Models/Identity/TokenModel.cs b/src/DevHive.Common/Models/Identity/TokenModel.cs
new file mode 100644
index 0000000..0fb6c82
--- /dev/null
+++ b/src/DevHive.Common/Models/Identity/TokenModel.cs
@@ -0,0 +1,12 @@
+namespace DevHive.Common.Models.Identity
+{
+ public class TokenModel
+ {
+ public TokenModel(string token)
+ {
+ this.Token = token;
+ }
+
+ public string Token { get; set; }
+ }
+} \ No newline at end of file
diff --git a/src/DevHive.Data/DevHive.Data.csproj b/src/DevHive.Data/DevHive.Data.csproj
index d472d1c..c1e1592 100644
--- a/src/DevHive.Data/DevHive.Data.csproj
+++ b/src/DevHive.Data/DevHive.Data.csproj
@@ -16,4 +16,8 @@
</PackageReference>
</ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\DevHive.Common\DevHive.Common.csproj" />
+ </ItemGroup>
+
</Project>
diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs
index 08efd18..a33bd5b 100644
--- a/src/DevHive.Data/Repositories/LanguageRepository.cs
+++ b/src/DevHive.Data/Repositories/LanguageRepository.cs
@@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using Data.Models.Interfaces.Database;
+using DevHive.Common.Models.Data;
using DevHive.Data.Models;
using Microsoft.EntityFrameworkCore;
@@ -22,7 +23,7 @@ namespace DevHive.Data.Repositories
.Set<Language>()
.AddAsync(entity);
- return await this.SaveChangesAsync();
+ return await RepositoryMethods.SaveChangesAsync(this._context);
}
//Read
@@ -56,7 +57,7 @@ namespace DevHive.Data.Repositories
.Set<Language>()
.Update(newEntity);
- return await this.SaveChangesAsync();
+ return await RepositoryMethods.SaveChangesAsync(this._context);
}
//Delete
@@ -66,14 +67,7 @@ namespace DevHive.Data.Repositories
.Set<Language>()
.Remove(entity);
- return await this.SaveChangesAsync();
+ return await RepositoryMethods.SaveChangesAsync(this._context);
}
-
- private async Task<bool> SaveChangesAsync()
- {
- int result = await this._context.SaveChangesAsync();
-
- return result >= 0;
- }
}
} \ No newline at end of file
diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs
index 9b6cf14..ad9bda0 100644
--- a/src/DevHive.Data/Repositories/RoleRepository.cs
+++ b/src/DevHive.Data/Repositories/RoleRepository.cs
@@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using Data.Models.Interfaces.Database;
+using DevHive.Common.Models.Data;
using DevHive.Data.Models;
using Microsoft.EntityFrameworkCore;
@@ -16,13 +17,13 @@ namespace DevHive.Data.Repositories
}
//Create
- public async Task AddAsync(Role entity)
+ public async Task<bool> AddAsync(Role entity)
{
await this._context
.Set<Role>()
.AddAsync(entity);
- await this._context.SaveChangesAsync();
+ return await RepositoryMethods.SaveChangesAsync(this._context);
}
//Read
@@ -35,23 +36,26 @@ namespace DevHive.Data.Repositories
}
//Update
- public async Task EditAsync(Role newEntity)
+ public async Task<bool> EditAsync(Role newEntity)
{
+ Role role = await this.GetByIdAsync(newEntity.Id);
+
this._context
- .Set<Role>()
- .Update(newEntity);
+ .Entry(role)
+ .CurrentValues
+ .SetValues(newEntity);
- await this._context.SaveChangesAsync();
+ return await RepositoryMethods.SaveChangesAsync(this._context);
}
//Delete
- public async Task DeleteAsync(Role entity)
+ public async Task<bool> DeleteAsync(Role entity)
{
this._context
.Set<Role>()
.Remove(entity);
- await this._context.SaveChangesAsync();
+ return await RepositoryMethods.SaveChangesAsync(this._context);
}
public async Task<bool> DoesNameExist(string name)
diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs
index 714218d..f5a074b 100644
--- a/src/DevHive.Data/Repositories/UserRepository.cs
+++ b/src/DevHive.Data/Repositories/UserRepository.cs
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Data.Models.Interfaces.Database;
+using DevHive.Common.Models.Data;
using DevHive.Data.Models;
using Microsoft.EntityFrameworkCore;
@@ -18,13 +19,13 @@ namespace DevHive.Data.Repositories
}
//Create
- public async Task AddAsync(User entity)
+ public async Task<bool> AddAsync(User entity)
{
await this._context
.Set<User>()
.AddAsync(entity);
- await this._context.SaveChangesAsync();
+ return await RepositoryMethods.SaveChangesAsync(this._context);
}
//Read
@@ -51,7 +52,7 @@ namespace DevHive.Data.Repositories
}
//Update
- public async Task EditAsync(User newEntity)
+ public async Task<bool> EditAsync(User newEntity)
{
User user = await this.GetByIdAsync(newEntity.Id);
@@ -60,17 +61,17 @@ namespace DevHive.Data.Repositories
.CurrentValues
.SetValues(newEntity);
- await this._context.SaveChangesAsync();
+ return await RepositoryMethods.SaveChangesAsync(this._context);
}
//Delete
- public async Task DeleteAsync(User entity)
+ public async Task<bool> DeleteAsync(User entity)
{
this._context
.Set<User>()
.Remove(entity);
- await this._context.SaveChangesAsync();
+ return await RepositoryMethods.SaveChangesAsync(this._context);
}
//Validations
diff --git a/src/DevHive.Services/DevHive.Services.csproj b/src/DevHive.Services/DevHive.Services.csproj
index 280610d..19f67d8 100644
--- a/src/DevHive.Services/DevHive.Services.csproj
+++ b/src/DevHive.Services/DevHive.Services.csproj
@@ -16,5 +16,9 @@
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.0" />
</ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\DevHive.Common\DevHive.Common.csproj" />
+ </ItemGroup>
+
</Project>
diff --git a/src/DevHive.Services/Models/Identity/User/TokenServiceModel.cs b/src/DevHive.Services/Models/Identity/User/TokenServiceModel.cs
deleted file mode 100644
index 631808e..0000000
--- a/src/DevHive.Services/Models/Identity/User/TokenServiceModel.cs
+++ /dev/null
@@ -1,12 +0,0 @@
-namespace DevHive.Services.Models.Identity.User
-{
- public class TokenServiceModel
- {
- public TokenServiceModel(string token)
- {
- this.Token = token;
- }
-
- public string Token { get; set; }
- }
-} \ No newline at end of file
diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs
index 24f74f5..63b8389 100644
--- a/src/DevHive.Services/Services/UserService.cs
+++ b/src/DevHive.Services/Services/UserService.cs
@@ -3,7 +3,6 @@ using DevHive.Data.Repositories;
using DevHive.Services.Options;
using DevHive.Services.Models.Identity.User;
using System.Threading.Tasks;
-using Microsoft.AspNetCore.Mvc;
using DevHive.Data.Models;
using System;
using System.IdentityModel.Tokens.Jwt;
@@ -12,6 +11,7 @@ using Microsoft.IdentityModel.Tokens;
using System.Security.Cryptography;
using System.Text;
using System.Collections.Generic;
+using DevHive.Common.Models.Identity;
namespace DevHive.Services.Services
{
@@ -28,7 +28,7 @@ namespace DevHive.Services.Services
this._jwtOptions = jwtOptions;
}
- public async Task<TokenServiceModel> LoginUser(LoginServiceModel loginModel)
+ public async Task<TokenModel> LoginUser(LoginServiceModel loginModel)
{
if (!await this._userRepository.IsUsernameValid(loginModel.UserName))
throw new ArgumentException("Invalid username!");
@@ -38,10 +38,10 @@ namespace DevHive.Services.Services
if (user.PasswordHash != GeneratePasswordHash(loginModel.Password))
throw new ArgumentException("Incorrect password!");
- return new TokenServiceModel(WriteJWTSecurityToken(user.Role));
+ return new TokenModel(WriteJWTSecurityToken(user.Role));
}
- public async Task<TokenServiceModel> RegisterUser(RegisterServiceModel registerModel)
+ public async Task<TokenModel> RegisterUser(RegisterServiceModel registerModel)
{
if (await this._userRepository.DoesUsernameExist(registerModel.UserName))
throw new ArgumentException("Username already exists!");
@@ -55,7 +55,7 @@ namespace DevHive.Services.Services
await this._userRepository.AddAsync(user);
- return new TokenServiceModel(WriteJWTSecurityToken(user.Role));
+ return new TokenModel(WriteJWTSecurityToken(user.Role));
}
public async Task<UserServiceModel> GetUserById(Guid id)
@@ -76,7 +76,10 @@ namespace DevHive.Services.Services
throw new ArgumentException("Username already exists!");
User user = this._userMapper.Map<User>(updateModel);
- await this._userRepository.EditAsync(user);
+ bool result = await this._userRepository.EditAsync(user);
+
+ if (!result)
+ throw new InvalidOperationException("Unable to edit user!");
return this._userMapper.Map<UserServiceModel>(user);;
}
@@ -87,7 +90,10 @@ namespace DevHive.Services.Services
throw new ArgumentException("User does not exist!");
User user = await this._userRepository.GetByIdAsync(id);
- await this._userRepository.DeleteAsync(user);
+ bool result = await this._userRepository.DeleteAsync(user);
+
+ if (!result)
+ throw new InvalidOperationException("Unable to delete user!");
}
private string GeneratePasswordHash(string password)
diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs
index 1fdfc6a..4420368 100644
--- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs
+++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs
@@ -1,7 +1,7 @@
-using DevHive.Data.Models;
using AutoMapper;
using DevHive.Services.Models.Identity.User;
using DevHive.Web.Models.Identity.User;
+using DevHive.Common.Models.Identity;
namespace DevHive.Web.Configurations.Mapping
{
@@ -16,7 +16,7 @@ namespace DevHive.Web.Configurations.Mapping
CreateMap<UserServiceModel, UserWebModel>();
- CreateMap<TokenServiceModel, TokenWebModel>();
+ CreateMap<TokenModel, TokenWebModel>();
}
}
}
diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs
index 80e1bde..1360bfe 100644
--- a/src/DevHive.Web/Controllers/UserController.cs
+++ b/src/DevHive.Web/Controllers/UserController.cs
@@ -1,7 +1,6 @@
using System;
using System.Threading.Tasks;
using AutoMapper;
-using DevHive.Data.Models;
using DevHive.Data.Repositories;
using DevHive.Services.Models.Identity.User;
using DevHive.Services.Options;
@@ -9,6 +8,7 @@ using DevHive.Services.Services;
using DevHive.Web.Models.Identity.User;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
+using DevHive.Common.Models.Identity;
namespace DevHive.Web.Controllers
{
@@ -33,8 +33,8 @@ namespace DevHive.Web.Controllers
{
LoginServiceModel loginServiceModel = this._userMapper.Map<LoginServiceModel>(loginModel);
- TokenServiceModel tokenServiceModel = await this._userService.LoginUser(loginServiceModel);
- TokenWebModel tokenWebModel = this._userMapper.Map<TokenWebModel>(tokenServiceModel);
+ TokenModel TokenModel = await this._userService.LoginUser(loginServiceModel);
+ TokenWebModel tokenWebModel = this._userMapper.Map<TokenWebModel>(TokenModel);
return new OkObjectResult(tokenWebModel);
}
@@ -46,8 +46,8 @@ namespace DevHive.Web.Controllers
{
RegisterServiceModel registerServiceModel = this._userMapper.Map<RegisterServiceModel>(registerModel);
- TokenServiceModel tokenServiceModel = await this._userService.RegisterUser(registerServiceModel);
- TokenWebModel tokenWebModel = this._userMapper.Map<TokenWebModel>(tokenServiceModel);
+ TokenModel TokenModel = await this._userService.RegisterUser(registerServiceModel);
+ TokenWebModel tokenWebModel = this._userMapper.Map<TokenWebModel>(TokenModel);
return new CreatedResult("Register", tokenWebModel);
}
diff --git a/src/DevHive.Web/DevHive.Web.csproj b/src/DevHive.Web/DevHive.Web.csproj
index 2a1faf1..0be5bdc 100644
--- a/src/DevHive.Web/DevHive.Web.csproj
+++ b/src/DevHive.Web/DevHive.Web.csproj
@@ -20,5 +20,6 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DevHive.Services\DevHive.Services.csproj" />
+ <ProjectReference Include="..\DevHive.Common\DevHive.Common.csproj" />
</ItemGroup>
</Project> \ No newline at end of file
diff --git a/src/DevHive.code-workspace b/src/DevHive.code-workspace
index 730aab1..7e952c7 100644
--- a/src/DevHive.code-workspace
+++ b/src/DevHive.code-workspace
@@ -12,6 +12,10 @@
"name": "DevHive.Data",
"path": "./DevHive.Data"
},
+ {
+ "name": "DevHive.Common",
+ "path": "./DevHive.Common"
+ },
],
"settings": {
"files.exclude": {