From dee2e37a4a8759108390c664e06bf147b8385cbf Mon Sep 17 00:00:00 2001 From: transtrike Date: Mon, 14 Dec 2020 23:29:14 +0200 Subject: Stabalized project for compilation. Next step after init architecture --- src/DevHive.Web/Configurations/Mapping/UserMappings.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/DevHive.Web/Configurations/Mapping/UserMappings.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs new file mode 100644 index 0000000..f3daf5a --- /dev/null +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -0,0 +1,16 @@ +using DevHive.Data.Models; +using AutoMapper; +using DevHive.Services.Models.Identity; + +namespace DevHive.Web.Configurations.Mapping +{ + public class UserMappings : Profile + { + public UserMappings() + { + CreateMap(); + CreateMap(); + CreateMap(); + } + } +} -- cgit v1.2.3 From 1f84b7d7da1464fab8178188f97164e4718527ed Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 15 Dec 2020 11:02:54 +0200 Subject: Updated role controller and role service to use custom models and implemented role mapping --- .../Models/Identity/Role/RoleServiceModel.cs | 10 +++++++ src/DevHive.Services/Services/RoleService.cs | 20 ++++++++++--- .../Configurations/Mapping/RoleMappings.cs | 15 ++++++++++ src/DevHive.Web/Controllers/RoleController.cs | 34 ++++++++++++++++------ .../Models/Identity/Role/UpdateRoleWebModel.cs | 4 ++- 5 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs create mode 100644 src/DevHive.Web/Configurations/Mapping/RoleMappings.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs new file mode 100644 index 0000000..3f834ef --- /dev/null +++ b/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Services.Models.Identity.Role +{ + public class RoleServiceModel + { + public Guid Id { get; set; } + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index 202c611..9077a47 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -1,26 +1,38 @@ using System; using System.Threading.Tasks; using DevHive.Data.Repositories; +using DevHive.Services.Models.Identity.Role; +using Microsoft.AspNetCore.Mvc; namespace DevHive.Services.Services { public class RoleService { - /* private readonly DevHiveContext _context; + private readonly DevHiveContext _context; public RoleService(DevHiveContext context) { this._context = context; } - public Task CreatePost(string name) + public Task CreateRole(RoleServiceModel roleServiceModel) { throw new NotImplementedException(); } - public Task GetPostById(uint postId) + public Task GetRoleById(Guid id) { throw new NotImplementedException(); - }*/ + } + + public Task UpdateRole(RoleServiceModel roleServiceModel) + { + throw new NotImplementedException(); + } + + public Task DeleteRole(Guid id) + { + throw new NotImplementedException(); + } } } diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs new file mode 100644 index 0000000..9ccb7d7 --- /dev/null +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -0,0 +1,15 @@ +using AutoMapper; +using DevHive.Web.Models.Identity.Role; +using DevHive.Services.Models.Identity.Role; + +namespace DevHive.Web.Configurations.Mapping +{ + public class RoleMappings : Profile + { + public RoleMappings() + { + CreateMap(); + CreateMap(); + } + } +} diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index fda8bb2..e39d858 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -1,34 +1,50 @@ -using System; using System.Threading.Tasks; using DevHive.Data.Repositories; using DevHive.Services.Services; using Microsoft.AspNetCore.Mvc; +using DevHive.Web.Models.Identity.Role; +using AutoMapper; +using DevHive.Services.Models.Identity.Role; +using System; namespace DevHive.Web.Controllers { - [ApiController] + [ApiController] [Route("/api/[controller]")] public class RoleController { private readonly RoleService _service; + private readonly IMapper _roleMapper; public RoleController(DevHiveContext context) { - //this._service = new RoleService(context); + this._service = new RoleService(context); } [HttpPost] - public Task Create(string name) + public Task Create(CreateRoleWebModel createRoleWebModel) { - //return this._service.CreatePost(name); - throw new NotImplementedException(); + RoleServiceModel roleServiceModel = this._roleMapper.Map(createRoleWebModel); + return this._service.CreateRole(roleServiceModel); } [HttpGet] - public Task ShowPost(uint postId) + public Task Get(Guid id) + { + return this._service.GetRoleById(id); + } + + [HttpPut] + public Task Update(UpdateRoleWebModel updateRoleWebModel) + { + RoleServiceModel roleServiceModel = this._roleMapper.Map(updateRoleWebModel); + return this._service.UpdateRole(roleServiceModel); + } + + [HttpDelete] + public Task Delete(Guid id) { - //return this._service.GetPostById(postId); - throw new NotImplementedException(); + return this._service.DeleteRole(id); } } } diff --git a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs index 58eef1f..b970dd5 100644 --- a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs @@ -1,8 +1,10 @@ +using System; + namespace DevHive.Web.Models.Identity.Role { public class UpdateRoleWebModel { - public string OldName { get; set; } + public Guid Id { get; set; } public string NewName { get; set; } } } -- cgit v1.2.3 From 54d081a513117c732ab4d62312b440d37dfe0d67 Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 15 Dec 2020 19:38:50 +0200 Subject: User Controller, Service & Data implemented --- src/DevHive.Data/Repositories/UserRepository.cs | 73 ++++++++++---- src/DevHive.Services/Services/UserService.cs | 105 ++++++++++++--------- .../Configurations/Mapping/UserMappings.cs | 9 +- src/DevHive.Web/Controllers/UserController.cs | 31 +++--- src/DevHive.Web/appsettings.json | 7 ++ 5 files changed, 141 insertions(+), 84 deletions(-) (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 5b30c30..13ee2bc 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -17,21 +17,7 @@ namespace DevHive.Data.Repositories this._context = context; } - public bool DoesUserExist(Guid id) - { - return this._context - .Set() - .Any(x => x.Id == id); - } - - public bool HasThisUsername(Guid id, string username) - { - return this._context - .Set() - .Any(x => x.Id == id && - x.UserName == username); - } - + //Create public async Task AddAsync(User entity) { await this._context @@ -40,24 +26,31 @@ namespace DevHive.Data.Repositories await this._context.SaveChangesAsync(); } - - public IEnumerable Query(int count) + + //Read + public IEnumerable QueryAll() { return this._context .Set() .AsNoTracking() - .Take(count) .AsEnumerable(); - } - public async Task FindByIdAsync(Guid id) + public async Task GetByIdAsync(Guid id) { return await this._context .Set() .FindAsync(id); } + public async Task GetByUsername(string username) + { + return await this._context + .Set() + .FirstOrDefaultAsync(x => x.UserName == username); + } + + //Update public async Task EditAsync(User newEntity) { this._context @@ -67,6 +60,7 @@ namespace DevHive.Data.Repositories await this._context.SaveChangesAsync(); } + //Delete public async Task DeleteAsync(User entity) { this._context @@ -75,5 +69,44 @@ namespace DevHive.Data.Repositories await this._context.SaveChangesAsync(); } + + //Validations + public bool DoesUserExist(Guid id) + { + return this._context + .Set() + .Any(x => x.Id == id); + } + + public Task IsUsernameValid(string username) + { + return this._context + .Set() + .AnyAsync(u => u.UserName == username); + } + + public bool DoesUserHaveThisUsername(Guid id, string username) + { + return this._context + .Set() + .Any(x => x.Id == id && + x.UserName == username); + } + + public async Task DoesUsernameExist(string username) + { + return await this._context + .Set() + .AsNoTracking() + .AnyAsync(u => u.UserName == username); + } + + public async Task DoesEmailExist(string email) + { + return await this._context + .Set() + .AsNoTracking() + .AnyAsync(u => u.Email == email); + } } } diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index d235755..460c3c9 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -1,14 +1,16 @@ using AutoMapper; using DevHive.Data.Repositories; using DevHive.Services.Options; +using DevHive.Services.Models.Identity.User; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; -using System.IdentityModel.Tokens.Jwt; using DevHive.Data.Models; -using System.Text; -using Microsoft.IdentityModel.Tokens; -using System.Security.Claims; using System; +using System.IdentityModel.Tokens.Jwt; +using System.Security.Claims; +using Microsoft.IdentityModel.Tokens; +using System.Security.Cryptography; +using System.Text; namespace DevHive.Services.Services { @@ -25,61 +27,44 @@ namespace DevHive.Services.Services this._jwtOptions = jwtOptions; } - public async Task LoginUser(LoginWebModel loginDTO) + public async Task LoginUser(LoginServiceModel loginModel) { - User user = this._userRepository.FindByUsername(loginDTO.UserName); + if (!await this._userRepository.IsUsernameValid(loginModel.UserName)) + return new BadRequestObjectResult("Invalid username!"); - if (user == null) - return new NotFoundObjectResult("User does not exist!"); + User user = await this._userRepository + .GetByUsername(loginModel.UserName); - byte[] key = Encoding.ASCII.GetBytes(_jwtOptions.Secret); - - if (user.PasswordHash != GeneratePasswordHash(loginDTO.Password)) + if (user.PasswordHash != GeneratePasswordHash(loginModel.Password)) return new BadRequestObjectResult("Incorrect password!"); - // Create Jwt Token configuration - var tokenDescriptor = new SecurityTokenDescriptor + return new OkObjectResult(new { - Subject = new ClaimsIdentity(new Claim[] - { - new Claim(ClaimTypes.Role, user.Role) // Authorize user by role - }), - Expires = DateTime.UtcNow.AddDays(7), - SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha512Signature) - }; - - var tokenHandler = new JwtSecurityTokenHandler(); - var token = tokenHandler.CreateToken(tokenDescriptor); - var tokenString = tokenHandler.WriteToken(token); - - return new OkObjectResult(new { Token = tokenString }); + Token = WriteJWTSecurityToken(user.Role) + }); } - public async Task RegisterUser(RegisterDTO registerDTO) + public async Task RegisterUser(RegisterServiceModel registerModel) { - if (this._userRepository.DoesUsernameExist(registerDTO.UserName)) + if (await this._userRepository.DoesUsernameExist(registerModel.UserName)) return new BadRequestObjectResult("Username already exists!"); - User user = this._userMapper.Map(registerDTO); + if (await this._userRepository.DoesEmailExist(registerModel.Email)) + return new BadRequestObjectResult("Username already exists!"); - user.Role = UserRoles.User; - user.PasswordHash = GeneratePasswordHash(registerDTO.Password); + User user = this._userMapper.Map(registerModel); + user.Role = "User"; + user.PasswordHash = GeneratePasswordHash(registerModel.Password); await this._userRepository.AddAsync(user); return new CreatedResult("CreateUser", user); } - private string GeneratePasswordHash(string password) - { - //TODO: Implement - return password; - } - - public async Task GetUserById(Guid id) + public async Task GetUserById(Guid id) { - User user = await this._userRepository.FindByIdAsync(id); + User user = await this._userRepository.GetByIdAsync(id); if (user == null) return new NotFoundObjectResult("User does not exist!"); @@ -87,17 +72,17 @@ namespace DevHive.Services.Services return new OkObjectResult(user); } - public async Task UpdateUser(Guid id, UserDTO userDTO) + public async Task UpdateUser(Guid id, UpdateUserServiceModel updateModel) { if (!this._userRepository.DoesUserExist(id)) return new NotFoundObjectResult("User does not exist!"); - if (!this._userRepository.HasThisUsername(id, userDTO.UserName) - && this._userRepository.DoesUsernameExist(userDTO.UserName)) + if (!this._userRepository.DoesUserHaveThisUsername(id, updateModel.UserName) + && await this._userRepository.IsUsernameValid(updateModel.UserName)) return new BadRequestObjectResult("Username already exists!"); - User user = this._userMapper.Map(userDTO); - await this._userRepository.EditAsync(id, user); + User user = this._userMapper.Map(updateModel); + await this._userRepository.EditAsync(user); return new AcceptedResult("UpdateUser", user); } @@ -107,9 +92,37 @@ namespace DevHive.Services.Services if (!this._userRepository.DoesUserExist(id)) return new NotFoundObjectResult("User does not exist!"); - await this._userDbRepository.DeleteAsync(id); - + User user = await this._userRepository.GetByIdAsync(id); + await this._userRepository.DeleteAsync(user); + return new OkResult(); } + + private string GeneratePasswordHash(string password) + { + return SHA512.HashData(Encoding.ASCII.GetBytes(password)).ToString(); + } + + private string WriteJWTSecurityToken(string role) + { + //TODO: Try generating the key + byte[] signingKey = Convert.FromBase64String(_jwtOptions.Secret); + + SecurityTokenDescriptor tokenDescriptor = new() + { + Subject = new ClaimsIdentity(new Claim[] + { + new Claim(ClaimTypes.Role, role) + }), + Expires = DateTime.Today.AddDays(7), + SigningCredentials = new SigningCredentials( + new SymmetricSecurityKey(signingKey), + SecurityAlgorithms.HmacSha512Signature) + }; + + JwtSecurityTokenHandler tokenHandler = new(); + SecurityToken token = tokenHandler.CreateToken(tokenDescriptor); + return tokenHandler.WriteToken(token); + } } } diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index f3daf5a..2964a00 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -1,6 +1,7 @@ using DevHive.Data.Models; using AutoMapper; -using DevHive.Services.Models.Identity; +using DevHive.Services.Models.Identity.User; +using DevHive.Web.Models.Identity.User; namespace DevHive.Web.Configurations.Mapping { @@ -8,9 +9,9 @@ namespace DevHive.Web.Configurations.Mapping { public UserMappings() { - CreateMap(); - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 14ecb73..480fbe4 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -2,6 +2,7 @@ using System; using System.Threading.Tasks; using AutoMapper; using DevHive.Data.Repositories; +using DevHive.Services.Models.Identity.User; using DevHive.Services.Options; using DevHive.Services.Services; using DevHive.Web.Models.Identity.User; @@ -15,44 +16,47 @@ namespace DevHive.Web.Controllers public class UserController: ControllerBase { private readonly UserService _service; + private readonly IMapper _userMapper; public UserController(DevHiveContext context, IMapper mapper, JWTOptions jwtOptions) { this._service = new UserService(context, mapper, jwtOptions); + this._userMapper = mapper; } [HttpPost] [Route("Login")] - public async Task Login([FromBody] LoginWebModel loginWebModel) + public async Task Login([FromBody] LoginWebModel loginModel) { - var loginDTO = - return await this._service.LoginUser(loginDTO); - //throw new NotImplementedException(); + LoginServiceModel loginServiceModel = this._userMapper.Map(loginModel); + + return await this._service.LoginUser(loginServiceModel); } [HttpPost] [Route("Register")] - public async Task Register([FromBody] RegisterWebModel registerWebModel) + public async Task Register([FromBody] RegisterWebModel registerModel) { - //return await this._service.RegisterUser(registerDto); - throw new NotImplementedException(); + RegisterServiceModel registerServiceModel = this._userMapper.Map(registerModel); + + return await this._service.RegisterUser(registerServiceModel); } //Read [HttpGet] public async Task GetById(Guid id) { - //return await this._service.GetUserById(id); - throw new NotImplementedException(); + return await this._service.GetUserById(id); } //Update [HttpPut] [Authorize] - public async Task Update(Guid id, [FromBody] UpdateUserWebModel updateUserWebModel) + public async Task Update(Guid id, [FromBody] UpdateUserWebModel updateModel) { - //return await this._service.UpdateUser(id, userDTO); - throw new NotImplementedException(); + UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map(updateModel); + + return await this._service.UpdateUser(id, updateUserServiceModel); } //Delete @@ -60,8 +64,7 @@ namespace DevHive.Web.Controllers [Authorize] public async Task Delete(Guid id) { - //return await this._service.DeleteUser(id); - throw new NotImplementedException(); + return await this._service.DeleteUser(id); } } } diff --git a/src/DevHive.Web/appsettings.json b/src/DevHive.Web/appsettings.json index 289208b..b0c8a57 100644 --- a/src/DevHive.Web/appsettings.json +++ b/src/DevHive.Web/appsettings.json @@ -4,5 +4,12 @@ }, "ConnectionStrings" : { "DEV": "Server=localhost;Port=5432;Database=API;User Id=postgres;Password=;" + }, + "Logging" : { + "LogLevel" : { + "Default" : "Information", + "Microsoft" : "Warning", + "Microsoft.Hosting.Lifetime" : "Information" + } } } -- cgit v1.2.3 From 30205791de8989f136eeafe652c9ab900855369d Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Tue, 15 Dec 2020 22:00:15 +0200 Subject: Adding LanguageController (Implemented all Language layers) --- .../Configurations/Mapping/LanguageMappings.cs | 15 +++++++ src/DevHive.Web/Controllers/LanguageController.cs | 48 ++++++++++++++++++++++ src/DevHive.Web/Controllers/RoleController.cs | 1 + .../Models/Language/CreateLanguageWebModel.cs | 7 ++++ .../Models/Language/UpdateLanguageWebModel.cs | 10 +++++ 5 files changed, 81 insertions(+) create mode 100644 src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs create mode 100644 src/DevHive.Web/Controllers/LanguageController.cs create mode 100644 src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs create mode 100644 src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs new file mode 100644 index 0000000..9a394eb --- /dev/null +++ b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs @@ -0,0 +1,15 @@ +using AutoMapper; +using DevHive.Web.Models.Language; +using DevHive.Services.Models.Language; + +namespace DevHive.Web.Configurations.Mapping +{ + public class LanguageMappings : Profile + { + public LanguageMappings() + { + CreateMap(); + CreateMap(); + } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Controllers/LanguageController.cs b/src/DevHive.Web/Controllers/LanguageController.cs new file mode 100644 index 0000000..0109cbc --- /dev/null +++ b/src/DevHive.Web/Controllers/LanguageController.cs @@ -0,0 +1,48 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Repositories; +using DevHive.Services.Models.Language; +using DevHive.Services.Services; +using DevHive.Web.Models.Language; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + public class LanguageController + { + private readonly LanguageService _languageService; + private readonly IMapper _languageMapper; + + public LanguageController(DevHiveContext context, IMapper mapper) + { + this._languageService = new LanguageService(context, mapper); + this._languageMapper = mapper; + } + + [HttpPost] + public Task Create(CreateLanguageWebModel createLanguageWebModel) + { + LanguageServiceModel languageServiceModel = this._languageMapper.Map(createLanguageWebModel); + + return this._languageService.CreateLanguage(languageServiceModel); + } + + [HttpGet] + public Task GetById(Guid id) + { + return this._languageService.GetLanguageById(id); + } + + + [HttpPut] + public Task Update(UpdateLanguageWebModel updateLanguageWebModel) + { + LanguageServiceModel languageServiceModel = this._languageMapper.Map(updateLanguageWebModel); + + return this._languageService.UpdateLanguage(languageServiceModel); + } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index fb26594..8ccc1c6 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -19,6 +19,7 @@ namespace DevHive.Web.Controllers public RoleController(DevHiveContext context, IMapper mapper) { this._service = new RoleService(context, mapper); + this._roleMapper = mapper; } [HttpPost] diff --git a/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs b/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs new file mode 100644 index 0000000..dbba4cd --- /dev/null +++ b/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Web.Models.Language +{ + public class CreateLanguageWebModel + { + public string Name { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs new file mode 100644 index 0000000..95804b0 --- /dev/null +++ b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Web.Models.Language +{ + public class UpdateLanguageWebModel + { + public Guid Id { get; set; } + public string NewName { get; set; } + } +} \ No newline at end of file -- cgit v1.2.3 From 7ff7c171bec9a832ba660c73a5aa32f5ef7870f5 Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 15 Dec 2020 22:34:59 +0200 Subject: Fixed Mapping & duplication --- src/DevHive.Web/Configurations/Mapping/UserMappings.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index 2964a00..49f0348 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -10,7 +10,7 @@ namespace DevHive.Web.Configurations.Mapping public UserMappings() { CreateMap(); - CreateMap(); + CreateMap(); CreateMap(); } } -- cgit v1.2.3 From d80b44003ca03cd09bf28278bf2e243581c00332 Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 16 Dec 2020 10:23:15 +0200 Subject: Fixed GetById to return only public info --- src/DevHive.Services/Configurations/Mapping/UserMappings.cs | 2 ++ src/DevHive.Services/Services/UserService.cs | 11 ++++------- .../Configurations/Extensions/ConfigureDatabase.cs | 6 ++++++ src/DevHive.Web/Configurations/Mapping/UserMappings.cs | 3 +++ src/DevHive.Web/Controllers/UserController.cs | 7 +++++-- src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs | 6 +----- src/DevHive.Web/Models/Identity/User/UserWebModel.cs | 11 +++++++++++ 7 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 src/DevHive.Web/Models/Identity/User/UserWebModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs index 9a35e43..ca8fa20 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs @@ -11,6 +11,8 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); CreateMap(); + + CreateMap(); } } } diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index c71209e..06f8b1b 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -11,7 +11,6 @@ using System.Security.Claims; using Microsoft.IdentityModel.Tokens; using System.Security.Cryptography; using System.Text; -using System.Collections.Immutable; namespace DevHive.Services.Services { @@ -62,14 +61,12 @@ namespace DevHive.Services.Services return new CreatedResult("CreateUser", user); } - public async Task GetUserById(Guid id) + public async Task GetUserById(Guid id) { - User user = await this._userRepository.GetByIdAsync(id); - - if (user == null) - return new NotFoundObjectResult("User does not exist!"); + User user = await this._userRepository.GetByIdAsync(id) + ?? throw new ArgumentException("User does not exist!"); - return new OkObjectResult(user); + return this._userMapper.Map(user); } public async Task UpdateUser(UpdateUserServiceModel updateModel) diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs index f308957..0fe32de 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs @@ -37,6 +37,12 @@ namespace DevHive.Web.Configurations.Extensions options.Stores.MaxLengthForKeys = 20; }); + + services.AddAuthorization(options => + { + options.AddPolicy($"{Role.DefaultRole}", + policy => policy.RequireRole($"{Role.DefaultRole}")); + }); } public static void UseDatabaseConfiguration(this IApplicationBuilder app) diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index 49f0348..06083de 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -11,7 +11,10 @@ namespace DevHive.Web.Configurations.Mapping { CreateMap(); CreateMap(); + CreateMap(); CreateMap(); + + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index f241409..74eccd4 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -1,6 +1,7 @@ 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; @@ -46,7 +47,9 @@ namespace DevHive.Web.Controllers [HttpGet] public async Task GetById(Guid id) { - return await this._userService.GetUserById(id); + UserServiceModel serviceModel = await this._userService.GetUserById(id); + + return new OkObjectResult(this._userMapper.Map(serviceModel)); } //Update @@ -62,7 +65,7 @@ namespace DevHive.Web.Controllers //Delete [HttpDelete] - [Authorize] + [Authorize(Roles = Role.DefaultRole)] public async Task Delete(Guid id) { return await this._userService.DeleteUser(id); diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index 3d96189..e04e7da 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -1,11 +1,7 @@ namespace DevHive.Web.Models.Identity.User { - public class UpdateUserWebModel + public class UpdateUserWebModel : UserWebModel { - public string UserName { get; set; } - public string Email { get; set; } - public string FirstName { get; set; } - public string LastName { get; set; } public string Password { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs new file mode 100644 index 0000000..e070d44 --- /dev/null +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -0,0 +1,11 @@ +namespace DevHive.Web.Models.Identity.User +{ + public class UserWebModel + { + public string UserName { get; set; } + public string Email { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public string Role { get; set; } + } +} -- cgit v1.2.3 From 534f3012d19b8a8a114153c11e7f8109577ac6e9 Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 16 Dec 2020 17:48:55 +0200 Subject: Fixed User Service and User Controller return types --- .../Models/Identity/User/TokenServiceModel.cs | 12 ++++++++ src/DevHive.Services/Services/UserService.cs | 36 +++++++++------------- .../Configurations/Mapping/UserMappings.cs | 2 ++ src/DevHive.Web/Controllers/UserController.cs | 26 +++++++++++----- .../Models/Identity/User/TokenWebModel.cs | 12 ++++++++ 5 files changed, 60 insertions(+), 28 deletions(-) create mode 100644 src/DevHive.Services/Models/Identity/User/TokenServiceModel.cs create mode 100644 src/DevHive.Web/Models/Identity/User/TokenWebModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Services/Models/Identity/User/TokenServiceModel.cs b/src/DevHive.Services/Models/Identity/User/TokenServiceModel.cs new file mode 100644 index 0000000..631808e --- /dev/null +++ b/src/DevHive.Services/Models/Identity/User/TokenServiceModel.cs @@ -0,0 +1,12 @@ +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 06f8b1b..1e81837 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -27,30 +27,26 @@ namespace DevHive.Services.Services this._jwtOptions = jwtOptions; } - public async Task LoginUser(LoginServiceModel loginModel) + public async Task LoginUser(LoginServiceModel loginModel) { if (!await this._userRepository.IsUsernameValid(loginModel.UserName)) - return new BadRequestObjectResult("Invalid username!"); + throw new ArgumentException("Invalid username!"); - User user = await this._userRepository - .GetByUsername(loginModel.UserName); + User user = await this._userRepository.GetByUsername(loginModel.UserName); if (user.PasswordHash != GeneratePasswordHash(loginModel.Password)) - return new BadRequestObjectResult("Incorrect password!"); + throw new ArgumentException("Incorrect password!"); - return new OkObjectResult(new - { - Token = WriteJWTSecurityToken(user.Role) - }); + return new TokenServiceModel(WriteJWTSecurityToken(user.Role)); } - public async Task RegisterUser(RegisterServiceModel registerModel) + public async Task RegisterUser(RegisterServiceModel registerModel) { if (await this._userRepository.DoesUsernameExist(registerModel.UserName)) - return new BadRequestObjectResult("Username already exists!"); + throw new ArgumentException("Username already exists!"); if (await this._userRepository.DoesEmailExist(registerModel.Email)) - return new BadRequestObjectResult("Email already exists!"); + throw new ArgumentException("Email already exists!"); User user = this._userMapper.Map(registerModel); user.Role = Role.DefaultRole; @@ -58,7 +54,7 @@ namespace DevHive.Services.Services await this._userRepository.AddAsync(user); - return new CreatedResult("CreateUser", user); + return this._userMapper.Map(user); } public async Task GetUserById(Guid id) @@ -69,30 +65,28 @@ namespace DevHive.Services.Services return this._userMapper.Map(user); } - public async Task UpdateUser(UpdateUserServiceModel updateModel) + public async Task UpdateUser(UpdateUserServiceModel updateModel) { if (!this._userRepository.DoesUserExist(updateModel.Id)) - return new NotFoundObjectResult("User does not exist!"); + throw new ArgumentException("User does not exist!"); if (!this._userRepository.DoesUserHaveThisUsername(updateModel.Id, updateModel.UserName) && await this._userRepository.IsUsernameValid(updateModel.UserName)) - return new BadRequestObjectResult("Username already exists!"); + throw new ArgumentException("Username already exists!"); User user = this._userMapper.Map(updateModel); await this._userRepository.EditAsync(user); - return new AcceptedResult("UpdateUser", user); + return this._userMapper.Map(user);; } - public async Task DeleteUser(Guid id) + public async Task DeleteUser(Guid id) { if (!this._userRepository.DoesUserExist(id)) - return new NotFoundObjectResult("User does not exist!"); + throw new ArgumentException("User does not exist!"); User user = await this._userRepository.GetByIdAsync(id); await this._userRepository.DeleteAsync(user); - - return new OkResult(); } private string GeneratePasswordHash(string password) diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index 06083de..1fdfc6a 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -15,6 +15,8 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap(); + + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 74eccd4..44828e0 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -31,7 +31,10 @@ namespace DevHive.Web.Controllers { LoginServiceModel loginServiceModel = this._userMapper.Map(loginModel); - return await this._userService.LoginUser(loginServiceModel); + TokenServiceModel tokenServiceModel = await this._userService.LoginUser(loginServiceModel); + TokenWebModel tokenWebModel = this._userMapper.Map(tokenServiceModel); + + return new OkObjectResult(tokenWebModel); } [HttpPost] @@ -40,27 +43,35 @@ namespace DevHive.Web.Controllers { RegisterServiceModel registerServiceModel = this._userMapper.Map(registerModel); - return await this._userService.RegisterUser(registerServiceModel); + UserServiceModel userServiceModel = await this._userService.RegisterUser(registerServiceModel); + UserWebModel userWebModel = this._userMapper.Map(userServiceModel); + + return new CreatedResult("Register", userWebModel); } //Read [HttpGet] public async Task GetById(Guid id) { - UserServiceModel serviceModel = await this._userService.GetUserById(id); + UserServiceModel userServiceModel = await this._userService.GetUserById(id); + UserWebModel userWebModel = this._userMapper.Map(userServiceModel); - return new OkObjectResult(this._userMapper.Map(serviceModel)); + return new OkObjectResult(userWebModel); } //Update [HttpPut] - [Authorize] + [Authorize(Roles = Role.DefaultRole)] public async Task Update(Guid id, [FromBody] UpdateUserWebModel updateModel) { UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map(updateModel); updateUserServiceModel.Id = id; - return await this._userService.UpdateUser(updateUserServiceModel); + UserServiceModel userServiceModel = await this._userService.UpdateUser(updateUserServiceModel); + UserWebModel userWebModel = this._userMapper.Map(userServiceModel); + + return new AcceptedResult("UpdateUser", userWebModel); + } //Delete @@ -68,7 +79,8 @@ namespace DevHive.Web.Controllers [Authorize(Roles = Role.DefaultRole)] public async Task Delete(Guid id) { - return await this._userService.DeleteUser(id); + await this._userService.DeleteUser(id); + return new OkResult(); } } } diff --git a/src/DevHive.Web/Models/Identity/User/TokenWebModel.cs b/src/DevHive.Web/Models/Identity/User/TokenWebModel.cs new file mode 100644 index 0000000..154b64b --- /dev/null +++ b/src/DevHive.Web/Models/Identity/User/TokenWebModel.cs @@ -0,0 +1,12 @@ +namespace DevHive.Web.Models.Identity.User +{ + public class TokenWebModel + { + public TokenWebModel(string token) + { + this.Token = token; + } + + public string Token { get; set; } + } +} \ No newline at end of file -- cgit v1.2.3 From 1d696036bb0afa88a1a88da8dc8e5cfa9d2944cd Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Wed, 16 Dec 2020 22:29:27 +0200 Subject: Refactored LanguageController and Language web models --- .../Configurations/Mapping/LanguageMappings.cs | 3 +- src/DevHive.Web/Controllers/ErrorController.cs | 1 + src/DevHive.Web/Controllers/LanguageController.cs | 44 +++++++++++++++++----- .../Models/Language/CreateLanguageWebModel.cs | 7 ---- .../Models/Language/LanguageWebModel.cs | 7 ++++ .../Models/Language/UpdateLanguageWebModel.cs | 3 +- 6 files changed, 45 insertions(+), 20 deletions(-) delete mode 100644 src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs create mode 100644 src/DevHive.Web/Models/Language/LanguageWebModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs index 9a394eb..481d576 100644 --- a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs @@ -8,8 +8,9 @@ namespace DevHive.Web.Configurations.Mapping { public LanguageMappings() { - CreateMap(); + CreateMap(); CreateMap(); + CreateMap(); } } } \ No newline at end of file diff --git a/src/DevHive.Web/Controllers/ErrorController.cs b/src/DevHive.Web/Controllers/ErrorController.cs index 189b083..19fbb21 100644 --- a/src/DevHive.Web/Controllers/ErrorController.cs +++ b/src/DevHive.Web/Controllers/ErrorController.cs @@ -1,3 +1,4 @@ +using System; using System.Net.Http; using Microsoft.AspNetCore.Mvc; diff --git a/src/DevHive.Web/Controllers/LanguageController.cs b/src/DevHive.Web/Controllers/LanguageController.cs index 0109cbc..f6cd3e6 100644 --- a/src/DevHive.Web/Controllers/LanguageController.cs +++ b/src/DevHive.Web/Controllers/LanguageController.cs @@ -23,26 +23,50 @@ namespace DevHive.Web.Controllers } [HttpPost] - public Task Create(CreateLanguageWebModel createLanguageWebModel) + public async Task Create([FromBody] LanguageWebModel languageWebModel) { - LanguageServiceModel languageServiceModel = this._languageMapper.Map(createLanguageWebModel); - - return this._languageService.CreateLanguage(languageServiceModel); + LanguageServiceModel languageServiceModel = this._languageMapper.Map(languageWebModel); + + bool result = await this._languageService.CreateLanguage(languageServiceModel); + + if(!result) + return new BadRequestObjectResult("Could not create Language"); + + return new OkResult(); } [HttpGet] - public Task GetById(Guid id) + public async Task GetById(Guid id) { - return this._languageService.GetLanguageById(id); - } + LanguageServiceModel languageServiceModel = await this._languageService.GetLanguageById(id); + LanguageWebModel languageWebModel = this._languageMapper.Map(languageServiceModel); + return new OkObjectResult(languageWebModel); + } [HttpPut] - public Task Update(UpdateLanguageWebModel updateLanguageWebModel) + public async Task Update(Guid id, [FromBody] UpdateLanguageWebModel updateModel) { - LanguageServiceModel languageServiceModel = this._languageMapper.Map(updateLanguageWebModel); + UpdateLanguageServiceModel updatelanguageServiceModel = this._languageMapper.Map(updateModel); + updatelanguageServiceModel.Id = id; + + bool result = await this._languageService.UpdateLanguage(updatelanguageServiceModel); + + if(!result) + return new BadRequestObjectResult("Could not update Language"); + + return new OkResult(); + } + + [HttpDelete] + public async Task Delete(Guid id) + { + bool result = await this._languageService.DeleteLanguage(id); + + if(!result) + return new BadRequestObjectResult("Could not delete Language"); - return this._languageService.UpdateLanguage(languageServiceModel); + return new OkResult(); } } } \ No newline at end of file diff --git a/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs b/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs deleted file mode 100644 index dbba4cd..0000000 --- a/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace DevHive.Web.Models.Language -{ - public class CreateLanguageWebModel - { - public string Name { get; set; } - } -} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Language/LanguageWebModel.cs b/src/DevHive.Web/Models/Language/LanguageWebModel.cs new file mode 100644 index 0000000..1ec38f3 --- /dev/null +++ b/src/DevHive.Web/Models/Language/LanguageWebModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Web.Models.Language +{ + public class LanguageWebModel + { + public string Name { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs index 95804b0..26eb6c2 100644 --- a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs +++ b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs @@ -2,9 +2,8 @@ using System; namespace DevHive.Web.Models.Language { - public class UpdateLanguageWebModel + public class UpdateLanguageWebModel : LanguageWebModel { public Guid Id { get; set; } - public string NewName { get; set; } } } \ No newline at end of file -- cgit v1.2.3 From cd02428e748e691a0005b2687edf69a99766aac6 Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 16 Dec 2020 22:38:25 +0200 Subject: Added DevHive.Common; Changed repositories behavior; Abstracted some common logic --- src/DevHive.Common/DevHive.Common.csproj | 12 ++++++++++++ src/DevHive.Common/Models/Data/RepositoryMethods.cs | 15 +++++++++++++++ src/DevHive.Common/Models/Identity/TokenModel.cs | 12 ++++++++++++ src/DevHive.Data/DevHive.Data.csproj | 4 ++++ src/DevHive.Data/Repositories/LanguageRepository.cs | 14 ++++---------- src/DevHive.Data/Repositories/RoleRepository.cs | 20 ++++++++++++-------- src/DevHive.Data/Repositories/UserRepository.cs | 13 +++++++------ src/DevHive.Services/DevHive.Services.csproj | 4 ++++ .../Models/Identity/User/TokenServiceModel.cs | 12 ------------ src/DevHive.Services/Services/UserService.cs | 20 +++++++++++++------- .../Configurations/Mapping/UserMappings.cs | 4 ++-- src/DevHive.Web/Controllers/UserController.cs | 10 +++++----- src/DevHive.Web/DevHive.Web.csproj | 1 + src/DevHive.code-workspace | 4 ++++ 14 files changed, 95 insertions(+), 50 deletions(-) create mode 100644 src/DevHive.Common/DevHive.Common.csproj create mode 100644 src/DevHive.Common/Models/Data/RepositoryMethods.cs create mode 100644 src/DevHive.Common/Models/Identity/TokenModel.cs delete mode 100644 src/DevHive.Services/Models/Identity/User/TokenServiceModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') 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 @@ + + + + net5.0 + + + + + + + + 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 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 @@ + + + + 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() .AddAsync(entity); - return await this.SaveChangesAsync(); + return await RepositoryMethods.SaveChangesAsync(this._context); } //Read @@ -56,7 +57,7 @@ namespace DevHive.Data.Repositories .Set() .Update(newEntity); - return await this.SaveChangesAsync(); + return await RepositoryMethods.SaveChangesAsync(this._context); } //Delete @@ -66,14 +67,7 @@ namespace DevHive.Data.Repositories .Set() .Remove(entity); - return await this.SaveChangesAsync(); + return await RepositoryMethods.SaveChangesAsync(this._context); } - - private async Task 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 AddAsync(Role entity) { await this._context .Set() .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 EditAsync(Role newEntity) { + Role role = await this.GetByIdAsync(newEntity.Id); + this._context - .Set() - .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 DeleteAsync(Role entity) { this._context .Set() .Remove(entity); - await this._context.SaveChangesAsync(); + return await RepositoryMethods.SaveChangesAsync(this._context); } public async Task 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 AddAsync(User entity) { await this._context .Set() .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 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 DeleteAsync(User entity) { this._context .Set() .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 @@ + + + + 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 LoginUser(LoginServiceModel loginModel) + public async Task 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 RegisterUser(RegisterServiceModel registerModel) + public async Task 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 GetUserById(Guid id) @@ -76,7 +76,10 @@ namespace DevHive.Services.Services throw new ArgumentException("Username already exists!"); User user = this._userMapper.Map(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(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(); - CreateMap(); + CreateMap(); } } } 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(loginModel); - TokenServiceModel tokenServiceModel = await this._userService.LoginUser(loginServiceModel); - TokenWebModel tokenWebModel = this._userMapper.Map(tokenServiceModel); + TokenModel TokenModel = await this._userService.LoginUser(loginServiceModel); + TokenWebModel tokenWebModel = this._userMapper.Map(TokenModel); return new OkObjectResult(tokenWebModel); } @@ -46,8 +46,8 @@ namespace DevHive.Web.Controllers { RegisterServiceModel registerServiceModel = this._userMapper.Map(registerModel); - TokenServiceModel tokenServiceModel = await this._userService.RegisterUser(registerServiceModel); - TokenWebModel tokenWebModel = this._userMapper.Map(tokenServiceModel); + TokenModel TokenModel = await this._userService.RegisterUser(registerServiceModel); + TokenWebModel tokenWebModel = this._userMapper.Map(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 @@ + \ 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": { -- cgit v1.2.3 From fadb0fd1b7a13d1c6210e11f2db2add7c8fd45a9 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 17 Dec 2020 11:59:52 +0200 Subject: Reworked RoleController and RoleService to be consistent with the app structure --- .../Configurations/Mapping/RoleMapings.cs | 1 + src/DevHive.Services/Services/RoleService.cs | 39 +++++++++------------- .../Configurations/Mapping/RoleMappings.cs | 2 ++ src/DevHive.Web/Controllers/RoleController.cs | 35 ++++++++++++++----- .../Models/Identity/Role/RoleWebModel.cs | 10 ++++++ .../Models/Identity/Role/UpdateRoleWebModel.cs | 6 +--- 6 files changed, 56 insertions(+), 37 deletions(-) create mode 100644 src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index 0e06523..25b314a 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -9,6 +9,7 @@ namespace DevHive.Services.Configurations.Mapping public RoleMappings() { CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index c04bf31..d5c1848 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -4,7 +4,6 @@ using AutoMapper; using DevHive.Data.Models; using DevHive.Data.Repositories; using DevHive.Services.Models.Identity.Role; -using Microsoft.AspNetCore.Mvc; namespace DevHive.Services.Services { @@ -19,51 +18,43 @@ namespace DevHive.Services.Services this._roleMapper = mapper; } - public async Task CreateRole(RoleServiceModel roleServiceModel) + public async Task CreateRole(RoleServiceModel roleServiceModel) { if (await this._roleRepository.DoesNameExist(roleServiceModel.Name)) - return new BadRequestObjectResult("Role already exists!"); + throw new ArgumentException("Role already exists!"); Role role = this._roleMapper.Map(roleServiceModel); - await this._roleRepository.AddAsync(role); - - return new CreatedResult("CreateRole", role); + return await this._roleRepository.AddAsync(role); } - public async Task GetRoleById(Guid id) + public async Task GetRoleById(Guid id) { - Role role = await this._roleRepository.GetByIdAsync(id); - - if (role == null) - return new NotFoundObjectResult("Role does not exist!"); + Role role = await this._roleRepository.GetByIdAsync(id) + ?? throw new ArgumentException("Role does not exist!"); - return new ObjectResult(role); + return this._roleMapper.Map(role); } - public async Task UpdateRole(RoleServiceModel roleServiceModel) + public async Task UpdateRole(RoleServiceModel roleServiceModel) { if (!await this._roleRepository.DoesRoleExist(roleServiceModel.Id)) - return new NotFoundObjectResult("Role does not exist!"); + throw new ArgumentException("Role does not exist!"); - if (!await this._roleRepository.DoesNameExist(roleServiceModel.Name)) - return new BadRequestObjectResult("Role name already exists!"); + if (await this._roleRepository.DoesNameExist(roleServiceModel.Name)) + throw new ArgumentException("Role name already exists!"); Role role = this._roleMapper.Map(roleServiceModel); - await this._roleRepository.EditAsync(role); - - return new AcceptedResult("UpdateRole", role); + return await this._roleRepository.EditAsync(role); } - public async Task DeleteRole(Guid id) + public async Task DeleteRole(Guid id) { if (!await this._roleRepository.DoesRoleExist(id)) - return new NotFoundObjectResult("Role does not exist!"); + throw new ArgumentException("Role does not exist!"); Role role = await this._roleRepository.GetByIdAsync(id); - await this._roleRepository.DeleteAsync(role); - - return new OkResult(); + return await this._roleRepository.DeleteAsync(role); } } } diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 9ccb7d7..2743df3 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -10,6 +10,8 @@ namespace DevHive.Web.Configurations.Mapping { CreateMap(); CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index 610d370..1664f4f 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -25,33 +25,52 @@ namespace DevHive.Web.Controllers } [HttpPost] - public Task Create(CreateRoleWebModel createRoleWebModel) + public async Task Create([FromBody] CreateRoleWebModel createRoleWebModel) { RoleServiceModel roleServiceModel = this._roleMapper.Map(createRoleWebModel); - return this._roleService.CreateRole(roleServiceModel); + bool result = await this._roleService.CreateRole(roleServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not create role!"); + + return new OkResult(); } [HttpGet] - public Task GetById(Guid id) + public async Task GetById(Guid id) { - return this._roleService.GetRoleById(id); + RoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); + RoleWebModel roleWebModel = this._roleMapper.Map(roleServiceModel); + + return new OkObjectResult(roleWebModel); } [HttpPut] - public Task Update(UpdateRoleWebModel updateRoleWebModel) + public async Task Update(Guid id, [FromBody] UpdateRoleWebModel updateRoleWebModel) { RoleServiceModel roleServiceModel = this._roleMapper.Map(updateRoleWebModel); + roleServiceModel.Id = id; + + bool result = await this._roleService.UpdateRole(roleServiceModel); - return this._roleService.UpdateRole(roleServiceModel); + if (!result) + return new BadRequestObjectResult("Could not update role!"); + + return new OkResult(); } [HttpDelete] - public Task Delete(Guid id) + public async Task Delete(Guid id) { - return this._roleService.DeleteRole(id); + bool result = await this._roleService.DeleteRole(id); + + if (!result) + return new BadRequestObjectResult("Could nor delete role!"); + + return new OkResult(); } } } diff --git a/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs new file mode 100644 index 0000000..d8ae465 --- /dev/null +++ b/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Web.Models.Identity.Role +{ + public class RoleWebModel + { + public Guid Id { get; set; } + public string Name { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs index b970dd5..213ec55 100644 --- a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs @@ -1,10 +1,6 @@ -using System; - namespace DevHive.Web.Models.Identity.Role { - public class UpdateRoleWebModel + public class UpdateRoleWebModel : CreateRoleWebModel { - public Guid Id { get; set; } - public string NewName { get; set; } } } -- cgit v1.2.3 From 94a3b0661106e91ab3a1a523af3c60df131a4f63 Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 17 Dec 2020 19:13:01 +0200 Subject: Friends implementation added. UserController/AddAFriend added --- src/DevHive.Common/Models/IdModel.cs | 9 + src/DevHive.Data/Models/User.cs | 4 +- src/DevHive.Data/Repositories/DevHiveContext.cs | 8 +- src/DevHive.Data/Repositories/UserRepository.cs | 9 + .../Models/Identity/User/UserServiceModel.cs | 7 +- src/DevHive.Services/Services/UserService.cs | 22 +- .../Configurations/Mapping/RoleMappings.cs | 1 + src/DevHive.Web/Controllers/UserController.cs | 11 + src/DevHive.Web/DevHive.Web.csproj | 22 +- .../20201217163729_Friends_Implemented.Designer.cs | 364 +++++++++++++++++++++ .../20201217163729_Friends_Implemented.cs | 45 +++ .../Migrations/DevHiveContextModelSnapshot.cs | 17 + .../Models/Identity/User/UserWebModel.cs | 6 +- 13 files changed, 505 insertions(+), 20 deletions(-) create mode 100644 src/DevHive.Common/Models/IdModel.cs create mode 100644 src/DevHive.Web/Migrations/20201217163729_Friends_Implemented.Designer.cs create mode 100644 src/DevHive.Web/Migrations/20201217163729_Friends_Implemented.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Common/Models/IdModel.cs b/src/DevHive.Common/Models/IdModel.cs new file mode 100644 index 0000000..a342e6d --- /dev/null +++ b/src/DevHive.Common/Models/IdModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Common.Models +{ + public class IdModel + { + public Guid Id { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs index fd0ee7b..eef0af2 100644 --- a/src/DevHive.Data/Models/User.cs +++ b/src/DevHive.Data/Models/User.cs @@ -14,8 +14,8 @@ namespace DevHive.Data.Models public string ProfilePicture { get; set; } - public virtual IList Roles { get; set; } + public virtual IList Roles { get; set; } = new List(); - //public List Friends { get; set; } + public IList Friends { get; set; } = new List(); } } diff --git a/src/DevHive.Data/Repositories/DevHiveContext.cs b/src/DevHive.Data/Repositories/DevHiveContext.cs index 373eb5e..7fa8130 100644 --- a/src/DevHive.Data/Repositories/DevHiveContext.cs +++ b/src/DevHive.Data/Repositories/DevHiveContext.cs @@ -18,7 +18,13 @@ namespace DevHive.Data.Repositories builder.Entity() .HasIndex(x => x.UserName) .IsUnique(); - + + builder.Entity() + .HasMany(x => x.Roles); + + builder.Entity() + .HasMany(x => x.Friends); + builder.Entity() .HasIndex(x => x.Id) .IsUnique(); diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 130f96e..577f02f 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -43,6 +43,7 @@ namespace DevHive.Data.Repositories return await this._context .Set() .Include(x => x.Roles) + .Include(x => x.Friends) .FirstOrDefaultAsync(x => x.Id == id); } @@ -115,5 +116,13 @@ namespace DevHive.Data.Repositories .AsNoTracking() .AnyAsync(u => u.Email == email); } + + public async Task AddFriend(User user, User friend) + { + this._context.Update(user); + user.Friends.Add(friend); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } } } diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index 576e502..868e7ba 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -1,7 +1,12 @@ +using System.Collections.Generic; +using DevHive.Data.Models; +using DevHive.Services.Models.Identity.Role; + namespace DevHive.Services.Models.Identity.User { public class UserServiceModel : BaseUserServiceModel { - public string Role { get; set;} + public IList Role { get; set; } = new List(); + public List Friends { get; set; } = new List(); } } diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index bd9eaa6..3e65dab 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -72,7 +72,13 @@ namespace DevHive.Services.Services User user = await this._userRepository.GetByIdAsync(id) ?? throw new ArgumentException("User does not exist!"); - return this._userMapper.Map(user); + //Here User has 1 role + + UserServiceModel model = this._userMapper.Map(user); + + //here model has 0 roles + + return model; } public async Task UpdateUser(UpdateUserServiceModel updateModel) @@ -105,6 +111,16 @@ namespace DevHive.Services.Services throw new InvalidOperationException("Unable to delete user!"); } + public async Task AddFriend(Guid userId, Guid friendId) + { + User user = await this._userRepository.GetByIdAsync(userId); + User friend = await this._userRepository.GetByIdAsync(friendId); + + return user != default(User) && friend != default(User) ? + await this._userRepository.AddFriend(user, friend) : + throw new ArgumentException("Invalid user!"); + } + private string GeneratePasswordHash(string password) { return string.Join(string.Empty, SHA512.HashData(Encoding.ASCII.GetBytes(password))); @@ -114,9 +130,9 @@ namespace DevHive.Services.Services { byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret); - List claims = new List() + List claims = new() { - new Claim(ClaimTypes.Role, roles[0].Name) // TODO: add support for mulitple roles + new Claim(ClaimTypes.Role, roles[0].Name) // TODO: add support for multiple roles }; SecurityTokenDescriptor tokenDescriptor = new() diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 2743df3..4bc25c4 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -10,6 +10,7 @@ namespace DevHive.Web.Configurations.Mapping { CreateMap(); CreateMap(); + CreateMap(); CreateMap(); } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 1360bfe..cdb8dc1 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -9,6 +9,7 @@ using DevHive.Web.Models.Identity.User; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using DevHive.Common.Models.Identity; +using DevHive.Common.Models; namespace DevHive.Web.Controllers { @@ -39,6 +40,7 @@ namespace DevHive.Web.Controllers return new OkObjectResult(tokenWebModel); } + //Create [HttpPost] [Route("Register")] [AllowAnonymous] @@ -52,6 +54,15 @@ namespace DevHive.Web.Controllers return new CreatedResult("Register", tokenWebModel); } + [HttpPost] + [Route("AddAFriend")] + public async Task AddAFriend(Guid userId, [FromBody] IdModel friendIdModel) + { + return await this._userService.AddFriend(userId, friendIdModel.Id) ? + new OkResult() : + new BadRequestResult(); + } + //Read [HttpGet] public async Task GetById(Guid id) diff --git a/src/DevHive.Web/DevHive.Web.csproj b/src/DevHive.Web/DevHive.Web.csproj index 0be5bdc..ad23ce9 100644 --- a/src/DevHive.Web/DevHive.Web.csproj +++ b/src/DevHive.Web/DevHive.Web.csproj @@ -3,23 +3,21 @@ net5.0 - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all - - - - - - - - + + + + + + - - + + \ No newline at end of file diff --git a/src/DevHive.Web/Migrations/20201217163729_Friends_Implemented.Designer.cs b/src/DevHive.Web/Migrations/20201217163729_Friends_Implemented.Designer.cs new file mode 100644 index 0000000..d066f18 --- /dev/null +++ b/src/DevHive.Web/Migrations/20201217163729_Friends_Implemented.Designer.cs @@ -0,0 +1,364 @@ +// +using System; +using DevHive.Data.Repositories; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace DevHive.Web.Migrations +{ + [DbContext(typeof(DevHiveContext))] + [Migration("20201217163729_Friends_Implemented")] + partial class Friends_Implemented + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseIdentityByDefaultColumns() + .HasAnnotation("Relational:MaxIdentifierLength", 63) + .HasAnnotation("ProductVersion", "5.0.1"); + + modelBuilder.Entity("DevHive.Data.Models.Language", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Languages"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("Id") + .IsUnique(); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Technology", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Technologies"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AccessFailedCount") + .HasColumnType("integer"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("EmailConfirmed") + .HasColumnType("boolean"); + + b.Property("FirstName") + .HasColumnType("text"); + + b.Property("LastName") + .HasColumnType("text"); + + b.Property("LockoutEnabled") + .HasColumnType("boolean"); + + b.Property("LockoutEnd") + .HasColumnType("timestamp with time zone"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("PasswordHash") + .HasColumnType("text"); + + b.Property("PhoneNumber") + .HasColumnType("text"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("boolean"); + + b.Property("ProfilePicture") + .HasColumnType("text"); + + b.Property("SecurityStamp") + .HasColumnType("text"); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.HasIndex("UserId"); + + b.HasIndex("UserName") + .IsUnique(); + + b.ToTable("AspNetUsers"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasMaxLength(20) + .HasColumnType("character varying(20)"); + + b.Property("ProviderKey") + .HasMaxLength(20) + .HasColumnType("character varying(20)"); + + b.Property("ProviderDisplayName") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("LoginProvider") + .HasMaxLength(20) + .HasColumnType("character varying(20)"); + + b.Property("Name") + .HasMaxLength(20) + .HasColumnType("character varying(20)"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.Property("RolesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("RolesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("RoleUser"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany("Friends") + .HasForeignKey("UserId"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RolesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Navigation("Friends"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/DevHive.Web/Migrations/20201217163729_Friends_Implemented.cs b/src/DevHive.Web/Migrations/20201217163729_Friends_Implemented.cs new file mode 100644 index 0000000..e20bfc1 --- /dev/null +++ b/src/DevHive.Web/Migrations/20201217163729_Friends_Implemented.cs @@ -0,0 +1,45 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace DevHive.Web.Migrations +{ + public partial class Friends_Implemented : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "UserId", + table: "AspNetUsers", + type: "uuid", + nullable: true); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUsers_UserId", + table: "AspNetUsers", + column: "UserId"); + + migrationBuilder.AddForeignKey( + name: "FK_AspNetUsers_AspNetUsers_UserId", + table: "AspNetUsers", + column: "UserId", + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_AspNetUsers_AspNetUsers_UserId", + table: "AspNetUsers"); + + migrationBuilder.DropIndex( + name: "IX_AspNetUsers_UserId", + table: "AspNetUsers"); + + migrationBuilder.DropColumn( + name: "UserId", + table: "AspNetUsers"); + } + } +} diff --git a/src/DevHive.Web/Migrations/DevHiveContextModelSnapshot.cs b/src/DevHive.Web/Migrations/DevHiveContextModelSnapshot.cs index c1464d4..328eb9c 100644 --- a/src/DevHive.Web/Migrations/DevHiveContextModelSnapshot.cs +++ b/src/DevHive.Web/Migrations/DevHiveContextModelSnapshot.cs @@ -135,6 +135,9 @@ namespace DevHive.Web.Migrations b.Property("TwoFactorEnabled") .HasColumnType("boolean"); + b.Property("UserId") + .HasColumnType("uuid"); + b.Property("UserName") .HasMaxLength(256) .HasColumnType("character varying(256)"); @@ -148,6 +151,8 @@ namespace DevHive.Web.Migrations .IsUnique() .HasDatabaseName("UserNameIndex"); + b.HasIndex("UserId"); + b.HasIndex("UserName") .IsUnique(); @@ -274,6 +279,13 @@ namespace DevHive.Web.Migrations b.ToTable("RoleUser"); }); + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany("Friends") + .HasForeignKey("UserId"); + }); + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => { b.HasOne("DevHive.Data.Models.Role", null) @@ -339,6 +351,11 @@ namespace DevHive.Web.Migrations .OnDelete(DeleteBehavior.Cascade) .IsRequired(); }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Navigation("Friends"); + }); #pragma warning restore 612, 618 } } diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 88ee1b7..e7bdb2a 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -1,7 +1,11 @@ +using System.Collections.Generic; +using DevHive.Web.Models.Identity.Role; + namespace DevHive.Web.Models.Identity.User { public class UserWebModel : BaseUserWebModel { - public string Role { get; set; } + public IList Role { get; set; } = new List(); + public IList Friends { get; set; } = new List(); } } -- cgit v1.2.3 From 875857f118d0364a94eb88bf03673e1d0165c23c Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 17 Dec 2020 19:42:45 +0200 Subject: Fixed namespaces --- .../Configurations/Mapping/RoleMapings.cs | 6 +++--- .../Models/Identity/User/UserServiceModel.cs | 2 -- src/DevHive.Services/Services/RoleService.cs | 10 +++++----- .../Configurations/Mapping/RoleMappings.cs | 10 +++++----- src/DevHive.Web/Controllers/RoleController.cs | 21 ++++++++++----------- .../Models/Identity/Role/CreateRoleWebModel.cs | 2 +- .../Models/Identity/Role/UpdateRoleWebModel.cs | 2 +- 7 files changed, 25 insertions(+), 28 deletions(-) (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index 25b314a..db4473e 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -1,6 +1,6 @@ using DevHive.Data.Models; using AutoMapper; -using DevHive.Services.Models.Identity.Role; +using DevHive.Common.Models.Identity; namespace DevHive.Services.Configurations.Mapping { @@ -8,8 +8,8 @@ namespace DevHive.Services.Configurations.Mapping { public RoleMappings() { - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index 8ac71b1..de57faf 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -1,7 +1,5 @@ using System.Collections.Generic; using DevHive.Common.Models.Identity; -using DevHive.Data.Models; -using DevHive.Services.Models.Identity.Role; namespace DevHive.Services.Models.Identity.User { diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index d5c1848..c0b9062 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -1,9 +1,9 @@ using System; using System.Threading.Tasks; using AutoMapper; +using DevHive.Common.Models.Identity; using DevHive.Data.Models; using DevHive.Data.Repositories; -using DevHive.Services.Models.Identity.Role; namespace DevHive.Services.Services { @@ -18,7 +18,7 @@ namespace DevHive.Services.Services this._roleMapper = mapper; } - public async Task CreateRole(RoleServiceModel roleServiceModel) + public async Task CreateRole(RoleModel roleServiceModel) { if (await this._roleRepository.DoesNameExist(roleServiceModel.Name)) throw new ArgumentException("Role already exists!"); @@ -28,15 +28,15 @@ namespace DevHive.Services.Services return await this._roleRepository.AddAsync(role); } - public async Task GetRoleById(Guid id) + public async Task GetRoleById(Guid id) { Role role = await this._roleRepository.GetByIdAsync(id) ?? throw new ArgumentException("Role does not exist!"); - return this._roleMapper.Map(role); + return this._roleMapper.Map(role); } - public async Task UpdateRole(RoleServiceModel roleServiceModel) + public async Task UpdateRole(RoleModel roleServiceModel) { if (!await this._roleRepository.DoesRoleExist(roleServiceModel.Id)) throw new ArgumentException("Role does not exist!"); diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 4bc25c4..8c5cd85 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -1,6 +1,6 @@ using AutoMapper; using DevHive.Web.Models.Identity.Role; -using DevHive.Services.Models.Identity.Role; +using DevHive.Common.Models.Identity; namespace DevHive.Web.Configurations.Mapping { @@ -8,11 +8,11 @@ namespace DevHive.Web.Configurations.Mapping { public RoleMappings() { - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index 1664f4f..fc05481 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -4,13 +4,12 @@ using DevHive.Services.Services; using Microsoft.AspNetCore.Mvc; using DevHive.Web.Models.Identity.Role; using AutoMapper; -using DevHive.Services.Models.Identity.Role; using System; -using Microsoft.AspNetCore.Authorization; +using DevHive.Common.Models.Identity; namespace DevHive.Web.Controllers { - [ApiController] + [ApiController] [Route("/api/[controller]")] //[Authorize(Roles = "Admin")] public class RoleController @@ -25,10 +24,10 @@ namespace DevHive.Web.Controllers } [HttpPost] - public async Task Create([FromBody] CreateRoleWebModel createRoleWebModel) + public async Task Create([FromBody] CreateRoleModel createRoleModel) { - RoleServiceModel roleServiceModel = - this._roleMapper.Map(createRoleWebModel); + RoleModel roleServiceModel = + this._roleMapper.Map(createRoleModel); bool result = await this._roleService.CreateRole(roleServiceModel); @@ -41,17 +40,17 @@ namespace DevHive.Web.Controllers [HttpGet] public async Task GetById(Guid id) { - RoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); - RoleWebModel roleWebModel = this._roleMapper.Map(roleServiceModel); + RoleModel roleServiceModel = await this._roleService.GetRoleById(id); + RoleModel roleWebModel = this._roleMapper.Map(roleServiceModel); return new OkObjectResult(roleWebModel); } [HttpPut] - public async Task Update(Guid id, [FromBody] UpdateRoleWebModel updateRoleWebModel) + public async Task Update(Guid id, [FromBody] UpdateRoleModel updateRoleModel) { - RoleServiceModel roleServiceModel = - this._roleMapper.Map(updateRoleWebModel); + RoleModel roleServiceModel = + this._roleMapper.Map(updateRoleModel); roleServiceModel.Id = id; bool result = await this._roleService.UpdateRole(roleServiceModel); diff --git a/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs index e872428..becb3c9 100644 --- a/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs @@ -1,6 +1,6 @@ namespace DevHive.Web.Models.Identity.Role { - public class CreateRoleWebModel + public class CreateRoleModel { public string Name { get; set; } } diff --git a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs index 213ec55..1eaad57 100644 --- a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs @@ -1,6 +1,6 @@ namespace DevHive.Web.Models.Identity.Role { - public class UpdateRoleWebModel : CreateRoleWebModel + public class UpdateRoleModel : CreateRoleModel { } } -- cgit v1.2.3 From 8cc02d6195784ca2783b7ae8daa1cf9fd20902a9 Mon Sep 17 00:00:00 2001 From: transtrike Date: Fri, 18 Dec 2020 08:04:47 +0200 Subject: Fixed Id showing in web model --- src/DevHive.Web/Configurations/Mapping/RoleMappings.cs | 4 ++-- src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs | 10 ++++++++++ src/DevHive.Web/Models/Identity/User/UserWebModel.cs | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 8c5cd85..5d33c56 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -11,8 +11,8 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap(); - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs new file mode 100644 index 0000000..7a3ed96 --- /dev/null +++ b/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Web.Models.Identity.Role +{ + public class RoleWebModel + { + public string Name { get; set; } + } +} +2 \ No newline at end of file diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 4b523b5..37d6553 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -6,7 +6,7 @@ namespace DevHive.Web.Models.Identity.User { public class UserWebModel : BaseUserWebModel { - public IList Roles { get; set; } = new List(); + public IList Roles { get; set; } = new List(); public IList Friends { get; set; } = new List(); } } -- cgit v1.2.3 From 7acec816fe021db4a3922b70ce9b3f50e7600c79 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Fri, 18 Dec 2020 09:42:13 +0200 Subject: Adding Technology Web layer --- src/DevHive.Services/Services/TechnologyService.cs | 6 +- .../Configurations/Mapping/TechnolofyMappings.cs | 16 +++++ .../Controllers/TechnologyController.cs | 72 ++++++++++++++++++++++ .../Models/Technology/TechnologyWebModel.cs | 7 +++ .../Models/Technology/UpdateTechnologyWebModel.cs | 9 +++ 5 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 src/DevHive.Web/Configurations/Mapping/TechnolofyMappings.cs create mode 100644 src/DevHive.Web/Controllers/TechnologyController.cs create mode 100644 src/DevHive.Web/Models/Technology/TechnologyWebModel.cs create mode 100644 src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Services/Services/TechnologyService.cs b/src/DevHive.Services/Services/TechnologyService.cs index 5617e9b..246ad2c 100644 --- a/src/DevHive.Services/Services/TechnologyService.cs +++ b/src/DevHive.Services/Services/TechnologyService.cs @@ -53,13 +53,13 @@ namespace DevHive.Services.Services return result; } - public async Task DeleteLanguage(Guid id) + public async Task DeleteTechnology(Guid id) { if (!await this._technologyRepository.DoesTechnologyExist(id)) throw new ArgumentException("Technology does not exist!"); - Technology language = await this._technologyRepository.GetByIdAsync(id); - bool result = await this._technologyRepository.DeleteAsync(language); + Technology technology = await this._technologyRepository.GetByIdAsync(id); + bool result = await this._technologyRepository.DeleteAsync(technology); return result; } diff --git a/src/DevHive.Web/Configurations/Mapping/TechnolofyMappings.cs b/src/DevHive.Web/Configurations/Mapping/TechnolofyMappings.cs new file mode 100644 index 0000000..25ce5ca --- /dev/null +++ b/src/DevHive.Web/Configurations/Mapping/TechnolofyMappings.cs @@ -0,0 +1,16 @@ +using AutoMapper; +using DevHive.Web.Models.Technology; +using DevHive.Services.Models.Technology; + +namespace DevHive.Web.Configurations.Mapping +{ + public class TechnologyMappings : Profile + { + public TechnologyMappings() + { + CreateMap(); + CreateMap(); + CreateMap(); + } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Controllers/TechnologyController.cs b/src/DevHive.Web/Controllers/TechnologyController.cs new file mode 100644 index 0000000..9e3cf3c --- /dev/null +++ b/src/DevHive.Web/Controllers/TechnologyController.cs @@ -0,0 +1,72 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Repositories; +using DevHive.Services.Models.Technology; +using DevHive.Services.Services; +using DevHive.Web.Models.Technology; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + public class TechnologyController + { + private readonly TechnologyService _technologyService; + private readonly IMapper _technologyMapper; + + public TechnologyController(DevHiveContext context, IMapper mapper) + { + this._technologyService = new TechnologyService(context, mapper); + this._technologyMapper = mapper; + } + + [HttpPost] + public async Task Create([FromBody] TechnologyWebModel technologyWebModel) + { + TechnologyServiceModel technologyServiceModel = this._technologyMapper.Map(technologyWebModel); + + bool result = await this._technologyService.Create(technologyServiceModel); + + if(!result) + return new BadRequestObjectResult("Could not create the Technology"); + + return new OkResult(); + } + + [HttpGet] + public async Task GetById(Guid id) + { + TechnologyServiceModel technologyServiceModel = await this._technologyService.GetTechnologyById(id); + TechnologyWebModel technologyWebModel = this._technologyMapper.Map(technologyServiceModel); + + return new OkObjectResult(technologyWebModel); + } + + [HttpPut] + public async Task Update(Guid id, [FromBody] UpdateTechnologyWebModel updateModel) + { + UpdateTechnologyServiceModel updateTechnologyWebModel = this._technologyMapper.Map(updateModel); + updateTechnologyWebModel.Id = id; + + bool result = await this._technologyService.UpdateTechnology(updateTechnologyWebModel); + + if(!result) + return new BadRequestObjectResult("Could not update Technology"); + + return new OkResult(); + } + + [HttpDelete] + public async Task Delete(Guid id) + { + bool result = await this._technologyService.DeleteTechnology(id); + + if(!result) + return new BadRequestObjectResult("Could not delete Technology"); + + return new OkResult(); + } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Technology/TechnologyWebModel.cs b/src/DevHive.Web/Models/Technology/TechnologyWebModel.cs new file mode 100644 index 0000000..cb6b998 --- /dev/null +++ b/src/DevHive.Web/Models/Technology/TechnologyWebModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Web.Models.Technology +{ + public class TechnologyWebModel + { + public string Name { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs b/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs new file mode 100644 index 0000000..b94496e --- /dev/null +++ b/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Web.Models.Technology +{ + public class UpdateTechnologyWebModel : TechnologyWebModel + { + public Guid Id { get; set; } + } +} \ No newline at end of file -- cgit v1.2.3 From 1b44e6fd8f83177666fce396dd4ae8c8f53fe09e Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Fri, 18 Dec 2020 10:13:18 +0200 Subject: Fixed Technology and Language validations --- .../Configurations/Mapping/LanguageMappings.cs | 1 + .../Configurations/Mapping/TechnologyMappings.cs | 1 + src/DevHive.Services/Services/LanguageService.cs | 4 ++-- src/DevHive.Services/Services/TechnologyService.cs | 4 ++-- .../Configurations/Mapping/LanguageMappings.cs | 1 + .../Configurations/Mapping/TechnolofyMappings.cs | 16 ---------------- .../Configurations/Mapping/TechnologyMappings.cs | 17 +++++++++++++++++ 7 files changed, 24 insertions(+), 20 deletions(-) delete mode 100644 src/DevHive.Web/Configurations/Mapping/TechnolofyMappings.cs create mode 100644 src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs index 0be9ca2..34b4996 100644 --- a/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs @@ -10,6 +10,7 @@ namespace DevHive.Services.Configurations.Mapping { CreateMap(); CreateMap(); + CreateMap(); } } } \ No newline at end of file diff --git a/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs index 6be70d6..f7e6d3e 100644 --- a/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs @@ -10,6 +10,7 @@ namespace DevHive.Services.Configurations.Mapping { CreateMap(); CreateMap(); + CreateMap(); } } } \ No newline at end of file diff --git a/src/DevHive.Services/Services/LanguageService.cs b/src/DevHive.Services/Services/LanguageService.cs index 0454aaf..017bcbf 100644 --- a/src/DevHive.Services/Services/LanguageService.cs +++ b/src/DevHive.Services/Services/LanguageService.cs @@ -20,7 +20,7 @@ namespace DevHive.Services.Services public async Task CreateLanguage(LanguageServiceModel languageServiceModel) { - if (!await this._languageRepository.DoesLanguageNameExist(languageServiceModel.Name)) + if (await this._languageRepository.DoesLanguageNameExist(languageServiceModel.Name)) throw new ArgumentException("Language already exists!"); Language language = this._languageMapper.Map(languageServiceModel); @@ -44,7 +44,7 @@ namespace DevHive.Services.Services if (!await this._languageRepository.DoesLanguageExist(languageServiceModel.Id)) throw new ArgumentException("Language does not exist!"); - if (!await this._languageRepository.DoesLanguageNameExist(languageServiceModel.Name)) + if (await this._languageRepository.DoesLanguageNameExist(languageServiceModel.Name)) throw new ArgumentException("Language name already exists!"); Language language = this._languageMapper.Map(languageServiceModel); diff --git a/src/DevHive.Services/Services/TechnologyService.cs b/src/DevHive.Services/Services/TechnologyService.cs index 246ad2c..6715606 100644 --- a/src/DevHive.Services/Services/TechnologyService.cs +++ b/src/DevHive.Services/Services/TechnologyService.cs @@ -20,7 +20,7 @@ namespace DevHive.Services.Services public async Task Create(TechnologyServiceModel technologyServiceModel) { - if (!await this._technologyRepository.DoesTechnologyNameExist(technologyServiceModel.Name)) + if (await this._technologyRepository.DoesTechnologyNameExist(technologyServiceModel.Name)) throw new ArgumentException("Technology already exists!"); Technology technology = this._technologyMapper.Map(technologyServiceModel); @@ -44,7 +44,7 @@ namespace DevHive.Services.Services if (!await this._technologyRepository.DoesTechnologyExist(updateTechnologyServiceModel.Id)) throw new ArgumentException("Technology does not exist!"); - if (!await this._technologyRepository.DoesTechnologyNameExist(updateTechnologyServiceModel.Name)) + if (await this._technologyRepository.DoesTechnologyNameExist(updateTechnologyServiceModel.Name)) throw new ArgumentException("Technology name already exists!"); Technology technology = this._technologyMapper.Map(updateTechnologyServiceModel); diff --git a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs index 481d576..5715b13 100644 --- a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs @@ -11,6 +11,7 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap(); CreateMap(); + CreateMap(); } } } \ No newline at end of file diff --git a/src/DevHive.Web/Configurations/Mapping/TechnolofyMappings.cs b/src/DevHive.Web/Configurations/Mapping/TechnolofyMappings.cs deleted file mode 100644 index 25ce5ca..0000000 --- a/src/DevHive.Web/Configurations/Mapping/TechnolofyMappings.cs +++ /dev/null @@ -1,16 +0,0 @@ -using AutoMapper; -using DevHive.Web.Models.Technology; -using DevHive.Services.Models.Technology; - -namespace DevHive.Web.Configurations.Mapping -{ - public class TechnologyMappings : Profile - { - public TechnologyMappings() - { - CreateMap(); - CreateMap(); - CreateMap(); - } - } -} \ No newline at end of file diff --git a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs new file mode 100644 index 0000000..723a4cb --- /dev/null +++ b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs @@ -0,0 +1,17 @@ +using AutoMapper; +using DevHive.Web.Models.Technology; +using DevHive.Services.Models.Technology; + +namespace DevHive.Web.Configurations.Mapping +{ + public class TechnologyMappings : Profile + { + public TechnologyMappings() + { + CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); + } + } +} \ No newline at end of file -- cgit v1.2.3 From 13cdc46cbe5ebe1aa607f90e554de5f222adce8d Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Fri, 18 Dec 2020 13:17:19 +0200 Subject: Fixed a bug where updating a Technology would create a new instence instead of updating the old one --- src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs | 2 +- src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs index f7e6d3e..5446f3e 100644 --- a/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs @@ -10,7 +10,7 @@ namespace DevHive.Services.Configurations.Mapping { CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); } } } \ No newline at end of file diff --git a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs index 723a4cb..849e47f 100644 --- a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs @@ -9,9 +9,9 @@ namespace DevHive.Web.Configurations.Mapping public TechnologyMappings() { CreateMap(); - CreateMap(); + CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); } } } \ No newline at end of file -- cgit v1.2.3 From fb56c49681746c8c47c1da43c918b37df5f214a1 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Wed, 30 Dec 2020 17:48:30 +0200 Subject: Adding coomment layers --- src/DevHive.Data/ConnectionString.json | 2 +- src/DevHive.Data/DevHiveContext.cs | 1 + .../20201230154737_CommentMigration.Designer.cs | 377 +++++++++++++++++++++ .../Migrations/20201230154737_CommentMigration.cs | 31 ++ .../Migrations/DevHiveContextModelSnapshot.cs | 20 ++ src/DevHive.Data/Models/Comment.cs | 11 + src/DevHive.Data/Repositories/CommentRepository.cs | 62 ++++ .../Configurations/Mapping/CommentMappings.cs | 17 + .../Models/Comment/CommentServiceModel.cs | 10 + .../Models/Comment/GetByIdCommentServiceModel.cs | 9 + .../Models/Comment/UpdateCommnetServiceModel.cs | 9 + src/DevHive.Services/Services/CommentService.cs | 62 ++++ .../Configurations/Mapping/CommentMappings.cs | 18 + src/DevHive.Web/Controllers/CommentController.cs | 72 ++++ src/DevHive.Web/Models/Comment/CommentWebModel.cs | 10 + .../Models/Comment/GetByIdCommentWebModel.cs | 9 + 16 files changed, 719 insertions(+), 1 deletion(-) create mode 100644 src/DevHive.Data/Migrations/20201230154737_CommentMigration.Designer.cs create mode 100644 src/DevHive.Data/Migrations/20201230154737_CommentMigration.cs create mode 100644 src/DevHive.Data/Models/Comment.cs create mode 100644 src/DevHive.Data/Repositories/CommentRepository.cs create mode 100644 src/DevHive.Services/Configurations/Mapping/CommentMappings.cs create mode 100644 src/DevHive.Services/Models/Comment/CommentServiceModel.cs create mode 100644 src/DevHive.Services/Models/Comment/GetByIdCommentServiceModel.cs create mode 100644 src/DevHive.Services/Models/Comment/UpdateCommnetServiceModel.cs create mode 100644 src/DevHive.Services/Services/CommentService.cs create mode 100644 src/DevHive.Web/Configurations/Mapping/CommentMappings.cs create mode 100644 src/DevHive.Web/Controllers/CommentController.cs create mode 100644 src/DevHive.Web/Models/Comment/CommentWebModel.cs create mode 100644 src/DevHive.Web/Models/Comment/GetByIdCommentWebModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Data/ConnectionString.json b/src/DevHive.Data/ConnectionString.json index 00e95c2..c8300b2 100644 --- a/src/DevHive.Data/ConnectionString.json +++ b/src/DevHive.Data/ConnectionString.json @@ -1,5 +1,5 @@ { "ConnectionStrings": { - "DEV": "Server=localhost;Port=5432;Database=API;User Id=postgres;Password=;" + "DEV": "Server=localhost;Port=5432;Database=API;User Id=postgres;Password=password;" } } \ No newline at end of file diff --git a/src/DevHive.Data/DevHiveContext.cs b/src/DevHive.Data/DevHiveContext.cs index 39d39d3..c9959dc 100644 --- a/src/DevHive.Data/DevHiveContext.cs +++ b/src/DevHive.Data/DevHiveContext.cs @@ -12,6 +12,7 @@ namespace DevHive.Data public DbSet Technologies { get; set; } public DbSet Languages { get; set; } + public DbSet Comments { get; set; } protected override void OnModelCreating(ModelBuilder builder) { diff --git a/src/DevHive.Data/Migrations/20201230154737_CommentMigration.Designer.cs b/src/DevHive.Data/Migrations/20201230154737_CommentMigration.Designer.cs new file mode 100644 index 0000000..0df3199 --- /dev/null +++ b/src/DevHive.Data/Migrations/20201230154737_CommentMigration.Designer.cs @@ -0,0 +1,377 @@ +// +using System; +using DevHive.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace DevHive.Data.Migrations +{ + [DbContext(typeof(DevHiveContext))] + [Migration("20201230154737_CommentMigration")] + partial class CommentMigration + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseIdentityByDefaultColumns() + .HasAnnotation("Relational:MaxIdentifierLength", 63) + .HasAnnotation("ProductVersion", "5.0.1"); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Date") + .HasColumnType("timestamp without time zone"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.ToTable("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Language", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Languages"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Technology", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Technologies"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AccessFailedCount") + .HasColumnType("integer"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("EmailConfirmed") + .HasColumnType("boolean"); + + b.Property("FirstName") + .HasColumnType("text"); + + b.Property("LastName") + .HasColumnType("text"); + + b.Property("LockoutEnabled") + .HasColumnType("boolean"); + + b.Property("LockoutEnd") + .HasColumnType("timestamp with time zone"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("PasswordHash") + .HasColumnType("text"); + + b.Property("PhoneNumber") + .HasColumnType("text"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("boolean"); + + b.Property("ProfilePicture") + .HasColumnType("text"); + + b.Property("SecurityStamp") + .HasColumnType("text"); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.HasIndex("UserId"); + + b.HasIndex("UserName") + .IsUnique(); + + b.ToTable("AspNetUsers"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("ProviderKey") + .HasColumnType("text"); + + b.Property("ProviderDisplayName") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.Property("RolesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("RolesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("RoleUser"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany("Friends") + .HasForeignKey("UserId"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RolesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Navigation("Friends"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/DevHive.Data/Migrations/20201230154737_CommentMigration.cs b/src/DevHive.Data/Migrations/20201230154737_CommentMigration.cs new file mode 100644 index 0000000..b952be5 --- /dev/null +++ b/src/DevHive.Data/Migrations/20201230154737_CommentMigration.cs @@ -0,0 +1,31 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace DevHive.Data.Migrations +{ + public partial class CommentMigration : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Comments", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + UserId = table.Column(type: "uuid", nullable: false), + Message = table.Column(type: "text", nullable: true), + Date = table.Column(type: "timestamp without time zone", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Comments", x => x.Id); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Comments"); + } + } +} diff --git a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs index b5300a9..c7ff6c6 100644 --- a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs +++ b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs @@ -19,6 +19,26 @@ namespace DevHive.Data.Migrations .HasAnnotation("Relational:MaxIdentifierLength", 63) .HasAnnotation("ProductVersion", "5.0.1"); + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Date") + .HasColumnType("timestamp without time zone"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.ToTable("Comments"); + }); + modelBuilder.Entity("DevHive.Data.Models.Language", b => { b.Property("Id") diff --git a/src/DevHive.Data/Models/Comment.cs b/src/DevHive.Data/Models/Comment.cs new file mode 100644 index 0000000..f949a42 --- /dev/null +++ b/src/DevHive.Data/Models/Comment.cs @@ -0,0 +1,11 @@ +using System; +namespace DevHive.Data.Models +{ + public class Comment : IModel + { + public Guid Id { get; set; } + public Guid UserId { get; set; } + public string Message { get; set; } + public DateTime Date { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/CommentRepository.cs b/src/DevHive.Data/Repositories/CommentRepository.cs new file mode 100644 index 0000000..5a4ef17 --- /dev/null +++ b/src/DevHive.Data/Repositories/CommentRepository.cs @@ -0,0 +1,62 @@ +using System; +using System.Threading.Tasks; +using Data.Models.Interfaces.Database; +using DevHive.Common.Models.Data; +using DevHive.Data.Models; +using Microsoft.EntityFrameworkCore; + +namespace DevHive.Data.Repositories +{ + public class CommentRepository : IRepository + { + private readonly DevHiveContext _context; + + public CommentRepository(DevHiveContext context) + { + this._context = context; + } + + public async Task AddAsync(Comment entity) + { + await this._context + .Set() + .AddAsync(entity); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task GetByIdAsync(Guid id) + { + return await this._context + .Set() + .FindAsync(id); + } + + + public async Task EditAsync(Comment newEntity) + { + this._context + .Set() + .Update(newEntity); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task DoesCommentExist(Guid id) + { + return await this._context + .Set() + .AsNoTracking() + .AnyAsync(r => r.Id == id); + } + + public async Task DeleteAsync(Comment entity) + { + this._context + .Set() + .Remove(entity); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs b/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs new file mode 100644 index 0000000..8120488 --- /dev/null +++ b/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs @@ -0,0 +1,17 @@ +using DevHive.Data.Models; +using AutoMapper; +using DevHive.Services.Models.Comment; + +namespace DevHive.Services.Configurations.Mapping +{ + public class CommentMappings : Profile + { + public CommentMappings() + { + CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); + } + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Comment/CommentServiceModel.cs b/src/DevHive.Services/Models/Comment/CommentServiceModel.cs new file mode 100644 index 0000000..f3638ac --- /dev/null +++ b/src/DevHive.Services/Models/Comment/CommentServiceModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Services.Models.Comment +{ + public class CommentServiceModel + { + public Guid UserId { get; set; } + public string Message { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Comment/GetByIdCommentServiceModel.cs b/src/DevHive.Services/Models/Comment/GetByIdCommentServiceModel.cs new file mode 100644 index 0000000..c2b84b3 --- /dev/null +++ b/src/DevHive.Services/Models/Comment/GetByIdCommentServiceModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Services.Models.Comment +{ + public class GetByIdCommentServiceModel : CommentServiceModel + { + public DateTime Date { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Comment/UpdateCommnetServiceModel.cs b/src/DevHive.Services/Models/Comment/UpdateCommnetServiceModel.cs new file mode 100644 index 0000000..3a461c5 --- /dev/null +++ b/src/DevHive.Services/Models/Comment/UpdateCommnetServiceModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Services.Models.Comment +{ + public class UpdateCommentServiceModel : CommentServiceModel + { + public Guid Id { get; set; } + } +} diff --git a/src/DevHive.Services/Services/CommentService.cs b/src/DevHive.Services/Services/CommentService.cs new file mode 100644 index 0000000..69dbcc0 --- /dev/null +++ b/src/DevHive.Services/Services/CommentService.cs @@ -0,0 +1,62 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Models; +using DevHive.Data.Repositories; +using DevHive.Services.Models.Comment; + +namespace DevHive.Services.Services +{ + public class CommentService + { + private readonly CommentRepository _commentRepository; + private readonly IMapper _commentMapper; + + public CommentService(CommentRepository commentRepository, IMapper mapper) + { + this._commentRepository = commentRepository; + this._commentMapper = mapper; + } + + public async Task CreateComment(CommentServiceModel commentServiceModel) + { + Comment comment = this._commentMapper.Map(commentServiceModel); + comment.Date = DateTime.Now; + bool result = await this._commentRepository.AddAsync(comment); + + return result; + } + + public async Task GetCommentById(Guid id) + { + Comment comment = await this._commentRepository.GetByIdAsync(id); + + if(comment == null) + throw new ArgumentException("The comment does not exist"); + + return this._commentMapper.Map(comment); + } + + public async Task UpdateComment(UpdateCommentServiceModel commentServiceModel) + { + if (!await this._commentRepository.DoesCommentExist(commentServiceModel.Id)) + throw new ArgumentException("Comment does not exist!"); + + Comment comment = this._commentMapper.Map(commentServiceModel); + bool result = await this._commentRepository.EditAsync(comment); + + return result; + } + + public async Task DeleteComment(Guid id) + { + if (!await this._commentRepository.DoesCommentExist(id)) + throw new ArgumentException("Comment does not exist!"); + + Comment comment = await this._commentRepository.GetByIdAsync(id); + bool result = await this._commentRepository.DeleteAsync(comment); + + return result; + } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs new file mode 100644 index 0000000..dd11420 --- /dev/null +++ b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs @@ -0,0 +1,18 @@ +using AutoMapper; +using DevHive.Web.Models.Comment; +using DevHive.Services.Models.Comment; + +namespace DevHive.Web.Configurations.Mapping +{ + public class CommentMappings : Profile + { + public CommentMappings() + { + CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); + } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Controllers/CommentController.cs b/src/DevHive.Web/Controllers/CommentController.cs new file mode 100644 index 0000000..5b6b0ee --- /dev/null +++ b/src/DevHive.Web/Controllers/CommentController.cs @@ -0,0 +1,72 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Repositories; +using DevHive.Services.Models.Comment; +using DevHive.Services.Services; +using DevHive.Web.Models.Comment; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + public class CommentController + { + private readonly CommentService _commentService; + private readonly IMapper _commentMapper; + + public CommentController(CommentService commentService, IMapper mapper) + { + this._commentService = commentService; + this._commentMapper = mapper; + } + + [HttpPost] + public async Task Create([FromBody] CommentWebModel commentWebModel) + { + CommentServiceModel commentServiceModel = this._commentMapper.Map(commentWebModel); + + bool result = await this._commentService.CreateComment(commentServiceModel); + + if(!result) + return new BadRequestObjectResult("Could not create the Comment"); + + return new OkResult(); + } + + [HttpGet] + public async Task GetById(Guid id) + { + GetByIdCommentServiceModel getByIdCommentServiceModel = await this._commentService.GetCommentById(id); + GetByIdCommentWebModel getByIdCommentWebModel = this._commentMapper.Map(getByIdCommentServiceModel); + + return new OkObjectResult(getByIdCommentWebModel); + } + + [HttpPut] + public async Task Update(Guid id, [FromBody] CommentWebModel commentWebModel) + { + UpdateCommentServiceModel updateCommentServiceModel = this._commentMapper.Map(commentWebModel); + updateCommentServiceModel.Id = id; + + bool result = await this._commentService.UpdateComment(updateCommentServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not update Comment"); + + return new OkResult(); + } + + [HttpDelete] + public async Task Delete(Guid id) + { + bool result = await this._commentService.DeleteComment(id); + + if (!result) + return new BadRequestObjectResult("Could not delete Comment"); + + return new OkResult(); + } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Comment/CommentWebModel.cs b/src/DevHive.Web/Models/Comment/CommentWebModel.cs new file mode 100644 index 0000000..37806a5 --- /dev/null +++ b/src/DevHive.Web/Models/Comment/CommentWebModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Web.Models.Comment +{ + public class CommentWebModel + { + public Guid UserId { get; set; } + public string Message { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Comment/GetByIdCommentWebModel.cs b/src/DevHive.Web/Models/Comment/GetByIdCommentWebModel.cs new file mode 100644 index 0000000..3d03348 --- /dev/null +++ b/src/DevHive.Web/Models/Comment/GetByIdCommentWebModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Web.Models.Comment +{ + public class GetByIdCommentWebModel : CommentWebModel + { + public DateTime Date { get; set; } + } +} \ No newline at end of file -- cgit v1.2.3 From 278130d86378a6b2db6ba443631f303fb7d7e207 Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 30 Dec 2020 21:21:49 +0200 Subject: Implemented Posts and merged Comment to Post --- src/DevHive.Common/Models/Data/IdModel.cs | 9 -- .../Models/Data/RepositoryMethods.cs | 2 +- src/DevHive.Common/Models/Misc/IdModel.cs | 9 ++ src/DevHive.Data/Models/Comment.cs | 4 +- src/DevHive.Data/Models/Post.cs | 21 ++++ src/DevHive.Data/Repositories/CommentRepository.cs | 62 ---------- .../Repositories/LanguageRepository.cs | 2 +- src/DevHive.Data/Repositories/PostRepository.cs | 107 +++++++++++++++++ src/DevHive.Data/Repositories/RoleRepository.cs | 2 +- .../Repositories/TechnologyRepository.cs | 2 +- src/DevHive.Data/Repositories/UserRepository.cs | 2 +- .../Configurations/Mapping/CommentMappings.cs | 7 +- .../Configurations/Mapping/PostMappings.cs | 16 +++ .../Models/Comment/CommentServiceModel.cs | 10 -- .../Models/Comment/GetByIdCommentServiceModel.cs | 9 -- .../Models/Comment/UpdateCommnetServiceModel.cs | 9 -- .../Models/Post/Comment/BaseCommentServiceModel.cs | 11 ++ .../Models/Post/Comment/CommentServiceModel.cs | 9 ++ .../Post/Comment/CreateCommentServiceModel.cs | 9 ++ .../Post/Comment/UpdateCommnetServiceModel.cs | 8 ++ .../Models/Post/Post/BasePostServiceModel.cs | 11 ++ .../Models/Post/Post/CreatePostServiceModel.cs | 9 ++ .../Models/Post/Post/PostServiceModel.cs | 12 ++ .../Models/Post/Post/UpdatePostServiceModel.cs | 7 ++ src/DevHive.Services/Services/CommentService.cs | 62 ---------- src/DevHive.Services/Services/PostService.cs | 98 +++++++++++++++ .../Extensions/ConfigureDependencyInjection.cs | 4 +- .../Configurations/Mapping/CommentMappings.cs | 7 +- src/DevHive.Web/Controllers/CommentController.cs | 72 ----------- src/DevHive.Web/Controllers/PostController.cs | 132 +++++++++++++++++++++ src/DevHive.Web/Controllers/RoleController.cs | 2 +- src/DevHive.Web/Controllers/UserController.cs | 2 +- src/DevHive.Web/Models/Comment/CommentWebModel.cs | 10 -- .../Models/Comment/GetByIdCommentWebModel.cs | 9 -- .../Models/Post/Comment/CommentWebModel.cs | 10 ++ .../Models/Post/Post/BasePostWebModel.cs | 10 ++ .../Models/Post/Post/CreatePostWebModel.cs | 8 ++ src/DevHive.Web/Models/Post/Post/PostWebModel.cs | 21 ++++ .../Models/Post/Post/UpdatePostModel.cs | 7 ++ src/DevHive.Web/Startup.cs | 4 - 40 files changed, 533 insertions(+), 274 deletions(-) delete mode 100644 src/DevHive.Common/Models/Data/IdModel.cs create mode 100644 src/DevHive.Common/Models/Misc/IdModel.cs create mode 100644 src/DevHive.Data/Models/Post.cs delete mode 100644 src/DevHive.Data/Repositories/CommentRepository.cs create mode 100644 src/DevHive.Data/Repositories/PostRepository.cs create mode 100644 src/DevHive.Services/Configurations/Mapping/PostMappings.cs delete mode 100644 src/DevHive.Services/Models/Comment/CommentServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Comment/GetByIdCommentServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Comment/UpdateCommnetServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/Comment/CommentServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/Post/BasePostServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/Post/PostServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs delete mode 100644 src/DevHive.Services/Services/CommentService.cs create mode 100644 src/DevHive.Services/Services/PostService.cs delete mode 100644 src/DevHive.Web/Controllers/CommentController.cs create mode 100644 src/DevHive.Web/Controllers/PostController.cs delete mode 100644 src/DevHive.Web/Models/Comment/CommentWebModel.cs delete mode 100644 src/DevHive.Web/Models/Comment/GetByIdCommentWebModel.cs create mode 100644 src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs create mode 100644 src/DevHive.Web/Models/Post/Post/BasePostWebModel.cs create mode 100644 src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs create mode 100644 src/DevHive.Web/Models/Post/Post/PostWebModel.cs create mode 100644 src/DevHive.Web/Models/Post/Post/UpdatePostModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Common/Models/Data/IdModel.cs b/src/DevHive.Common/Models/Data/IdModel.cs deleted file mode 100644 index 1f2bf9a..0000000 --- a/src/DevHive.Common/Models/Data/IdModel.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace DevHive.Common.Models.Data -{ - public class IdModel - { - public Guid Id { get; set; } - } -} \ No newline at end of file diff --git a/src/DevHive.Common/Models/Data/RepositoryMethods.cs b/src/DevHive.Common/Models/Data/RepositoryMethods.cs index b56aad9..bfd057f 100644 --- a/src/DevHive.Common/Models/Data/RepositoryMethods.cs +++ b/src/DevHive.Common/Models/Data/RepositoryMethods.cs @@ -1,7 +1,7 @@ using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; -namespace DevHive.Common.Models.Data +namespace DevHive.Common.Models.Misc { public static class RepositoryMethods { diff --git a/src/DevHive.Common/Models/Misc/IdModel.cs b/src/DevHive.Common/Models/Misc/IdModel.cs new file mode 100644 index 0000000..a5c7b65 --- /dev/null +++ b/src/DevHive.Common/Models/Misc/IdModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Common.Models.Misc +{ + public class IdModel + { + public Guid Id { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Data/Models/Comment.cs b/src/DevHive.Data/Models/Comment.cs index f949a42..8cf848f 100644 --- a/src/DevHive.Data/Models/Comment.cs +++ b/src/DevHive.Data/Models/Comment.cs @@ -4,8 +4,8 @@ namespace DevHive.Data.Models public class Comment : IModel { public Guid Id { get; set; } - public Guid UserId { get; set; } + public Guid IssuerId { get; set; } public string Message { get; set; } - public DateTime Date { get; set; } + public DateTime TimeCreated { get; set; } } } \ No newline at end of file diff --git a/src/DevHive.Data/Models/Post.cs b/src/DevHive.Data/Models/Post.cs new file mode 100644 index 0000000..a5abf12 --- /dev/null +++ b/src/DevHive.Data/Models/Post.cs @@ -0,0 +1,21 @@ +using System; +using System.ComponentModel.DataAnnotations.Schema; + +namespace DevHive.Data.Models +{ + [Table("Posts")] + public class Post + { + public Guid Id { get; set; } + + public Guid IssuerId { get; set; } + + public DateTime TimeCreated { get; set; } + + public string Message { get; set; } + + //public File[] Files { get; set; } + + public Comment[] Comments { get; set; } + } +} diff --git a/src/DevHive.Data/Repositories/CommentRepository.cs b/src/DevHive.Data/Repositories/CommentRepository.cs deleted file mode 100644 index 5a4ef17..0000000 --- a/src/DevHive.Data/Repositories/CommentRepository.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System; -using System.Threading.Tasks; -using Data.Models.Interfaces.Database; -using DevHive.Common.Models.Data; -using DevHive.Data.Models; -using Microsoft.EntityFrameworkCore; - -namespace DevHive.Data.Repositories -{ - public class CommentRepository : IRepository - { - private readonly DevHiveContext _context; - - public CommentRepository(DevHiveContext context) - { - this._context = context; - } - - public async Task AddAsync(Comment entity) - { - await this._context - .Set() - .AddAsync(entity); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - - public async Task GetByIdAsync(Guid id) - { - return await this._context - .Set() - .FindAsync(id); - } - - - public async Task EditAsync(Comment newEntity) - { - this._context - .Set() - .Update(newEntity); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - - public async Task DoesCommentExist(Guid id) - { - return await this._context - .Set() - .AsNoTracking() - .AnyAsync(r => r.Id == id); - } - - public async Task DeleteAsync(Comment entity) - { - this._context - .Set() - .Remove(entity); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - } -} \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index 6e3a2e3..1ab870a 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; using Data.Models.Interfaces.Database; -using DevHive.Common.Models.Data; +using DevHive.Common.Models.Misc; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs new file mode 100644 index 0000000..5dfee0b --- /dev/null +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -0,0 +1,107 @@ +using System; +using System.Threading.Tasks; +using Data.Models.Interfaces.Database; +using DevHive.Common.Models.Misc; +using DevHive.Data.Models; +using Microsoft.EntityFrameworkCore; + +namespace DevHive.Data.Repositories +{ + public class PostRepository : IRepository + { + private readonly DevHiveContext _context; + + public PostRepository(DevHiveContext context) + { + this._context = context; + } + + //Create + public async Task AddAsync(Post post) + { + await this._context + .Set() + .AddAsync(post); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task AddCommentAsync(Comment entity) + { + await this._context + .Set() + .AddAsync(entity); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + //Read + public async Task GetByIdAsync(Guid id) + { + return await this._context + .Set() + .FindAsync(id); + } + + public async Task GetCommentByIdAsync(Guid id) + { + return await this._context + .Set() + .FindAsync(id); + } + + //Update + public async Task EditAsync(Post newPost) + { + this._context + .Set() + .Update(newPost); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task EditCommentAsync(Comment newEntity) + { + this._context + .Set() + .Update(newEntity); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + //Delete + public async Task DeleteAsync(Post post) + { + this._context + .Set() + .Remove(post); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task DeleteCommentAsync(Comment entity) + { + this._context + .Set() + .Remove(entity); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task DoesPostExist(Guid postId) + { + return await this._context + .Set() + .AsNoTracking() + .AnyAsync(r => r.Id == postId); + } + + public async Task DoesCommentExist(Guid id) + { + return await this._context + .Set() + .AsNoTracking() + .AnyAsync(r => r.Id == id); + } + } +} \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs index 1d6f2da..fd04866 100644 --- a/src/DevHive.Data/Repositories/RoleRepository.cs +++ b/src/DevHive.Data/Repositories/RoleRepository.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; using Data.Models.Interfaces.Database; -using DevHive.Common.Models.Data; +using DevHive.Common.Models.Misc; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index e2e4257..935582c 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -3,7 +3,7 @@ using System.Threading.Tasks; using Data.Models.Interfaces.Database; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; -using DevHive.Common.Models.Data; +using DevHive.Common.Models.Misc; namespace DevHive.Data.Repositories { diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index d2a8830..5600451 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -3,7 +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.Common.Models.Misc; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; diff --git a/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs b/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs index f4197a0..f903128 100644 --- a/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs @@ -1,6 +1,7 @@ using DevHive.Data.Models; using AutoMapper; -using DevHive.Services.Models.Comment; +using DevHive.Services.Models.Post.Comment; +using DevHive.Common.Models.Misc; namespace DevHive.Services.Configurations.Mapping { @@ -11,8 +12,8 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); CreateMap(); - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } \ No newline at end of file diff --git a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs new file mode 100644 index 0000000..695ffdb --- /dev/null +++ b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs @@ -0,0 +1,16 @@ +using DevHive.Data.Models; +using AutoMapper; +using DevHive.Services.Models.Post; +using DevHive.Services.Models.Post.Post; + +namespace DevHive.Services.Configurations.Mapping +{ + public class PostMappings : Profile + { + public PostMappings() + { + CreateMap(); + CreateMap(); + } + } +} diff --git a/src/DevHive.Services/Models/Comment/CommentServiceModel.cs b/src/DevHive.Services/Models/Comment/CommentServiceModel.cs deleted file mode 100644 index f3638ac..0000000 --- a/src/DevHive.Services/Models/Comment/CommentServiceModel.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Comment -{ - public class CommentServiceModel - { - public Guid UserId { get; set; } - public string Message { get; set; } - } -} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Comment/GetByIdCommentServiceModel.cs b/src/DevHive.Services/Models/Comment/GetByIdCommentServiceModel.cs deleted file mode 100644 index c2b84b3..0000000 --- a/src/DevHive.Services/Models/Comment/GetByIdCommentServiceModel.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Comment -{ - public class GetByIdCommentServiceModel : CommentServiceModel - { - public DateTime Date { get; set; } - } -} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Comment/UpdateCommnetServiceModel.cs b/src/DevHive.Services/Models/Comment/UpdateCommnetServiceModel.cs deleted file mode 100644 index 3a461c5..0000000 --- a/src/DevHive.Services/Models/Comment/UpdateCommnetServiceModel.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Comment -{ - public class UpdateCommentServiceModel : CommentServiceModel - { - public Guid Id { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs new file mode 100644 index 0000000..3aa92ee --- /dev/null +++ b/src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs @@ -0,0 +1,11 @@ +using System; + +namespace DevHive.Services.Models.Post.Comment +{ + public class BaseCommentServiceModel + { + public Guid Id { get; set; } + public Guid IssuerId { get; set; } + public string Message { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Post/Comment/CommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/CommentServiceModel.cs new file mode 100644 index 0000000..a0fa53e --- /dev/null +++ b/src/DevHive.Services/Models/Post/Comment/CommentServiceModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Services.Models.Post.Comment +{ + public class CommentServiceModel : CreateCommentServiceModel + { + + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs new file mode 100644 index 0000000..33f3bfe --- /dev/null +++ b/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Services.Models.Post.Comment +{ + public class CreateCommentServiceModel : BaseCommentServiceModel + { + public DateTime TimeCreated { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs new file mode 100644 index 0000000..9516a2b --- /dev/null +++ b/src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs @@ -0,0 +1,8 @@ +using System; + +namespace DevHive.Services.Models.Post.Comment +{ + public class UpdateCommentServiceModel : BaseCommentServiceModel + { + } +} diff --git a/src/DevHive.Services/Models/Post/Post/BasePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/BasePostServiceModel.cs new file mode 100644 index 0000000..45a677c --- /dev/null +++ b/src/DevHive.Services/Models/Post/Post/BasePostServiceModel.cs @@ -0,0 +1,11 @@ +using System; + +namespace DevHive.Services.Models.Post.Post +{ + public class BasePostServiceModel + { + public Guid Id { get; set; } + public Guid IssuerId { get; set; } + public string Message { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs new file mode 100644 index 0000000..97225c7 --- /dev/null +++ b/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Services.Models.Post.Post +{ + public class CreatePostServiceModel : BasePostServiceModel + { + public DateTime TimeCreated { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Post/Post/PostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/PostServiceModel.cs new file mode 100644 index 0000000..b9c2128 --- /dev/null +++ b/src/DevHive.Services/Models/Post/Post/PostServiceModel.cs @@ -0,0 +1,12 @@ +using System; + +namespace DevHive.Services.Models.Post.Post +{ + public class PostServiceModel : CreatePostServiceModel + { + + //public File[] Files { get; set; } + + //public Comment[] Comments { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs new file mode 100644 index 0000000..de61a72 --- /dev/null +++ b/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Services.Models.Post.Post +{ + public class UpdatePostServiceModel : BasePostServiceModel + { + + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Services/CommentService.cs b/src/DevHive.Services/Services/CommentService.cs deleted file mode 100644 index 69dbcc0..0000000 --- a/src/DevHive.Services/Services/CommentService.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System; -using System.Threading.Tasks; -using AutoMapper; -using DevHive.Data.Models; -using DevHive.Data.Repositories; -using DevHive.Services.Models.Comment; - -namespace DevHive.Services.Services -{ - public class CommentService - { - private readonly CommentRepository _commentRepository; - private readonly IMapper _commentMapper; - - public CommentService(CommentRepository commentRepository, IMapper mapper) - { - this._commentRepository = commentRepository; - this._commentMapper = mapper; - } - - public async Task CreateComment(CommentServiceModel commentServiceModel) - { - Comment comment = this._commentMapper.Map(commentServiceModel); - comment.Date = DateTime.Now; - bool result = await this._commentRepository.AddAsync(comment); - - return result; - } - - public async Task GetCommentById(Guid id) - { - Comment comment = await this._commentRepository.GetByIdAsync(id); - - if(comment == null) - throw new ArgumentException("The comment does not exist"); - - return this._commentMapper.Map(comment); - } - - public async Task UpdateComment(UpdateCommentServiceModel commentServiceModel) - { - if (!await this._commentRepository.DoesCommentExist(commentServiceModel.Id)) - throw new ArgumentException("Comment does not exist!"); - - Comment comment = this._commentMapper.Map(commentServiceModel); - bool result = await this._commentRepository.EditAsync(comment); - - return result; - } - - public async Task DeleteComment(Guid id) - { - if (!await this._commentRepository.DoesCommentExist(id)) - throw new ArgumentException("Comment does not exist!"); - - Comment comment = await this._commentRepository.GetByIdAsync(id); - bool result = await this._commentRepository.DeleteAsync(comment); - - return result; - } - } -} \ No newline at end of file diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs new file mode 100644 index 0000000..0c0fd5c --- /dev/null +++ b/src/DevHive.Services/Services/PostService.cs @@ -0,0 +1,98 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Models; +using DevHive.Data.Repositories; +using DevHive.Services.Models.Post.Comment; +using DevHive.Services.Models.Post.Post; + +namespace DevHive.Services.Services +{ + public class PostService + { + private readonly PostRepository _postRepository; + private readonly IMapper _postMapper; + + public PostService(PostRepository postRepository, IMapper postMapper) + { + this._postRepository = postRepository; + this._postMapper = postMapper; + } + + //Create + public async Task CreatePost(CreatePostServiceModel postServiceModel) + { + Post post = this._postMapper.Map(postServiceModel); + + return await this._postRepository.AddAsync(post); + } + + public async Task AddComment(CreateCommentServiceModel commentServiceModel) + { + commentServiceModel.TimeCreated = DateTime.Now; + Comment comment = this._postMapper.Map(commentServiceModel); + + bool result = await this._postRepository.AddCommentAsync(comment); + + return result; + } + + //Read + public async Task GetPostById(Guid id) + { + Post post = await this._postRepository.GetByIdAsync(id) + ?? throw new ArgumentException("Post does not exist!"); + + return this._postMapper.Map(post); + } + + public async Task GetCommentById(Guid id) + { + Comment comment = await this._postRepository.GetCommentByIdAsync(id); + + if(comment == null) + throw new ArgumentException("The comment does not exist"); + + return this._postMapper.Map(comment); + } + + //Update + public async Task UpdatePost(UpdatePostServiceModel postServiceModel) + { + if (!await this._postRepository.DoesPostExist(postServiceModel.IssuerId)) + throw new ArgumentException("Comment does not exist!"); + + Post post = this._postMapper.Map(postServiceModel); + return await this._postRepository.EditAsync(post); + } + + public async Task UpdateComment(UpdateCommentServiceModel commentServiceModel) + { + if (!await this._postRepository.DoesCommentExist(commentServiceModel.Id)) + throw new ArgumentException("Comment does not exist!"); + + Comment comment = this._postMapper.Map(commentServiceModel); + bool result = await this._postRepository.EditCommentAsync(comment); + + return result; + } + + //Delete + public async Task DeletePost(Guid id) + { + Post post = await this._postRepository.GetByIdAsync(id); + return await this._postRepository.DeleteAsync(post); + } + + public async Task DeleteComment(Guid id) + { + if (!await this._postRepository.DoesCommentExist(id)) + throw new ArgumentException("Comment does not exist!"); + + Comment comment = await this._postRepository.GetCommentByIdAsync(id); + bool result = await this._postRepository.DeleteCommentAsync(comment); + + return result; + } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs index 76d7c6f..2707d91 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs @@ -12,13 +12,13 @@ namespace DevHive.Web.Configurations.Extensions services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs index dd11420..394490e 100644 --- a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs @@ -1,10 +1,10 @@ using AutoMapper; -using DevHive.Web.Models.Comment; -using DevHive.Services.Models.Comment; +using DevHive.Services.Models.Post.Comment; +using DevHive.Web.Models.Post.Comment; namespace DevHive.Web.Configurations.Mapping { - public class CommentMappings : Profile + public class CommentMappings : Profile { public CommentMappings() { @@ -12,7 +12,6 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap(); CreateMap(); - CreateMap(); } } } \ No newline at end of file diff --git a/src/DevHive.Web/Controllers/CommentController.cs b/src/DevHive.Web/Controllers/CommentController.cs deleted file mode 100644 index 5b6b0ee..0000000 --- a/src/DevHive.Web/Controllers/CommentController.cs +++ /dev/null @@ -1,72 +0,0 @@ -using System; -using System.Threading.Tasks; -using AutoMapper; -using DevHive.Data.Repositories; -using DevHive.Services.Models.Comment; -using DevHive.Services.Services; -using DevHive.Web.Models.Comment; -using Microsoft.AspNetCore.Mvc; - -namespace DevHive.Web.Controllers -{ - [ApiController] - [Route("/api/[controller]")] - public class CommentController - { - private readonly CommentService _commentService; - private readonly IMapper _commentMapper; - - public CommentController(CommentService commentService, IMapper mapper) - { - this._commentService = commentService; - this._commentMapper = mapper; - } - - [HttpPost] - public async Task Create([FromBody] CommentWebModel commentWebModel) - { - CommentServiceModel commentServiceModel = this._commentMapper.Map(commentWebModel); - - bool result = await this._commentService.CreateComment(commentServiceModel); - - if(!result) - return new BadRequestObjectResult("Could not create the Comment"); - - return new OkResult(); - } - - [HttpGet] - public async Task GetById(Guid id) - { - GetByIdCommentServiceModel getByIdCommentServiceModel = await this._commentService.GetCommentById(id); - GetByIdCommentWebModel getByIdCommentWebModel = this._commentMapper.Map(getByIdCommentServiceModel); - - return new OkObjectResult(getByIdCommentWebModel); - } - - [HttpPut] - public async Task Update(Guid id, [FromBody] CommentWebModel commentWebModel) - { - UpdateCommentServiceModel updateCommentServiceModel = this._commentMapper.Map(commentWebModel); - updateCommentServiceModel.Id = id; - - bool result = await this._commentService.UpdateComment(updateCommentServiceModel); - - if (!result) - return new BadRequestObjectResult("Could not update Comment"); - - return new OkResult(); - } - - [HttpDelete] - public async Task Delete(Guid id) - { - bool result = await this._commentService.DeleteComment(id); - - if (!result) - return new BadRequestObjectResult("Could not delete Comment"); - - return new OkResult(); - } - } -} \ No newline at end of file diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs new file mode 100644 index 0000000..397ddbc --- /dev/null +++ b/src/DevHive.Web/Controllers/PostController.cs @@ -0,0 +1,132 @@ +using System.Threading.Tasks; +using DevHive.Services.Services; +using Microsoft.AspNetCore.Mvc; +using AutoMapper; +using System; +using DevHive.Web.Models.Post.Post; +using DevHive.Services.Models.Post.Post; +using DevHive.Web.Models.Post.Comment; +using DevHive.Services.Models.Post.Comment; +using DevHive.Common.Models.Misc; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + //[Authorize(Posts = "Admin")] + public class PostController + { + private readonly PostService _postService; + private readonly IMapper _postMapper; + + public PostController(PostService postService, IMapper mapper) + { + this._postService = postService; + this._postMapper = mapper; + } + + //Create + [HttpPost] + public async Task Create([FromBody] CreatePostWebModel createPostModel) + { + CreatePostServiceModel postServiceModel = + this._postMapper.Map(createPostModel); + + bool result = await this._postService.CreatePost(postServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not create post!"); + + return new OkResult(); + } + + [HttpPost] + [Route("Comment")] + public async Task AddComment([FromBody] CommentWebModel commentWebModel) + { + CommentServiceModel commentServiceModel = this._postMapper.Map(commentWebModel); + + bool result = await this._postService.AddComment(commentServiceModel); + + if(!result) + return new BadRequestObjectResult("Could not create the Comment"); + + return new OkResult(); + } + + //Read + [HttpGet] + public async Task GetById(Guid id) + { + PostServiceModel postServiceModel = await this._postService.GetPostById(id); + PostWebModel postWebModel = this._postMapper.Map(postServiceModel); + + return new OkObjectResult(postWebModel); + } + + [HttpGet] + [Route("Comment")] + public async Task GetCommentById(Guid id) + { + CommentServiceModel commentServiceModel = await this._postService.GetCommentById(id); + IdModel idModel = this._postMapper.Map(commentServiceModel); + + return new OkObjectResult(idModel); + } + + //Update + [HttpPut] + public async Task Update(Guid id, [FromBody] UpdatePostWebModel updatePostModel) + { + UpdatePostServiceModel postServiceModel = + this._postMapper.Map(updatePostModel); + postServiceModel.IssuerId = id; + + bool result = await this._postService.UpdatePost(postServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not update post!"); + + return new OkResult(); + } + + [HttpPut] + [Route("Comment")] + public async Task UpdateComment(Guid id, [FromBody] CommentWebModel commentWebModel) + { + UpdateCommentServiceModel updateCommentServiceModel = this._postMapper.Map(commentWebModel); + updateCommentServiceModel.Id = id; + + bool result = await this._postService.UpdateComment(updateCommentServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not update Comment"); + + return new OkResult(); + } + + //Delete + [HttpDelete] + public async Task Delete(Guid id) + { + bool result = await this._postService.DeletePost(id); + + if (!result) + return new BadRequestObjectResult("Could not delete post!"); + + return new OkResult(); + } + + [HttpDelete] + [Route("Comment")] + public async Task DeleteComment(Guid id) + { + bool result = await this._postService.DeleteComment(id); + + if (!result) + return new BadRequestObjectResult("Could not delete Comment"); + + return new OkResult(); + } + } +} diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index 6b81ab8..d710f5a 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -67,7 +67,7 @@ namespace DevHive.Web.Controllers bool result = await this._roleService.DeleteRole(id); if (!result) - return new BadRequestObjectResult("Could nor delete role!"); + return new BadRequestObjectResult("Could not delete role!"); return new OkResult(); } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 02cae0c..ce972a5 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -9,7 +9,7 @@ using DevHive.Web.Models.Identity.User; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using DevHive.Common.Models.Identity; -using DevHive.Common.Models.Data; +using DevHive.Common.Models.Misc; namespace DevHive.Web.Controllers { diff --git a/src/DevHive.Web/Models/Comment/CommentWebModel.cs b/src/DevHive.Web/Models/Comment/CommentWebModel.cs deleted file mode 100644 index 37806a5..0000000 --- a/src/DevHive.Web/Models/Comment/CommentWebModel.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; - -namespace DevHive.Web.Models.Comment -{ - public class CommentWebModel - { - public Guid UserId { get; set; } - public string Message { get; set; } - } -} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Comment/GetByIdCommentWebModel.cs b/src/DevHive.Web/Models/Comment/GetByIdCommentWebModel.cs deleted file mode 100644 index 3d03348..0000000 --- a/src/DevHive.Web/Models/Comment/GetByIdCommentWebModel.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace DevHive.Web.Models.Comment -{ - public class GetByIdCommentWebModel : CommentWebModel - { - public DateTime Date { get; set; } - } -} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs new file mode 100644 index 0000000..2a8650a --- /dev/null +++ b/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Web.Models.Post.Comment +{ + public class CommentWebModel + { + public Guid IssuerId { get; set; } + public string Message { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Post/Post/BasePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/BasePostWebModel.cs new file mode 100644 index 0000000..caa9925 --- /dev/null +++ b/src/DevHive.Web/Models/Post/Post/BasePostWebModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Web.Models.Post.Post +{ + public class BasePostWebModel + { + public Guid IssuerId { get; set; } + public string Message { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs new file mode 100644 index 0000000..7558b2c --- /dev/null +++ b/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs @@ -0,0 +1,8 @@ +using System; + +namespace DevHive.Web.Models.Post.Post +{ + public class CreatePostWebModel : BasePostWebModel + { + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Post/Post/PostWebModel.cs b/src/DevHive.Web/Models/Post/Post/PostWebModel.cs new file mode 100644 index 0000000..fa18c3a --- /dev/null +++ b/src/DevHive.Web/Models/Post/Post/PostWebModel.cs @@ -0,0 +1,21 @@ +using DevHive.Web.Models.Post.Comment; + +namespace DevHive.Web.Models.Post.Post +{ + public class PostWebModel + { + //public string Picture { get; set; } + + public string IssuerFirstName { get; set; } + + public string IssuerLastName { get; set; } + + public string IssuerUsername { get; set; } + + public string Message { get; set; } + + //public Files[] Files { get; set; } + + public CommentWebModel[] Comments { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Post/Post/UpdatePostModel.cs b/src/DevHive.Web/Models/Post/Post/UpdatePostModel.cs new file mode 100644 index 0000000..c774900 --- /dev/null +++ b/src/DevHive.Web/Models/Post/Post/UpdatePostModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Web.Models.Post.Post +{ + public class UpdatePostWebModel : BasePostWebModel + { + //public Files[] Files { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Startup.cs b/src/DevHive.Web/Startup.cs index de1295c..42fc88a 100644 --- a/src/DevHive.Web/Startup.cs +++ b/src/DevHive.Web/Startup.cs @@ -1,14 +1,10 @@ -using System; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using DevHive.Web.Configurations.Extensions; -using AutoMapper; using Newtonsoft.Json; -using DevHive.Data.Repositories; -using DevHive.Services.Services; namespace DevHive.Web { -- cgit v1.2.3 From 11bd1d9a9760c7bc6a601d78b3d89ec9028647a2 Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 12 Jan 2021 13:16:39 +0200 Subject: Language layers refactored; User implements adding & removing Languages; Migrations added --- ...112111416_User_Implements_Languages.Designer.cs | 405 +++++++++++++++++++++ .../20210112111416_User_Implements_Languages.cs | 106 ++++++ .../Migrations/DevHiveContextModelSnapshot.cs | 38 +- src/DevHive.Data/Models/Technology.cs | 1 - src/DevHive.Data/Models/User.cs | 12 +- .../Repositories/Contracts/ILanguageRepository.cs | 13 - .../Repositories/Contracts/IPostRepository.cs | 21 -- .../Repositories/Contracts/IRepository.cs | 21 -- .../Repositories/Contracts/IRoleRepository.cs | 15 - .../Contracts/ITechnologyRepository.cs | 13 - .../Repositories/Contracts/IUserRepository.cs | 26 -- src/DevHive.Data/Repositories/IRepository.cs | 21 ++ .../Repositories/LanguageRepository.cs | 55 +-- src/DevHive.Data/Repositories/PostRepository.cs | 6 +- src/DevHive.Data/Repositories/RoleRepository.cs | 3 +- .../Repositories/TechnologyRepository.cs | 5 +- src/DevHive.Data/Repositories/UserRepository.cs | 162 +++++++-- .../Configurations/Mapping/LanguageMappings.cs | 1 - .../Models/Identity/User/RegisterServiceModel.cs | 8 +- .../Models/Language/CreateLanguageServiceModel.cs | 9 + .../Models/Language/LanguageServiceModel.cs | 4 +- .../Models/Language/UpdateLanguageServiceModel.cs | 7 +- src/DevHive.Services/Services/LanguageService.cs | 38 +- src/DevHive.Services/Services/PostService.cs | 8 +- src/DevHive.Services/Services/RoleService.cs | 6 +- src/DevHive.Services/Services/TechnologyService.cs | 6 +- src/DevHive.Services/Services/UserService.cs | 146 +++++--- .../TechnologyRepository.Tests.cs | 4 +- .../Configurations/Mapping/LanguageMappings.cs | 7 +- src/DevHive.Web/Controllers/LanguageController.cs | 4 +- .../Models/Language/CreateLanguageWebModel.cs | 9 + .../Models/Language/LanguageWebModel.cs | 4 +- .../Models/Language/UpdateLanguageWebModel.cs | 5 +- 33 files changed, 912 insertions(+), 277 deletions(-) create mode 100644 src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs create mode 100644 src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.cs delete mode 100644 src/DevHive.Data/Repositories/Contracts/ILanguageRepository.cs delete mode 100644 src/DevHive.Data/Repositories/Contracts/IPostRepository.cs delete mode 100644 src/DevHive.Data/Repositories/Contracts/IRepository.cs delete mode 100644 src/DevHive.Data/Repositories/Contracts/IRoleRepository.cs delete mode 100644 src/DevHive.Data/Repositories/Contracts/ITechnologyRepository.cs delete mode 100644 src/DevHive.Data/Repositories/Contracts/IUserRepository.cs create mode 100644 src/DevHive.Data/Repositories/IRepository.cs create mode 100644 src/DevHive.Services/Models/Language/CreateLanguageServiceModel.cs create mode 100644 src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs b/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs new file mode 100644 index 0000000..0f1aa80 --- /dev/null +++ b/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs @@ -0,0 +1,405 @@ +// +using System; +using DevHive.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace DevHive.Data.Migrations +{ + [DbContext(typeof(DevHiveContext))] + [Migration("20210112111416_User_Implements_Languages")] + partial class User_Implements_Languages + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseIdentityByDefaultColumns() + .HasAnnotation("Relational:MaxIdentifierLength", 63) + .HasAnnotation("ProductVersion", "5.0.1"); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("IssuerId") + .HasColumnType("uuid"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.ToTable("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Language", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Languages"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Technology", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Technologies"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AccessFailedCount") + .HasColumnType("integer"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("EmailConfirmed") + .HasColumnType("boolean"); + + b.Property("FirstName") + .HasColumnType("text"); + + b.Property("LastName") + .HasColumnType("text"); + + b.Property("LockoutEnabled") + .HasColumnType("boolean"); + + b.Property("LockoutEnd") + .HasColumnType("timestamp with time zone"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("PasswordHash") + .HasColumnType("text"); + + b.Property("PhoneNumber") + .HasColumnType("text"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("boolean"); + + b.Property("ProfilePictureUrl") + .HasColumnType("text"); + + b.Property("SecurityStamp") + .HasColumnType("text"); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.HasIndex("UserId"); + + b.HasIndex("UserName") + .IsUnique(); + + b.ToTable("AspNetUsers"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("ProviderKey") + .HasColumnType("text"); + + b.Property("ProviderDisplayName") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.Property("RolesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("RolesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("RoleUser"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Language", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany("Langauges") + .HasForeignKey("UserId"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Technology", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany("Technologies") + .HasForeignKey("UserId"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany("Friends") + .HasForeignKey("UserId"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RolesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Navigation("Friends"); + + b.Navigation("Langauges"); + + b.Navigation("Technologies"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.cs b/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.cs new file mode 100644 index 0000000..a51ad09 --- /dev/null +++ b/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.cs @@ -0,0 +1,106 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace DevHive.Data.Migrations +{ + public partial class User_Implements_Languages : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.RenameColumn( + name: "UserId", + table: "Comments", + newName: "IssuerId"); + + migrationBuilder.RenameColumn( + name: "Date", + table: "Comments", + newName: "TimeCreated"); + + migrationBuilder.RenameColumn( + name: "ProfilePicture", + table: "AspNetUsers", + newName: "ProfilePictureUrl"); + + migrationBuilder.AddColumn( + name: "UserId", + table: "Technologies", + type: "uuid", + nullable: true); + + migrationBuilder.AddColumn( + name: "UserId", + table: "Languages", + type: "uuid", + nullable: true); + + migrationBuilder.CreateIndex( + name: "IX_Technologies_UserId", + table: "Technologies", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_Languages_UserId", + table: "Languages", + column: "UserId"); + + migrationBuilder.AddForeignKey( + name: "FK_Languages_AspNetUsers_UserId", + table: "Languages", + column: "UserId", + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Technologies_AspNetUsers_UserId", + table: "Technologies", + column: "UserId", + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Languages_AspNetUsers_UserId", + table: "Languages"); + + migrationBuilder.DropForeignKey( + name: "FK_Technologies_AspNetUsers_UserId", + table: "Technologies"); + + migrationBuilder.DropIndex( + name: "IX_Technologies_UserId", + table: "Technologies"); + + migrationBuilder.DropIndex( + name: "IX_Languages_UserId", + table: "Languages"); + + migrationBuilder.DropColumn( + name: "UserId", + table: "Technologies"); + + migrationBuilder.DropColumn( + name: "UserId", + table: "Languages"); + + migrationBuilder.RenameColumn( + name: "TimeCreated", + table: "Comments", + newName: "Date"); + + migrationBuilder.RenameColumn( + name: "IssuerId", + table: "Comments", + newName: "UserId"); + + migrationBuilder.RenameColumn( + name: "ProfilePictureUrl", + table: "AspNetUsers", + newName: "ProfilePicture"); + } + } +} diff --git a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs index c7ff6c6..cc6d24d 100644 --- a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs +++ b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs @@ -25,14 +25,14 @@ namespace DevHive.Data.Migrations .ValueGeneratedOnAdd() .HasColumnType("uuid"); - b.Property("Date") - .HasColumnType("timestamp without time zone"); + b.Property("IssuerId") + .HasColumnType("uuid"); b.Property("Message") .HasColumnType("text"); - b.Property("UserId") - .HasColumnType("uuid"); + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); b.HasKey("Id"); @@ -48,8 +48,13 @@ namespace DevHive.Data.Migrations b.Property("Name") .HasColumnType("text"); + b.Property("UserId") + .HasColumnType("uuid"); + b.HasKey("Id"); + b.HasIndex("UserId"); + b.ToTable("Languages"); }); @@ -89,8 +94,13 @@ namespace DevHive.Data.Migrations b.Property("Name") .HasColumnType("text"); + b.Property("UserId") + .HasColumnType("uuid"); + b.HasKey("Id"); + b.HasIndex("UserId"); + b.ToTable("Technologies"); }); @@ -143,7 +153,7 @@ namespace DevHive.Data.Migrations b.Property("PhoneNumberConfirmed") .HasColumnType("boolean"); - b.Property("ProfilePicture") + b.Property("ProfilePictureUrl") .HasColumnType("text"); b.Property("SecurityStamp") @@ -292,6 +302,20 @@ namespace DevHive.Data.Migrations b.ToTable("RoleUser"); }); + modelBuilder.Entity("DevHive.Data.Models.Language", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany("Langauges") + .HasForeignKey("UserId"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Technology", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany("Technologies") + .HasForeignKey("UserId"); + }); + modelBuilder.Entity("DevHive.Data.Models.User", b => { b.HasOne("DevHive.Data.Models.User", null) @@ -368,6 +392,10 @@ namespace DevHive.Data.Migrations modelBuilder.Entity("DevHive.Data.Models.User", b => { b.Navigation("Friends"); + + b.Navigation("Langauges"); + + b.Navigation("Technologies"); }); #pragma warning restore 612, 618 } diff --git a/src/DevHive.Data/Models/Technology.cs b/src/DevHive.Data/Models/Technology.cs index 2e0aeed..a462d20 100644 --- a/src/DevHive.Data/Models/Technology.cs +++ b/src/DevHive.Data/Models/Technology.cs @@ -5,7 +5,6 @@ namespace DevHive.Data.Models public class Technology : IModel { public Guid Id { get; set; } - public string Name { get; set; } } } diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs index eef0af2..fda4651 100644 --- a/src/DevHive.Data/Models/User.cs +++ b/src/DevHive.Data/Models/User.cs @@ -12,7 +12,17 @@ namespace DevHive.Data.Models public string LastName { get; set; } - public string ProfilePicture { get; set; } + public string ProfilePictureUrl { get; set; } + + /// + /// Languages that the user uses or is familiar with + /// + public IList Langauges { get; set; } + + /// + /// Technologies that the user uses or is familiar with + /// + public IList Technologies { get; set; } public virtual IList Roles { get; set; } = new List(); diff --git a/src/DevHive.Data/Repositories/Contracts/ILanguageRepository.cs b/src/DevHive.Data/Repositories/Contracts/ILanguageRepository.cs deleted file mode 100644 index e44d27b..0000000 --- a/src/DevHive.Data/Repositories/Contracts/ILanguageRepository.cs +++ /dev/null @@ -1,13 +0,0 @@ -using DevHive.Data.Models; -using System; -using System.Threading.Tasks; - -namespace DevHive.Data.Repositories.Contracts -{ - public interface ILanguageRepository : IRepository - { - public Task DoesLanguageNameExist(string languageName); - - public Task DoesLanguageExist(Guid id); - } -} diff --git a/src/DevHive.Data/Repositories/Contracts/IPostRepository.cs b/src/DevHive.Data/Repositories/Contracts/IPostRepository.cs deleted file mode 100644 index 930138a..0000000 --- a/src/DevHive.Data/Repositories/Contracts/IPostRepository.cs +++ /dev/null @@ -1,21 +0,0 @@ -using DevHive.Data.Models; -using System; -using System.Threading.Tasks; - -namespace DevHive.Data.Repositories.Contracts -{ - public interface IPostRepository : IRepository - { - public Task AddCommentAsync(Comment entity); - - public Task GetCommentByIdAsync(Guid id); - - public Task EditCommentAsync(Comment newEntity); - - public Task DeleteCommentAsync(Comment entity); - - public Task DoesPostExist(Guid postId); - - public Task DoesCommentExist(Guid id); - } -} diff --git a/src/DevHive.Data/Repositories/Contracts/IRepository.cs b/src/DevHive.Data/Repositories/Contracts/IRepository.cs deleted file mode 100644 index 37c5170..0000000 --- a/src/DevHive.Data/Repositories/Contracts/IRepository.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Threading.Tasks; - -namespace DevHive.Data.Repositories.Contracts -{ - public interface IRepository - where TEntity : class - { - //Add Entity to database - Task AddAsync(TEntity entity); - - //Find entity by id - Task GetByIdAsync(Guid id); - - //Modify Entity from database - Task EditAsync(TEntity newEntity); - - //Delete Entity from database - Task DeleteAsync(TEntity entity); - } -} \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/Contracts/IRoleRepository.cs b/src/DevHive.Data/Repositories/Contracts/IRoleRepository.cs deleted file mode 100644 index 6cb8a4e..0000000 --- a/src/DevHive.Data/Repositories/Contracts/IRoleRepository.cs +++ /dev/null @@ -1,15 +0,0 @@ -using DevHive.Data.Models; -using System; -using System.Threading.Tasks; - -namespace DevHive.Data.Repositories.Contracts -{ - public interface IRoleRepository : IRepository - { - public Task GetByNameAsync(string name); - - public Task DoesNameExist(string name); - - public Task DoesRoleExist(Guid id); - } -} diff --git a/src/DevHive.Data/Repositories/Contracts/ITechnologyRepository.cs b/src/DevHive.Data/Repositories/Contracts/ITechnologyRepository.cs deleted file mode 100644 index 3c4a6b6..0000000 --- a/src/DevHive.Data/Repositories/Contracts/ITechnologyRepository.cs +++ /dev/null @@ -1,13 +0,0 @@ -using DevHive.Data.Models; -using System; -using System.Threading.Tasks; - -namespace DevHive.Data.Repositories.Contracts -{ - public interface ITechnologyRepository : IRepository - { - public Task DoesTechnologyNameExist(string technologyName); - - public Task DoesTechnologyExist(Guid id); - } -} diff --git a/src/DevHive.Data/Repositories/Contracts/IUserRepository.cs b/src/DevHive.Data/Repositories/Contracts/IUserRepository.cs deleted file mode 100644 index 74c4486..0000000 --- a/src/DevHive.Data/Repositories/Contracts/IUserRepository.cs +++ /dev/null @@ -1,26 +0,0 @@ -using DevHive.Data.Models; -using System; -using System.Collections.Generic; -using System.Threading.Tasks; - -namespace DevHive.Data.Repositories.Contracts -{ - public interface IUserRepository : IRepository - { - public Task AddFriendAsync(User user, User friend); - - public IEnumerable QueryAll(); - - public Task GetByUsername(string username); - - public Task RemoveFriendAsync(User user, User friend); - - public bool DoesUserExist(Guid id); - - public bool DoesUserHaveThisUsername(Guid id, string username); - - public Task DoesUsernameExist(string username); - - public Task DoesEmailExist(string email); - } -} diff --git a/src/DevHive.Data/Repositories/IRepository.cs b/src/DevHive.Data/Repositories/IRepository.cs new file mode 100644 index 0000000..920ba13 --- /dev/null +++ b/src/DevHive.Data/Repositories/IRepository.cs @@ -0,0 +1,21 @@ +using System; +using System.Threading.Tasks; + +namespace DevHive.Data.Repositories +{ + public interface IRepository + where TEntity : class + { + //Add Entity to database + Task AddAsync(TEntity entity); + + //Find entity by id + Task GetByIdAsync(Guid id); + + //Modify Entity from database + Task EditAsync(TEntity newEntity); + + //Delete Entity from database + Task DeleteAsync(TEntity entity); + } +} \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index 243192a..5d8217a 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -1,13 +1,13 @@ using System; +using System.Linq; using System.Threading.Tasks; using DevHive.Common.Models.Misc; using DevHive.Data.Models; -using DevHive.Data.Repositories.Contracts; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class LanguageRepository : ILanguageRepository + public class LanguageRepository : IRepository { private readonly DevHiveContext _context; @@ -16,7 +16,8 @@ namespace DevHive.Data.Repositories this._context = context; } - //Create + #region Create + public async Task AddAsync(Language entity) { await this._context @@ -25,42 +26,32 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } + #endregion + + #region Read - //Read public async Task GetByIdAsync(Guid id) { return await this._context .Set() .FindAsync(id); } + #endregion - public async Task DoesLanguageNameExist(string languageName) - { - return await this._context - .Set() - .AsNoTracking() - .AnyAsync(r => r.Name == languageName); - } - - public async Task DoesLanguageExist(Guid id) - { - return await this._context - .Set() - .AsNoTracking() - .AnyAsync(r => r.Id == id); - } + #region Update - //Update public async Task EditAsync(Language newEntity) { - this._context - .Set() - .Update(newEntity); + this._context + .Set() + .Update(newEntity); return await RepositoryMethods.SaveChangesAsync(this._context); } + #endregion + + #region Delete - //Delete public async Task DeleteAsync(Language entity) { this._context @@ -69,5 +60,21 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } + #endregion + + #region Validations + + public async Task DoesLanguageNameExistAsync(string languageName) + { + return await this._context.Languages + .AnyAsync(r => r.Name == languageName); + } + + public async Task DoesLanguageExistAsync(Guid id) + { + return await this._context.Languages + .AnyAsync(r => r.Id == id); + } + #endregion } } \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index 0acfc23..002fb17 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -2,12 +2,11 @@ using System; using System.Threading.Tasks; using DevHive.Common.Models.Misc; using DevHive.Data.Models; -using DevHive.Data.Repositories.Contracts; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class PostRepository : IPostRepository + public class PostRepository : IRepository { private readonly DevHiveContext _context; @@ -88,6 +87,8 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } + #region Validations + public async Task DoesPostExist(Guid postId) { return await this._context @@ -103,5 +104,6 @@ namespace DevHive.Data.Repositories .AsNoTracking() .AnyAsync(r => r.Id == id); } + #endregion } } \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs index d6f83a8..0ca1646 100644 --- a/src/DevHive.Data/Repositories/RoleRepository.cs +++ b/src/DevHive.Data/Repositories/RoleRepository.cs @@ -2,12 +2,11 @@ using System; using System.Threading.Tasks; using DevHive.Common.Models.Misc; using DevHive.Data.Models; -using DevHive.Data.Repositories.Contracts; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class RoleRepository : IRoleRepository + public class RoleRepository : IRepository { private readonly DevHiveContext _context; diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index 27918ca..2ed3a23 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -2,13 +2,12 @@ using System; using System.Threading.Tasks; using DevHive.Common.Models.Misc; using DevHive.Data.Models; -using DevHive.Data.Repositories.Contracts; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public abstract class TechnologyRepository : ITechnologyRepository + public abstract class TechnologyRepository : IRepository { private DevHiveContext _context; @@ -27,7 +26,7 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } - //Read + //Read public async Task GetByIdAsync(Guid id) { return await this._context diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 5142b82..e3c1304 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -4,12 +4,11 @@ using System.Linq; using System.Threading.Tasks; using DevHive.Common.Models.Misc; using DevHive.Data.Models; -using DevHive.Data.Repositories.Contracts; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class UserRepository : IUserRepository + public class UserRepository : IRepository { private readonly DevHiveContext _context; @@ -18,11 +17,11 @@ namespace DevHive.Data.Repositories this._context = context; } - //Create + #region Create + public async Task AddAsync(User entity) { - await this._context - .Set() + await this._context.Users .AddAsync(entity); return await RepositoryMethods.SaveChangesAsync(this._context); @@ -35,12 +34,31 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } - - //Read + + public async Task AddLanguageToUserAsync(User user, Language language) + { + this._context.Update(user); + + user.Langauges.Add(language); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task AddTechnologyToUserAsync(User user, Technology technology) + { + this._context.Update(user); + + user.Technologies.Add(technology); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + #endregion + + #region Read + public IEnumerable QueryAll() { - return this._context - .Set() + return this._context.Users .Include(x => x.Roles) .AsNoTracking() .AsEnumerable(); @@ -48,8 +66,7 @@ namespace DevHive.Data.Repositories public async Task GetByIdAsync(Guid id) { - return await this._context - .Set() + return await this._context.Users .Include(x => x.Roles) .Include(x => x.Friends) .FirstOrDefaultAsync(x => x.Id == id); @@ -57,13 +74,36 @@ namespace DevHive.Data.Repositories public async Task GetByUsername(string username) { - return await this._context - .Set() + return await this._context.Users .Include(u => u.Roles) .FirstOrDefaultAsync(x => x.UserName == username); } - //Update + public IList GetUserLanguages(User user) + { + return user.Langauges; + } + + public Language GetUserLanguage(User user, Language language) + { + return user.Langauges + .FirstOrDefault(x => x.Id == language.Id); + } + + public IList GetUserTechnologies(User user) + { + return user.Technologies; + } + + public Technology GetUserTechnology(User user, Technology technology) + { + return user.Technologies + .FirstOrDefault(x => x.Id == technology.Id); + } + #endregion + + #region Update + public async Task EditAsync(User newEntity) { User user = await this.GetByIdAsync(newEntity.Id); @@ -76,11 +116,32 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } - //Delete + public async Task EditUserLanguage(User user, Language oldLang, Language newLang) + { + this._context.Update(user); + + user.Langauges.Remove(oldLang); + user.Langauges.Add(newLang); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task EditUserTechnologies(User user, Technology oldTech, Technology newTech) + { + this._context.Update(user); + + user.Technologies.Remove(oldTech); + user.Technologies.Add(newTech); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + #endregion + + #region Delete + public async Task DeleteAsync(User entity) { - this._context - .Set() + this._context.Users .Remove(entity); return await RepositoryMethods.SaveChangesAsync(this._context); @@ -93,37 +154,70 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } - - //Validations - public bool DoesUserExist(Guid id) + + public async Task RemoveLanguageFromUserAsync(User user, Language language) { - return this._context - .Set() - .Any(x => x.Id == id); + this._context.Update(user); + + user.Langauges.Remove(language); + + return await RepositoryMethods.SaveChangesAsync(this._context); } - public bool DoesUserHaveThisUsername(Guid id, string username) + public async Task RemoveTechnologyFromUserAsync(User user, Technology technology) { - return this._context - .Set() - .Any(x => x.Id == id && - x.UserName == username); + this._context.Update(user); + + user.Technologies.Remove(technology); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + #endregion + + #region Validations + + public async Task DoesUserExistAsync(Guid id) + { + return await this._context.Users + .AnyAsync(x => x.Id == id); } - public async Task DoesUsernameExist(string username) + public async Task DoesUsernameExistAsync(string username) { - return await this._context - .Set() + return await this._context.Users .AsNoTracking() .AnyAsync(u => u.UserName == username); } - public async Task DoesEmailExist(string email) + public async Task DoesEmailExistAsync(string email) { - return await this._context - .Set() + return await this._context.Users .AsNoTracking() .AnyAsync(u => u.Email == email); } + + public async Task DoesUserHaveThisFriendAsync(Guid userId, Guid friendId) + { + User user = await this._context.Users + .FirstOrDefaultAsync(x => x.Id == userId); + + User friend = await this._context.Users + .FirstOrDefaultAsync(x => x.Id == friendId); + + return user.Friends.Contains(friend); + } + + public bool DoesUserHaveThisUsername(Guid id, string username) + { + return this._context.Users + .Any(x => x.Id == id && + x.UserName == username); + } + + public bool DoesUserHaveFriends(User user) + { + return user.Friends.Count >= 1; + } + #endregion } } diff --git a/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs index 27f392d..0be9ca2 100644 --- a/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs @@ -10,7 +10,6 @@ namespace DevHive.Services.Configurations.Mapping { CreateMap(); CreateMap(); - CreateMap(); } } } \ No newline at end of file diff --git a/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs b/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs index 77f2733..74f66b4 100644 --- a/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs @@ -1,7 +1,13 @@ +using System.Collections.Generic; +using DevHive.Services.Models.Language; +using DevHive.Services.Models.Technology; + namespace DevHive.Services.Models.Identity.User { public class RegisterServiceModel : BaseUserServiceModel - { + { + public IList Languages { get; set; } + public IList Technologies { get; set; } public string Password { get; set; } } } diff --git a/src/DevHive.Services/Models/Language/CreateLanguageServiceModel.cs b/src/DevHive.Services/Models/Language/CreateLanguageServiceModel.cs new file mode 100644 index 0000000..75e7714 --- /dev/null +++ b/src/DevHive.Services/Models/Language/CreateLanguageServiceModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Services.Models.Language +{ + public class CreateLanguageServiceModel : LanguageServiceModel + { + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Language/LanguageServiceModel.cs b/src/DevHive.Services/Models/Language/LanguageServiceModel.cs index f06ebb6..a07aa16 100644 --- a/src/DevHive.Services/Models/Language/LanguageServiceModel.cs +++ b/src/DevHive.Services/Models/Language/LanguageServiceModel.cs @@ -1,7 +1,9 @@ +using System; + namespace DevHive.Services.Models.Language { public class LanguageServiceModel { - public string Name { get; set; } + public Guid Id { get; set; } } } diff --git a/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs b/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs index 30194dd..fdc309e 100644 --- a/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs +++ b/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs @@ -1,9 +1,4 @@ -using System; - namespace DevHive.Services.Models.Language { - public class UpdateLanguageServiceModel : LanguageServiceModel - { - public Guid Id { get; set; } - } + public class UpdateLanguageServiceModel : CreateLanguageServiceModel { } } diff --git a/src/DevHive.Services/Services/LanguageService.cs b/src/DevHive.Services/Services/LanguageService.cs index 3c011c6..127bde4 100644 --- a/src/DevHive.Services/Services/LanguageService.cs +++ b/src/DevHive.Services/Services/LanguageService.cs @@ -2,25 +2,25 @@ using System; using System.Threading.Tasks; using AutoMapper; using DevHive.Data.Models; -using DevHive.Data.Repositories.Contracts; +using DevHive.Data.Repositories; using DevHive.Services.Models.Language; namespace DevHive.Services.Services { public class LanguageService { - private readonly ILanguageRepository _languageRepository; + private readonly LanguageRepository _languageRepository; private readonly IMapper _languageMapper; - public LanguageService(ILanguageRepository languageRepository, IMapper mapper) + public LanguageService(LanguageRepository languageRepository, IMapper mapper) { this._languageRepository = languageRepository; this._languageMapper = mapper; } - public async Task CreateLanguage(LanguageServiceModel languageServiceModel) + public async Task CreateLanguage(CreateLanguageServiceModel languageServiceModel) { - if (await this._languageRepository.DoesLanguageNameExist(languageServiceModel.Name)) + if (await this._languageRepository.DoesLanguageNameExistAsync(languageServiceModel.Name)) throw new ArgumentException("Language already exists!"); Language language = this._languageMapper.Map(languageServiceModel); @@ -33,7 +33,7 @@ namespace DevHive.Services.Services { Language language = await this._languageRepository.GetByIdAsync(id); - if(language == null) + if (language == null) throw new ArgumentException("The language does not exist"); return this._languageMapper.Map(language); @@ -41,28 +41,28 @@ namespace DevHive.Services.Services public async Task UpdateLanguage(UpdateLanguageServiceModel languageServiceModel) { - if (!await this._languageRepository.DoesLanguageExist(languageServiceModel.Id)) - throw new ArgumentException("Language does not exist!"); + Task langExist = this._languageRepository.DoesLanguageExistAsync(languageServiceModel.Id); + Task newLangNameExists = this._languageRepository.DoesLanguageNameExistAsync(languageServiceModel.Name); - if (await this._languageRepository.DoesLanguageNameExist(languageServiceModel.Name)) - throw new ArgumentException("Language name already exists!"); + await Task.WhenAny(langExist, newLangNameExists); - Language language = this._languageMapper.Map(languageServiceModel); - //language.Id = languageServiceModel.Id; - bool result = await this._languageRepository.EditAsync(language); + if (!langExist.Result) + throw new ArgumentException("Language already exists!"); - return result; + if (newLangNameExists.Result) + throw new ArgumentException("This name is already in our datbase!"); + + Language lang = this._languageMapper.Map(languageServiceModel); + return await this._languageRepository.EditAsync(lang); } - + public async Task DeleteLanguage(Guid id) { - if (!await this._languageRepository.DoesLanguageExist(id)) + if (!await this._languageRepository.DoesLanguageExistAsync(id)) throw new ArgumentException("Language does not exist!"); Language language = await this._languageRepository.GetByIdAsync(id); - bool result = await this._languageRepository.DeleteAsync(language); - - return result; + return await this._languageRepository.DeleteAsync(language); } } } \ No newline at end of file diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index e0a2be9..321c694 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -7,17 +7,17 @@ using DevHive.Services.Models.Post.Comment; using DevHive.Services.Models.Post.Post; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; -using DevHive.Data.Repositories.Contracts; +using DevHive.Data.Repositories; namespace DevHive.Services.Services { public class PostService { - private readonly IPostRepository _postRepository; - private readonly IUserRepository _userRepository; + private readonly PostRepository _postRepository; + private readonly UserRepository _userRepository; private readonly IMapper _postMapper; - public PostService(IPostRepository postRepository, IUserRepository userRepository , IMapper postMapper) + public PostService(PostRepository postRepository, UserRepository userRepository , IMapper postMapper) { this._postRepository = postRepository; this._userRepository = userRepository; diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index 372984d..7ba0a98 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -3,16 +3,16 @@ using System.Threading.Tasks; using AutoMapper; using DevHive.Common.Models.Identity; using DevHive.Data.Models; -using DevHive.Data.Repositories.Contracts; +using DevHive.Data.Repositories; namespace DevHive.Services.Services { public class RoleService { - private readonly IRoleRepository _roleRepository; + private readonly RoleRepository _roleRepository; private readonly IMapper _roleMapper; - public RoleService(IRoleRepository roleRepository, IMapper mapper) + public RoleService(RoleRepository roleRepository, IMapper mapper) { this._roleRepository = roleRepository; this._roleMapper = mapper; diff --git a/src/DevHive.Services/Services/TechnologyService.cs b/src/DevHive.Services/Services/TechnologyService.cs index e03d4d1..2913a55 100644 --- a/src/DevHive.Services/Services/TechnologyService.cs +++ b/src/DevHive.Services/Services/TechnologyService.cs @@ -2,17 +2,17 @@ using System; using System.Threading.Tasks; using AutoMapper; using DevHive.Data.Models; -using DevHive.Data.Repositories.Contracts; +using DevHive.Data.Repositories; using DevHive.Services.Models.Technology; namespace DevHive.Services.Services { public class TechnologyService { - private readonly ITechnologyRepository _technologyRepository; + private readonly TechnologyRepository _technologyRepository; private readonly IMapper _technologyMapper; - public TechnologyService(ITechnologyRepository technologyRepository, IMapper technologyMapper) + public TechnologyService(TechnologyRepository technologyRepository, IMapper technologyMapper) { this._technologyRepository = technologyRepository; this._technologyMapper = technologyMapper; diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index c8bcab9..e1f925d 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -11,28 +11,40 @@ using System.Security.Cryptography; using System.Text; using System.Collections.Generic; using DevHive.Common.Models.Identity; -using DevHive.Data.Repositories.Contracts; +using DevHive.Services.Models.Language; +using DevHive.Data.Repositories; namespace DevHive.Services.Services { public class UserService { - private readonly IUserRepository _userRepository; - private readonly IRoleRepository _roleRepository; + private readonly UserRepository _userRepository; + private readonly RoleRepository _roleRepository; + private readonly LanguageRepository _languageRepository; + private readonly TechnologyRepository _technologyRepository; private readonly IMapper _userMapper; private readonly JWTOptions _jwtOptions; - public UserService(IUserRepository userRepository, IRoleRepository roleRepository, IMapper mapper, JWTOptions jwtOptions) + public UserService(UserRepository userRepository, + LanguageRepository languageRepository, + RoleRepository roleRepository, + TechnologyRepository technologyRepository, + IMapper mapper, + JWTOptions jwtOptions) { this._userRepository = userRepository; this._roleRepository = roleRepository; this._userMapper = mapper; this._jwtOptions = jwtOptions; + this._languageRepository = languageRepository; + this._technologyRepository = technologyRepository; } + #region Authentication + public async Task LoginUser(LoginServiceModel loginModel) { - if (!await this._userRepository.DoesUsernameExist(loginModel.UserName)) + if (!await this._userRepository.DoesUsernameExistAsync(loginModel.UserName)) throw new ArgumentException("Invalid username!"); User user = await this._userRepository.GetByUsername(loginModel.UserName); @@ -45,10 +57,10 @@ namespace DevHive.Services.Services public async Task RegisterUser(RegisterServiceModel registerModel) { - if (await this._userRepository.DoesUsernameExist(registerModel.UserName)) + if (await this._userRepository.DoesUsernameExistAsync(registerModel.UserName)) throw new ArgumentException("Username already exists!"); - if (await this._userRepository.DoesEmailExist(registerModel.Email)) + if (await this._userRepository.DoesEmailExistAsync(registerModel.Email)) throw new ArgumentException("Email already exists!"); User user = this._userMapper.Map(registerModel); @@ -66,20 +78,58 @@ namespace DevHive.Services.Services return new TokenModel(WriteJWTSecurityToken(user.UserName, user.Roles)); } + #endregion + + #region Create public async Task AddFriend(Guid userId, Guid friendId) { - User user = await this._userRepository.GetByIdAsync(userId); - User friend = await this._userRepository.GetByIdAsync(friendId); + Task userExists = this._userRepository.DoesUserExistAsync(userId); + Task friendExists = this._userRepository.DoesUserExistAsync(friendId); + + await Task.WhenAll(userExists, friendExists); + + if (!userExists.Result) + throw new ArgumentException("User doesn't exist!"); - if (DoesUserHaveThisFriend(user, friend)) + if (!friendExists.Result) + throw new ArgumentException("Friend doesn't exist!"); + + if (await this._userRepository.DoesUserHaveThisFriendAsync(userId, friendId)) throw new ArgumentException("Friend already exists in your friends list."); - return user != default(User) && friend != default(User) ? - await this._userRepository.AddFriendAsync(user, friend) : + User user = await this._userRepository.GetByIdAsync(userId); + User friend = await this._userRepository.GetByIdAsync(friendId); + + return user != default(User) && friend != default(User) ? + await this._userRepository.AddFriendAsync(user, friend) : throw new ArgumentException("Invalid user!"); } + public async Task AddLanguageToUser(Guid userId, LanguageServiceModel languageServiceModel) + { + Task userExists = this._userRepository.DoesUserExistAsync(userId); + Task languageExists = this._languageRepository.DoesLanguageExistAsync(languageServiceModel.Id); + + await Task.WhenAll(userExists, languageExists); + + if (!userExists.Result) + throw new ArgumentException("User does not exist!"); + + if (!languageExists.Result) + throw new ArgumentException("Language does not exist!"); + + Task user = this._userRepository.GetByIdAsync(userId); + Task language = this._languageRepository.GetByIdAsync(languageServiceModel.Id); + + await Task.WhenAll(user, language); + + return await this._userRepository.AddLanguageToUserAsync(user.Result, language.Result); + } + #endregion + + #region Read + public async Task GetUserById(Guid id) { User user = await this._userRepository.GetByIdAsync(id) @@ -90,21 +140,24 @@ namespace DevHive.Services.Services public async Task GetFriendById(Guid friendId) { - if(!_userRepository.DoesUserExist(friendId)) + if (!await _userRepository.DoesUserExistAsync(friendId)) throw new ArgumentException("User does not exist!"); User friend = await this._userRepository.GetByIdAsync(friendId); return this._userMapper.Map(friend); } + #endregion + + #region Update public async Task UpdateUser(UpdateUserServiceModel updateModel) { - if (!this._userRepository.DoesUserExist(updateModel.Id)) + if (!await this._userRepository.DoesUserExistAsync(updateModel.Id)) throw new ArgumentException("User does not exist!"); if (!this._userRepository.DoesUserHaveThisUsername(updateModel.Id, updateModel.UserName) - && await this._userRepository.DoesUsernameExist(updateModel.UserName)) + && await this._userRepository.DoesUsernameExistAsync(updateModel.UserName)) throw new ArgumentException("Username already exists!"); User user = this._userMapper.Map(updateModel); @@ -113,12 +166,15 @@ namespace DevHive.Services.Services if (!result) throw new InvalidOperationException("Unable to edit user!"); - return this._userMapper.Map(user);; + return this._userMapper.Map(user); ; } + #endregion + + #region Delete public async Task DeleteUser(Guid id) { - if (!this._userRepository.DoesUserExist(id)) + if (!await this._userRepository.DoesUserExistAsync(id)) throw new ArgumentException("User does not exist!"); User user = await this._userRepository.GetByIdAsync(id); @@ -130,21 +186,25 @@ namespace DevHive.Services.Services public async Task RemoveFriend(Guid userId, Guid friendId) { - if(!this._userRepository.DoesUserExist(userId) && - !this._userRepository.DoesUserExist(friendId)) - throw new ArgumentException("Invalid user!"); + Task userExists = this._userRepository.DoesUserExistAsync(userId); + Task friendExists = this._userRepository.DoesUserExistAsync(friendId); - User user = await this._userRepository.GetByIdAsync(userId); - User friend = await this._userRepository.GetByIdAsync(friendId); + await Task.WhenAll(userExists, friendExists); + + if (!userExists.Result) + throw new ArgumentException("User doesn't exist!"); - if(!this.DoesUserHaveFriends(user)) - throw new ArgumentException("User does not have any friends."); + if (!friendExists.Result) + throw new ArgumentException("Friend doesn't exist!"); - if (!DoesUserHaveThisFriend(user, friend)) + if (!await this._userRepository.DoesUserHaveThisFriendAsync(userId, friendId)) throw new ArgumentException("This ain't your friend, amigo."); - return await this.RemoveFriend(user.Id, friendId); + return await this.RemoveFriend(userId, friendId); } + #endregion + + #region Validations public async Task ValidJWT(Guid id, string rawTokenData) { @@ -153,7 +213,7 @@ namespace DevHive.Services.Services string jwtUserName = this.GetClaimTypeValues("unique_name", jwt.Claims)[0]; List jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims); - + User user = await this._userRepository.GetByUsername(jwtUserName) ?? throw new ArgumentException("User does not exist!"); @@ -164,9 +224,9 @@ namespace DevHive.Services.Services return false; /* Check roles */ - + // Check if jwt contains all user roles (if it doesn't, jwt is either old or tampered with) - foreach(var role in user.Roles) + foreach (var role in user.Roles) { if (!jwtRoleNames.Contains(role.Name)) return false; @@ -179,26 +239,11 @@ namespace DevHive.Services.Services return true; } - private string GeneratePasswordHash(string password) - { - return string.Join(string.Empty, SHA512.HashData(Encoding.ASCII.GetBytes(password))); - } - - private bool DoesUserHaveThisFriend(User user, User friend) - { - return user.Friends.Contains(friend); - } - - private bool DoesUserHaveFriends(User user) - { - return user.Friends.Count >= 1; - } - private List GetClaimTypeValues(string type, IEnumerable claims) { List toReturn = new(); - - foreach(var claim in claims) + + foreach (var claim in claims) if (claim.Type == type) toReturn.Add(claim.Value); @@ -214,7 +259,7 @@ namespace DevHive.Services.Services new Claim(ClaimTypes.Name, userName), }; - foreach(var role in roles) + foreach (var role in roles) { claims.Add(new Claim(ClaimTypes.Role, role.Name)); } @@ -232,5 +277,12 @@ namespace DevHive.Services.Services SecurityToken token = tokenHandler.CreateToken(tokenDescriptor); return tokenHandler.WriteToken(token); } + + private string GeneratePasswordHash(string password) + { + return string.Join(string.Empty, SHA512.HashData(Encoding.ASCII.GetBytes(password))); + } + + #endregion } } diff --git a/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs b/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs index ee0ca4b..beac798 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs @@ -34,7 +34,7 @@ namespace DevHive.Data.Tests this.Context.Database.EnsureDeleted(); } - #region AddAync + #region AddAsync [Test] public void AddAsync_AddsTheGivenTechnologyToTheDatabase() { @@ -127,7 +127,7 @@ namespace DevHive.Data.Tests { bool result = await this.TechnologyRepository.DoesTechnologyNameExist(TECHNOLOGY_NAME); - Assert.False(result, "DoesTechnologyNameExist returns true when tehcnology name does not exist"); + Assert.False(result, "DoesTechnologyNameExist returns true when technology name does not exist"); }).GetAwaiter().GetResult(); } #endregion diff --git a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs index 5715b13..bae8562 100644 --- a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs @@ -9,9 +9,12 @@ namespace DevHive.Web.Configurations.Mapping public LanguageMappings() { CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } \ No newline at end of file diff --git a/src/DevHive.Web/Controllers/LanguageController.cs b/src/DevHive.Web/Controllers/LanguageController.cs index d71d622..29c1e99 100644 --- a/src/DevHive.Web/Controllers/LanguageController.cs +++ b/src/DevHive.Web/Controllers/LanguageController.cs @@ -23,9 +23,9 @@ namespace DevHive.Web.Controllers } [HttpPost] - public async Task Create([FromBody] LanguageWebModel languageWebModel) + public async Task Create([FromBody] CreateLanguageWebModel createLanguageWebModel) { - LanguageServiceModel languageServiceModel = this._languageMapper.Map(languageWebModel); + CreateLanguageServiceModel languageServiceModel = this._languageMapper.Map(createLanguageWebModel); bool result = await this._languageService.CreateLanguage(languageServiceModel); diff --git a/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs b/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs new file mode 100644 index 0000000..111beed --- /dev/null +++ b/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Web.Models.Language +{ + public class CreateLanguageWebModel : LanguageWebModel + { + public string Name { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Language/LanguageWebModel.cs b/src/DevHive.Web/Models/Language/LanguageWebModel.cs index 1ec38f3..455b559 100644 --- a/src/DevHive.Web/Models/Language/LanguageWebModel.cs +++ b/src/DevHive.Web/Models/Language/LanguageWebModel.cs @@ -1,7 +1,9 @@ +using System; + namespace DevHive.Web.Models.Language { public class LanguageWebModel { - public string Name { get; set; } + public Guid Id { get; set; } } } \ No newline at end of file diff --git a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs index 26eb6c2..2da8217 100644 --- a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs +++ b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs @@ -2,8 +2,5 @@ using System; namespace DevHive.Web.Models.Language { - public class UpdateLanguageWebModel : LanguageWebModel - { - public Guid Id { get; set; } - } + public class UpdateLanguageWebModel : CreateLanguageWebModel {} } \ No newline at end of file -- cgit v1.2.3 From 61c51944844ed404cd4f174440d6e81b2a8591ba Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 13 Jan 2021 23:06:18 +0200 Subject: Fixed sln-wide code formatting --- .../Configurations/Extensions/ConfigureDatabase.cs | 4 +- .../Configurations/Mapping/CommentMappings.cs | 4 +- .../Configurations/Mapping/LanguageMappings.cs | 6 +- .../Configurations/Mapping/RoleMappings.cs | 6 +- .../Configurations/Mapping/TechnologyMappings.cs | 6 +- .../Configurations/Mapping/UserMappings.cs | 2 +- src/DevHive.Web/Controllers/ErrorController.cs | 2 +- src/DevHive.Web/Controllers/LanguageController.cs | 12 +- src/DevHive.Web/Controllers/PostController.cs | 12 +- src/DevHive.Web/Controllers/RoleController.cs | 8 +- .../Controllers/TechnologyController.cs | 12 +- src/DevHive.Web/Controllers/UserController.cs | 6 +- .../Models/Identity/User/BaseUserWebModel.cs | 4 +- .../Models/Identity/User/LoginWebModel.cs | 4 +- .../Models/Identity/User/RegisterWebModel.cs | 4 +- .../Models/Identity/User/UpdateUserWebModel.cs | 2 +- .../Models/Identity/User/UserWebModel.cs | 2 +- .../Validation/GoodPasswordModelValidation.cs | 2 +- .../Models/Language/UpdateLanguageWebModel.cs | 4 +- src/DevHive.Web/Startup.cs | 142 ++++++++++----------- 20 files changed, 122 insertions(+), 122 deletions(-) (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs index b42ae05..4831435 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs @@ -20,7 +20,7 @@ namespace DevHive.Web.Configurations.Extensions services.AddIdentity() .AddEntityFrameworkStores(); - + services.Configure(options => { options.User.RequireUniqueEmail = true; @@ -41,7 +41,7 @@ namespace DevHive.Web.Configurations.Extensions services.AddAuthorization(options => { - options.AddPolicy("User", options => + options.AddPolicy("User", options => { options.RequireAuthenticatedUser(); options.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme); diff --git a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs index 394490e..5998e7a 100644 --- a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs @@ -13,5 +13,5 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap(); } - } -} \ No newline at end of file + } +} diff --git a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs index bae8562..3c2a4d0 100644 --- a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs @@ -4,7 +4,7 @@ using DevHive.Services.Models.Language; namespace DevHive.Web.Configurations.Mapping { - public class LanguageMappings : Profile + public class LanguageMappings : Profile { public LanguageMappings() { @@ -16,5 +16,5 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap(); } - } -} \ No newline at end of file + } +} diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 5d33c56..afa3a94 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -4,15 +4,15 @@ using DevHive.Common.Models.Identity; namespace DevHive.Web.Configurations.Mapping { - public class RoleMappings : Profile + public class RoleMappings : Profile { public RoleMappings() { CreateMap(); CreateMap(); - + CreateMap(); CreateMap(); } - } + } } diff --git a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs index 849e47f..8523897 100644 --- a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs @@ -4,7 +4,7 @@ using DevHive.Services.Models.Technology; namespace DevHive.Web.Configurations.Mapping { - public class TechnologyMappings : Profile + public class TechnologyMappings : Profile { public TechnologyMappings() { @@ -13,5 +13,5 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap(); } - } -} \ No newline at end of file + } +} diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index 4420368..59003ea 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -18,5 +18,5 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); } - } + } } diff --git a/src/DevHive.Web/Controllers/ErrorController.cs b/src/DevHive.Web/Controllers/ErrorController.cs index c3f1e55..b187501 100644 --- a/src/DevHive.Web/Controllers/ErrorController.cs +++ b/src/DevHive.Web/Controllers/ErrorController.cs @@ -19,7 +19,7 @@ namespace DevHive.Web.Controllers IExceptionHandlerFeature exception = HttpContext.Features.Get(); - + object result = ProcessException(requestId, exception); return new BadRequestObjectResult(JsonConvert.SerializeObject(result)); } diff --git a/src/DevHive.Web/Controllers/LanguageController.cs b/src/DevHive.Web/Controllers/LanguageController.cs index 29c1e99..486e16e 100644 --- a/src/DevHive.Web/Controllers/LanguageController.cs +++ b/src/DevHive.Web/Controllers/LanguageController.cs @@ -29,7 +29,7 @@ namespace DevHive.Web.Controllers bool result = await this._languageService.CreateLanguage(languageServiceModel); - if(!result) + if (!result) return new BadRequestObjectResult("Could not create Language"); return new OkResult(); @@ -52,21 +52,21 @@ namespace DevHive.Web.Controllers bool result = await this._languageService.UpdateLanguage(updatelanguageServiceModel); - if(!result) + if (!result) return new BadRequestObjectResult("Could not update Language"); return new OkResult(); } - + [HttpDelete] public async Task Delete(Guid id) { bool result = await this._languageService.DeleteLanguage(id); - - if(!result) + + if (!result) return new BadRequestObjectResult("Could not delete Language"); return new OkResult(); } } -} \ No newline at end of file +} diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs index 753897c..a906e47 100644 --- a/src/DevHive.Web/Controllers/PostController.cs +++ b/src/DevHive.Web/Controllers/PostController.cs @@ -30,9 +30,9 @@ namespace DevHive.Web.Controllers [HttpPost] public async Task Create([FromBody] CreatePostWebModel createPostModel) { - CreatePostServiceModel postServiceModel = - this._postMapper.Map(createPostModel); - + CreatePostServiceModel postServiceModel = + this._postMapper.Map(createPostModel); + bool result = await this._postService.CreatePost(postServiceModel); if (!result) @@ -49,7 +49,7 @@ namespace DevHive.Web.Controllers bool result = await this._postService.AddComment(createCommentServiceModel); - if(!result) + if (!result) return new BadRequestObjectResult("Could not create the Comment"); return new OkResult(); @@ -81,7 +81,7 @@ namespace DevHive.Web.Controllers [HttpPut] public async Task Update(Guid id, [FromBody] UpdatePostWebModel updatePostModel) { - UpdatePostServiceModel postServiceModel = + UpdatePostServiceModel postServiceModel = this._postMapper.Map(updatePostModel); postServiceModel.IssuerId = id; @@ -129,7 +129,7 @@ namespace DevHive.Web.Controllers { if (!await this._postService.ValidateJwtForComment(id, authorization)) return new UnauthorizedResult(); - + bool result = await this._postService.DeleteComment(id); if (!result) diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index d710f5a..0a8f7a1 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -26,9 +26,9 @@ namespace DevHive.Web.Controllers [HttpPost] public async Task Create([FromBody] CreateRoleModel createRoleModel) { - RoleModel roleServiceModel = - this._roleMapper.Map(createRoleModel); - + RoleModel roleServiceModel = + this._roleMapper.Map(createRoleModel); + bool result = await this._roleService.CreateRole(roleServiceModel); if (!result) @@ -49,7 +49,7 @@ namespace DevHive.Web.Controllers [HttpPut] public async Task Update(Guid id, [FromBody] UpdateRoleModel updateRoleModel) { - RoleModel roleServiceModel = + RoleModel roleServiceModel = this._roleMapper.Map(updateRoleModel); roleServiceModel.Id = id; diff --git a/src/DevHive.Web/Controllers/TechnologyController.cs b/src/DevHive.Web/Controllers/TechnologyController.cs index e02ca3d..905a71d 100644 --- a/src/DevHive.Web/Controllers/TechnologyController.cs +++ b/src/DevHive.Web/Controllers/TechnologyController.cs @@ -29,7 +29,7 @@ namespace DevHive.Web.Controllers bool result = await this._technologyService.Create(technologyServiceModel); - if(!result) + if (!result) return new BadRequestObjectResult("Could not create the Technology"); return new OkResult(); @@ -51,21 +51,21 @@ namespace DevHive.Web.Controllers bool result = await this._technologyService.UpdateTechnology(updateTechnologyWebModel); - if(!result) + if (!result) return new BadRequestObjectResult("Could not update Technology"); return new OkResult(); } - + [HttpDelete] public async Task Delete(Guid id) { bool result = await this._technologyService.DeleteTechnology(id); - - if(!result) + + if (!result) return new BadRequestObjectResult("Could not delete Technology"); return new OkResult(); } } -} \ No newline at end of file +} diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 0960915..26271b2 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -20,7 +20,7 @@ namespace DevHive.Web.Controllers [ApiController] [Route("/api/[controller]")] [Authorize(Roles = "User")] - public class UserController: ControllerBase + public class UserController : ControllerBase { private readonly UserService _userService; private readonly IMapper _userMapper; @@ -154,7 +154,7 @@ namespace DevHive.Web.Controllers await this._userService.RemoveFriend(userId, friendId); return new OkResult(); } - + [HttpDelete] [Route("RemoveLanguageFromUser")] public async Task RemoveLanguageFromUser(Guid userId, [FromBody] LanguageWebModel languageWebModel) @@ -176,7 +176,7 @@ namespace DevHive.Web.Controllers new OkResult() : new BadRequestResult(); } - + #endregion } } diff --git a/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs index ff9fac5..2d99786 100644 --- a/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs @@ -1,9 +1,9 @@ using System.ComponentModel.DataAnnotations; using DevHive.Web.Models.Identity.Validation; -namespace DevHive.Web.Models.Identity.User +namespace DevHive.Web.Models.Identity.User { - public class BaseUserWebModel + public class BaseUserWebModel { [Required] [MinLength(3)] diff --git a/src/DevHive.Web/Models/Identity/User/LoginWebModel.cs b/src/DevHive.Web/Models/Identity/User/LoginWebModel.cs index 3bd7428..87c7416 100644 --- a/src/DevHive.Web/Models/Identity/User/LoginWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/LoginWebModel.cs @@ -1,6 +1,6 @@ -namespace DevHive.Web.Models.Identity.User +namespace DevHive.Web.Models.Identity.User { - public class LoginWebModel + public class LoginWebModel { public string UserName { get; set; } public string Password { get; set; } diff --git a/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs b/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs index 22b178b..273c2d3 100644 --- a/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs @@ -1,9 +1,9 @@ using System.ComponentModel.DataAnnotations; using DevHive.Web.Models.Identity.Validation; -namespace DevHive.Web.Models.Identity.User +namespace DevHive.Web.Models.Identity.User { - public class RegisterWebModel : BaseUserWebModel + public class RegisterWebModel : BaseUserWebModel { [Required] [GoodPassword] diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index fbe02a5..91fbc64 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -1,7 +1,7 @@ using System.ComponentModel.DataAnnotations; using DevHive.Web.Models.Identity.Validation; -namespace DevHive.Web.Models.Identity.User +namespace DevHive.Web.Models.Identity.User { public class UpdateUserWebModel : BaseUserWebModel { diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 260d34c..8f7995c 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -4,7 +4,7 @@ using DevHive.Web.Models.Identity.Role; using DevHive.Web.Models.Language; using DevHive.Web.Models.Technology; -namespace DevHive.Web.Models.Identity.User +namespace DevHive.Web.Models.Identity.User { public class UserWebModel : BaseUserWebModel { diff --git a/src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs b/src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs index f69121a..f920c35 100644 --- a/src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs +++ b/src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs @@ -3,7 +3,7 @@ using System.ComponentModel.DataAnnotations; namespace DevHive.Web.Models.Identity.Validation { - public class GoodPassword : ValidationAttribute + public class GoodPassword : ValidationAttribute { public override bool IsValid(object value) { diff --git a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs index 2da8217..deca0fc 100644 --- a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs +++ b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs @@ -2,5 +2,5 @@ using System; namespace DevHive.Web.Models.Language { - public class UpdateLanguageWebModel : CreateLanguageWebModel {} -} \ No newline at end of file + public class UpdateLanguageWebModel : CreateLanguageWebModel { } +} diff --git a/src/DevHive.Web/Startup.cs b/src/DevHive.Web/Startup.cs index 96ab318..94aabe8 100644 --- a/src/DevHive.Web/Startup.cs +++ b/src/DevHive.Web/Startup.cs @@ -1,71 +1,71 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using DevHive.Web.Configurations.Extensions; -using Newtonsoft.Json; - -namespace DevHive.Web -{ - public class Startup - { - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - public IConfiguration Configuration { get; } - - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) - { - services.AddCors(); - - services.AddControllers() - .AddNewtonsoftJson(x => - { - x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; - }); - - services.DatabaseConfiguration(Configuration); - services.SwaggerConfiguration(); - services.JWTConfiguration(Configuration); - services.AutoMapperConfiguration(); - services.DependencyInjectionConfiguration(); - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - app.UseCors(x => x - .AllowAnyMethod() - .AllowAnyHeader() - .SetIsOriginAllowed(origin => true) // allow any origin - .AllowCredentials()); // allow credentials - - if (env.IsDevelopment()) - { - //app.UseDeveloperExceptionPage(); - app.UseExceptionHandler("/api/Error"); - app.UseSwaggerConfiguration(); - } - else - { - app.UseExceptionHandler("/api/Error"); - app.UseHsts(); - } - - app.UseDatabaseConfiguration(); - app.UseAutoMapperConfiguration(); - - app.UseEndpoints(endpoints => - { - endpoints.MapControllerRoute( - name: "default", - pattern: "api/{controller}/{action}" - ); - }); - } - } -} +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using DevHive.Web.Configurations.Extensions; +using Newtonsoft.Json; + +namespace DevHive.Web +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddCors(); + + services.AddControllers() + .AddNewtonsoftJson(x => + { + x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; + }); + + services.DatabaseConfiguration(Configuration); + services.SwaggerConfiguration(); + services.JWTConfiguration(Configuration); + services.AutoMapperConfiguration(); + services.DependencyInjectionConfiguration(); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + app.UseCors(x => x + .AllowAnyMethod() + .AllowAnyHeader() + .SetIsOriginAllowed(origin => true) // allow any origin + .AllowCredentials()); // allow credentials + + if (env.IsDevelopment()) + { + //app.UseDeveloperExceptionPage(); + app.UseExceptionHandler("/api/Error"); + app.UseSwaggerConfiguration(); + } + else + { + app.UseExceptionHandler("/api/Error"); + app.UseHsts(); + } + + app.UseDatabaseConfiguration(); + app.UseAutoMapperConfiguration(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllerRoute( + name: "default", + pattern: "api/{controller}/{action}" + ); + }); + } + } +} -- cgit v1.2.3 From d77add41838823ad50caf8d1ae5ee8e61c1bb26b Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 13 Jan 2021 23:16:55 +0200 Subject: Fixed technology mappings --- src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs | 6 ++++-- src/DevHive.Services/Models/Technology/TechnologyServiceModel.cs | 2 +- src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs | 7 +++++-- 3 files changed, 10 insertions(+), 5 deletions(-) (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs index 5446f3e..667b071 100644 --- a/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs @@ -8,9 +8,11 @@ namespace DevHive.Services.Configurations.Mapping { public TechnologyMappings() { + CreateMap(); + CreateMap(); CreateMap(); + CreateMap(); - CreateMap(); } } -} \ No newline at end of file +} diff --git a/src/DevHive.Services/Models/Technology/TechnologyServiceModel.cs b/src/DevHive.Services/Models/Technology/TechnologyServiceModel.cs index afea6cf..cb5c881 100644 --- a/src/DevHive.Services/Models/Technology/TechnologyServiceModel.cs +++ b/src/DevHive.Services/Models/Technology/TechnologyServiceModel.cs @@ -6,4 +6,4 @@ namespace DevHive.Services.Models.Technology { public Guid Id { get; set; } } -} \ No newline at end of file +} diff --git a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs index 8523897..828dac1 100644 --- a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs @@ -8,10 +8,13 @@ namespace DevHive.Web.Configurations.Mapping { public TechnologyMappings() { - CreateMap(); + CreateMap(); CreateMap(); - CreateMap(); CreateMap(); + + CreateMap(); + CreateMap(); + CreateMap(); } } } -- cgit v1.2.3 From b4454f83c3b7d668fc8b18714a659b91576882be Mon Sep 17 00:00:00 2001 From: transtrike Date: Sun, 17 Jan 2021 12:55:32 +0200 Subject: Lang layer working --- src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs | 4 +++- src/DevHive.Services/Interfaces/ILanguageService.cs | 2 +- src/DevHive.Services/Models/Language/ReadLanguageServiceModel.cs | 7 +++++++ src/DevHive.Services/Services/LanguageService.cs | 4 ++-- src/DevHive.Services/Services/UserService.cs | 3 --- src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs | 2 ++ src/DevHive.Web/Controllers/LanguageController.cs | 4 ++-- src/DevHive.Web/Controllers/UserController.cs | 2 +- src/DevHive.Web/Models/Language/ReadLanguageWebModel.cs | 7 +++++++ 9 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 src/DevHive.Services/Models/Language/ReadLanguageServiceModel.cs create mode 100644 src/DevHive.Web/Models/Language/ReadLanguageWebModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs index e483fff..9c572df 100644 --- a/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs @@ -9,12 +9,14 @@ namespace DevHive.Services.Configurations.Mapping public LanguageMappings() { CreateMap(); + CreateMap(); CreateMap(); CreateMap(); CreateMap(); + CreateMap(); CreateMap(); CreateMap(); } } -} \ No newline at end of file +} diff --git a/src/DevHive.Services/Interfaces/ILanguageService.cs b/src/DevHive.Services/Interfaces/ILanguageService.cs index 1b39dfb..4d16ea3 100644 --- a/src/DevHive.Services/Interfaces/ILanguageService.cs +++ b/src/DevHive.Services/Interfaces/ILanguageService.cs @@ -8,7 +8,7 @@ namespace DevHive.Services.Interfaces { Task CreateLanguage(CreateLanguageServiceModel createLanguageServiceModel); - Task GetLanguageById(Guid id); + Task GetLanguageById(Guid id); Task UpdateLanguage(UpdateLanguageServiceModel languageServiceModel); diff --git a/src/DevHive.Services/Models/Language/ReadLanguageServiceModel.cs b/src/DevHive.Services/Models/Language/ReadLanguageServiceModel.cs new file mode 100644 index 0000000..653444e --- /dev/null +++ b/src/DevHive.Services/Models/Language/ReadLanguageServiceModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Services.Models.Language +{ + public class ReadLanguageServiceModel + { + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Services/LanguageService.cs b/src/DevHive.Services/Services/LanguageService.cs index 12e230e..f457a31 100644 --- a/src/DevHive.Services/Services/LanguageService.cs +++ b/src/DevHive.Services/Services/LanguageService.cs @@ -35,14 +35,14 @@ namespace DevHive.Services.Services #region Read - public async Task GetLanguageById(Guid id) + public async Task GetLanguageById(Guid id) { Language language = await this._languageRepository.GetByIdAsync(id); if (language == null) throw new ArgumentException("The language does not exist"); - return this._languageMapper.Map(language); + return this._languageMapper.Map(language); } #endregion diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index cbcb6b3..1dc1bd5 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -11,10 +11,7 @@ using System.Security.Cryptography; using System.Text; using System.Collections.Generic; using DevHive.Common.Models.Identity; -using DevHive.Services.Models.Language; using DevHive.Services.Interfaces; -using DevHive.Services.Models.Technology; -using DevHive.Data.Repositories; using DevHive.Data.Interfaces.Repositories; namespace DevHive.Services.Services diff --git a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs index 3c2a4d0..8cac3ca 100644 --- a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs @@ -9,10 +9,12 @@ namespace DevHive.Web.Configurations.Mapping public LanguageMappings() { CreateMap(); + CreateMap(); CreateMap(); CreateMap(); CreateMap(); + CreateMap(); CreateMap(); CreateMap(); } diff --git a/src/DevHive.Web/Controllers/LanguageController.cs b/src/DevHive.Web/Controllers/LanguageController.cs index 784e535..bbac409 100644 --- a/src/DevHive.Web/Controllers/LanguageController.cs +++ b/src/DevHive.Web/Controllers/LanguageController.cs @@ -37,8 +37,8 @@ namespace DevHive.Web.Controllers [HttpGet] public async Task GetById(Guid id) { - LanguageServiceModel languageServiceModel = await this._languageService.GetLanguageById(id); - LanguageWebModel languageWebModel = this._languageMapper.Map(languageServiceModel); + ReadLanguageServiceModel languageServiceModel = await this._languageService.GetLanguageById(id); + ReadLanguageWebModel languageWebModel = this._languageMapper.Map(languageServiceModel); return new OkObjectResult(languageWebModel); } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 8d48705..bba55e8 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -72,7 +72,7 @@ namespace DevHive.Web.Controllers } [HttpGet] - [Route("GetAFriend")] + [Route("GetFriend")] [AllowAnonymous] public async Task GetAFriend(string username) { diff --git a/src/DevHive.Web/Models/Language/ReadLanguageWebModel.cs b/src/DevHive.Web/Models/Language/ReadLanguageWebModel.cs new file mode 100644 index 0000000..f1e0ecc --- /dev/null +++ b/src/DevHive.Web/Models/Language/ReadLanguageWebModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Web.Models.Language +{ + public class ReadLanguageWebModel + { + public string Name { get; set; } + } +} -- cgit v1.2.3 From 8d1d0b40d56f90248f948e474330258bf57cf0b6 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 17 Jan 2021 14:45:48 +0200 Subject: Fixed role implementation by bringing back and improving all role models --- src/DevHive.Common/Models/Identity/RoleModel.cs | 10 -------- .../Configurations/Mapping/RoleMapings.cs | 8 +++---- src/DevHive.Services/Interfaces/IRoleService.cs | 10 ++++---- .../Models/Identity/Role/RoleServiceModel.cs | 10 ++++++++ .../Models/Identity/Role/UpdateRoleServiceModel.cs | 7 ++++++ .../Models/Identity/User/UserServiceModel.cs | 6 ++--- src/DevHive.Services/Services/RoleService.cs | 24 +++++++++++++------ .../Configurations/Mapping/RoleMappings.cs | 12 +++++----- src/DevHive.Web/Controllers/RoleController.cs | 28 +++++++++++----------- .../Models/Identity/Role/CreateRoleWebModel.cs | 2 +- .../Models/Identity/Role/RoleWebModel.cs | 2 -- .../Models/Identity/Role/UpdateRoleWebModel.cs | 2 +- 12 files changed, 68 insertions(+), 53 deletions(-) delete mode 100644 src/DevHive.Common/Models/Identity/RoleModel.cs create mode 100644 src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs create mode 100644 src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Common/Models/Identity/RoleModel.cs b/src/DevHive.Common/Models/Identity/RoleModel.cs deleted file mode 100644 index 5db8df9..0000000 --- a/src/DevHive.Common/Models/Identity/RoleModel.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; - -namespace DevHive.Common.Models.Identity -{ - public class RoleModel - { - public Guid Id { get; set; } - public string Name { get; set; } - } -} diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index 65b0b5a..4ddd253 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -1,15 +1,15 @@ using DevHive.Data.Models; using AutoMapper; -using DevHive.Common.Models.Identity; +using DevHive.Services.Models.Identity.Role; namespace DevHive.Services.Configurations.Mapping { - public class RoleMappings : Profile + public class RoleMappings : Profile { public RoleMappings() { - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Interfaces/IRoleService.cs b/src/DevHive.Services/Interfaces/IRoleService.cs index 2c7195c..a7a0e47 100644 --- a/src/DevHive.Services/Interfaces/IRoleService.cs +++ b/src/DevHive.Services/Interfaces/IRoleService.cs @@ -1,16 +1,16 @@ using System; using System.Threading.Tasks; -using DevHive.Common.Models.Identity; +using DevHive.Services.Models.Identity.Role; namespace DevHive.Services.Interfaces { - public interface IRoleService + public interface IRoleService { - Task CreateRole(RoleModel roleServiceModel); + Task CreateRole(RoleServiceModel roleServiceModel); - Task GetRoleById(Guid id); + Task GetRoleById(Guid id); - Task UpdateRole(RoleModel roleServiceModel); + Task UpdateRole(RoleServiceModel roleServiceModel); Task DeleteRole(Guid id); } diff --git a/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs new file mode 100644 index 0000000..3f834ef --- /dev/null +++ b/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Services.Models.Identity.Role +{ + public class RoleServiceModel + { + public Guid Id { get; set; } + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs new file mode 100644 index 0000000..be71771 --- /dev/null +++ b/src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Services.Models.Identity.Role +{ + public class UpdateRoleServiceModel + { + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index ecf8c42..aa77a0c 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -1,13 +1,13 @@ using System.Collections.Generic; -using DevHive.Common.Models.Identity; +using DevHive.Services.Models.Identity.Role; using DevHive.Services.Models.Language; using DevHive.Services.Models.Technology; namespace DevHive.Services.Models.Identity.User { - public class UserServiceModel : BaseUserServiceModel + public class UserServiceModel : BaseUserServiceModel { - public IList Roles { get; set; } = new List(); + public IList Roles { get; set; } = new List(); public IList Friends { get; set; } = new List(); diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index c38ac74..0945624 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -1,14 +1,15 @@ using System; using System.Threading.Tasks; using AutoMapper; -using DevHive.Common.Models.Identity; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using DevHive.Services.Interfaces; +using DevHive.Services.Models.Identity.Role; +using DevHive.Services.Models.Language; namespace DevHive.Services.Services { - public class RoleService : IRoleService + public class RoleService : IRoleService { private readonly IRoleRepository _roleRepository; private readonly IMapper _roleMapper; @@ -19,25 +20,34 @@ namespace DevHive.Services.Services this._roleMapper = mapper; } - public async Task CreateRole(RoleModel roleServiceModel) + public async Task CreateRole(RoleServiceModel roleServiceModel) { if (await this._roleRepository.DoesNameExist(roleServiceModel.Name)) throw new ArgumentException("Role already exists!"); + Role role = this._roleMapper.Map(roleServiceModel); + bool success = await this._roleRepository.AddAsync(role); + + if(success) + { + Role newRole = await this._roleRepository.GetByNameAsync(roleServiceModel.Name); + return newRole.Id; + } + else + return Guid.Empty; - return await this._roleRepository.AddAsync(role); } - public async Task GetRoleById(Guid id) + public async Task GetRoleById(Guid id) { Role role = await this._roleRepository.GetByIdAsync(id) ?? throw new ArgumentException("Role does not exist!"); - return this._roleMapper.Map(role); + return this._roleMapper.Map(role); } - public async Task UpdateRole(RoleModel roleServiceModel) + public async Task UpdateRole(RoleServiceModel roleServiceModel) { if (!await this._roleRepository.DoesRoleExist(roleServiceModel.Id)) throw new ArgumentException("Role does not exist!"); diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index afa3a94..bce7c07 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -1,18 +1,18 @@ using AutoMapper; using DevHive.Web.Models.Identity.Role; -using DevHive.Common.Models.Identity; +using DevHive.Services.Models.Identity.Role; namespace DevHive.Web.Configurations.Mapping { - public class RoleMappings : Profile + public class RoleMappings : Profile { public RoleMappings() { - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index 8ea2711..5b3dca5 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -3,12 +3,12 @@ using Microsoft.AspNetCore.Mvc; using DevHive.Web.Models.Identity.Role; using AutoMapper; using System; -using DevHive.Common.Models.Identity; using DevHive.Services.Interfaces; +using DevHive.Services.Models.Identity.Role; namespace DevHive.Web.Controllers { - [ApiController] + [ApiController] [Route("/api/[controller]")] //[Authorize(Roles = "Admin")] public class RoleController @@ -23,33 +23,33 @@ namespace DevHive.Web.Controllers } [HttpPost] - public async Task Create([FromBody] CreateRoleModel createRoleModel) + public async Task Create([FromBody] CreateRoleWebModel createRoleWebModel) { - RoleModel roleServiceModel = - this._roleMapper.Map(createRoleModel); + RoleServiceModel roleServiceModel = + this._roleMapper.Map(createRoleWebModel); - bool result = await this._roleService.CreateRole(roleServiceModel); + Guid id = await this._roleService.CreateRole(roleServiceModel); - if (!result) - return new BadRequestObjectResult("Could not create role!"); + return id == Guid.Empty ? + new BadRequestObjectResult($"Could not create role {createRoleWebModel.Name}") : + new OkObjectResult(new { Id = id }); - return new OkResult(); } [HttpGet] public async Task GetById(Guid id) { - RoleModel roleServiceModel = await this._roleService.GetRoleById(id); - RoleModel roleWebModel = this._roleMapper.Map(roleServiceModel); + RoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); + RoleWebModel roleWebModel = this._roleMapper.Map(roleServiceModel); return new OkObjectResult(roleWebModel); } [HttpPut] - public async Task Update(Guid id, [FromBody] UpdateRoleModel updateRoleModel) + public async Task Update(Guid id, [FromBody] UpdateRoleWebModel updateRoleWebModel) { - RoleModel roleServiceModel = - this._roleMapper.Map(updateRoleModel); + RoleServiceModel roleServiceModel = + this._roleMapper.Map(updateRoleWebModel); roleServiceModel.Id = id; bool result = await this._roleService.UpdateRole(roleServiceModel); diff --git a/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs index becb3c9..e872428 100644 --- a/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs @@ -1,6 +1,6 @@ namespace DevHive.Web.Models.Identity.Role { - public class CreateRoleModel + public class CreateRoleWebModel { public string Name { get; set; } } diff --git a/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs index 9e97ffc..41ade23 100644 --- a/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs @@ -1,5 +1,3 @@ -using System; - namespace DevHive.Web.Models.Identity.Role { public class RoleWebModel diff --git a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs index 1eaad57..213ec55 100644 --- a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs @@ -1,6 +1,6 @@ namespace DevHive.Web.Models.Identity.Role { - public class UpdateRoleModel : CreateRoleModel + public class UpdateRoleWebModel : CreateRoleWebModel { } } -- cgit v1.2.3 From 8f6a50566a069c782482a167f601e6eca9588e73 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 17 Jan 2021 15:30:09 +0200 Subject: User update now updates languages, roles and friends --- .../Configurations/Mapping/UserMappings.cs | 3 ++ .../User/UpdateUserCollectionServiceModel.cs | 7 +++++ .../Models/Identity/User/UpdateUserServiceModel.cs | 12 +++++++- src/DevHive.Services/Services/UserService.cs | 33 +++++++++++++++++++++- .../Configurations/Mapping/UserMappings.cs | 7 +++++ 5 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs index d57c6ba..97355d6 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs @@ -11,6 +11,9 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); CreateMap(); + CreateMap() + .ForMember(up => up.UserName, u => u.MapFrom(src => src.Name)); + CreateMap(); } diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs new file mode 100644 index 0000000..c40a980 --- /dev/null +++ b/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Services.Models.Identity.User +{ + public class UpdateUserCollectionServiceModel + { + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs index 96d1ff0..5b5a178 100644 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs @@ -1,9 +1,19 @@ using System; +using System.Collections.Generic; namespace DevHive.Services.Models.Identity.User { - public class UpdateUserServiceModel : UserServiceModel + public class UpdateUserServiceModel : BaseUserServiceModel { public Guid Id { get; set; } + + public IList Roles { get; set; } = new List(); + + public IList Friends { get; set; } = new List(); + + public IList Languages { get; set; } = new List(); + + public IList Technologies { get; set; } = new List(); + } } diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 1dc1bd5..3dd030a 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -13,6 +13,7 @@ using System.Collections.Generic; using DevHive.Common.Models.Identity; using DevHive.Services.Interfaces; using DevHive.Data.Interfaces.Repositories; +using DevHive.Services.Models.Language; namespace DevHive.Services.Services { @@ -139,7 +140,7 @@ namespace DevHive.Services.Services && await this._userRepository.DoesUsernameExistAsync(updateModel.UserName)) throw new ArgumentException("Username already exists!"); - //Add validations for everything else + await this.ValidateUserCollections(updateModel); User user = this._userMapper.Map(updateModel); bool result = await this._userRepository.EditAsync(user); @@ -149,6 +150,36 @@ namespace DevHive.Services.Services return this._userMapper.Map(user); ; } + + private async Task ValidateUserCollections(UpdateUserServiceModel updateUserServiceModel) + { + // Friends + foreach (UpdateUserCollectionServiceModel friend in updateUserServiceModel.Friends) + { + User returnedFriend = await this._userRepository.GetByUsernameAsync(friend.Name); + + if (default(User) == returnedFriend) + throw new ArgumentException($"User {friend.Name} does not exist!"); + } + + // Languages + foreach (UpdateUserCollectionServiceModel language in updateUserServiceModel.Languages) + { + Language returnedLanguage = await this._languageRepository.GetByNameAsync(language.Name); + + if (default(Language) == returnedLanguage) + throw new ArgumentException($"Language {language.Name} does not exist!"); + } + + // Technology + foreach (UpdateUserCollectionServiceModel technology in updateUserServiceModel.Technologies) + { + Technology returnedTechnology = await this._technologyRepository.GetByNameAsync(technology.Name); + + if (default(Technology) == returnedTechnology) + throw new ArgumentException($"Technology {technology.Name} does not exist!"); + } + } #endregion #region Delete diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index 59003ea..aa22ce2 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -2,6 +2,8 @@ using AutoMapper; using DevHive.Services.Models.Identity.User; using DevHive.Web.Models.Identity.User; using DevHive.Common.Models.Identity; +using DevHive.Web.Models.Language; +using DevHive.Web.Models.Technology; namespace DevHive.Web.Configurations.Mapping { @@ -17,6 +19,11 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap(); + + CreateMap() + .ForMember(f => f.Name, u => u.MapFrom(src => src.Username)); + CreateMap(); + CreateMap(); } } } -- cgit v1.2.3 From f0398cf1b7e6477bbd184e7509a1030054fc1926 Mon Sep 17 00:00:00 2001 From: transtrike Date: Sun, 17 Jan 2021 16:26:32 +0200 Subject: Fix lang naming --- src/DevHive.Data/Interfaces/Models/IUser.cs | 2 +- ...112111416_User_Implements_Languages.Designer.cs | 4 ++-- .../Migrations/DevHiveContextModelSnapshot.cs | 4 ++-- src/DevHive.Data/Models/User.cs | 4 ++-- src/DevHive.Data/Repositories/UserRepository.cs | 16 +++++++-------- .../Mapping/UserCollectionMappings.cs | 21 ++++++++++++++++++++ .../Configurations/Mapping/UserMappings.cs | 3 --- src/DevHive.Services/Services/UserService.cs | 23 ++++++++++++++-------- .../DevHive.Data.Tests/UserRepositoryTests.cs | 8 ++++---- .../Configurations/Mapping/RoleMappings.cs | 2 +- 10 files changed, 56 insertions(+), 31 deletions(-) create mode 100644 src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Data/Interfaces/Models/IUser.cs b/src/DevHive.Data/Interfaces/Models/IUser.cs index 0a770f0..ef8c927 100644 --- a/src/DevHive.Data/Interfaces/Models/IUser.cs +++ b/src/DevHive.Data/Interfaces/Models/IUser.cs @@ -8,7 +8,7 @@ namespace DevHive.Data.Interfaces.Models string FirstName { get; set; } string LastName { get; set; } string ProfilePictureUrl { get; set; } - IList Langauges { get; set; } + IList Languages { get; set; } IList Technologies { get; set; } IList Roles { get; set; } IList Friends { get; set; } diff --git a/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs b/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs index 0f1aa80..1605b5b 100644 --- a/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs +++ b/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs @@ -307,7 +307,7 @@ namespace DevHive.Data.Migrations modelBuilder.Entity("DevHive.Data.Models.Language", b => { b.HasOne("DevHive.Data.Models.User", null) - .WithMany("Langauges") + .WithMany("Languages") .HasForeignKey("UserId"); }); @@ -395,7 +395,7 @@ namespace DevHive.Data.Migrations { b.Navigation("Friends"); - b.Navigation("Langauges"); + b.Navigation("Languages"); b.Navigation("Technologies"); }); diff --git a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs index cc6d24d..7197c81 100644 --- a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs +++ b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs @@ -305,7 +305,7 @@ namespace DevHive.Data.Migrations modelBuilder.Entity("DevHive.Data.Models.Language", b => { b.HasOne("DevHive.Data.Models.User", null) - .WithMany("Langauges") + .WithMany("Languages") .HasForeignKey("UserId"); }); @@ -393,7 +393,7 @@ namespace DevHive.Data.Migrations { b.Navigation("Friends"); - b.Navigation("Langauges"); + b.Navigation("Languages"); b.Navigation("Technologies"); }); diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs index 944bf6a..31e36ac 100644 --- a/src/DevHive.Data/Models/User.cs +++ b/src/DevHive.Data/Models/User.cs @@ -18,12 +18,12 @@ namespace DevHive.Data.Models /// /// Languages that the user uses or is familiar with /// - public IList Langauges { get; set; } + public IList Languages { get; set; } /// /// Technologies that the user uses or is familiar with /// - public IList Technologies { get; set; } + public IList Technologies { get; set; } = new List(); public IList Roles { get; set; } = new List(); diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 17ca93b..6d4a0bf 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -40,7 +40,7 @@ namespace DevHive.Data.Repositories { this._context.Update(user); - user.Langauges.Add(language); + user.Languages.Add(language); return await RepositoryMethods.SaveChangesAsync(this._context); } @@ -70,7 +70,7 @@ namespace DevHive.Data.Repositories return await this._context.Users .Include(x => x.Friends) .Include(x => x.Roles) - .Include(x => x.Langauges) + .Include(x => x.Languages) .Include(x => x.Technologies) .FirstOrDefaultAsync(x => x.Id == id); } @@ -84,12 +84,12 @@ namespace DevHive.Data.Repositories public IList GetUserLanguages(User user) { - return user.Langauges; + return user.Languages; } public Language GetUserLanguage(User user, Language language) { - return user.Langauges + return user.Languages .FirstOrDefault(x => x.Id == language.Id); } @@ -123,8 +123,8 @@ namespace DevHive.Data.Repositories { this._context.Update(user); - user.Langauges.Remove(oldLang); - user.Langauges.Add(newLang); + user.Languages.Remove(oldLang); + user.Languages.Add(newLang); return await RepositoryMethods.SaveChangesAsync(this._context); } @@ -162,7 +162,7 @@ namespace DevHive.Data.Repositories { this._context.Update(user); - user.Langauges.Remove(language); + user.Languages.Remove(language); return await RepositoryMethods.SaveChangesAsync(this._context); } @@ -224,7 +224,7 @@ namespace DevHive.Data.Repositories public bool DoesUserHaveThisLanguage(User user, Language language) { - return user.Langauges.Contains(language); + return user.Languages.Contains(language); } public bool DoesUserHaveThisTechnology(User user, Technology technology) diff --git a/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs new file mode 100644 index 0000000..ee505a2 --- /dev/null +++ b/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs @@ -0,0 +1,21 @@ +using AutoMapper; +using DevHive.Data.Models; +using DevHive.Services.Models.Identity.User; + +namespace DevHive.Services.Configurations.Mapping +{ + public class UserCollectionMappings : Profile + { + public UserCollectionMappings() + { + CreateMap() + .ForMember(up => up.UserName, u => u.MapFrom(src => src.Name)); + CreateMap() + .ForMember(r => r.Name, u => u.MapFrom(src => src.Name)); + CreateMap() + .ForMember(r => r.Name, u => u.MapFrom(src => src.Name)); + CreateMap() + .ForMember(r => r.Name, u => u.MapFrom(src => src.Name)); + } + } +} diff --git a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs index 97355d6..d57c6ba 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs @@ -11,9 +11,6 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); CreateMap(); - CreateMap() - .ForMember(up => up.UserName, u => u.MapFrom(src => src.Name)); - CreateMap(); } diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 3dd030a..b549b1c 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -131,21 +131,28 @@ namespace DevHive.Services.Services #region Update - public async Task UpdateUser(UpdateUserServiceModel updateModel) + public async Task UpdateUser(UpdateUserServiceModel updateUserServiceModel) { - if (!await this._userRepository.DoesUserExistAsync(updateModel.Id)) + if (!await this._userRepository.DoesUserExistAsync(updateUserServiceModel.Id)) throw new ArgumentException("User does not exist!"); - if (!this._userRepository.DoesUserHaveThisUsername(updateModel.Id, updateModel.UserName) - && await this._userRepository.DoesUsernameExistAsync(updateModel.UserName)) + if (!this._userRepository.DoesUserHaveThisUsername(updateUserServiceModel.Id, updateUserServiceModel.UserName) + && await this._userRepository.DoesUsernameExistAsync(updateUserServiceModel.UserName)) throw new ArgumentException("Username already exists!"); - await this.ValidateUserCollections(updateModel); + await this.ValidateUserCollections(updateUserServiceModel); - User user = this._userMapper.Map(updateModel); - bool result = await this._userRepository.EditAsync(user); + //Query proper lang, tech and role and insert the full class in updateUserServiceModel + List properLanguages = new(); + foreach (UpdateUserCollectionServiceModel lang in updateUserServiceModel.Languages) + properLanguages.Add(await this._languageRepository.GetByNameAsync(lang.Name)); - if (!result) + User user = this._userMapper.Map(updateUserServiceModel); + user.Languages = properLanguages; + + bool success = await this._userRepository.EditAsync(user); + + if (!success) throw new InvalidOperationException("Unable to edit user!"); return this._userMapper.Map(user); ; diff --git a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs index 46f8aaf..b0a5b93 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs @@ -86,7 +86,7 @@ namespace DevHive.Data.Tests //Assert Assert.True(result, "The language isn't inserted properly to the database"); - Assert.True(dummyUser.Langauges.Contains(language), "The language doesn't get added properly to the user"); + Assert.True(dummyUser.Languages.Contains(language), "The language doesn't get added properly to the user"); } [Test] @@ -165,7 +165,7 @@ namespace DevHive.Data.Tests //Arrange User dummyUser = CreateDummyUser(); await this._userRepository.AddAsync(dummyUser); - IList dummyUserLanguages = dummyUser.Langauges; + IList dummyUserLanguages = dummyUser.Languages; //Act IList languages = this._userRepository.GetUserLanguages(dummyUser); @@ -229,7 +229,7 @@ namespace DevHive.Data.Tests FirstName = "Spas", LastName = "Spasov", Email = "abv@abv.bg", - Langauges = languages, + Languages = languages, Technologies = technologies, Roles = roles }; @@ -271,7 +271,7 @@ namespace DevHive.Data.Tests FirstName = "Alex", LastName = "Spiridonov", Email = "a_spiridonov@abv.bg", - Langauges = languages, + Languages = languages, Technologies = technologies, Roles = roles }; diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index bce7c07..66ae8e3 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -4,7 +4,7 @@ using DevHive.Services.Models.Identity.Role; namespace DevHive.Web.Configurations.Mapping { - public class RoleMappings : Profile + public class RoleMappings : Profile { public RoleMappings() { -- cgit v1.2.3 From cb38f51c346722fda36215eb5e631ec36103c2bf Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 19 Jan 2021 21:54:57 +0200 Subject: Fixed mapping issue --- src/DevHive.Web/Configurations/Mapping/UserMappings.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index aa22ce2..beb9607 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -21,7 +21,7 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap() - .ForMember(f => f.Name, u => u.MapFrom(src => src.Username)); + .ForMember(f => f.Name, u => u.MapFrom(src => src.UserName)); CreateMap(); CreateMap(); } -- cgit v1.2.3 From aa4f7bdd9a2df09fc47e82c2b85fb7647203ba8d Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 19 Jan 2021 23:01:33 +0200 Subject: Config ExceptionMiddleware; Config Mapper; Fixed User editing; Implmeneted Friend add trough HttpPatch --- src/DevHive.Data/Models/User.cs | 1 + src/DevHive.Data/Repositories/UserRepository.cs | 12 +++++----- .../Configurations/Mapping/UserMappings.cs | 2 ++ .../Models/Identity/User/FriendServiceModel.cs | 7 ++++++ .../Models/Identity/User/UserServiceModel.cs | 2 +- src/DevHive.Services/Services/UserService.cs | 26 +++++++++++++++++++--- .../Extensions/ConfigureCustomMiddleware.cs | 16 ------------- .../ConfigureExceptionHandlerMiddleware.cs | 16 +++++++++++++ .../Configurations/Mapping/UserMappings.cs | 3 +++ .../Models/Identity/User/UserWebModel.cs | 2 +- src/DevHive.Web/Startup.cs | 4 ++-- 11 files changed, 63 insertions(+), 28 deletions(-) create mode 100644 src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs delete mode 100644 src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs create mode 100644 src/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs (limited to 'src/DevHive.Web/Configurations/Mapping') 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 /// /// Languages that the user uses or is familiar with /// + // [Unique] public IList Languages { get; set; } /// 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 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(); CreateMap(); CreateMap(); + CreateMap(); CreateMap(); + CreateMap(); } } } 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 Roles { get; set; } = new List(); - public IList Friends { get; set; } = new List(); + public IList Friends { get; set; } = new List(); public IList Languages { get; set; } = new List(); 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 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(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(); - } - } -} 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(); + } + } +} 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(); + CreateMap(); + CreateMap(); + CreateMap() .ForMember(f => f.Name, u => u.MapFrom(src => src.UserName)); CreateMap(); 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 Friends { get; set; } = new List(); + public IList Friends { get; set; } = new List(); [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 => { -- cgit v1.2.3 From 009e01dc3dc2f78db6a660c65bf0d20bae702348 Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 21 Jan 2021 09:23:06 +0200 Subject: Tryed implementing the http patch --- .../Models/Misc/PasswordModifications.cs | 13 ++++ src/DevHive.Common/Models/Misc/Patch.cs | 9 +++ src/DevHive.Data/Repositories/UserRepository.cs | 7 -- .../Configurations/Mapping/RoleMapings.cs | 3 + .../Mapping/UserCollectionMappings.cs | 17 ++--- .../Configurations/Mapping/UserMappings.cs | 6 +- src/DevHive.Services/Interfaces/IUserService.cs | 6 +- .../Models/Identity/User/BaseUserServiceModel.cs | 1 + .../Technology/ReadTechnologyServiceModel.cs | 7 ++ src/DevHive.Services/Services/UserService.cs | 78 ++++++++++++---------- .../Configurations/Mapping/TechnologyMappings.cs | 2 + src/DevHive.Web/Controllers/UserController.cs | 19 ++---- .../Models/Technology/ReadTechnologyWebModel.cs | 14 ++++ 13 files changed, 113 insertions(+), 69 deletions(-) create mode 100644 src/DevHive.Common/Models/Misc/PasswordModifications.cs create mode 100644 src/DevHive.Common/Models/Misc/Patch.cs create mode 100644 src/DevHive.Services/Models/Technology/ReadTechnologyServiceModel.cs create mode 100644 src/DevHive.Web/Models/Technology/ReadTechnologyWebModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Common/Models/Misc/PasswordModifications.cs b/src/DevHive.Common/Models/Misc/PasswordModifications.cs new file mode 100644 index 0000000..f10a334 --- /dev/null +++ b/src/DevHive.Common/Models/Misc/PasswordModifications.cs @@ -0,0 +1,13 @@ +using System.Security.Cryptography; +using System.Text; + +namespace DevHive.Common.Models.Misc +{ + public static class PasswordModifications + { + public static string GeneratePasswordHash(string password) + { + return string.Join(string.Empty, SHA512.HashData(Encoding.ASCII.GetBytes(password))); + } + } +} diff --git a/src/DevHive.Common/Models/Misc/Patch.cs b/src/DevHive.Common/Models/Misc/Patch.cs new file mode 100644 index 0000000..ea5a4f1 --- /dev/null +++ b/src/DevHive.Common/Models/Misc/Patch.cs @@ -0,0 +1,9 @@ +namespace DevHive.Common.Models.Misc +{ + public class Patch + { + public string Name { get; set; } + public object Value { get; set; } + public string Action { get; set; } + } +} diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 492d46b..3f9af70 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -109,13 +109,6 @@ namespace DevHive.Data.Repositories public async Task EditAsync(User newEntity) { - // User user = await this.GetByIdAsync(newEntity.Id); - - // 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/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index 4ddd253..b5541f9 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -9,6 +9,9 @@ namespace DevHive.Services.Configurations.Mapping public RoleMappings() { CreateMap(); + CreateMap(); + + CreateMap(); CreateMap(); } } diff --git a/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs index ee505a2..7a773e8 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs @@ -8,14 +8,15 @@ namespace DevHive.Services.Configurations.Mapping { public UserCollectionMappings() { - CreateMap() - .ForMember(up => up.UserName, u => u.MapFrom(src => src.Name)); - CreateMap() - .ForMember(r => r.Name, u => u.MapFrom(src => src.Name)); - CreateMap() - .ForMember(r => r.Name, u => u.MapFrom(src => src.Name)); - CreateMap() - .ForMember(r => r.Name, u => u.MapFrom(src => src.Name)); + CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); + + CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs index 541e16e..5d9e41c 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs @@ -1,6 +1,7 @@ using DevHive.Data.Models; using AutoMapper; using DevHive.Services.Models.Identity.User; +using DevHive.Common.Models.Misc; namespace DevHive.Services.Configurations.Mapping { @@ -10,10 +11,13 @@ namespace DevHive.Services.Configurations.Mapping { CreateMap(); CreateMap(); - CreateMap(); + CreateMap() + .AfterMap((src, dest) => dest.PasswordHash = PasswordModifications.GeneratePasswordHash(src.Password)); CreateMap(); CreateMap(); + CreateMap() + .ForMember(x => x.Password, opt => opt.Ignore()); CreateMap(); } } diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs index 121fec3..88be0c8 100644 --- a/src/DevHive.Services/Interfaces/IUserService.cs +++ b/src/DevHive.Services/Interfaces/IUserService.cs @@ -1,9 +1,9 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using DevHive.Common.Models.Identity; -using DevHive.Data.Models; +using DevHive.Common.Models.Misc; using DevHive.Services.Models.Identity.User; -using Microsoft.AspNetCore.JsonPatch; namespace DevHive.Services.Interfaces { @@ -18,7 +18,7 @@ namespace DevHive.Services.Interfaces Task GetUserById(Guid id); Task UpdateUser(UpdateUserServiceModel updateModel); - Task PatchUser(Guid id, JsonPatchDocument jsonPatch); + Task PatchUser(Guid id, List patch); Task DeleteUser(Guid id); Task RemoveFriend(Guid userId, Guid friendId); diff --git a/src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs index 514f82a..7a160f8 100644 --- a/src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs @@ -6,5 +6,6 @@ namespace DevHive.Services.Models.Identity.User public string Email { get; set; } public string FirstName { get; set; } public string LastName { get; set; } + public string Password { get; set; } } } diff --git a/src/DevHive.Services/Models/Technology/ReadTechnologyServiceModel.cs b/src/DevHive.Services/Models/Technology/ReadTechnologyServiceModel.cs new file mode 100644 index 0000000..cbfdc7d --- /dev/null +++ b/src/DevHive.Services/Models/Technology/ReadTechnologyServiceModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Services.Models.Technology +{ + public class ReadTechnologyServiceModel + { + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 51c4432..629b489 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -7,15 +7,14 @@ using System; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using Microsoft.IdentityModel.Tokens; -using System.Security.Cryptography; using System.Text; using System.Collections.Generic; using DevHive.Common.Models.Identity; using DevHive.Services.Interfaces; using DevHive.Data.Interfaces.Repositories; -using Microsoft.AspNetCore.JsonPatch; using System.Linq; -using Newtonsoft.Json; +using DevHive.Common.Models.Misc; +using System.Reflection; namespace DevHive.Services.Services { @@ -52,7 +51,7 @@ namespace DevHive.Services.Services User user = await this._userRepository.GetByUsernameAsync(loginModel.UserName); - if (user.PasswordHash != GeneratePasswordHash(loginModel.Password)) + if (user.PasswordHash != PasswordModifications.GeneratePasswordHash(loginModel.Password)) throw new ArgumentException("Incorrect password!"); return new TokenModel(WriteJWTSecurityToken(user.Id, user.Roles)); @@ -67,7 +66,7 @@ namespace DevHive.Services.Services throw new ArgumentException("Email already exists!"); User user = this._userMapper.Map(registerModel); - user.PasswordHash = GeneratePasswordHash(registerModel.Password); + user.PasswordHash = PasswordModifications.GeneratePasswordHash(registerModel.Password); // Make sure the default role exists if (!await this._roleRepository.DoesNameExist(Role.DefaultRole)) @@ -135,6 +134,7 @@ namespace DevHive.Services.Services public async Task UpdateUser(UpdateUserServiceModel updateUserServiceModel) { + //Method: ValidateUserOnUpdate if (!await this._userRepository.DoesUserExistAsync(updateUserServiceModel.Id)) throw new ArgumentException("User does not exist!"); @@ -144,6 +144,7 @@ namespace DevHive.Services.Services await this.ValidateUserCollections(updateUserServiceModel); + //Method: Insert collections to user HashSet languages = new(); foreach (UpdateUserCollectionServiceModel lang in updateUserServiceModel.Languages) languages.Add(await this._languageRepository.GetByNameAsync(lang.Name) ?? @@ -159,51 +160,35 @@ namespace DevHive.Services.Services user.Languages = languages; user.Technologies = technologies; - bool success = await this._userRepository.EditAsync(user); + bool successful = await this._userRepository.EditAsync(user); - if (!success) + if (!successful) throw new InvalidOperationException("Unable to edit user!"); return this._userMapper.Map(user); ; } - public async Task PatchUser(Guid id, JsonPatchDocument jsonPatch) + public async Task PatchUser(Guid id, List patchList) { User user = await this._userRepository.GetByIdAsync(id) ?? throw new ArgumentException("User does not exist!"); - object password = jsonPatch.Operations - .Where(x => x.path == "/password") - .Select(x => x.value) - .FirstOrDefault(); - - IEnumerable friends = jsonPatch.Operations - .Where(x => x.path == "/friends") - .Select(x => x.value); - - if(password != null) - { - string passwordHash = this.GeneratePasswordHash(password.ToString()); - user.PasswordHash = passwordHash; - } + UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map(user); - if (friends != null) + foreach (Patch patch in patchList) { - foreach (object friendObj in friends) + bool successful = patch.Action switch { - FriendServiceModel friendServiceModel = - JsonConvert.DeserializeObject(friendObj.ToString()); - - User amigo = await this._userRepository.GetByUsernameAsync(friendServiceModel.UserName) - ?? throw new ArgumentException($"User {friendServiceModel.UserName} does not exist!"); - - user.Friends.Add(amigo); - } + "replace" => ReplacePatch(updateUserServiceModel, patch), + "add" => AddPatch(updateUserServiceModel, patch), + "remove" => RemovePatch(updateUserServiceModel, patch), + _ => throw new ArgumentException("Invalid patch operation!"), + }; + + if (!successful) + throw new ArgumentException("A problem occurred while applying patch"); } - //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) { @@ -326,6 +311,11 @@ namespace DevHive.Services.Services } } + private async Task ValidateUserOnUpdate(UpdateUserServiceModel updateUserServiceModel) + { + + } + private string WriteJWTSecurityToken(Guid userId, HashSet roles) { byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret); @@ -354,9 +344,23 @@ namespace DevHive.Services.Services return tokenHandler.WriteToken(token); } - private string GeneratePasswordHash(string password) + private bool AddPatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) + { + // Type type = typeof(UpdateUserServiceModel); + // PropertyInfo property = type.GetProperty(patch.Name); + + // property.SetValue(updateUserServiceModel, patch.Value); + throw new NotImplementedException(); + } + + private bool RemovePatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) + { + throw new NotImplementedException(); + } + + private bool ReplacePatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) { - return string.Join(string.Empty, SHA512.HashData(Encoding.ASCII.GetBytes(password))); + throw new NotImplementedException(); } #endregion } diff --git a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs index 828dac1..4ecd5f3 100644 --- a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs @@ -9,10 +9,12 @@ namespace DevHive.Web.Configurations.Mapping public TechnologyMappings() { CreateMap(); + CreateMap(); CreateMap(); CreateMap(); CreateMap(); + CreateMap(); CreateMap(); CreateMap(); } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 471d2bb..7121ac8 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -6,14 +6,10 @@ using DevHive.Web.Models.Identity.User; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using DevHive.Common.Models.Identity; -using DevHive.Common.Models.Misc; -using DevHive.Web.Models.Language; -using DevHive.Services.Models.Language; -using DevHive.Web.Models.Technology; -using DevHive.Services.Models.Technology; using DevHive.Services.Interfaces; -using DevHive.Data.Models; using Microsoft.AspNetCore.JsonPatch; +using DevHive.Common.Models.Misc; +using System.Collections.Generic; namespace DevHive.Web.Controllers { @@ -87,15 +83,12 @@ namespace DevHive.Web.Controllers #region Update [HttpPut] - public async Task Update(Guid id, [FromBody] UpdateUserWebModel updateModel, [FromHeader] string authorization) + public async Task Update(Guid id, [FromBody] UpdateUserWebModel updateUserWebModel, [FromHeader] string authorization) { if (!await this._userService.ValidJWT(id, authorization)) return new UnauthorizedResult(); - // if (!ModelState.IsValid) - // return BadRequest("Not a valid model!"); - - UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map(updateModel); + UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map(updateUserWebModel); updateUserServiceModel.Id = id; UserServiceModel userServiceModel = await this._userService.UpdateUser(updateUserServiceModel); @@ -105,12 +98,12 @@ namespace DevHive.Web.Controllers } [HttpPatch] - public async Task Patch(Guid id, [FromBody] JsonPatchDocument jsonPatch, [FromHeader] string authorization) + public async Task Patch(Guid id, [FromBody] List patch, [FromHeader] string authorization) { if (!await this._userService.ValidJWT(id, authorization)) return new UnauthorizedResult(); - UserServiceModel userServiceModel = await this._userService.PatchUser(id, jsonPatch); + UserServiceModel userServiceModel = await this._userService.PatchUser(id, patch); if (userServiceModel == null) return new BadRequestObjectResult("Wrong patch properties"); diff --git a/src/DevHive.Web/Models/Technology/ReadTechnologyWebModel.cs b/src/DevHive.Web/Models/Technology/ReadTechnologyWebModel.cs new file mode 100644 index 0000000..edaaaef --- /dev/null +++ b/src/DevHive.Web/Models/Technology/ReadTechnologyWebModel.cs @@ -0,0 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + +namespace DevHive.Web.Models.Technology +{ + public class ReadTechnologyWebModel + { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] + public string Name { get; set; } + } +} -- cgit v1.2.3 From 9e86699c9b3aff17e0c4d19850b41b792a9625ef Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 21 Jan 2021 19:12:04 +0200 Subject: Removed HTTP Patch; Refactored HTTP Put; Fixed Update bug --- .../Repositories/LanguageRepository.cs | 7 +- .../Repositories/TechnologyRepository.cs | 1 + src/DevHive.Data/Repositories/UserRepository.cs | 15 ++- .../Configurations/Mapping/RoleMapings.cs | 2 +- .../Configurations/Mapping/TechnologyMappings.cs | 2 + .../Mapping/UserCollectionMappings.cs | 22 ---- src/DevHive.Services/Interfaces/IUserService.cs | 1 - .../Models/Identity/Role/CreateRoleServiceModel.cs | 14 +++ .../Models/Identity/User/FriendServiceModel.cs | 3 + .../Identity/User/UpdateFriendServiceModel.cs | 10 ++ .../User/UpdateUserCollectionServiceModel.cs | 7 -- .../Models/Identity/User/UpdateUserServiceModel.cs | 11 +- src/DevHive.Services/Services/UserService.cs | 135 +++++++++------------ .../Configurations/Mapping/LanguageMappings.cs | 7 +- .../Configurations/Mapping/RoleMappings.cs | 9 +- .../Configurations/Mapping/TechnologyMappings.cs | 3 +- .../Configurations/Mapping/UserMappings.cs | 13 +- src/DevHive.Web/Controllers/UserController.cs | 14 --- .../Models/Identity/User/UpdateUserWebModel.cs | 5 + 19 files changed, 132 insertions(+), 149 deletions(-) delete mode 100644 src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs create mode 100644 src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs create mode 100644 src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index 108b307..4c51cf3 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -20,8 +20,7 @@ namespace DevHive.Data.Repositories public async Task AddAsync(Language entity) { - await this._context - .Set() + await this._context.Languages .AddAsync(entity); return await this.SaveChangesAsync(this._context); @@ -32,14 +31,14 @@ namespace DevHive.Data.Repositories public async Task GetByIdAsync(Guid id) { - return await this._context - .Set() + return await this._context.Languages .FindAsync(id); } public async Task GetByNameAsync(string languageName) { return await this._context.Languages + .AsNoTracking() .FirstOrDefaultAsync(x => x.Name == languageName); } #endregion diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index 390ad3f..a41d4fb 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -40,6 +40,7 @@ namespace DevHive.Data.Repositories public async Task GetByNameAsync(string technologyName) { return await this._context.Technologies + .AsNoTracking() .FirstOrDefaultAsync(x => x.Name == technologyName); } #endregion diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 3f9af70..c769f7e 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -6,6 +6,7 @@ using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.ChangeTracking; namespace DevHive.Data.Repositories { @@ -78,7 +79,9 @@ namespace DevHive.Data.Repositories public async Task GetByUsernameAsync(string username) { return await this._context.Users - .Include(u => u.Roles) + .AsNoTracking() + .Include(x => x.Languages) + .Include(x => x.Technologies) .FirstOrDefaultAsync(x => x.UserName == username); } @@ -107,9 +110,13 @@ namespace DevHive.Data.Repositories #region Update - public async Task EditAsync(User newEntity) + public async Task EditAsync(User entity) { - this._context.Update(newEntity); + User user = await this._context.Users + .FirstOrDefaultAsync(x => x.Id == entity.Id); + + this._context.Update(user); + this._context.Entry(entity).CurrentValues.SetValues(entity); return await this.SaveChangesAsync(this._context); } @@ -177,6 +184,7 @@ namespace DevHive.Data.Repositories public async Task DoesUserExistAsync(Guid id) { return await this._context.Users + .AsNoTracking() .AnyAsync(x => x.Id == id); } @@ -208,6 +216,7 @@ namespace DevHive.Data.Repositories public bool DoesUserHaveThisUsername(Guid id, string username) { return this._context.Users + .AsNoTracking() .Any(x => x.Id == id && x.UserName == username); } diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index b5541f9..d6c8511 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -12,7 +12,7 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs index 079ec3e..0103ccf 100644 --- a/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs @@ -11,8 +11,10 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); CreateMap(); + CreateMap(); CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs deleted file mode 100644 index 7a773e8..0000000 --- a/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs +++ /dev/null @@ -1,22 +0,0 @@ -using AutoMapper; -using DevHive.Data.Models; -using DevHive.Services.Models.Identity.User; - -namespace DevHive.Services.Configurations.Mapping -{ - public class UserCollectionMappings : Profile - { - public UserCollectionMappings() - { - CreateMap(); - CreateMap(); - CreateMap(); - CreateMap(); - - CreateMap(); - CreateMap(); - CreateMap(); - CreateMap(); - } - } -} diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs index 88be0c8..923e9bb 100644 --- a/src/DevHive.Services/Interfaces/IUserService.cs +++ b/src/DevHive.Services/Interfaces/IUserService.cs @@ -18,7 +18,6 @@ namespace DevHive.Services.Interfaces Task GetUserById(Guid id); Task UpdateUser(UpdateUserServiceModel updateModel); - Task PatchUser(Guid id, List patch); Task DeleteUser(Guid id); Task RemoveFriend(Guid userId, Guid friendId); diff --git a/src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs new file mode 100644 index 0000000..53bea9e --- /dev/null +++ b/src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs @@ -0,0 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + +namespace DevHive.Services.Models.Identity.Role +{ + public class CreateRoleServiceModel + { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs b/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs index 63d57f7..a784f5c 100644 --- a/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs @@ -1,7 +1,10 @@ +using System; + namespace DevHive.Services.Models.Identity.User { public class FriendServiceModel { + public Guid Id { get; set; } public string UserName { get; set; } } } diff --git a/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs new file mode 100644 index 0000000..83fcc34 --- /dev/null +++ b/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Services.Models.Identity.User +{ + public class UpdateFriendServiceModel + { + public Guid Id { get; set; } + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs deleted file mode 100644 index c40a980..0000000 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace DevHive.Services.Models.Identity.User -{ - public class UpdateUserCollectionServiceModel - { - public string Name { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs index 835bf54..9277e3e 100644 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs @@ -1,5 +1,8 @@ using System; using System.Collections.Generic; +using DevHive.Services.Models.Identity.Role; +using DevHive.Services.Models.Language; +using DevHive.Services.Models.Technology; namespace DevHive.Services.Models.Identity.User { @@ -9,13 +12,13 @@ namespace DevHive.Services.Models.Identity.User public string Password { get; set; } - public HashSet Roles { get; set; } = new HashSet(); + public HashSet Roles { get; set; } = new HashSet(); - public HashSet Friends { get; set; } = new HashSet(); + public HashSet Friends { get; set; } = new HashSet(); - public HashSet Languages { get; set; } = new HashSet(); + public HashSet Languages { get; set; } = new HashSet(); - public HashSet Technologies { get; set; } = new HashSet(); + public HashSet Technologies { get; set; } = new HashSet(); } } diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index dca00fa..a57fd23 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -14,8 +14,9 @@ using DevHive.Services.Interfaces; using DevHive.Data.Interfaces.Repositories; using System.Linq; using DevHive.Common.Models.Misc; -using System.Reflection; -using Microsoft.CodeAnalysis.CSharp.Syntax; +using DevHive.Services.Models.Language; +using DevHive.Services.Models.Technology; +using DevHive.Services.Models.Identity.Role; namespace DevHive.Services.Services { @@ -135,69 +136,57 @@ namespace DevHive.Services.Services public async Task UpdateUser(UpdateUserServiceModel updateUserServiceModel) { - //Method: ValidateUserOnUpdate - if (!await this._userRepository.DoesUserExistAsync(updateUserServiceModel.Id)) - throw new ArgumentException("User does not exist!"); - - if (!this._userRepository.DoesUserHaveThisUsername(updateUserServiceModel.Id, updateUserServiceModel.UserName) - && await this._userRepository.DoesUsernameExistAsync(updateUserServiceModel.UserName)) - throw new ArgumentException("Username already exists!"); + await this.ValidateUserOnUpdate(updateUserServiceModel); await this.ValidateUserCollections(updateUserServiceModel); - //Method: Insert collections to user - HashSet languages = new(); - foreach (UpdateUserCollectionServiceModel lang in updateUserServiceModel.Languages) - languages.Add(await this._languageRepository.GetByNameAsync(lang.Name) ?? - throw new ArgumentException("Invalid language name!")); + //Preserve roles + int roleCount = updateUserServiceModel.Roles.Count; + for (int i = 0; i < roleCount; i++) + { + Role role = await this._roleRepository.GetByNameAsync(updateUserServiceModel.Roles.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid role name!"); - HashSet technologies = new(); - foreach (UpdateUserCollectionServiceModel tech in updateUserServiceModel.Technologies) - technologies.Add(await this._technologyRepository.GetByNameAsync(tech.Name) ?? - throw new ArgumentException("Invalid technology name!")); + UpdateRoleServiceModel updateRoleServiceModel = this._userMapper.Map(role); - User user = this._userMapper.Map(updateUserServiceModel); + updateUserServiceModel.Roles.Add(updateRoleServiceModel); + } - user.Languages = languages; - user.Technologies = technologies; + int langCount = updateUserServiceModel.Languages.Count; + for (int i = 0; i < langCount; i++) + { + Language language = await this._languageRepository.GetByNameAsync(updateUserServiceModel.Languages.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid language name!"); - bool successful = await this._userRepository.EditAsync(user); + UpdateLanguageServiceModel updateLanguageServiceModel = this._userMapper.Map(language); - if (!successful) - throw new InvalidOperationException("Unable to edit user!"); + updateUserServiceModel.Languages.Add(updateLanguageServiceModel); + } - return this._userMapper.Map(user); ; - } + //Clean the already replaced languages + updateUserServiceModel.Languages.RemoveWhere(x => x.Id == Guid.Empty); - public async Task PatchUser(Guid id, List patchList) - { - User user = await this._userRepository.GetByIdAsync(id) ?? - throw new ArgumentException("User does not exist!"); + int techCount = updateUserServiceModel.Technologies.Count; + for (int i = 0; i < techCount; i++) + { + Technology technology = await this._technologyRepository.GetByNameAsync(updateUserServiceModel.Technologies.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid technology name!"); - UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map(user); + UpdateTechnologyServiceModel updateTechnologyServiceModel = this._userMapper.Map(technology); - foreach (Patch patch in patchList) - { - bool successful = patch.Action switch - { - "replace" => ReplacePatch(updateUserServiceModel, patch), - "add" => AddPatch(updateUserServiceModel, patch), - "remove" => RemovePatch(updateUserServiceModel, patch), - _ => throw new ArgumentException("Invalid patch operation!"), - }; - - if (!successful) - throw new ArgumentException("A problem occurred while applying patch"); + updateUserServiceModel.Technologies.Add(updateTechnologyServiceModel); } - bool success = await this._userRepository.EditAsync(user); - if (success) - { - user = await this._userRepository.GetByIdAsync(id); - return this._userMapper.Map(user); - } - else - return null; + //Clean the already replaced technologies + updateUserServiceModel.Technologies.RemoveWhere(x => x.Id == Guid.Empty); + + User user = this._userMapper.Map(updateUserServiceModel); + bool successful = await this._userRepository.EditAsync(user); + + if (!successful) + throw new InvalidOperationException("Unable to edit user!"); + + return this._userMapper.Map(user); } #endregion @@ -282,10 +271,20 @@ namespace DevHive.Services.Services return toReturn; } + private async Task ValidateUserOnUpdate(UpdateUserServiceModel updateUserServiceModel) + { + if (!await this._userRepository.DoesUserExistAsync(updateUserServiceModel.Id)) + throw new ArgumentException("User does not exist!"); + + if (!this._userRepository.DoesUserHaveThisUsername(updateUserServiceModel.Id, updateUserServiceModel.UserName) + && await this._userRepository.DoesUsernameExistAsync(updateUserServiceModel.UserName)) + throw new ArgumentException("Username already exists!"); + } + private async Task ValidateUserCollections(UpdateUserServiceModel updateUserServiceModel) { // Friends - foreach (UpdateUserCollectionServiceModel friend in updateUserServiceModel.Friends) + foreach (var friend in updateUserServiceModel.Friends) { User returnedFriend = await this._userRepository.GetByUsernameAsync(friend.Name); @@ -294,29 +293,24 @@ namespace DevHive.Services.Services } // Languages - foreach (UpdateUserCollectionServiceModel language in updateUserServiceModel.Languages) + foreach (var language in updateUserServiceModel.Languages) { Language returnedLanguage = await this._languageRepository.GetByNameAsync(language.Name); - if (default(Language) == returnedLanguage) + if (returnedLanguage == null) throw new ArgumentException($"Language {language.Name} does not exist!"); } // Technology - foreach (UpdateUserCollectionServiceModel technology in updateUserServiceModel.Technologies) + foreach (var technology in updateUserServiceModel.Technologies) { Technology returnedTechnology = await this._technologyRepository.GetByNameAsync(technology.Name); - if (default(Technology) == returnedTechnology) + if (returnedTechnology == null) throw new ArgumentException($"Technology {technology.Name} does not exist!"); } } - private async Task ValidateUserOnUpdate(UpdateUserServiceModel updateUserServiceModel) - { - throw new NotImplementedException(); - } - private string WriteJWTSecurityToken(Guid userId, HashSet roles) { byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret); @@ -344,25 +338,6 @@ namespace DevHive.Services.Services SecurityToken token = tokenHandler.CreateToken(tokenDescriptor); return tokenHandler.WriteToken(token); } - - private bool AddPatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) - { - // Type type = typeof(UpdateUserServiceModel); - // PropertyInfo property = type.GetProperty(patch.Name); - - // property.SetValue(updateUserServiceModel, patch.Value); - throw new NotImplementedException(); - } - - private bool RemovePatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) - { - throw new NotImplementedException(); - } - - private bool ReplacePatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) - { - throw new NotImplementedException(); - } #endregion } } diff --git a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs index 8cac3ca..eca0d1a 100644 --- a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs @@ -8,10 +8,11 @@ namespace DevHive.Web.Configurations.Mapping { public LanguageMappings() { - CreateMap(); - CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap(); CreateMap(); CreateMap(); diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 66ae8e3..2ea2742 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -8,11 +8,14 @@ namespace DevHive.Web.Configurations.Mapping { public RoleMappings() { - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap(); + CreateMap(); + CreateMap(); CreateMap(); - CreateMap(); } } } diff --git a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs index 4ecd5f3..708b6ac 100644 --- a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs @@ -10,7 +10,8 @@ namespace DevHive.Web.Configurations.Mapping { CreateMap(); CreateMap(); - CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); CreateMap(); CreateMap(); diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index 5faf4b5..9dbf613 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -20,13 +20,14 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); - CreateMap(); - CreateMap(); + //Update + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); - CreateMap() - .ForMember(f => f.Name, u => u.MapFrom(src => src.UserName)); - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 7121ac8..fbbbbff 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -96,20 +96,6 @@ namespace DevHive.Web.Controllers return new AcceptedResult("UpdateUser", userWebModel); } - - [HttpPatch] - public async Task Patch(Guid id, [FromBody] List patch, [FromHeader] string authorization) - { - if (!await this._userService.ValidJWT(id, authorization)) - return new UnauthorizedResult(); - - UserServiceModel userServiceModel = await this._userService.PatchUser(id, patch); - - if (userServiceModel == null) - return new BadRequestObjectResult("Wrong patch properties"); - else - return new OkObjectResult(this._userMapper.Map(userServiceModel)); - } #endregion #region Delete diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index 3c38ab6..30c66fb 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Diagnostics.CodeAnalysis; using DevHive.Web.Attributes; +using DevHive.Web.Models.Identity.Role; using DevHive.Web.Models.Language; using DevHive.Web.Models.Technology; @@ -18,6 +19,10 @@ namespace DevHive.Web.Models.Identity.User [Required] public HashSet Friends { get; set; } + [NotNull] + [Required] + public HashSet Roles { get; set; } + [NotNull] [Required] public HashSet Languages { get; set; } -- cgit v1.2.3 From e2564f5a2ba87f06ef32137fad5c748f494fa42a Mon Sep 17 00:00:00 2001 From: transtrike Date: Sat, 23 Jan 2021 20:27:11 +0200 Subject: Fixed Comment Models&Mappings --- src/DevHive.Data/Interfaces/Models/IComment.cs | 4 ++++ .../Configurations/Mapping/CommentMappings.cs | 16 +++++++++------- .../Models/Post/Comment/CreateCommentServiceModel.cs | 9 ++++++++- .../Models/Post/Comment/ReadCommentServiceModel.cs | 14 ++++++++++---- .../Models/Post/Comment/UpdateCommnetServiceModel.cs | 4 +++- .../Configurations/Mapping/CommentMappings.cs | 14 ++++++++++---- .../Models/Post/Comment/CreateCommentWebModel.cs | 13 ++++++++++++- .../Models/Post/Comment/ReadCommentWebModel.cs | 2 ++ .../Models/Post/Comment/UpdateCommentWebModel.cs | 3 ++- 9 files changed, 60 insertions(+), 19 deletions(-) (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Data/Interfaces/Models/IComment.cs b/src/DevHive.Data/Interfaces/Models/IComment.cs index f6afb3f..6368e3b 100644 --- a/src/DevHive.Data/Interfaces/Models/IComment.cs +++ b/src/DevHive.Data/Interfaces/Models/IComment.cs @@ -4,8 +4,12 @@ namespace DevHive.Data.Interfaces.Models { public interface IComment : IModel { + Guid PostId { get; set; } + Guid IssuerId { get; set; } + string Message { get; set; } + DateTime TimeCreated { get; set; } } } diff --git a/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs b/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs index f903128..46404da 100644 --- a/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs @@ -1,7 +1,6 @@ using DevHive.Data.Models; using AutoMapper; using DevHive.Services.Models.Post.Comment; -using DevHive.Common.Models.Misc; namespace DevHive.Services.Configurations.Mapping { @@ -9,11 +8,14 @@ namespace DevHive.Services.Configurations.Mapping { public CommentMappings() { - CreateMap(); - CreateMap(); - CreateMap(); - CreateMap(); - CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap() + .ForMember(src => src.Id, dest => dest.MapFrom(p => p.CommentId)); + + CreateMap(); + CreateMap() + .ForMember(src => src.CommentId, dest => dest.MapFrom(p => p.Id)); } } -} \ No newline at end of file +} diff --git a/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs index 7c00b8f..4dfd848 100644 --- a/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs @@ -2,7 +2,14 @@ using System; namespace DevHive.Services.Models.Post.Comment { - public class CreateCommentServiceModel : BaseCommentServiceModel + public class CreateCommentServiceModel { + public Guid PostId { get; set; } + + public Guid IssuerId { get; set; } + + public string Message { get; set; } + + public DateTime TimeCreated { get; set; } } } diff --git a/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs index ad68b58..c6ff612 100644 --- a/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs @@ -1,11 +1,17 @@ +using System; + namespace DevHive.Services.Models.Post.Comment { - public class ReadCommentServiceModel : BaseCommentServiceModel + public class ReadCommentServiceModel { - public string IssuerFirstName { get; set; } + public Guid CommentId { get; set; } + + public Guid IssuerId { get; set; } + + public Guid PostId { get; set; } - public string IssuerLastName { get; set; } + public string Message { get; set; } - public string IssuerUsername { get; set; } + public DateTime TimeCreated { get; set; } } } diff --git a/src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs index 424ea65..51cd739 100644 --- a/src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs @@ -2,8 +2,10 @@ using System; namespace DevHive.Services.Models.Post.Comment { - public class UpdateCommentServiceModel : BaseCommentServiceModel + public class UpdateCommentServiceModel { public Guid CommentId { get; set; } + + public string NewMessage { get; set; } } } diff --git a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs index 5998e7a..296704e 100644 --- a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs @@ -8,10 +8,16 @@ namespace DevHive.Web.Configurations.Mapping { public CommentMappings() { - CreateMap(); - CreateMap(); - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); + + CreateMap() + .ForMember(dest => dest.IssuerFirstName, src => src.Ignore()) + .ForMember(dest => dest.IssuerLastName, src => src.Ignore()) + .ForMember(dest => dest.IssuerUsername, src => src.Ignore()); } } } + + + diff --git a/src/DevHive.Web/Models/Post/Comment/CreateCommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/CreateCommentWebModel.cs index 3680727..85c67bf 100644 --- a/src/DevHive.Web/Models/Post/Comment/CreateCommentWebModel.cs +++ b/src/DevHive.Web/Models/Post/Comment/CreateCommentWebModel.cs @@ -1,6 +1,17 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + namespace DevHive.Web.Models.Post.Comment { - public class CreateCommentWebModel : BaseCommentWebModel + public class CreateCommentWebModel { + [NotNull] + [Required] + public Guid PostId { get; set; } + + [NotNull] + [Required] + public string Message { get; set; } } } diff --git a/src/DevHive.Web/Models/Post/Comment/ReadCommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/ReadCommentWebModel.cs index 2c4a367..5320c3c 100644 --- a/src/DevHive.Web/Models/Post/Comment/ReadCommentWebModel.cs +++ b/src/DevHive.Web/Models/Post/Comment/ReadCommentWebModel.cs @@ -4,6 +4,8 @@ namespace DevHive.Web.Models.Post.Comment { public class ReadCommentWebModel { + public Guid CommentId { get; set; } + public Guid PostId { get; set; } public string IssuerFirstName { get; set; } diff --git a/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs index 49f4540..8e78a48 100644 --- a/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs +++ b/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs @@ -1,6 +1,7 @@ namespace DevHive.Web.Models.Post.Comment { - public class UpdateCommentWebModel : BaseCommentWebModel + public class UpdateCommentWebModel { + public string NewMessage { get; set; } } } -- cgit v1.2.3 From fac1bf2d1772e60cbecc1cf0381c6063a0e97ffd Mon Sep 17 00:00:00 2001 From: transtrike Date: Sat, 23 Jan 2021 20:59:12 +0200 Subject: Post&Comment Models implemented; Mappings Configured --- src/DevHive.Data/Interfaces/Models/IPost.cs | 12 +++++-- src/DevHive.Data/Models/Post.cs | 11 +++--- .../Configurations/Mapping/PostMappings.cs | 12 +++++-- .../Models/Post/Comment/BaseCommentServiceModel.cs | 15 --------- .../Models/Post/Comment/CommentServiceModel.cs | 9 ----- .../Models/Post/Post/BasePostServiceModel.cs | 11 ------ .../Models/Post/Post/CreatePostServiceModel.cs | 8 ++++- .../Models/Post/Post/PostServiceModel.cs | 12 ------- .../Models/Post/Post/ReadPostServiceModel.cs | 21 ++++++++++++ .../Models/Post/Post/UpdatePostServiceModel.cs | 12 +++++-- .../Configurations/Mapping/PostMappings.cs | 20 +++++++++++ .../Models/Post/Comment/BaseCommentWebModel.cs | 21 ------------ .../Models/Post/Comment/CommentWebModel.cs | 25 -------------- .../Models/Post/Post/BasePostWebModel.cs | 17 ---------- .../Models/Post/Post/CreatePostWebModel.cs | 15 ++++++++- src/DevHive.Web/Models/Post/Post/PostWebModel.cs | 39 ---------------------- .../Models/Post/Post/ReadPostWebModel.cs | 25 ++++++++++++++ .../Models/Post/Post/UpdatePostModel.cs | 7 ---- .../Models/Post/Post/UpdatePostWebModel.cs | 13 ++++++++ 19 files changed, 134 insertions(+), 171 deletions(-) delete mode 100644 src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Post/Comment/CommentServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Post/Post/BasePostServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Post/Post/PostServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs create mode 100644 src/DevHive.Web/Configurations/Mapping/PostMappings.cs delete mode 100644 src/DevHive.Web/Models/Post/Comment/BaseCommentWebModel.cs delete mode 100644 src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs delete mode 100644 src/DevHive.Web/Models/Post/Post/BasePostWebModel.cs delete mode 100644 src/DevHive.Web/Models/Post/Post/PostWebModel.cs create mode 100644 src/DevHive.Web/Models/Post/Post/ReadPostWebModel.cs delete mode 100644 src/DevHive.Web/Models/Post/Post/UpdatePostModel.cs create mode 100644 src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Data/Interfaces/Models/IPost.cs b/src/DevHive.Data/Interfaces/Models/IPost.cs index 117d859..e68407d 100644 --- a/src/DevHive.Data/Interfaces/Models/IPost.cs +++ b/src/DevHive.Data/Interfaces/Models/IPost.cs @@ -1,13 +1,19 @@ using System; +using System.Collections.Generic; using DevHive.Data.Models; namespace DevHive.Data.Interfaces.Models { public interface IPost : IModel { - Guid IssuerId { get; set; } - DateTime TimeCreated { get; set; } + Guid CreatorId { get; set; } + string Message { get; set; } - Comment[] Comments { get; set; } + + DateTime TimeCreated { get; set; } + + List Comments { get; set; } + + //List Files } } diff --git a/src/DevHive.Data/Models/Post.cs b/src/DevHive.Data/Models/Post.cs index 54576b7..f7bca43 100644 --- a/src/DevHive.Data/Models/Post.cs +++ b/src/DevHive.Data/Models/Post.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using DevHive.Data.Interfaces.Models; @@ -9,14 +10,14 @@ namespace DevHive.Data.Models { public Guid Id { get; set; } - public Guid IssuerId { get; set; } - - public DateTime TimeCreated { get; set; } + public Guid CreatorId { get; set; } public string Message { get; set; } - //public File[] Files { get; set; } + public DateTime TimeCreated { get; set; } + + public List Comments { get; set; } - public Comment[] Comments { get; set; } + // public List Files { get; set; } } } diff --git a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs index 7f99c66..e4924a5 100644 --- a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs @@ -1,6 +1,5 @@ using DevHive.Data.Models; using AutoMapper; -using DevHive.Services.Models.Post; using DevHive.Services.Models.Post.Post; namespace DevHive.Services.Configurations.Mapping @@ -9,8 +8,15 @@ namespace DevHive.Services.Configurations.Mapping { public PostMappings() { - CreateMap(); - CreateMap(); + CreateMap(); + // .ForMember(dest => dest.Files, src => src.Ignore()); + CreateMap() + .ForMember(dest => dest.Id, src => src.MapFrom(p => p.PostId)) + // .ForMember(dest => dest.Files, src => src.Ignore()) + .ForMember(dest => dest.Message, src => src.MapFrom(p => p.NewMessage)); + + CreateMap() + .ForMember(dest => dest.PostId, src => src.MapFrom(p => p.Id)); } } } diff --git a/src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs deleted file mode 100644 index dae7ec3..0000000 --- a/src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Post.Comment -{ - public class BaseCommentServiceModel - { - public Guid PostId { get; set; } - - public Guid IssuerId { get; set; } - - public string Message { get; set; } - - public DateTime TimeCreated { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Post/Comment/CommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/CommentServiceModel.cs deleted file mode 100644 index a0fa53e..0000000 --- a/src/DevHive.Services/Models/Post/Comment/CommentServiceModel.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Post.Comment -{ - public class CommentServiceModel : CreateCommentServiceModel - { - - } -} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Post/Post/BasePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/BasePostServiceModel.cs deleted file mode 100644 index 45a677c..0000000 --- a/src/DevHive.Services/Models/Post/Post/BasePostServiceModel.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Post.Post -{ - public class BasePostServiceModel - { - public Guid Id { get; set; } - public Guid IssuerId { get; set; } - public string Message { get; set; } - } -} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs index 1bf60ae..6b83f3e 100644 --- a/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs @@ -2,8 +2,14 @@ using System; namespace DevHive.Services.Models.Post.Post { - public class CreatePostServiceModel : BasePostServiceModel + public class CreatePostServiceModel { + public Guid IssuerId { get; set; } + + public string Message { get; set; } + public DateTime TimeCreated { get; set; } + + // public List Files { get; set; } } } diff --git a/src/DevHive.Services/Models/Post/Post/PostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/PostServiceModel.cs deleted file mode 100644 index b9c2128..0000000 --- a/src/DevHive.Services/Models/Post/Post/PostServiceModel.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Post.Post -{ - public class PostServiceModel : CreatePostServiceModel - { - - //public File[] Files { get; set; } - - //public Comment[] Comments { get; set; } - } -} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs new file mode 100644 index 0000000..52b9232 --- /dev/null +++ b/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using DevHive.Services.Models.Post.Comment; + +namespace DevHive.Services.Models.Post.Post +{ + public class ReadPostServiceModel + { + public Guid PostId { get; set; } + + public Guid CreatorId { get; set; } + + public string Message { get; set; } + + public DateTime TimeCreated { get; set; } + + public List Comments { get; set; } + + //public List Files { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs index 5a4f621..67ee711 100644 --- a/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs @@ -1,5 +1,13 @@ +using System; + namespace DevHive.Services.Models.Post.Post { - public class UpdatePostServiceModel : BasePostServiceModel - { } + public class UpdatePostServiceModel + { + public Guid PostId { get; set; } + + public string NewMessage { get; set; } + + // public List Files { get; set; } + } } diff --git a/src/DevHive.Web/Configurations/Mapping/PostMappings.cs b/src/DevHive.Web/Configurations/Mapping/PostMappings.cs new file mode 100644 index 0000000..0e966cc --- /dev/null +++ b/src/DevHive.Web/Configurations/Mapping/PostMappings.cs @@ -0,0 +1,20 @@ +using AutoMapper; +using DevHive.Services.Models.Post.Post; +using DevHive.Web.Models.Post.Post; + +namespace DevHive.Web.Configurations.Mapping +{ + public class PostMappings : Profile + { + public PostMappings() + { + CreateMap(); + CreateMap(); + + CreateMap() + .ForMember(dest => dest.CreatorFirstName, src => src.Ignore()) + .ForMember(dest => dest.CreatorLastName, src => src.Ignore()) + .ForMember(dest => dest.CreatorUsername, src => src.Ignore()); + } + } +} diff --git a/src/DevHive.Web/Models/Post/Comment/BaseCommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/BaseCommentWebModel.cs deleted file mode 100644 index 9ba0b87..0000000 --- a/src/DevHive.Web/Models/Post/Comment/BaseCommentWebModel.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; -using System.Diagnostics.CodeAnalysis; - -namespace DevHive.Web.Models.Post.Comment -{ - public class BaseCommentWebModel - { - [NotNull] - [Required] - public Guid PostId { get; set; } - - [NotNull] - [Required] - public Guid CommentId { get; set; } - - [NotNull] - [Required] - public string Message { get; set; } - } -} diff --git a/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs deleted file mode 100644 index 590851d..0000000 --- a/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; -using System.Diagnostics.CodeAnalysis; - -namespace DevHive.Web.Models.Post.Comment -{ - public class CommentWebModel - { - [NotNull] - [Required] - public Guid IssuerId { get; set; } - - [NotNull] - [Required] - public Guid PostId { get; set; } - - [NotNull] - [Required] - public string Message { get; set; } - - [NotNull] - [Required] - public DateTime TimeCreated { get; set; } - } -} diff --git a/src/DevHive.Web/Models/Post/Post/BasePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/BasePostWebModel.cs deleted file mode 100644 index 35ddd34..0000000 --- a/src/DevHive.Web/Models/Post/Post/BasePostWebModel.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; -using System.Diagnostics.CodeAnalysis; - -namespace DevHive.Web.Models.Post.Post -{ - public class BasePostWebModel - { - [NotNull] - [Required] - public Guid IssuerId { get; set; } - - [NotNull] - [Required] - public string Message { get; set; } - } -} diff --git a/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs index 389ff9e..647b30e 100644 --- a/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs +++ b/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs @@ -1,6 +1,19 @@ using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; namespace DevHive.Web.Models.Post.Post { - public class CreatePostWebModel : BasePostWebModel { } + public class CreatePostWebModel + { + [NotNull] + [Required] + public Guid CreatorId { get; set; } + + [NotNull] + [Required] + public string Message { get; set; } + + // public List Files { get; set; } + } } diff --git a/src/DevHive.Web/Models/Post/Post/PostWebModel.cs b/src/DevHive.Web/Models/Post/Post/PostWebModel.cs deleted file mode 100644 index fe35cee..0000000 --- a/src/DevHive.Web/Models/Post/Post/PostWebModel.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.ComponentModel.DataAnnotations; -using System.Diagnostics.CodeAnalysis; -using DevHive.Web.Attributes; -using DevHive.Web.Models.Post.Comment; - -namespace DevHive.Web.Models.Post.Post -{ - public class PostWebModel - { - //public string Picture { get; set; } - - [NotNull] - [Required] - [MinLength(3)] - [MaxLength(50)] - public string IssuerFirstName { get; set; } - - [NotNull] - [Required] - [MinLength(3)] - [MaxLength(50)] - public string IssuerLastName { get; set; } - - [NotNull] - [Required] - [MinLength(3)] - [MaxLength(50)] - [OnlyAlphanumerics(ErrorMessage = "Username can only contain letters and digits!")] - public string IssuerUsername { get; set; } - - [NotNull] - [Required] - public string Message { get; set; } - - //public Files[] Files { get; set; } - - public CommentWebModel[] Comments { get; set; } - } -} diff --git a/src/DevHive.Web/Models/Post/Post/ReadPostWebModel.cs b/src/DevHive.Web/Models/Post/Post/ReadPostWebModel.cs new file mode 100644 index 0000000..04c6275 --- /dev/null +++ b/src/DevHive.Web/Models/Post/Post/ReadPostWebModel.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using DevHive.Web.Models.Post.Comment; + +namespace DevHive.Web.Models.Post.Post +{ + public class ReadPostWebModel + { + public Guid PostId { get; set; } + + public string CreatorFirstName { get; set; } + + public string CreatorLastName { get; set; } + + public string CreatorUsername { get; set; } + + public string Message { get; set; } + + public DateTime TimeCreated { get; set; } + + public List Comments { get; set; } + + //public Files[] Files { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Post/Post/UpdatePostModel.cs b/src/DevHive.Web/Models/Post/Post/UpdatePostModel.cs deleted file mode 100644 index c774900..0000000 --- a/src/DevHive.Web/Models/Post/Post/UpdatePostModel.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace DevHive.Web.Models.Post.Post -{ - public class UpdatePostWebModel : BasePostWebModel - { - //public Files[] Files { get; set; } - } -} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs new file mode 100644 index 0000000..5b66436 --- /dev/null +++ b/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs @@ -0,0 +1,13 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + +namespace DevHive.Web.Models.Post.Post +{ + public class UpdatePostWebModel + { + [NotNull] + [Required] + public string Message { get; set; } + } +} -- cgit v1.2.3 From f910a2a63cb83b35c6589591400a69c8f7f7917c Mon Sep 17 00:00:00 2001 From: transtrike Date: Sun, 24 Jan 2021 00:07:44 +0200 Subject: Migrations added; CRUD over Posts&Comments successfully completed --- src/DevHive.Data/Interfaces/Models/IComment.cs | 2 +- .../20210121083441_UserRefactor.Designer.cs | 474 -------------------- .../Migrations/20210121083441_UserRefactor.cs | 411 ------------------ ...23215634_PostAndComment_Implemented.Designer.cs | 476 +++++++++++++++++++++ .../20210123215634_PostAndComment_Implemented.cs | 411 ++++++++++++++++++ .../Migrations/DevHiveContextModelSnapshot.cs | 10 +- src/DevHive.Data/Models/Comment.cs | 2 +- src/DevHive.Data/Models/Language.cs | 4 +- src/DevHive.Data/Models/Post.cs | 4 +- src/DevHive.Data/Models/Role.cs | 2 +- src/DevHive.Data/Models/Technology.cs | 4 +- src/DevHive.Data/Models/User.cs | 8 +- src/DevHive.Data/Repositories/CommentRepository.cs | 2 +- src/DevHive.Data/Repositories/PostRepository.cs | 7 + .../Configurations/Mapping/CommentMappings.cs | 5 +- .../Configurations/Mapping/PostMappings.cs | 5 +- .../Post/Comment/CreateCommentServiceModel.cs | 4 +- .../Models/Post/Comment/ReadCommentServiceModel.cs | 6 +- .../Post/Comment/UpdateCommentServiceModel.cs | 15 + .../Post/Comment/UpdateCommnetServiceModel.cs | 11 - .../Models/Post/Post/CreatePostServiceModel.cs | 4 +- .../Models/Post/Post/ReadPostServiceModel.cs | 8 +- .../Models/Post/Post/UpdatePostServiceModel.cs | 2 + src/DevHive.Services/Services/PostService.cs | 64 ++- .../Configurations/Mapping/CommentMappings.cs | 5 +- .../Configurations/Mapping/PostMappings.cs | 5 +- src/DevHive.Web/Controllers/PostController.cs | 10 +- src/DevHive.Web/Controllers/RoleController.cs | 2 +- .../Models/Post/Comment/UpdateCommentWebModel.cs | 2 + .../Models/Post/Post/CreatePostWebModel.cs | 4 - .../Models/Post/Post/UpdatePostWebModel.cs | 2 +- 31 files changed, 1017 insertions(+), 954 deletions(-) delete mode 100644 src/DevHive.Data/Migrations/20210121083441_UserRefactor.Designer.cs delete mode 100644 src/DevHive.Data/Migrations/20210121083441_UserRefactor.cs create mode 100644 src/DevHive.Data/Migrations/20210123215634_PostAndComment_Implemented.Designer.cs create mode 100644 src/DevHive.Data/Migrations/20210123215634_PostAndComment_Implemented.cs create mode 100644 src/DevHive.Services/Models/Post/Comment/UpdateCommentServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Data/Interfaces/Models/IComment.cs b/src/DevHive.Data/Interfaces/Models/IComment.cs index 6368e3b..9a72da4 100644 --- a/src/DevHive.Data/Interfaces/Models/IComment.cs +++ b/src/DevHive.Data/Interfaces/Models/IComment.cs @@ -6,7 +6,7 @@ namespace DevHive.Data.Interfaces.Models { Guid PostId { get; set; } - Guid IssuerId { get; set; } + Guid CreatorId { get; set; } string Message { get; set; } diff --git a/src/DevHive.Data/Migrations/20210121083441_UserRefactor.Designer.cs b/src/DevHive.Data/Migrations/20210121083441_UserRefactor.Designer.cs deleted file mode 100644 index 7c7a092..0000000 --- a/src/DevHive.Data/Migrations/20210121083441_UserRefactor.Designer.cs +++ /dev/null @@ -1,474 +0,0 @@ -// -using System; -using DevHive.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; - -namespace DevHive.Data.Migrations -{ - [DbContext(typeof(DevHiveContext))] - [Migration("20210121083441_UserRefactor")] - partial class UserRefactor - { - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .UseIdentityByDefaultColumns() - .HasAnnotation("Relational:MaxIdentifierLength", 63) - .HasAnnotation("ProductVersion", "5.0.1"); - - modelBuilder.Entity("DevHive.Data.Models.Comment", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("uuid"); - - b.Property("IssuerId") - .HasColumnType("uuid"); - - b.Property("Message") - .HasColumnType("text"); - - b.Property("PostId") - .HasColumnType("uuid"); - - b.Property("TimeCreated") - .HasColumnType("timestamp without time zone"); - - b.HasKey("Id"); - - b.HasIndex("PostId"); - - b.ToTable("Comments"); - }); - - modelBuilder.Entity("DevHive.Data.Models.Language", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("uuid"); - - b.Property("Name") - .HasColumnType("text"); - - b.HasKey("Id"); - - b.ToTable("Languages"); - }); - - modelBuilder.Entity("DevHive.Data.Models.Post", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("uuid"); - - b.Property("IssuerId") - .HasColumnType("uuid"); - - b.Property("Message") - .HasColumnType("text"); - - b.Property("TimeCreated") - .HasColumnType("timestamp without time zone"); - - b.HasKey("Id"); - - b.ToTable("Posts"); - }); - - modelBuilder.Entity("DevHive.Data.Models.Role", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("uuid"); - - b.Property("ConcurrencyStamp") - .IsConcurrencyToken() - .HasColumnType("text"); - - b.Property("Name") - .HasMaxLength(256) - .HasColumnType("character varying(256)"); - - b.Property("NormalizedName") - .HasMaxLength(256) - .HasColumnType("character varying(256)"); - - b.HasKey("Id"); - - b.HasIndex("NormalizedName") - .IsUnique() - .HasDatabaseName("RoleNameIndex"); - - b.ToTable("AspNetRoles"); - }); - - modelBuilder.Entity("DevHive.Data.Models.Technology", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("uuid"); - - b.Property("Name") - .HasColumnType("text"); - - b.HasKey("Id"); - - b.ToTable("Technologies"); - }); - - modelBuilder.Entity("DevHive.Data.Models.User", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("uuid"); - - b.Property("AccessFailedCount") - .HasColumnType("integer"); - - b.Property("ConcurrencyStamp") - .IsConcurrencyToken() - .HasColumnType("text"); - - b.Property("Email") - .HasMaxLength(256) - .HasColumnType("character varying(256)"); - - b.Property("EmailConfirmed") - .HasColumnType("boolean"); - - b.Property("FirstName") - .HasColumnType("text"); - - b.Property("LastName") - .HasColumnType("text"); - - b.Property("LockoutEnabled") - .HasColumnType("boolean"); - - b.Property("LockoutEnd") - .HasColumnType("timestamp with time zone"); - - b.Property("NormalizedEmail") - .HasMaxLength(256) - .HasColumnType("character varying(256)"); - - b.Property("NormalizedUserName") - .HasMaxLength(256) - .HasColumnType("character varying(256)"); - - b.Property("PasswordHash") - .HasColumnType("text"); - - b.Property("PhoneNumber") - .HasColumnType("text"); - - b.Property("PhoneNumberConfirmed") - .HasColumnType("boolean"); - - b.Property("ProfilePictureUrl") - .HasColumnType("text"); - - b.Property("SecurityStamp") - .HasColumnType("text"); - - b.Property("TwoFactorEnabled") - .HasColumnType("boolean"); - - b.Property("UserId") - .HasColumnType("uuid"); - - b.Property("UserName") - .HasMaxLength(256) - .HasColumnType("character varying(256)"); - - b.HasKey("Id"); - - b.HasIndex("NormalizedEmail") - .HasDatabaseName("EmailIndex"); - - b.HasIndex("NormalizedUserName") - .IsUnique() - .HasDatabaseName("UserNameIndex"); - - b.HasIndex("UserId"); - - b.HasIndex("UserName") - .IsUnique(); - - b.ToTable("AspNetUsers"); - }); - - modelBuilder.Entity("LanguageUser", b => - { - b.Property("LanguagesId") - .HasColumnType("uuid"); - - b.Property("UsersId") - .HasColumnType("uuid"); - - b.HasKey("LanguagesId", "UsersId"); - - b.HasIndex("UsersId"); - - b.ToTable("LanguageUser"); - }); - - modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer") - .UseIdentityByDefaultColumn(); - - b.Property("ClaimType") - .HasColumnType("text"); - - b.Property("ClaimValue") - .HasColumnType("text"); - - b.Property("RoleId") - .HasColumnType("uuid"); - - b.HasKey("Id"); - - b.HasIndex("RoleId"); - - b.ToTable("AspNetRoleClaims"); - }); - - modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer") - .UseIdentityByDefaultColumn(); - - b.Property("ClaimType") - .HasColumnType("text"); - - b.Property("ClaimValue") - .HasColumnType("text"); - - b.Property("UserId") - .HasColumnType("uuid"); - - b.HasKey("Id"); - - b.HasIndex("UserId"); - - b.ToTable("AspNetUserClaims"); - }); - - modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => - { - b.Property("LoginProvider") - .HasColumnType("text"); - - b.Property("ProviderKey") - .HasColumnType("text"); - - b.Property("ProviderDisplayName") - .HasColumnType("text"); - - b.Property("UserId") - .HasColumnType("uuid"); - - b.HasKey("LoginProvider", "ProviderKey"); - - b.HasIndex("UserId"); - - b.ToTable("AspNetUserLogins"); - }); - - modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => - { - b.Property("UserId") - .HasColumnType("uuid"); - - b.Property("RoleId") - .HasColumnType("uuid"); - - b.HasKey("UserId", "RoleId"); - - b.HasIndex("RoleId"); - - b.ToTable("AspNetUserRoles"); - }); - - modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => - { - b.Property("UserId") - .HasColumnType("uuid"); - - b.Property("LoginProvider") - .HasColumnType("text"); - - b.Property("Name") - .HasColumnType("text"); - - b.Property("Value") - .HasColumnType("text"); - - b.HasKey("UserId", "LoginProvider", "Name"); - - b.ToTable("AspNetUserTokens"); - }); - - modelBuilder.Entity("RoleUser", b => - { - b.Property("RolesId") - .HasColumnType("uuid"); - - b.Property("UsersId") - .HasColumnType("uuid"); - - b.HasKey("RolesId", "UsersId"); - - b.HasIndex("UsersId"); - - b.ToTable("RoleUser"); - }); - - modelBuilder.Entity("TechnologyUser", b => - { - b.Property("TechnologiesId") - .HasColumnType("uuid"); - - b.Property("UsersId") - .HasColumnType("uuid"); - - b.HasKey("TechnologiesId", "UsersId"); - - b.HasIndex("UsersId"); - - b.ToTable("TechnologyUser"); - }); - - modelBuilder.Entity("DevHive.Data.Models.Comment", b => - { - b.HasOne("DevHive.Data.Models.Post", null) - .WithMany("Comments") - .HasForeignKey("PostId"); - }); - - modelBuilder.Entity("DevHive.Data.Models.User", b => - { - b.HasOne("DevHive.Data.Models.User", null) - .WithMany("Friends") - .HasForeignKey("UserId"); - }); - - modelBuilder.Entity("LanguageUser", b => - { - b.HasOne("DevHive.Data.Models.Language", null) - .WithMany() - .HasForeignKey("LanguagesId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("DevHive.Data.Models.User", null) - .WithMany() - .HasForeignKey("UsersId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - }); - - modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => - { - b.HasOne("DevHive.Data.Models.Role", null) - .WithMany() - .HasForeignKey("RoleId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - }); - - modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => - { - b.HasOne("DevHive.Data.Models.User", null) - .WithMany() - .HasForeignKey("UserId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - }); - - modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => - { - b.HasOne("DevHive.Data.Models.User", null) - .WithMany() - .HasForeignKey("UserId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - }); - - modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => - { - b.HasOne("DevHive.Data.Models.Role", null) - .WithMany() - .HasForeignKey("RoleId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("DevHive.Data.Models.User", null) - .WithMany() - .HasForeignKey("UserId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - }); - - modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => - { - b.HasOne("DevHive.Data.Models.User", null) - .WithMany() - .HasForeignKey("UserId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - }); - - modelBuilder.Entity("RoleUser", b => - { - b.HasOne("DevHive.Data.Models.Role", null) - .WithMany() - .HasForeignKey("RolesId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("DevHive.Data.Models.User", null) - .WithMany() - .HasForeignKey("UsersId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - }); - - modelBuilder.Entity("TechnologyUser", b => - { - b.HasOne("DevHive.Data.Models.Technology", null) - .WithMany() - .HasForeignKey("TechnologiesId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("DevHive.Data.Models.User", null) - .WithMany() - .HasForeignKey("UsersId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - }); - - modelBuilder.Entity("DevHive.Data.Models.Post", b => - { - b.Navigation("Comments"); - }); - - modelBuilder.Entity("DevHive.Data.Models.User", b => - { - b.Navigation("Friends"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/src/DevHive.Data/Migrations/20210121083441_UserRefactor.cs b/src/DevHive.Data/Migrations/20210121083441_UserRefactor.cs deleted file mode 100644 index 6eb1e38..0000000 --- a/src/DevHive.Data/Migrations/20210121083441_UserRefactor.cs +++ /dev/null @@ -1,411 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; -using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; - -namespace DevHive.Data.Migrations -{ - public partial class UserRefactor : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "AspNetRoles", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - Name = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - NormalizedName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - ConcurrencyStamp = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetRoles", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "AspNetUsers", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - FirstName = table.Column(type: "text", nullable: true), - LastName = table.Column(type: "text", nullable: true), - ProfilePictureUrl = table.Column(type: "text", nullable: true), - UserId = table.Column(type: "uuid", nullable: true), - UserName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - NormalizedUserName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - Email = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - NormalizedEmail = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - EmailConfirmed = table.Column(type: "boolean", nullable: false), - PasswordHash = table.Column(type: "text", nullable: true), - SecurityStamp = table.Column(type: "text", nullable: true), - ConcurrencyStamp = table.Column(type: "text", nullable: true), - PhoneNumber = table.Column(type: "text", nullable: true), - PhoneNumberConfirmed = table.Column(type: "boolean", nullable: false), - TwoFactorEnabled = table.Column(type: "boolean", nullable: false), - LockoutEnd = table.Column(type: "timestamp with time zone", nullable: true), - LockoutEnabled = table.Column(type: "boolean", nullable: false), - AccessFailedCount = table.Column(type: "integer", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetUsers", x => x.Id); - table.ForeignKey( - name: "FK_AspNetUsers_AspNetUsers_UserId", - column: x => x.UserId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - }); - - migrationBuilder.CreateTable( - name: "Languages", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - Name = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Languages", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "Posts", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - IssuerId = table.Column(type: "uuid", nullable: false), - TimeCreated = table.Column(type: "timestamp without time zone", nullable: false), - Message = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Posts", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "Technologies", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - Name = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Technologies", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "AspNetRoleClaims", - columns: table => new - { - Id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - RoleId = table.Column(type: "uuid", nullable: false), - ClaimType = table.Column(type: "text", nullable: true), - ClaimValue = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetRoleClaims", x => x.Id); - table.ForeignKey( - name: "FK_AspNetRoleClaims_AspNetRoles_RoleId", - column: x => x.RoleId, - principalTable: "AspNetRoles", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "AspNetUserClaims", - columns: table => new - { - Id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - UserId = table.Column(type: "uuid", nullable: false), - ClaimType = table.Column(type: "text", nullable: true), - ClaimValue = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetUserClaims", x => x.Id); - table.ForeignKey( - name: "FK_AspNetUserClaims_AspNetUsers_UserId", - column: x => x.UserId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "AspNetUserLogins", - columns: table => new - { - LoginProvider = table.Column(type: "text", nullable: false), - ProviderKey = table.Column(type: "text", nullable: false), - ProviderDisplayName = table.Column(type: "text", nullable: true), - UserId = table.Column(type: "uuid", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetUserLogins", x => new { x.LoginProvider, x.ProviderKey }); - table.ForeignKey( - name: "FK_AspNetUserLogins_AspNetUsers_UserId", - column: x => x.UserId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "AspNetUserRoles", - columns: table => new - { - UserId = table.Column(type: "uuid", nullable: false), - RoleId = table.Column(type: "uuid", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetUserRoles", x => new { x.UserId, x.RoleId }); - table.ForeignKey( - name: "FK_AspNetUserRoles_AspNetRoles_RoleId", - column: x => x.RoleId, - principalTable: "AspNetRoles", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_AspNetUserRoles_AspNetUsers_UserId", - column: x => x.UserId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "AspNetUserTokens", - columns: table => new - { - UserId = table.Column(type: "uuid", nullable: false), - LoginProvider = table.Column(type: "text", nullable: false), - Name = table.Column(type: "text", nullable: false), - Value = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetUserTokens", x => new { x.UserId, x.LoginProvider, x.Name }); - table.ForeignKey( - name: "FK_AspNetUserTokens_AspNetUsers_UserId", - column: x => x.UserId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "RoleUser", - columns: table => new - { - RolesId = table.Column(type: "uuid", nullable: false), - UsersId = table.Column(type: "uuid", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_RoleUser", x => new { x.RolesId, x.UsersId }); - table.ForeignKey( - name: "FK_RoleUser_AspNetRoles_RolesId", - column: x => x.RolesId, - principalTable: "AspNetRoles", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_RoleUser_AspNetUsers_UsersId", - column: x => x.UsersId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "LanguageUser", - columns: table => new - { - LanguagesId = table.Column(type: "uuid", nullable: false), - UsersId = table.Column(type: "uuid", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_LanguageUser", x => new { x.LanguagesId, x.UsersId }); - table.ForeignKey( - name: "FK_LanguageUser_AspNetUsers_UsersId", - column: x => x.UsersId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_LanguageUser_Languages_LanguagesId", - column: x => x.LanguagesId, - principalTable: "Languages", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "Comments", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - IssuerId = table.Column(type: "uuid", nullable: false), - Message = table.Column(type: "text", nullable: true), - TimeCreated = table.Column(type: "timestamp without time zone", nullable: false), - PostId = table.Column(type: "uuid", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Comments", x => x.Id); - table.ForeignKey( - name: "FK_Comments_Posts_PostId", - column: x => x.PostId, - principalTable: "Posts", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - }); - - migrationBuilder.CreateTable( - name: "TechnologyUser", - columns: table => new - { - TechnologiesId = table.Column(type: "uuid", nullable: false), - UsersId = table.Column(type: "uuid", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_TechnologyUser", x => new { x.TechnologiesId, x.UsersId }); - table.ForeignKey( - name: "FK_TechnologyUser_AspNetUsers_UsersId", - column: x => x.UsersId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_TechnologyUser_Technologies_TechnologiesId", - column: x => x.TechnologiesId, - principalTable: "Technologies", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateIndex( - name: "IX_AspNetRoleClaims_RoleId", - table: "AspNetRoleClaims", - column: "RoleId"); - - migrationBuilder.CreateIndex( - name: "RoleNameIndex", - table: "AspNetRoles", - column: "NormalizedName", - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_AspNetUserClaims_UserId", - table: "AspNetUserClaims", - column: "UserId"); - - migrationBuilder.CreateIndex( - name: "IX_AspNetUserLogins_UserId", - table: "AspNetUserLogins", - column: "UserId"); - - migrationBuilder.CreateIndex( - name: "IX_AspNetUserRoles_RoleId", - table: "AspNetUserRoles", - column: "RoleId"); - - migrationBuilder.CreateIndex( - name: "EmailIndex", - table: "AspNetUsers", - column: "NormalizedEmail"); - - migrationBuilder.CreateIndex( - name: "IX_AspNetUsers_UserId", - table: "AspNetUsers", - column: "UserId"); - - migrationBuilder.CreateIndex( - name: "IX_AspNetUsers_UserName", - table: "AspNetUsers", - column: "UserName", - unique: true); - - migrationBuilder.CreateIndex( - name: "UserNameIndex", - table: "AspNetUsers", - column: "NormalizedUserName", - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_Comments_PostId", - table: "Comments", - column: "PostId"); - - migrationBuilder.CreateIndex( - name: "IX_LanguageUser_UsersId", - table: "LanguageUser", - column: "UsersId"); - - migrationBuilder.CreateIndex( - name: "IX_RoleUser_UsersId", - table: "RoleUser", - column: "UsersId"); - - migrationBuilder.CreateIndex( - name: "IX_TechnologyUser_UsersId", - table: "TechnologyUser", - column: "UsersId"); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "AspNetRoleClaims"); - - migrationBuilder.DropTable( - name: "AspNetUserClaims"); - - migrationBuilder.DropTable( - name: "AspNetUserLogins"); - - migrationBuilder.DropTable( - name: "AspNetUserRoles"); - - migrationBuilder.DropTable( - name: "AspNetUserTokens"); - - migrationBuilder.DropTable( - name: "Comments"); - - migrationBuilder.DropTable( - name: "LanguageUser"); - - migrationBuilder.DropTable( - name: "RoleUser"); - - migrationBuilder.DropTable( - name: "TechnologyUser"); - - migrationBuilder.DropTable( - name: "Posts"); - - migrationBuilder.DropTable( - name: "Languages"); - - migrationBuilder.DropTable( - name: "AspNetRoles"); - - migrationBuilder.DropTable( - name: "AspNetUsers"); - - migrationBuilder.DropTable( - name: "Technologies"); - } - } -} diff --git a/src/DevHive.Data/Migrations/20210123215634_PostAndComment_Implemented.Designer.cs b/src/DevHive.Data/Migrations/20210123215634_PostAndComment_Implemented.Designer.cs new file mode 100644 index 0000000..0e4b103 --- /dev/null +++ b/src/DevHive.Data/Migrations/20210123215634_PostAndComment_Implemented.Designer.cs @@ -0,0 +1,476 @@ +// +using System; +using DevHive.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace DevHive.Data.Migrations +{ + [DbContext(typeof(DevHiveContext))] + [Migration("20210123215634_PostAndComment_Implemented")] + partial class PostAndComment_Implemented + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseIdentityByDefaultColumns() + .HasAnnotation("Relational:MaxIdentifierLength", 63) + .HasAnnotation("ProductVersion", "5.0.1"); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatorId") + .HasColumnType("uuid"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.HasIndex("PostId"); + + b.ToTable("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Language", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Languages"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatorId") + .HasColumnType("uuid"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.ToTable("Posts"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Technology", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Technologies"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AccessFailedCount") + .HasColumnType("integer"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("EmailConfirmed") + .HasColumnType("boolean"); + + b.Property("FirstName") + .HasColumnType("text"); + + b.Property("LastName") + .HasColumnType("text"); + + b.Property("LockoutEnabled") + .HasColumnType("boolean"); + + b.Property("LockoutEnd") + .HasColumnType("timestamp with time zone"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("PasswordHash") + .HasColumnType("text"); + + b.Property("PhoneNumber") + .HasColumnType("text"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("boolean"); + + b.Property("ProfilePictureUrl") + .HasColumnType("text"); + + b.Property("SecurityStamp") + .HasColumnType("text"); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.HasIndex("UserId"); + + b.HasIndex("UserName") + .IsUnique(); + + b.ToTable("AspNetUsers"); + }); + + modelBuilder.Entity("LanguageUser", b => + { + b.Property("LanguagesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("LanguagesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("LanguageUser"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("ProviderKey") + .HasColumnType("text"); + + b.Property("ProviderDisplayName") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.Property("RolesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("RolesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("RoleUser"); + }); + + modelBuilder.Entity("TechnologyUser", b => + { + b.Property("TechnologiesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("TechnologiesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("TechnologyUser"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.HasOne("DevHive.Data.Models.Post", null) + .WithMany("Comments") + .HasForeignKey("PostId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany("Friends") + .HasForeignKey("UserId"); + }); + + modelBuilder.Entity("LanguageUser", b => + { + b.HasOne("DevHive.Data.Models.Language", null) + .WithMany() + .HasForeignKey("LanguagesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RolesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("TechnologyUser", b => + { + b.HasOne("DevHive.Data.Models.Technology", null) + .WithMany() + .HasForeignKey("TechnologiesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Navigation("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Navigation("Friends"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/DevHive.Data/Migrations/20210123215634_PostAndComment_Implemented.cs b/src/DevHive.Data/Migrations/20210123215634_PostAndComment_Implemented.cs new file mode 100644 index 0000000..4c9f3bd --- /dev/null +++ b/src/DevHive.Data/Migrations/20210123215634_PostAndComment_Implemented.cs @@ -0,0 +1,411 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace DevHive.Data.Migrations +{ + public partial class PostAndComment_Implemented : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "AspNetRoles", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + Name = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + NormalizedName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + ConcurrencyStamp = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetRoles", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "AspNetUsers", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + FirstName = table.Column(type: "text", nullable: true), + LastName = table.Column(type: "text", nullable: true), + ProfilePictureUrl = table.Column(type: "text", nullable: true), + UserId = table.Column(type: "uuid", nullable: true), + UserName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + NormalizedUserName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + Email = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + NormalizedEmail = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + EmailConfirmed = table.Column(type: "boolean", nullable: false), + PasswordHash = table.Column(type: "text", nullable: true), + SecurityStamp = table.Column(type: "text", nullable: true), + ConcurrencyStamp = table.Column(type: "text", nullable: true), + PhoneNumber = table.Column(type: "text", nullable: true), + PhoneNumberConfirmed = table.Column(type: "boolean", nullable: false), + TwoFactorEnabled = table.Column(type: "boolean", nullable: false), + LockoutEnd = table.Column(type: "timestamp with time zone", nullable: true), + LockoutEnabled = table.Column(type: "boolean", nullable: false), + AccessFailedCount = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUsers", x => x.Id); + table.ForeignKey( + name: "FK_AspNetUsers_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "Languages", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + Name = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Languages", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Posts", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + CreatorId = table.Column(type: "uuid", nullable: false), + Message = table.Column(type: "text", nullable: true), + TimeCreated = table.Column(type: "timestamp without time zone", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Posts", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Technologies", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + Name = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Technologies", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "AspNetRoleClaims", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + RoleId = table.Column(type: "uuid", nullable: false), + ClaimType = table.Column(type: "text", nullable: true), + ClaimValue = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetRoleClaims", x => x.Id); + table.ForeignKey( + name: "FK_AspNetRoleClaims_AspNetRoles_RoleId", + column: x => x.RoleId, + principalTable: "AspNetRoles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserClaims", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + UserId = table.Column(type: "uuid", nullable: false), + ClaimType = table.Column(type: "text", nullable: true), + ClaimValue = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserClaims", x => x.Id); + table.ForeignKey( + name: "FK_AspNetUserClaims_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserLogins", + columns: table => new + { + LoginProvider = table.Column(type: "text", nullable: false), + ProviderKey = table.Column(type: "text", nullable: false), + ProviderDisplayName = table.Column(type: "text", nullable: true), + UserId = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserLogins", x => new { x.LoginProvider, x.ProviderKey }); + table.ForeignKey( + name: "FK_AspNetUserLogins_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserRoles", + columns: table => new + { + UserId = table.Column(type: "uuid", nullable: false), + RoleId = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserRoles", x => new { x.UserId, x.RoleId }); + table.ForeignKey( + name: "FK_AspNetUserRoles_AspNetRoles_RoleId", + column: x => x.RoleId, + principalTable: "AspNetRoles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AspNetUserRoles_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserTokens", + columns: table => new + { + UserId = table.Column(type: "uuid", nullable: false), + LoginProvider = table.Column(type: "text", nullable: false), + Name = table.Column(type: "text", nullable: false), + Value = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserTokens", x => new { x.UserId, x.LoginProvider, x.Name }); + table.ForeignKey( + name: "FK_AspNetUserTokens_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "RoleUser", + columns: table => new + { + RolesId = table.Column(type: "uuid", nullable: false), + UsersId = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_RoleUser", x => new { x.RolesId, x.UsersId }); + table.ForeignKey( + name: "FK_RoleUser_AspNetRoles_RolesId", + column: x => x.RolesId, + principalTable: "AspNetRoles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_RoleUser_AspNetUsers_UsersId", + column: x => x.UsersId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "LanguageUser", + columns: table => new + { + LanguagesId = table.Column(type: "uuid", nullable: false), + UsersId = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_LanguageUser", x => new { x.LanguagesId, x.UsersId }); + table.ForeignKey( + name: "FK_LanguageUser_AspNetUsers_UsersId", + column: x => x.UsersId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_LanguageUser_Languages_LanguagesId", + column: x => x.LanguagesId, + principalTable: "Languages", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Comments", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + PostId = table.Column(type: "uuid", nullable: false), + CreatorId = table.Column(type: "uuid", nullable: false), + Message = table.Column(type: "text", nullable: true), + TimeCreated = table.Column(type: "timestamp without time zone", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Comments", x => x.Id); + table.ForeignKey( + name: "FK_Comments_Posts_PostId", + column: x => x.PostId, + principalTable: "Posts", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "TechnologyUser", + columns: table => new + { + TechnologiesId = table.Column(type: "uuid", nullable: false), + UsersId = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_TechnologyUser", x => new { x.TechnologiesId, x.UsersId }); + table.ForeignKey( + name: "FK_TechnologyUser_AspNetUsers_UsersId", + column: x => x.UsersId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_TechnologyUser_Technologies_TechnologiesId", + column: x => x.TechnologiesId, + principalTable: "Technologies", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_AspNetRoleClaims_RoleId", + table: "AspNetRoleClaims", + column: "RoleId"); + + migrationBuilder.CreateIndex( + name: "RoleNameIndex", + table: "AspNetRoles", + column: "NormalizedName", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserClaims_UserId", + table: "AspNetUserClaims", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserLogins_UserId", + table: "AspNetUserLogins", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserRoles_RoleId", + table: "AspNetUserRoles", + column: "RoleId"); + + migrationBuilder.CreateIndex( + name: "EmailIndex", + table: "AspNetUsers", + column: "NormalizedEmail"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUsers_UserId", + table: "AspNetUsers", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUsers_UserName", + table: "AspNetUsers", + column: "UserName", + unique: true); + + migrationBuilder.CreateIndex( + name: "UserNameIndex", + table: "AspNetUsers", + column: "NormalizedUserName", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Comments_PostId", + table: "Comments", + column: "PostId"); + + migrationBuilder.CreateIndex( + name: "IX_LanguageUser_UsersId", + table: "LanguageUser", + column: "UsersId"); + + migrationBuilder.CreateIndex( + name: "IX_RoleUser_UsersId", + table: "RoleUser", + column: "UsersId"); + + migrationBuilder.CreateIndex( + name: "IX_TechnologyUser_UsersId", + table: "TechnologyUser", + column: "UsersId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "AspNetRoleClaims"); + + migrationBuilder.DropTable( + name: "AspNetUserClaims"); + + migrationBuilder.DropTable( + name: "AspNetUserLogins"); + + migrationBuilder.DropTable( + name: "AspNetUserRoles"); + + migrationBuilder.DropTable( + name: "AspNetUserTokens"); + + migrationBuilder.DropTable( + name: "Comments"); + + migrationBuilder.DropTable( + name: "LanguageUser"); + + migrationBuilder.DropTable( + name: "RoleUser"); + + migrationBuilder.DropTable( + name: "TechnologyUser"); + + migrationBuilder.DropTable( + name: "Posts"); + + migrationBuilder.DropTable( + name: "Languages"); + + migrationBuilder.DropTable( + name: "AspNetRoles"); + + migrationBuilder.DropTable( + name: "AspNetUsers"); + + migrationBuilder.DropTable( + name: "Technologies"); + } + } +} diff --git a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs index 0727d33..755c274 100644 --- a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs +++ b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs @@ -25,13 +25,13 @@ namespace DevHive.Data.Migrations .ValueGeneratedOnAdd() .HasColumnType("uuid"); - b.Property("IssuerId") + b.Property("CreatorId") .HasColumnType("uuid"); b.Property("Message") .HasColumnType("text"); - b.Property("PostId") + b.Property("PostId") .HasColumnType("uuid"); b.Property("TimeCreated") @@ -64,7 +64,7 @@ namespace DevHive.Data.Migrations .ValueGeneratedOnAdd() .HasColumnType("uuid"); - b.Property("IssuerId") + b.Property("CreatorId") .HasColumnType("uuid"); b.Property("Message") @@ -351,7 +351,9 @@ namespace DevHive.Data.Migrations { b.HasOne("DevHive.Data.Models.Post", null) .WithMany("Comments") - .HasForeignKey("PostId"); + .HasForeignKey("PostId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); }); modelBuilder.Entity("DevHive.Data.Models.User", b => diff --git a/src/DevHive.Data/Models/Comment.cs b/src/DevHive.Data/Models/Comment.cs index 5f4207d..c953355 100644 --- a/src/DevHive.Data/Models/Comment.cs +++ b/src/DevHive.Data/Models/Comment.cs @@ -9,7 +9,7 @@ namespace DevHive.Data.Models public Guid PostId { get; set; } - public Guid IssuerId { get; set; } + public Guid CreatorId { get; set; } public string Message { get; set; } diff --git a/src/DevHive.Data/Models/Language.cs b/src/DevHive.Data/Models/Language.cs index f2b2786..7ad8ff2 100644 --- a/src/DevHive.Data/Models/Language.cs +++ b/src/DevHive.Data/Models/Language.cs @@ -7,7 +7,9 @@ namespace DevHive.Data.Models public class Language : ILanguage { public Guid Id { get; set; } + public string Name { get; set; } - public HashSet Users { get; set; } + + public HashSet Users { get; set; } = new(); } } diff --git a/src/DevHive.Data/Models/Post.cs b/src/DevHive.Data/Models/Post.cs index f7bca43..1b1ec4d 100644 --- a/src/DevHive.Data/Models/Post.cs +++ b/src/DevHive.Data/Models/Post.cs @@ -16,8 +16,8 @@ namespace DevHive.Data.Models public DateTime TimeCreated { get; set; } - public List Comments { get; set; } + public List Comments { get; set; } = new(); - // public List Files { get; set; } + // public List Files { get; set; } = new(); } } diff --git a/src/DevHive.Data/Models/Role.cs b/src/DevHive.Data/Models/Role.cs index e0855aa..259d867 100644 --- a/src/DevHive.Data/Models/Role.cs +++ b/src/DevHive.Data/Models/Role.cs @@ -12,6 +12,6 @@ namespace DevHive.Data.Models public const string DefaultRole = "User"; public const string AdminRole = "Admin"; - public HashSet Users { get; set; } + public HashSet Users { get; set; } = new(); } } diff --git a/src/DevHive.Data/Models/Technology.cs b/src/DevHive.Data/Models/Technology.cs index a0728d5..6f98f0b 100644 --- a/src/DevHive.Data/Models/Technology.cs +++ b/src/DevHive.Data/Models/Technology.cs @@ -7,7 +7,9 @@ namespace DevHive.Data.Models public class Technology : ITechnology { public Guid Id { get; set; } + public string Name { get; set; } - public HashSet Users { get; set; } + + public HashSet Users { get; set; } = new(); } } diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs index 2ac7adf..df440d5 100644 --- a/src/DevHive.Data/Models/User.cs +++ b/src/DevHive.Data/Models/User.cs @@ -19,15 +19,15 @@ namespace DevHive.Data.Models /// Languages that the user uses or is familiar with /// // [Unique] - public HashSet Languages { get; set; } + public HashSet Languages { get; set; } = new(); /// /// Technologies that the user uses or is familiar with /// - public HashSet Technologies { get; set; } = new HashSet(); + public HashSet Technologies { get; set; } = new(); - public HashSet Roles { get; set; } = new HashSet(); + public HashSet Roles { get; set; } = new(); - public HashSet Friends { get; set; } = new HashSet(); + public HashSet Friends { get; set; } = new(); } } diff --git a/src/DevHive.Data/Repositories/CommentRepository.cs b/src/DevHive.Data/Repositories/CommentRepository.cs index 880631a..006326a 100644 --- a/src/DevHive.Data/Repositories/CommentRepository.cs +++ b/src/DevHive.Data/Repositories/CommentRepository.cs @@ -20,7 +20,7 @@ namespace DevHive.Data.Repositories public async Task GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) { return await this._context.Comments - .FirstOrDefaultAsync(p => p.IssuerId == issuerId && + .FirstOrDefaultAsync(p => p.CreatorId == issuerId && p.TimeCreated == timeCreated); } #endregion diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index a79eacf..e8180de 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -17,6 +17,13 @@ namespace DevHive.Data.Repositories } #region Read + public override async Task GetByIdAsync(Guid id) + { + return await this._context.Posts + .Include(x => x.Comments) + .FirstOrDefaultAsync(x => x.Id == id); + } + public async Task GetPostByCreatorAndTimeCreatedAsync(Guid creatorId, DateTime timeCreated) { return await this._context.Posts diff --git a/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs b/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs index 3cea801..ac3c8f6 100644 --- a/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs @@ -14,7 +14,10 @@ namespace DevHive.Services.Configurations.Mapping .ForMember(dest => dest.Message, src => src.MapFrom(p => p.NewMessage)); CreateMap() - .ForMember(dest => dest.CommentId, src => src.MapFrom(p => p.Id)); + .ForMember(dest => dest.CommentId, src => src.MapFrom(p => p.Id)) + .ForMember(dest => dest.IssuerFirstName, src => src.Ignore()) + .ForMember(dest => dest.IssuerLastName, src => src.Ignore()) + .ForMember(dest => dest.IssuerUsername, src => src.Ignore()); } } } diff --git a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs index e4924a5..cea7b1c 100644 --- a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs @@ -16,7 +16,10 @@ namespace DevHive.Services.Configurations.Mapping .ForMember(dest => dest.Message, src => src.MapFrom(p => p.NewMessage)); CreateMap() - .ForMember(dest => dest.PostId, src => src.MapFrom(p => p.Id)); + .ForMember(dest => dest.PostId, src => src.MapFrom(p => p.Id)) + .ForMember(dest => dest.CreatorFirstName, src => src.Ignore()) + .ForMember(dest => dest.CreatorLastName, src => src.Ignore()) + .ForMember(dest => dest.CreatorUsername, src => src.Ignore()); } } } diff --git a/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs index 4dfd848..8d49659 100644 --- a/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs @@ -6,10 +6,8 @@ namespace DevHive.Services.Models.Post.Comment { public Guid PostId { get; set; } - public Guid IssuerId { get; set; } + public Guid CreatorId { get; set; } public string Message { get; set; } - - public DateTime TimeCreated { get; set; } } } diff --git a/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs index c6ff612..12e29a0 100644 --- a/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs @@ -6,7 +6,11 @@ namespace DevHive.Services.Models.Post.Comment { public Guid CommentId { get; set; } - public Guid IssuerId { get; set; } + public string IssuerFirstName { get; set; } + + public string IssuerLastName { get; set; } + + public string IssuerUsername { get; set; } public Guid PostId { get; set; } diff --git a/src/DevHive.Services/Models/Post/Comment/UpdateCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/UpdateCommentServiceModel.cs new file mode 100644 index 0000000..3827d4d --- /dev/null +++ b/src/DevHive.Services/Models/Post/Comment/UpdateCommentServiceModel.cs @@ -0,0 +1,15 @@ +using System; + +namespace DevHive.Services.Models.Post.Comment +{ + public class UpdateCommentServiceModel + { + public Guid CreatorId { get; set; } + + public Guid CommentId { get; set; } + + public Guid PostId { get; set; } + + public string NewMessage { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs deleted file mode 100644 index 51cd739..0000000 --- a/src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Post.Comment -{ - public class UpdateCommentServiceModel - { - public Guid CommentId { get; set; } - - public string NewMessage { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs index 6b83f3e..36f6351 100644 --- a/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs @@ -4,12 +4,10 @@ namespace DevHive.Services.Models.Post.Post { public class CreatePostServiceModel { - public Guid IssuerId { get; set; } + public Guid CreatorId { get; set; } public string Message { get; set; } - public DateTime TimeCreated { get; set; } - // public List Files { get; set; } } } diff --git a/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs index 52b9232..3e673c1 100644 --- a/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs @@ -8,13 +8,17 @@ namespace DevHive.Services.Models.Post.Post { public Guid PostId { get; set; } - public Guid CreatorId { get; set; } + public string CreatorFirstName { get; set; } + + public string CreatorLastName { get; set; } + + public string CreatorUsername { get; set; } public string Message { get; set; } public DateTime TimeCreated { get; set; } - public List Comments { get; set; } + public List Comments { get; set; } = new(); //public List Files { get; set; } } diff --git a/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs index 67ee711..8924b07 100644 --- a/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs @@ -6,6 +6,8 @@ namespace DevHive.Services.Models.Post.Post { public Guid PostId { get; set; } + public Guid CreatorId { get; set; } + public string NewMessage { get; set; } // public List Files { get; set; } diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index 377fe05..7fc975c 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -31,6 +31,9 @@ namespace DevHive.Services.Services #region Create public async Task CreatePost(CreatePostServiceModel createPostServiceModel) { + if(!await this._userRepository.DoesUserExistAsync(createPostServiceModel.CreatorId)) + throw new ArgumentException("User does not exist!"); + Post post = this._postMapper.Map(createPostServiceModel); post.TimeCreated = DateTime.Now; @@ -38,7 +41,7 @@ namespace DevHive.Services.Services if (success) { Post newPost = await this._postRepository - .GetPostByCreatorAndTimeCreatedAsync(createPostServiceModel.IssuerId, createPostServiceModel.TimeCreated); + .GetPostByCreatorAndTimeCreatedAsync(post.CreatorId, post.TimeCreated); return newPost.Id; } @@ -52,13 +55,13 @@ namespace DevHive.Services.Services throw new ArgumentException("Post does not exist!"); Comment comment = this._postMapper.Map(createCommentServiceModel); - createCommentServiceModel.TimeCreated = DateTime.Now; + comment.TimeCreated = DateTime.Now; bool success = await this._commentRepository.AddAsync(comment); if (success) { Comment newComment = await this._commentRepository - .GetCommentByIssuerAndTimeCreatedAsync(createCommentServiceModel.IssuerId, createCommentServiceModel.TimeCreated); + .GetCommentByIssuerAndTimeCreatedAsync(comment.CreatorId, comment.TimeCreated); return newComment.Id; } @@ -73,7 +76,15 @@ namespace DevHive.Services.Services Post post = await this._postRepository.GetByIdAsync(id) ?? throw new ArgumentException("The post does not exist!"); - return this._postMapper.Map(post); + User user = await this._userRepository.GetByIdAsync(post.CreatorId) ?? + throw new ArgumentException("User does not exist He could've been deleted!"); + + ReadPostServiceModel readPostServiceModel = this._postMapper.Map(post); + readPostServiceModel.CreatorFirstName = user.FirstName; + readPostServiceModel.CreatorLastName = user.LastName; + readPostServiceModel.CreatorUsername = user.UserName; + + return readPostServiceModel; } public async Task GetCommentById(Guid id) @@ -81,7 +92,14 @@ namespace DevHive.Services.Services Comment comment = await this._commentRepository.GetByIdAsync(id) ?? throw new ArgumentException("The comment does not exist"); - return this._postMapper.Map(comment); + User user = await this._userRepository.GetByIdAsync(comment.CreatorId); + + ReadCommentServiceModel readCommentServiceModel = this._postMapper.Map(comment); + readCommentServiceModel.IssuerFirstName = user.FirstName; + readCommentServiceModel.IssuerLastName = user.LastName; + readCommentServiceModel.IssuerUsername = user.UserName; + + return readCommentServiceModel; } #endregion @@ -92,6 +110,8 @@ namespace DevHive.Services.Services throw new ArgumentException("Post does not exist!"); Post post = this._postMapper.Map(updatePostServiceModel); + post.TimeCreated = DateTime.Now; + bool result = await this._postRepository.EditAsync(updatePostServiceModel.PostId, post); if (result) @@ -106,6 +126,8 @@ namespace DevHive.Services.Services throw new ArgumentException("Comment does not exist!"); Comment comment = this._postMapper.Map(updateCommentServiceModel); + comment.TimeCreated = DateTime.Now; + bool result = await this._commentRepository.EditAsync(updateCommentServiceModel.CommentId, comment); if (result) @@ -138,29 +160,45 @@ namespace DevHive.Services.Services #region Validations public async Task ValidateJwtForPost(Guid postId, string rawTokenData) { - Post post = await this._postRepository.GetByIdAsync(postId); + Post post = await this._postRepository.GetByIdAsync(postId) ?? + throw new ArgumentException("Post does not exist!"); User user = await this.GetUserForValidation(rawTokenData); - return post.CreatorId == user.Id; + //If user made the post + if (post.CreatorId == user.Id) + return true; + //If user is admin + else if(user.Roles.Any(x => x.Name == Role.AdminRole)) + return true; + else + return false; } public async Task ValidateJwtForComment(Guid commentId, string rawTokenData) { - Comment comment = await this._commentRepository.GetByIdAsync(commentId); + Comment comment = await this._commentRepository.GetByIdAsync(commentId) ?? + throw new ArgumentException("Comment does not exist!"); User user = await this.GetUserForValidation(rawTokenData); - return comment.IssuerId == user.Id; + //If user made the comment + if (comment.CreatorId == user.Id) + return true; + //If user is admin + else if(user.Roles.Any(x => x.Name == Role.AdminRole)) + return true; + else + return false; } private async Task GetUserForValidation(string rawTokenData) { - var jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7)); + JwtSecurityToken jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7)); - string jwtUserName = this.GetClaimTypeValues("unique_name", jwt.Claims).First(); + Guid jwtUserId = Guid.Parse(this.GetClaimTypeValues("ID", jwt.Claims).First()); //HashSet jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims); - User user = await this._userRepository.GetByUsernameAsync(jwtUserName) - ?? throw new ArgumentException("User does not exist!"); + User user = await this._userRepository.GetByIdAsync(jwtUserId) ?? + throw new ArgumentException("User does not exist!"); return user; } diff --git a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs index 296704e..a28ee16 100644 --- a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs @@ -11,10 +11,7 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap(); - CreateMap() - .ForMember(dest => dest.IssuerFirstName, src => src.Ignore()) - .ForMember(dest => dest.IssuerLastName, src => src.Ignore()) - .ForMember(dest => dest.IssuerUsername, src => src.Ignore()); + CreateMap(); } } } diff --git a/src/DevHive.Web/Configurations/Mapping/PostMappings.cs b/src/DevHive.Web/Configurations/Mapping/PostMappings.cs index 0e966cc..bc7bc06 100644 --- a/src/DevHive.Web/Configurations/Mapping/PostMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/PostMappings.cs @@ -11,10 +11,7 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap(); - CreateMap() - .ForMember(dest => dest.CreatorFirstName, src => src.Ignore()) - .ForMember(dest => dest.CreatorLastName, src => src.Ignore()) - .ForMember(dest => dest.CreatorUsername, src => src.Ignore()); + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs index 8b8b525..b5e1c98 100644 --- a/src/DevHive.Web/Controllers/PostController.cs +++ b/src/DevHive.Web/Controllers/PostController.cs @@ -31,7 +31,7 @@ namespace DevHive.Web.Controllers { CreatePostServiceModel createPostServiceModel = this._postMapper.Map(createPostWebModel); - createPostServiceModel.IssuerId = userId; + createPostServiceModel.CreatorId = userId; Guid id = await this._postService.CreatePost(createPostServiceModel); @@ -46,7 +46,7 @@ namespace DevHive.Web.Controllers { CreateCommentServiceModel createCommentServiceModel = this._postMapper.Map(createCommentWebModel); - createCommentServiceModel.IssuerId = userId; + createCommentServiceModel.CreatorId = userId; Guid id = await this._postService.AddComment(createCommentServiceModel); @@ -83,11 +83,12 @@ namespace DevHive.Web.Controllers [HttpPut] public async Task Update(Guid userId, [FromBody] UpdatePostWebModel updatePostWebModel, [FromHeader] string authorization) { - if (!await this._postService.ValidateJwtForPost(userId, authorization)) + if (!await this._postService.ValidateJwtForPost(updatePostWebModel.PostId, authorization)) return new UnauthorizedResult(); UpdatePostServiceModel updatePostServiceModel = this._postMapper.Map(updatePostWebModel); + updatePostServiceModel.CreatorId = userId; Guid id = await this._postService.UpdatePost(updatePostServiceModel); @@ -100,11 +101,12 @@ namespace DevHive.Web.Controllers [Route("Comment")] public async Task UpdateComment(Guid userId, [FromBody] UpdateCommentWebModel updateCommentWebModel, [FromHeader] string authorization) { - if (!await this._postService.ValidateJwtForComment(userId, authorization)) + if (!await this._postService.ValidateJwtForComment(updateCommentWebModel.CommentId, authorization)) return new UnauthorizedResult(); UpdateCommentServiceModel updateCommentServiceModel = this._postMapper.Map(updateCommentWebModel); + updateCommentServiceModel.CreatorId = userId; Guid id = await this._postService.UpdateComment(updateCommentServiceModel); diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index 0206542..c68a32b 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -23,7 +23,7 @@ namespace DevHive.Web.Controllers } [HttpPost] - [Authorize(Policy = "Administrator")] + // [Authorize(Policy = "Administrator")] public async Task Create([FromBody] CreateRoleWebModel createRoleWebModel) { CreateRoleServiceModel roleServiceModel = diff --git a/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs index 6dff49e..cb1c60a 100644 --- a/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs +++ b/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs @@ -6,6 +6,8 @@ namespace DevHive.Web.Models.Post.Comment { public Guid CommentId { get; set; } + public Guid PostId { get; set; } + public string NewMessage { get; set; } } } diff --git a/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs index 647b30e..b7b4cf4 100644 --- a/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs +++ b/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs @@ -6,10 +6,6 @@ namespace DevHive.Web.Models.Post.Post { public class CreatePostWebModel { - [NotNull] - [Required] - public Guid CreatorId { get; set; } - [NotNull] [Required] public string Message { get; set; } diff --git a/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs index fe42715..685f08b 100644 --- a/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs +++ b/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs @@ -12,6 +12,6 @@ namespace DevHive.Web.Models.Post.Post [NotNull] [Required] - public string Message { get; set; } + public string NewMessage { get; set; } } } -- cgit v1.2.3 From d2bc08c0dcd6f0dc0822333bbb00c9fc851f49cb Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 26 Jan 2021 10:55:25 +0200 Subject: Brief testing of GetPost --- .../Interfaces/Repositories/IFeedRepository.cs | 12 +++++ src/DevHive.Data/Repositories/FeedRepository.cs | 35 ++++++++++++++ .../Configurations/Mapping/FeedMappings.cs | 11 +++++ .../Configurations/Mapping/PostMappings.cs | 1 + .../Configurations/Mapping/RoleMapings.cs | 4 +- .../Configurations/Mapping/UserMappings.cs | 2 + src/DevHive.Services/Interfaces/IFeedService.cs | 10 ++++ src/DevHive.Services/Interfaces/IRoleService.cs | 2 +- src/DevHive.Services/Interfaces/IUserService.cs | 2 + .../Models/Feed/GetPageServiceModel.cs | 15 ++++++ .../Models/Feed/ReadPageServiceModel.cs | 10 ++++ .../Models/Identity/User/UserServiceModel.cs | 2 +- src/DevHive.Services/Services/FeedService.cs | 47 +++++++++++++++++++ src/DevHive.Services/Services/RoleService.cs | 4 +- src/DevHive.Services/Services/UserService.cs | 53 +++++++++++++++++++--- .../Extensions/ConfigureDependencyInjection.cs | 2 + .../Configurations/Mapping/FeedMappings.cs | 18 ++++++++ .../Configurations/Mapping/RoleMappings.cs | 4 +- .../Configurations/Mapping/UserMappings.cs | 3 ++ src/DevHive.Web/Controllers/FeedController.cs | 36 +++++++++++++++ src/DevHive.Web/Controllers/PostController.cs | 2 +- src/DevHive.Web/Controllers/RoleController.cs | 2 +- src/DevHive.Web/Controllers/UserController.cs | 15 +++++- src/DevHive.Web/Models/Feed/GetPageWebModel.cs | 19 ++++++++ src/DevHive.Web/Models/Feed/ReadPageWebModel.cs | 10 ++++ 25 files changed, 303 insertions(+), 18 deletions(-) create mode 100644 src/DevHive.Data/Interfaces/Repositories/IFeedRepository.cs create mode 100644 src/DevHive.Data/Repositories/FeedRepository.cs create mode 100644 src/DevHive.Services/Configurations/Mapping/FeedMappings.cs create mode 100644 src/DevHive.Services/Interfaces/IFeedService.cs create mode 100644 src/DevHive.Services/Models/Feed/GetPageServiceModel.cs create mode 100644 src/DevHive.Services/Models/Feed/ReadPageServiceModel.cs create mode 100644 src/DevHive.Services/Services/FeedService.cs create mode 100644 src/DevHive.Web/Configurations/Mapping/FeedMappings.cs create mode 100644 src/DevHive.Web/Controllers/FeedController.cs create mode 100644 src/DevHive.Web/Models/Feed/GetPageWebModel.cs create mode 100644 src/DevHive.Web/Models/Feed/ReadPageWebModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Data/Interfaces/Repositories/IFeedRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IFeedRepository.cs new file mode 100644 index 0000000..e9fd48a --- /dev/null +++ b/src/DevHive.Data/Interfaces/Repositories/IFeedRepository.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using DevHive.Data.Models; + +namespace DevHive.Data.Interfaces.Repositories +{ + public interface IFeedRepository + { + Task> GetFriendsPosts(List friendsList, DateTime firstRequestIssued, int pageNumber, int pageSize); + } +} diff --git a/src/DevHive.Data/Repositories/FeedRepository.cs b/src/DevHive.Data/Repositories/FeedRepository.cs new file mode 100644 index 0000000..8bf1f9a --- /dev/null +++ b/src/DevHive.Data/Repositories/FeedRepository.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using AutoMapper.Internal; +using DevHive.Data.Interfaces.Repositories; +using DevHive.Data.Models; +using Microsoft.EntityFrameworkCore; + +namespace DevHive.Data.Repositories +{ + public class FeedRepository : IFeedRepository + { + private readonly DevHiveContext _context; + + public FeedRepository(DevHiveContext context) + { + this._context = context; + } + public async Task> GetFriendsPosts(List friendsList, DateTime firstRequestIssued, int pageNumber, int pageSize) + { + List friendsIds = friendsList.Select(f => f.Id).ToList(); + + List posts = await this._context.Posts + .Where(post => post.TimeCreated < firstRequestIssued) + .Where(p => friendsIds.Contains(p.CreatorId)) + .OrderByDescending(x => x.TimeCreated) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize) + .ToListAsync(); + + return posts; + } + } +} 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(); - CreateMap(); + CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); CreateMap(); } } 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() .AfterMap((src, dest) => dest.PasswordHash = PasswordModifications.GeneratePasswordHash(src.Password)); CreateMap(); + CreateMap() + .ForMember(dest => dest.UserName, src => src.MapFrom(p => p.Name)); CreateMap(); CreateMap() 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 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 CreateRole(CreateRoleServiceModel roleServiceModel); - Task GetRoleById(Guid id); + Task GetRoleById(Guid id); Task 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 ValidJWT(Guid id, string rawTokenData); + + Task 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 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 Roles { get; set; } = new HashSet(); + public HashSet Roles { get; set; } = new HashSet(); public HashSet Friends { get; set; } = new HashSet(); 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 GetPage(GetPageServiceModel model) + { + User user = await this._userRepository.GetByIdAsync(model.UserId) ?? + throw new ArgumentException("User doesn't exist!"); + + List friendsList = user.Friends.ToList(); + // if(friendsList.Count == 0) + // throw new ArgumentException("This user does not have any friends!"); + + List 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(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 GetRoleById(Guid id) + public async Task GetRoleById(Guid id) { Role role = await this._roleRepository.GetByIdAsync(id) ?? throw new ArgumentException("Role does not exist!"); - return this._roleMapper.Map(role); + return this._roleMapper.Map(role); } public async Task 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 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(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 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; + } } } diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs index bcf16ac..d7c859e 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs @@ -17,12 +17,14 @@ namespace DevHive.Web.Configurations.Extensions services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } diff --git a/src/DevHive.Web/Configurations/Mapping/FeedMappings.cs b/src/DevHive.Web/Configurations/Mapping/FeedMappings.cs new file mode 100644 index 0000000..159582d --- /dev/null +++ b/src/DevHive.Web/Configurations/Mapping/FeedMappings.cs @@ -0,0 +1,18 @@ +using AutoMapper; +using DevHive.Services.Models; +using DevHive.Web.Controllers; +using DevHive.Web.Models.Feed; + +namespace DevHive.Web.Configurations.Mapping +{ + public class FeedMappings : Profile + { + public FeedMappings() + { + CreateMap() + .ForMember(dest => dest.FirstRequestIssued, src => src.MapFrom(p => p.FirstPageTimeIssued)); + + CreateMap(); + } + } +} diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 2ea2742..2f01f77 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -11,11 +11,11 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap() .ForMember(src => src.Id, dest => dest.Ignore()); - CreateMap(); + CreateMap(); CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index 9dbf613..e80a69a 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -25,6 +25,9 @@ namespace DevHive.Web.Configurations.Mapping .ForMember(src => src.Id, dest => dest.Ignore()); CreateMap() .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()) + .ForMember(src => src.Name, dest => dest.MapFrom(p => p.UserName)); CreateMap(); CreateMap(); diff --git a/src/DevHive.Web/Controllers/FeedController.cs b/src/DevHive.Web/Controllers/FeedController.cs new file mode 100644 index 0000000..7d0269b --- /dev/null +++ b/src/DevHive.Web/Controllers/FeedController.cs @@ -0,0 +1,36 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Interfaces; +using DevHive.Services.Models; +using DevHive.Web.Models.Feed; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + public class FeedController + { + private readonly IFeedService _feedService; + private readonly IMapper _mapper; + + public FeedController(IFeedService feedService, IMapper mapper) + { + this._feedService = feedService; + this._mapper = mapper; + } + + [HttpGet] + public async Task GetPosts(Guid userId, [FromBody] GetPageWebModel getPageWebModel) + { + GetPageServiceModel getPageServiceModel = this._mapper.Map(getPageWebModel); + getPageServiceModel.UserId = userId; + + ReadPageServiceModel readPageServiceModel = await this._feedService.GetPage(getPageServiceModel); + ReadPageWebModel readPageWebModel = this._mapper.Map(readPageServiceModel); + + return new OkObjectResult(readPageWebModel); + } + } +} diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs index b5e1c98..151c688 100644 --- a/src/DevHive.Web/Controllers/PostController.cs +++ b/src/DevHive.Web/Controllers/PostController.cs @@ -13,7 +13,7 @@ namespace DevHive.Web.Controllers { [ApiController] [Route("/api/[controller]")] - // [Authorize(Roles = "User")] + [Authorize(Roles = "User,Admin")] public class PostController { private readonly IPostService _postService; diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index c68a32b..d8bb60c 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -40,7 +40,7 @@ namespace DevHive.Web.Controllers [Authorize(Policy = "User")] public async Task GetById(Guid id) { - RoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); + ReadRoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); RoleWebModel roleWebModel = this._roleMapper.Map(roleServiceModel); return new OkObjectResult(roleWebModel); diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index dd94089..e409eea 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -12,7 +12,7 @@ namespace DevHive.Web.Controllers { [ApiController] [Route("/api/[controller]")] - [Authorize(Policy = "User")] + [Authorize(Roles = "User,Admin")] public class UserController : ControllerBase { private readonly IUserService _userService; @@ -104,5 +104,18 @@ namespace DevHive.Web.Controllers return new OkResult(); } #endregion + + [HttpPost] + [Route("SuperSecretPromotionToAdmin")] + public async Task SuperSecretPromotionToAdmin(Guid userId) + { + object obj = new + { + UserId = userId, + AdminRoleId = await this._userService.SuperSecretPromotionToAdmin(userId) + }; + + return new OkObjectResult(obj); + } } } diff --git a/src/DevHive.Web/Models/Feed/GetPageWebModel.cs b/src/DevHive.Web/Models/Feed/GetPageWebModel.cs new file mode 100644 index 0000000..4ea44cc --- /dev/null +++ b/src/DevHive.Web/Models/Feed/GetPageWebModel.cs @@ -0,0 +1,19 @@ +using System; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; + +namespace DevHive.Web.Models.Feed +{ + public class GetPageWebModel + { + [Range(1, int.MaxValue)] + public int PageNumber { get; set; } + + [Required] + public DateTime FirstPageTimeIssued { get; set; } + + [DefaultValue(5)] + [Range(1, int.MaxValue)] + public int PageSize { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Feed/ReadPageWebModel.cs b/src/DevHive.Web/Models/Feed/ReadPageWebModel.cs new file mode 100644 index 0000000..40d29c9 --- /dev/null +++ b/src/DevHive.Web/Models/Feed/ReadPageWebModel.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; +using DevHive.Web.Models.Post.Post; + +namespace DevHive.Web.Controllers +{ + public class ReadPageWebModel + { + public List Posts { get; set; } = new(); + } +} -- cgit v1.2.3 From df52b1068e16adc50ffd365e2e8b8ea19b59fac3 Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 26 Jan 2021 11:44:41 +0200 Subject: UserUpdate does not allow updating roles if not admin; HTTP Put now works properly; UserUpdate validates properly --- .../Configurations/Mapping/RoleMapings.cs | 4 +- .../Configurations/Mapping/UserMappings.cs | 5 +- src/DevHive.Services/Interfaces/IRoleService.cs | 2 +- .../Identity/User/UpdateFriendServiceModel.cs | 2 +- .../Models/Identity/User/UserServiceModel.cs | 8 +- .../Models/Language/UpdateLanguageServiceModel.cs | 4 +- .../Technology/UpdateTechnologyServiceModel.cs | 4 +- src/DevHive.Services/Services/RoleService.cs | 4 +- src/DevHive.Services/Services/UserService.cs | 142 +++++++++++---------- .../Configurations/Mapping/RoleMappings.cs | 4 +- .../Configurations/Mapping/UserMappings.cs | 14 +- src/DevHive.Web/Controllers/RoleController.cs | 2 +- .../Models/Identity/Role/UpdateRoleWebModel.cs | 6 +- .../Models/Identity/User/FriendWebModel.cs | 16 --- .../Models/Identity/User/UpdateUserWebModel.cs | 2 +- .../Models/Identity/User/UserWebModel.cs | 2 +- .../Models/Identity/User/UsernameWebModel.cs | 16 +++ 17 files changed, 125 insertions(+), 112 deletions(-) delete mode 100644 src/DevHive.Web/Models/Identity/User/FriendWebModel.cs create mode 100644 src/DevHive.Web/Models/Identity/User/UsernameWebModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index 23bd46f..e61a107 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(); - CreateMap(); + CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); CreateMap(); } } diff --git a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs index 6797ce1..096af38 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs @@ -11,11 +11,10 @@ namespace DevHive.Services.Configurations.Mapping { CreateMap(); CreateMap(); + CreateMap(); CreateMap() .AfterMap((src, dest) => dest.PasswordHash = PasswordModifications.GeneratePasswordHash(src.Password)); - CreateMap(); - CreateMap() - .ForMember(dest => dest.UserName, src => src.MapFrom(p => p.Name)); + CreateMap(); CreateMap(); CreateMap() diff --git a/src/DevHive.Services/Interfaces/IRoleService.cs b/src/DevHive.Services/Interfaces/IRoleService.cs index d3a45e5..d47728c 100644 --- a/src/DevHive.Services/Interfaces/IRoleService.cs +++ b/src/DevHive.Services/Interfaces/IRoleService.cs @@ -8,7 +8,7 @@ namespace DevHive.Services.Interfaces { Task CreateRole(CreateRoleServiceModel roleServiceModel); - Task GetRoleById(Guid id); + Task GetRoleById(Guid id); Task UpdateRole(UpdateRoleServiceModel roleServiceModel); diff --git a/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs index 83fcc34..b0efe10 100644 --- a/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs @@ -5,6 +5,6 @@ namespace DevHive.Services.Models.Identity.User public class UpdateFriendServiceModel { public Guid Id { get; set; } - public string Name { get; set; } + 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 3e41057..7da54b8 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -7,12 +7,12 @@ namespace DevHive.Services.Models.Identity.User { public class UserServiceModel : BaseUserServiceModel { - public HashSet Roles { get; set; } = new HashSet(); + public HashSet Roles { get; set; } = new(); - public HashSet Friends { get; set; } = new HashSet(); + public HashSet Friends { get; set; } = new(); - public HashSet Languages { get; set; } = new HashSet(); + public HashSet Languages { get; set; } = new(); - public HashSet Technologies { get; set; } = new HashSet(); + public HashSet Technologies { get; set; } = new(); } } diff --git a/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs b/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs index 8536693..84b7f27 100644 --- a/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs +++ b/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs @@ -2,8 +2,10 @@ using System; namespace DevHive.Services.Models.Language { - public class UpdateLanguageServiceModel : LanguageServiceModel + public class UpdateLanguageServiceModel { + public Guid Id { get; set; } + public string Name { get; set; } } } diff --git a/src/DevHive.Services/Models/Technology/UpdateTechnologyServiceModel.cs b/src/DevHive.Services/Models/Technology/UpdateTechnologyServiceModel.cs index a18e286..f4c7921 100644 --- a/src/DevHive.Services/Models/Technology/UpdateTechnologyServiceModel.cs +++ b/src/DevHive.Services/Models/Technology/UpdateTechnologyServiceModel.cs @@ -2,8 +2,10 @@ using System; namespace DevHive.Services.Models.Technology { - public class UpdateTechnologyServiceModel : TechnologyServiceModel + public class UpdateTechnologyServiceModel { + public Guid Id { get; set; } + public string Name { get; set; } } } diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index 9f7a5ac..a8b8e17 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 GetRoleById(Guid id) + public async Task GetRoleById(Guid id) { Role role = await this._roleRepository.GetByIdAsync(id) ?? throw new ArgumentException("Role does not exist!"); - return this._roleMapper.Map(role); + return this._roleMapper.Map(role); } public async Task UpdateRole(UpdateRoleServiceModel updateRoleServiceModel) diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 1beb07f..960630e 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -111,61 +111,9 @@ namespace DevHive.Services.Services await this.ValidateUserCollections(updateUserServiceModel); - /* Roles */ - int roleCount = updateUserServiceModel.Roles.Count; - for (int i = 0; i < roleCount; i++) - { - Role role = await this._roleRepository.GetByNameAsync(updateUserServiceModel.Roles.ElementAt(i).Name) ?? - throw new ArgumentException("Invalid role name!"); - - UpdateRoleServiceModel updateRoleServiceModel = this._userMapper.Map(role); - - updateUserServiceModel.Roles.Add(updateRoleServiceModel); - } - - /* Languages */ - int langCount = updateUserServiceModel.Languages.Count; - for (int i = 0; i < langCount; i++) - { - Language language = await this._languageRepository.GetByNameAsync(updateUserServiceModel.Languages.ElementAt(i).Name) ?? - throw new ArgumentException("Invalid language name!"); - - UpdateLanguageServiceModel updateLanguageServiceModel = this._userMapper.Map(language); - - 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++) - { - Technology technology = await this._technologyRepository.GetByNameAsync(updateUserServiceModel.Technologies.ElementAt(i).Name) ?? - throw new ArgumentException("Invalid technology name!"); - - UpdateTechnologyServiceModel updateTechnologyServiceModel = this._userMapper.Map(technology); - - updateUserServiceModel.Technologies.Add(updateTechnologyServiceModel); - } - //Clean the already replaced technologies - updateUserServiceModel.Technologies.RemoveWhere(x => x.Id == Guid.Empty); - - /* Friends */ - HashSet 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); + updateUserServiceModel = await this.PopulateUpdateModelWithIds(updateUserServiceModel); User user = this._userMapper.Map(updateUserServiceModel); - user.Friends = friends; bool successful = await this._userRepository.EditAsync(updateUserServiceModel.Id, user); @@ -249,30 +197,49 @@ namespace DevHive.Services.Services private async Task ValidateUserCollections(UpdateUserServiceModel updateUserServiceModel) { + //Do NOT allow a user to change his roles, unless he is an Admin + bool isAdmin = (await this._userRepository.GetByIdAsync(updateUserServiceModel.Id)) + .Roles.Any(r => r.Name == Role.AdminRole); + + if (isAdmin) + { + // Roles + foreach (var role in updateUserServiceModel.Roles) + { + Role returnedRole = await this._roleRepository.GetByNameAsync(role.Name) ?? + throw new ArgumentException($"Role {role.Name} does not exist!"); + } + } + //Preserve original user roles + else + { + HashSet roles = (await this._userRepository.GetByIdAsync(updateUserServiceModel.Id)).Roles; + + foreach (var role in roles) + { + Role returnedRole = await this._roleRepository.GetByNameAsync(role.Name) ?? + throw new ArgumentException($"Role {role.Name} does not exist!"); + } + } + // Friends foreach (var friend in updateUserServiceModel.Friends) { - User returnedFriend = await this._userRepository.GetByUsernameAsync(friend.Name); - - if (returnedFriend == null) - throw new ArgumentException($"User {friend.Name} does not exist!"); + User returnedFriend = await this._userRepository.GetByUsernameAsync(friend.UserName) ?? + throw new ArgumentException($"User {friend.UserName} does not exist!"); } // Languages foreach (var language in updateUserServiceModel.Languages) { - Language returnedLanguage = await this._languageRepository.GetByNameAsync(language.Name); - - if (returnedLanguage == null) + Language returnedLanguage = await this._languageRepository.GetByNameAsync(language.Name) ?? throw new ArgumentException($"Language {language.Name} does not exist!"); } // Technology foreach (var technology in updateUserServiceModel.Technologies) { - Technology returnedTechnology = await this._technologyRepository.GetByNameAsync(technology.Name); - - if (returnedTechnology == null) + Technology returnedTechnology = await this._technologyRepository.GetByNameAsync(technology.Name) ?? throw new ArgumentException($"Technology {technology.Name} does not exist!"); } } @@ -306,12 +273,13 @@ namespace DevHive.Services.Services } #endregion + #region Misc public async Task 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")) + if (!await this._roleRepository.DoesNameExist("Admin")) { Role adminRole = new() { @@ -329,5 +297,51 @@ namespace DevHive.Services.Services return admin.Id; } + + private async Task PopulateUpdateModelWithIds(UpdateUserServiceModel updateUserServiceModel) + { + /* Roles */ + int roleCount = updateUserServiceModel.Roles.Count; + for (int i = 0; i < roleCount; i++) + { + Role role = await this._roleRepository.GetByNameAsync(updateUserServiceModel.Roles.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid role name!"); + + updateUserServiceModel.Roles.ElementAt(i).Id = role.Id; + } + + /* Languages */ + int langCount = updateUserServiceModel.Languages.Count; + for (int i = 0; i < langCount; i++) + { + Language language = await this._languageRepository.GetByNameAsync(updateUserServiceModel.Languages.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid language name!"); + + updateUserServiceModel.Languages.ElementAt(i).Id = language.Id; + } + + /* Technologies */ + int techCount = updateUserServiceModel.Technologies.Count; + for (int i = 0; i < techCount; i++) + { + Technology technology = await this._technologyRepository.GetByNameAsync(updateUserServiceModel.Technologies.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid technology name!"); + + updateUserServiceModel.Technologies.ElementAt(i).Id = technology.Id; + } + + /* Friends */ + int friendsCount = updateUserServiceModel.Friends.Count; + for (int i = 0; i < friendsCount; i++) + { + User friend = await this._userRepository.GetByUsernameAsync(updateUserServiceModel.Friends.ElementAt(i).UserName) ?? + throw new ArgumentException("Invalid friend's username!"); + + updateUserServiceModel.Friends.ElementAt(i).Id = friend.Id; + } + + return updateUserServiceModel; + } + #endregion } } diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 2f01f77..2ea2742 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -11,11 +11,11 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap() .ForMember(src => src.Id, dest => dest.Ignore()); - CreateMap(); + CreateMap(); CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index e80a69a..1b26cc9 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -2,8 +2,6 @@ using AutoMapper; using DevHive.Services.Models.Identity.User; using DevHive.Web.Models.Identity.User; using DevHive.Common.Models.Identity; -using DevHive.Web.Models.Language; -using DevHive.Web.Models.Technology; namespace DevHive.Web.Configurations.Mapping { @@ -21,16 +19,12 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); //Update - CreateMap() - .ForMember(src => src.Id, dest => dest.Ignore()); - CreateMap() - .ForMember(src => src.Id, dest => dest.Ignore()); - CreateMap() - .ForMember(src => src.Id, dest => dest.Ignore()) - .ForMember(src => src.Name, dest => dest.MapFrom(p => p.UserName)); + CreateMap(); + CreateMap(); + CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index d8bb60c..c68a32b 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -40,7 +40,7 @@ namespace DevHive.Web.Controllers [Authorize(Policy = "User")] public async Task GetById(Guid id) { - ReadRoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); + RoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); RoleWebModel roleWebModel = this._roleMapper.Map(roleServiceModel); return new OkObjectResult(roleWebModel); diff --git a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs index 254affc..3870481 100644 --- a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs @@ -4,10 +4,12 @@ using System.Diagnostics.CodeAnalysis; namespace DevHive.Web.Models.Identity.Role { - public class UpdateRoleWebModel : RoleWebModel + public class UpdateRoleWebModel { [NotNull] [Required] - public Guid Id { get; set; } + [MinLength(3)] + [MaxLength(50)] + public string Name { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs b/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs deleted file mode 100644 index d59bff5..0000000 --- a/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.ComponentModel.DataAnnotations; -using System.Diagnostics.CodeAnalysis; -using DevHive.Web.Attributes; - -namespace DevHive.Web.Models.Identity.User -{ - public class FriendWebModel - { - [NotNull] - [Required] - [MinLength(3)] - [MaxLength(50)] - [OnlyAlphanumerics(ErrorMessage = "Username can only contain letters and digits!")] - public string UserName { get; set; } - } -} diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index 30c66fb..62901f6 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -17,7 +17,7 @@ namespace DevHive.Web.Models.Identity.User [NotNull] [Required] - public HashSet Friends { get; set; } + public HashSet Friends { get; set; } [NotNull] [Required] diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 5b80ba3..4097901 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 HashSet Friends { get; set; } = new HashSet(); + public HashSet Friends { get; set; } = new HashSet(); [NotNull] [Required] diff --git a/src/DevHive.Web/Models/Identity/User/UsernameWebModel.cs b/src/DevHive.Web/Models/Identity/User/UsernameWebModel.cs new file mode 100644 index 0000000..a20c1bf --- /dev/null +++ b/src/DevHive.Web/Models/Identity/User/UsernameWebModel.cs @@ -0,0 +1,16 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; +using DevHive.Web.Attributes; + +namespace DevHive.Web.Models.Identity.User +{ + public class UsernameWebModel + { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] + [OnlyAlphanumerics(ErrorMessage = "Username can only contain letters and digits!")] + public string UserName { get; set; } + } +} -- cgit v1.2.3 From ff91162eb83dcf19402240ae8fa06f70cbf2b9e0 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 30 Jan 2021 11:31:21 +0200 Subject: Separated comment models, controler and service from post's --- .../Configurations/Mapping/CommentMappings.cs | 2 +- .../Configurations/Mapping/PostMappings.cs | 2 +- src/DevHive.Services/Interfaces/ICommentService.cs | 20 +++ src/DevHive.Services/Interfaces/IPostService.cs | 10 +- .../Models/Comment/CreateCommentServiceModel.cs | 13 ++ .../Models/Comment/ReadCommentServiceModel.cs | 21 +++ .../Models/Comment/UpdateCommentServiceModel.cs | 15 ++ .../Models/Feed/ReadPageServiceModel.cs | 2 +- .../Post/Comment/CreateCommentServiceModel.cs | 13 -- .../Models/Post/Comment/ReadCommentServiceModel.cs | 21 --- .../Post/Comment/UpdateCommentServiceModel.cs | 15 -- .../Models/Post/CreatePostServiceModel.cs | 15 ++ .../Models/Post/Post/CreatePostServiceModel.cs | 15 -- .../Models/Post/Post/ReadPostServiceModel.cs | 26 ---- .../Models/Post/Post/UpdatePostServiceModel.cs | 17 --- .../Models/Post/ReadPostServiceModel.cs | 26 ++++ .../Models/Post/UpdatePostServiceModel.cs | 17 +++ src/DevHive.Services/Services/CommentService.cs | 156 +++++++++++++++++++++ src/DevHive.Services/Services/FeedService.cs | 2 +- src/DevHive.Services/Services/PostService.cs | 72 +--------- .../Extensions/ConfigureDependencyInjection.cs | 4 +- .../Configurations/Mapping/CommentMappings.cs | 7 +- .../Configurations/Mapping/PostMappings.cs | 4 +- src/DevHive.Web/Controllers/CommentController.cs | 82 +++++++++++ src/DevHive.Web/Controllers/PostController.cs | 67 +-------- .../Models/Comment/CreateCommentWebModel.cs | 17 +++ .../Models/Comment/ReadCommentWebModel.cs | 21 +++ .../Models/Comment/UpdateCommentWebModel.cs | 13 ++ src/DevHive.Web/Models/Feed/ReadPageWebModel.cs | 2 +- .../Models/Post/Comment/CreateCommentWebModel.cs | 17 --- .../Models/Post/Comment/ReadCommentWebModel.cs | 21 --- .../Models/Post/Comment/UpdateCommentWebModel.cs | 13 -- src/DevHive.Web/Models/Post/CreatePostWebModel.cs | 16 +++ .../Models/Post/Post/CreatePostWebModel.cs | 17 --- .../Models/Post/Post/ReadPostWebModel.cs | 26 ---- .../Models/Post/Post/UpdatePostWebModel.cs | 21 --- src/DevHive.Web/Models/Post/ReadPostWebModel.cs | 26 ++++ src/DevHive.Web/Models/Post/UpdatePostWebModel.cs | 21 +++ 38 files changed, 497 insertions(+), 378 deletions(-) create mode 100644 src/DevHive.Services/Interfaces/ICommentService.cs create mode 100644 src/DevHive.Services/Models/Comment/CreateCommentServiceModel.cs create mode 100644 src/DevHive.Services/Models/Comment/ReadCommentServiceModel.cs create mode 100644 src/DevHive.Services/Models/Comment/UpdateCommentServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Post/Comment/UpdateCommentServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/CreatePostServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/ReadPostServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/UpdatePostServiceModel.cs create mode 100644 src/DevHive.Services/Services/CommentService.cs create mode 100644 src/DevHive.Web/Controllers/CommentController.cs create mode 100644 src/DevHive.Web/Models/Comment/CreateCommentWebModel.cs create mode 100644 src/DevHive.Web/Models/Comment/ReadCommentWebModel.cs create mode 100644 src/DevHive.Web/Models/Comment/UpdateCommentWebModel.cs delete mode 100644 src/DevHive.Web/Models/Post/Comment/CreateCommentWebModel.cs delete mode 100644 src/DevHive.Web/Models/Post/Comment/ReadCommentWebModel.cs delete mode 100644 src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs create mode 100644 src/DevHive.Web/Models/Post/CreatePostWebModel.cs delete mode 100644 src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs delete mode 100644 src/DevHive.Web/Models/Post/Post/ReadPostWebModel.cs delete mode 100644 src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs create mode 100644 src/DevHive.Web/Models/Post/ReadPostWebModel.cs create mode 100644 src/DevHive.Web/Models/Post/UpdatePostWebModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs b/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs index ac3c8f6..a43b64e 100644 --- a/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs @@ -1,6 +1,6 @@ using DevHive.Data.Models; using AutoMapper; -using DevHive.Services.Models.Post.Comment; +using DevHive.Services.Models.Comment; namespace DevHive.Services.Configurations.Mapping { diff --git a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs index c7466d9..81e6ecc 100644 --- a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs @@ -1,6 +1,6 @@ using DevHive.Data.Models; using AutoMapper; -using DevHive.Services.Models.Post.Post; +using DevHive.Services.Models.Post; namespace DevHive.Services.Configurations.Mapping { diff --git a/src/DevHive.Services/Interfaces/ICommentService.cs b/src/DevHive.Services/Interfaces/ICommentService.cs new file mode 100644 index 0000000..e7409a8 --- /dev/null +++ b/src/DevHive.Services/Interfaces/ICommentService.cs @@ -0,0 +1,20 @@ +using System; +using System.Threading.Tasks; +using DevHive.Services.Models.Comment; + +namespace DevHive.Services.Interfaces +{ + public interface ICommentService + { + Task AddComment(CreateCommentServiceModel createPostServiceModel); + + Task GetCommentById(Guid id); + + Task UpdateComment(UpdateCommentServiceModel updateCommentServiceModel); + + Task DeleteComment(Guid id); + + Task ValidateJwtForCreating(Guid userId, string rawTokenData); + Task ValidateJwtForComment(Guid commentId, string rawTokenData); + } +} diff --git a/src/DevHive.Services/Interfaces/IPostService.cs b/src/DevHive.Services/Interfaces/IPostService.cs index 71b558c..d35acfd 100644 --- a/src/DevHive.Services/Interfaces/IPostService.cs +++ b/src/DevHive.Services/Interfaces/IPostService.cs @@ -1,26 +1,20 @@ using System; using System.Threading.Tasks; -using DevHive.Services.Models.Post.Comment; -using DevHive.Services.Models.Post.Post; +using DevHive.Services.Models.Post; namespace DevHive.Services.Interfaces { - public interface IPostService + public interface IPostService { Task CreatePost(CreatePostServiceModel createPostServiceModel); - Task AddComment(CreateCommentServiceModel createPostServiceModel); Task GetPostById(Guid id); - Task GetCommentById(Guid id); Task UpdatePost(UpdatePostServiceModel updatePostServiceModel); - Task UpdateComment(UpdateCommentServiceModel updateCommentServiceModel); Task DeletePost(Guid id); - Task DeleteComment(Guid id); Task ValidateJwtForCreating(Guid userId, string rawTokenData); Task ValidateJwtForPost(Guid postId, string rawTokenData); - Task ValidateJwtForComment(Guid commentId, string rawTokenData); } } diff --git a/src/DevHive.Services/Models/Comment/CreateCommentServiceModel.cs b/src/DevHive.Services/Models/Comment/CreateCommentServiceModel.cs new file mode 100644 index 0000000..30e919b --- /dev/null +++ b/src/DevHive.Services/Models/Comment/CreateCommentServiceModel.cs @@ -0,0 +1,13 @@ +using System; + +namespace DevHive.Services.Models.Comment +{ + public class CreateCommentServiceModel + { + public Guid PostId { get; set; } + + public Guid CreatorId { get; set; } + + public string Message { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Comment/ReadCommentServiceModel.cs b/src/DevHive.Services/Models/Comment/ReadCommentServiceModel.cs new file mode 100644 index 0000000..3196233 --- /dev/null +++ b/src/DevHive.Services/Models/Comment/ReadCommentServiceModel.cs @@ -0,0 +1,21 @@ +using System; + +namespace DevHive.Services.Models.Comment +{ + public class ReadCommentServiceModel + { + public Guid CommentId { get; set; } + + public string IssuerFirstName { get; set; } + + public string IssuerLastName { get; set; } + + public string IssuerUsername { get; set; } + + public Guid PostId { get; set; } + + public string Message { get; set; } + + public DateTime TimeCreated { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Comment/UpdateCommentServiceModel.cs b/src/DevHive.Services/Models/Comment/UpdateCommentServiceModel.cs new file mode 100644 index 0000000..3b78200 --- /dev/null +++ b/src/DevHive.Services/Models/Comment/UpdateCommentServiceModel.cs @@ -0,0 +1,15 @@ +using System; + +namespace DevHive.Services.Models.Comment +{ + public class UpdateCommentServiceModel + { + public Guid CreatorId { get; set; } + + public Guid CommentId { get; set; } + + public Guid PostId { get; set; } + + public string NewMessage { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Feed/ReadPageServiceModel.cs b/src/DevHive.Services/Models/Feed/ReadPageServiceModel.cs index f291de7..95f6845 100644 --- a/src/DevHive.Services/Models/Feed/ReadPageServiceModel.cs +++ b/src/DevHive.Services/Models/Feed/ReadPageServiceModel.cs @@ -1,5 +1,5 @@ using System.Collections.Generic; -using DevHive.Services.Models.Post.Post; +using DevHive.Services.Models.Post; namespace DevHive.Services.Models { diff --git a/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs deleted file mode 100644 index 8d49659..0000000 --- a/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Post.Comment -{ - public class CreateCommentServiceModel - { - public Guid PostId { get; set; } - - public Guid CreatorId { get; set; } - - public string Message { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs deleted file mode 100644 index 12e29a0..0000000 --- a/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Post.Comment -{ - public class ReadCommentServiceModel - { - public Guid CommentId { get; set; } - - public string IssuerFirstName { get; set; } - - public string IssuerLastName { get; set; } - - public string IssuerUsername { get; set; } - - public Guid PostId { get; set; } - - public string Message { get; set; } - - public DateTime TimeCreated { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Post/Comment/UpdateCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/UpdateCommentServiceModel.cs deleted file mode 100644 index 3827d4d..0000000 --- a/src/DevHive.Services/Models/Post/Comment/UpdateCommentServiceModel.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Post.Comment -{ - public class UpdateCommentServiceModel - { - public Guid CreatorId { get; set; } - - public Guid CommentId { get; set; } - - public Guid PostId { get; set; } - - public string NewMessage { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Post/CreatePostServiceModel.cs b/src/DevHive.Services/Models/Post/CreatePostServiceModel.cs new file mode 100644 index 0000000..304eb90 --- /dev/null +++ b/src/DevHive.Services/Models/Post/CreatePostServiceModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using Microsoft.AspNetCore.Http; + +namespace DevHive.Services.Models.Post +{ + public class CreatePostServiceModel + { + public Guid CreatorId { get; set; } + + public string Message { get; set; } + + public List Files { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs deleted file mode 100644 index 8676f6c..0000000 --- a/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using Microsoft.AspNetCore.Http; - -namespace DevHive.Services.Models.Post.Post -{ - public class CreatePostServiceModel - { - public Guid CreatorId { get; set; } - - public string Message { get; set; } - - public List Files { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs deleted file mode 100644 index f0a4fe5..0000000 --- a/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using System.Collections.Generic; -using DevHive.Services.Models.Post.Comment; -using Microsoft.Extensions.FileProviders; - -namespace DevHive.Services.Models.Post.Post -{ - public class ReadPostServiceModel - { - public Guid PostId { get; set; } - - public string CreatorFirstName { get; set; } - - public string CreatorLastName { get; set; } - - public string CreatorUsername { get; set; } - - public string Message { get; set; } - - public DateTime TimeCreated { get; set; } - - public List Comments { get; set; } = new(); - - public List Files { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs deleted file mode 100644 index 24b0b74..0000000 --- a/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using Microsoft.AspNetCore.Http; - -namespace DevHive.Services.Models.Post.Post -{ - public class UpdatePostServiceModel - { - public Guid PostId { get; set; } - - public Guid CreatorId { get; set; } - - public string NewMessage { get; set; } - - public List Files { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Post/ReadPostServiceModel.cs b/src/DevHive.Services/Models/Post/ReadPostServiceModel.cs new file mode 100644 index 0000000..04ec6bd --- /dev/null +++ b/src/DevHive.Services/Models/Post/ReadPostServiceModel.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using DevHive.Services.Models.Comment; +using Microsoft.Extensions.FileProviders; + +namespace DevHive.Services.Models.Post +{ + public class ReadPostServiceModel + { + public Guid PostId { get; set; } + + public string CreatorFirstName { get; set; } + + public string CreatorLastName { get; set; } + + public string CreatorUsername { get; set; } + + public string Message { get; set; } + + public DateTime TimeCreated { get; set; } + + public List Comments { get; set; } = new(); + + public List Files { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Post/UpdatePostServiceModel.cs b/src/DevHive.Services/Models/Post/UpdatePostServiceModel.cs new file mode 100644 index 0000000..51b16bc --- /dev/null +++ b/src/DevHive.Services/Models/Post/UpdatePostServiceModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using Microsoft.AspNetCore.Http; + +namespace DevHive.Services.Models.Post +{ + public class UpdatePostServiceModel + { + public Guid PostId { get; set; } + + public Guid CreatorId { get; set; } + + public string NewMessage { get; set; } + + public List Files { get; set; } + } +} diff --git a/src/DevHive.Services/Services/CommentService.cs b/src/DevHive.Services/Services/CommentService.cs new file mode 100644 index 0000000..e0eb88a --- /dev/null +++ b/src/DevHive.Services/Services/CommentService.cs @@ -0,0 +1,156 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Models; +using DevHive.Services.Models.Comment; +using System.IdentityModel.Tokens.Jwt; +using System.Security.Claims; +using DevHive.Services.Interfaces; +using DevHive.Data.Interfaces.Repositories; +using System.Linq; + +namespace DevHive.Services.Services +{ + public class CommentService : ICommentService + { + private readonly IUserRepository _userRepository; + private readonly IPostRepository _postRepository; + private readonly ICommentRepository _commentRepository; + private readonly IMapper _postMapper; + + public CommentService(IUserRepository userRepository, IPostRepository postRepository, ICommentRepository commentRepository, IMapper postMapper) + { + this._userRepository = userRepository; + this._postRepository = postRepository; + this._commentRepository = commentRepository; + this._postMapper = postMapper; + } + + #region Create + public async Task AddComment(CreateCommentServiceModel createCommentServiceModel) + { + if (!await this._postRepository.DoesPostExist(createCommentServiceModel.PostId)) + throw new ArgumentException("Post does not exist!"); + + Comment comment = this._postMapper.Map(createCommentServiceModel); + comment.TimeCreated = DateTime.Now; + + comment.Creator = await this._userRepository.GetByIdAsync(createCommentServiceModel.CreatorId); + comment.Post = await this._postRepository.GetByIdAsync(createCommentServiceModel.PostId); + + bool success = await this._commentRepository.AddAsync(comment); + if (success) + { + Comment newComment = await this._commentRepository + .GetCommentByIssuerAndTimeCreatedAsync(comment.Creator.Id, comment.TimeCreated); + + return newComment.Id; + } + else + return Guid.Empty; + } + #endregion + + #region Read + public async Task GetCommentById(Guid id) + { + Comment comment = await this._commentRepository.GetByIdAsync(id) ?? + throw new ArgumentException("The comment does not exist"); + + User user = await this._userRepository.GetByIdAsync(comment.Creator.Id) ?? + throw new ArgumentException("The user does not exist"); + + ReadCommentServiceModel readCommentServiceModel = this._postMapper.Map(comment); + readCommentServiceModel.IssuerFirstName = user.FirstName; + readCommentServiceModel.IssuerLastName = user.LastName; + readCommentServiceModel.IssuerUsername = user.UserName; + + return readCommentServiceModel; + } + #endregion + + #region Update + public async Task UpdateComment(UpdateCommentServiceModel updateCommentServiceModel) + { + if (!await this._commentRepository.DoesCommentExist(updateCommentServiceModel.CommentId)) + throw new ArgumentException("Comment does not exist!"); + + Comment comment = this._postMapper.Map(updateCommentServiceModel); + comment.TimeCreated = DateTime.Now; + + comment.Creator = await this._userRepository.GetByIdAsync(updateCommentServiceModel.CreatorId); + comment.Post = await this._postRepository.GetByIdAsync(updateCommentServiceModel.PostId); + + bool result = await this._commentRepository.EditAsync(updateCommentServiceModel.CommentId, comment); + + if (result) + return (await this._commentRepository.GetByIdAsync(updateCommentServiceModel.CommentId)).Id; + else + return Guid.Empty; + } + #endregion + + #region Delete + public async Task DeleteComment(Guid id) + { + if (!await this._commentRepository.DoesCommentExist(id)) + throw new ArgumentException("Comment does not exist!"); + + Comment comment = await this._commentRepository.GetByIdAsync(id); + return await this._commentRepository.DeleteAsync(comment); + } + #endregion + + #region Validations + public async Task ValidateJwtForCreating(Guid userId, string rawTokenData) + { + User user = await this.GetUserForValidation(rawTokenData); + + return user.Id == userId; + } + + public async Task ValidateJwtForComment(Guid commentId, string rawTokenData) + { + Comment comment = await this._commentRepository.GetByIdAsync(commentId) ?? + throw new ArgumentException("Comment does not exist!"); + User user = await this.GetUserForValidation(rawTokenData); + + //If user made the comment + if (comment.Creator.Id == user.Id) + return true; + //If user is admin + else if (user.Roles.Any(x => x.Name == Role.AdminRole)) + return true; + else + return false; + } + + private async Task GetUserForValidation(string rawTokenData) + { + JwtSecurityToken jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7)); + + Guid jwtUserId = Guid.Parse(this.GetClaimTypeValues("ID", jwt.Claims).First()); + //HashSet jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims); + + User user = await this._userRepository.GetByIdAsync(jwtUserId) ?? + throw new ArgumentException("User does not exist!"); + + return user; + } + + + private List GetClaimTypeValues(string type, IEnumerable claims) + { + List toReturn = new(); + + foreach (var claim in claims) + if (claim.Type == type) + toReturn.Add(claim.Value); + + return toReturn; + } + #endregion + } +} + diff --git a/src/DevHive.Services/Services/FeedService.cs b/src/DevHive.Services/Services/FeedService.cs index 1bddac4..269471e 100644 --- a/src/DevHive.Services/Services/FeedService.cs +++ b/src/DevHive.Services/Services/FeedService.cs @@ -7,7 +7,7 @@ using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using DevHive.Services.Interfaces; using DevHive.Services.Models; -using DevHive.Services.Models.Post.Post; +using DevHive.Services.Models.Post; namespace DevHive.Services.Services { diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index 7ce7b58..0eaac94 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -3,8 +3,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using DevHive.Data.Models; -using DevHive.Services.Models.Post.Comment; -using DevHive.Services.Models.Post.Post; +using DevHive.Services.Models.Post; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using DevHive.Services.Interfaces; @@ -13,7 +12,7 @@ using System.Linq; namespace DevHive.Services.Services { - public class PostService : IPostService + public class PostService : IPostService { private readonly ICloudService _cloudService; private readonly IUserRepository _userRepository; @@ -55,29 +54,6 @@ namespace DevHive.Services.Services else return Guid.Empty; } - - public async Task AddComment(CreateCommentServiceModel createCommentServiceModel) - { - if (!await this._postRepository.DoesPostExist(createCommentServiceModel.PostId)) - throw new ArgumentException("Post does not exist!"); - - Comment comment = this._postMapper.Map(createCommentServiceModel); - comment.TimeCreated = DateTime.Now; - - comment.Creator = await this._userRepository.GetByIdAsync(createCommentServiceModel.CreatorId); - comment.Post = await this._postRepository.GetByIdAsync(createCommentServiceModel.PostId); - - bool success = await this._commentRepository.AddAsync(comment); - if (success) - { - Comment newComment = await this._commentRepository - .GetCommentByIssuerAndTimeCreatedAsync(comment.Creator.Id, comment.TimeCreated); - - return newComment.Id; - } - else - return Guid.Empty; - } #endregion #region Read @@ -96,22 +72,6 @@ namespace DevHive.Services.Services return readPostServiceModel; } - - public async Task GetCommentById(Guid id) - { - Comment comment = await this._commentRepository.GetByIdAsync(id) ?? - throw new ArgumentException("The comment does not exist"); - - User user = await this._userRepository.GetByIdAsync(comment.Creator.Id) ?? - throw new ArgumentException("The user does not exist"); - - ReadCommentServiceModel readCommentServiceModel = this._postMapper.Map(comment); - readCommentServiceModel.IssuerFirstName = user.FirstName; - readCommentServiceModel.IssuerLastName = user.LastName; - readCommentServiceModel.IssuerUsername = user.UserName; - - return readCommentServiceModel; - } #endregion #region Update @@ -146,25 +106,6 @@ namespace DevHive.Services.Services else return Guid.Empty; } - - public async Task UpdateComment(UpdateCommentServiceModel updateCommentServiceModel) - { - if (!await this._commentRepository.DoesCommentExist(updateCommentServiceModel.CommentId)) - throw new ArgumentException("Comment does not exist!"); - - Comment comment = this._postMapper.Map(updateCommentServiceModel); - comment.TimeCreated = DateTime.Now; - - comment.Creator = await this._userRepository.GetByIdAsync(updateCommentServiceModel.CreatorId); - comment.Post = await this._postRepository.GetByIdAsync(updateCommentServiceModel.PostId); - - bool result = await this._commentRepository.EditAsync(updateCommentServiceModel.CommentId, comment); - - if (result) - return (await this._commentRepository.GetByIdAsync(updateCommentServiceModel.CommentId)).Id; - else - return Guid.Empty; - } #endregion #region Delete @@ -185,15 +126,6 @@ namespace DevHive.Services.Services return await this._postRepository.DeleteAsync(post); } - - public async Task DeleteComment(Guid id) - { - if (!await this._commentRepository.DoesCommentExist(id)) - throw new ArgumentException("Comment does not exist!"); - - Comment comment = await this._commentRepository.GetByIdAsync(id); - return await this._commentRepository.DeleteAsync(comment); - } #endregion #region Validations diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs index fe2c788..8ba0d69 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs @@ -1,5 +1,4 @@ using DevHive.Data.Interfaces.Repositories; -using DevHive.Data.Models; using DevHive.Data.Repositories; using DevHive.Services.Interfaces; using DevHive.Services.Services; @@ -8,7 +7,7 @@ using Microsoft.Extensions.DependencyInjection; namespace DevHive.Web.Configurations.Extensions { - public static class ConfigureDependencyInjection + public static class ConfigureDependencyInjection { public static void DependencyInjectionConfiguration(this IServiceCollection services, IConfiguration configuration) { @@ -25,6 +24,7 @@ namespace DevHive.Web.Configurations.Extensions services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(options => new CloudinaryService( diff --git a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs index a28ee16..b8d6829 100644 --- a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs @@ -1,6 +1,6 @@ using AutoMapper; -using DevHive.Services.Models.Post.Comment; -using DevHive.Web.Models.Post.Comment; +using DevHive.Services.Models.Comment; +using DevHive.Web.Models.Comment; namespace DevHive.Web.Configurations.Mapping { @@ -15,6 +15,3 @@ namespace DevHive.Web.Configurations.Mapping } } } - - - diff --git a/src/DevHive.Web/Configurations/Mapping/PostMappings.cs b/src/DevHive.Web/Configurations/Mapping/PostMappings.cs index bc7bc06..a5b46ee 100644 --- a/src/DevHive.Web/Configurations/Mapping/PostMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/PostMappings.cs @@ -1,6 +1,6 @@ using AutoMapper; -using DevHive.Services.Models.Post.Post; -using DevHive.Web.Models.Post.Post; +using DevHive.Services.Models.Post; +using DevHive.Web.Models.Post; namespace DevHive.Web.Configurations.Mapping { diff --git a/src/DevHive.Web/Controllers/CommentController.cs b/src/DevHive.Web/Controllers/CommentController.cs new file mode 100644 index 0000000..ebcb87a --- /dev/null +++ b/src/DevHive.Web/Controllers/CommentController.cs @@ -0,0 +1,82 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using AutoMapper; +using System; +using DevHive.Web.Models.Comment; +using DevHive.Services.Models.Comment; +using Microsoft.AspNetCore.Authorization; +using DevHive.Services.Interfaces; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + [Authorize(Roles = "User,Admin")] + public class CommentController { + private readonly ICommentService _commentService; + private readonly IMapper _commentMapper; + + public CommentController(ICommentService commentService, IMapper commentMapper) + { + this._commentService = commentService; + this._commentMapper = commentMapper; + } + + [HttpPost] + public async Task AddComment(Guid userId, [FromBody] CreateCommentWebModel createCommentWebModel, [FromHeader] string authorization) + { + if (!await this._commentService.ValidateJwtForCreating(userId, authorization)) + return new UnauthorizedResult(); + + CreateCommentServiceModel createCommentServiceModel = + this._commentMapper.Map(createCommentWebModel); + createCommentServiceModel.CreatorId = userId; + + Guid id = await this._commentService.AddComment(createCommentServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult("Could not create comment!") : + new OkObjectResult(new { Id = id }); + } + + [HttpGet] + [AllowAnonymous] + public async Task GetCommentById(Guid id) + { + ReadCommentServiceModel readCommentServiceModel = await this._commentService.GetCommentById(id); + ReadCommentWebModel readCommentWebModel = this._commentMapper.Map(readCommentServiceModel); + + return new OkObjectResult(readCommentWebModel); + } + + [HttpPut] + public async Task UpdateComment(Guid userId, [FromBody] UpdateCommentWebModel updateCommentWebModel, [FromHeader] string authorization) + { + if (!await this._commentService.ValidateJwtForComment(updateCommentWebModel.CommentId, authorization)) + return new UnauthorizedResult(); + + UpdateCommentServiceModel updateCommentServiceModel = + this._commentMapper.Map(updateCommentWebModel); + updateCommentServiceModel.CreatorId = userId; + + Guid id = await this._commentService.UpdateComment(updateCommentServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult("Unable to update comment!") : + new OkObjectResult(new { Id = id }); + } + + [HttpDelete] + public async Task DeleteComment(Guid id, [FromHeader] string authorization) + { + if (!await this._commentService.ValidateJwtForComment(id, authorization)) + return new UnauthorizedResult(); + + return await this._commentService.DeleteComment(id) ? + new OkResult() : + new BadRequestObjectResult("Could not delete Comment"); + } + + } +} + diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs index fe71519..53adfce 100644 --- a/src/DevHive.Web/Controllers/PostController.cs +++ b/src/DevHive.Web/Controllers/PostController.cs @@ -2,16 +2,14 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using AutoMapper; using System; -using DevHive.Web.Models.Post.Post; -using DevHive.Services.Models.Post.Post; -using DevHive.Web.Models.Post.Comment; -using DevHive.Services.Models.Post.Comment; +using DevHive.Web.Models.Post; +using DevHive.Services.Models.Post; using Microsoft.AspNetCore.Authorization; using DevHive.Services.Interfaces; namespace DevHive.Web.Controllers { - [ApiController] + [ApiController] [Route("/api/[controller]")] [Authorize(Roles = "User,Admin")] public class PostController @@ -42,24 +40,6 @@ namespace DevHive.Web.Controllers new BadRequestObjectResult("Could not create post!") : new OkObjectResult(new { Id = id }); } - - [HttpPost] - [Route("Comment")] - public async Task AddComment(Guid userId, [FromBody] CreateCommentWebModel createCommentWebModel, [FromHeader] string authorization) - { - if (!await this._postService.ValidateJwtForCreating(userId, authorization)) - return new UnauthorizedResult(); - - CreateCommentServiceModel createCommentServiceModel = - this._postMapper.Map(createCommentWebModel); - createCommentServiceModel.CreatorId = userId; - - Guid id = await this._postService.AddComment(createCommentServiceModel); - - return id == Guid.Empty ? - new BadRequestObjectResult("Could not create comment!") : - new OkObjectResult(new { Id = id }); - } #endregion #region Read @@ -72,17 +52,6 @@ namespace DevHive.Web.Controllers return new OkObjectResult(postWebModel); } - - [HttpGet] - [Route("Comment")] - [AllowAnonymous] - public async Task GetCommentById(Guid id) - { - ReadCommentServiceModel readCommentServiceModel = await this._postService.GetCommentById(id); - ReadCommentWebModel readCommentWebModel = this._postMapper.Map(readCommentServiceModel); - - return new OkObjectResult(readCommentWebModel); - } #endregion #region Update @@ -102,24 +71,6 @@ namespace DevHive.Web.Controllers new BadRequestObjectResult("Unable to update post!") : new OkObjectResult(new { Id = id }); } - - [HttpPut] - [Route("Comment")] - public async Task UpdateComment(Guid userId, [FromBody] UpdateCommentWebModel updateCommentWebModel, [FromHeader] string authorization) - { - if (!await this._postService.ValidateJwtForComment(updateCommentWebModel.CommentId, authorization)) - return new UnauthorizedResult(); - - UpdateCommentServiceModel updateCommentServiceModel = - this._postMapper.Map(updateCommentWebModel); - updateCommentServiceModel.CreatorId = userId; - - Guid id = await this._postService.UpdateComment(updateCommentServiceModel); - - return id == Guid.Empty ? - new BadRequestObjectResult("Unable to update comment!") : - new OkObjectResult(new { Id = id }); - } #endregion #region Delete @@ -133,18 +84,6 @@ namespace DevHive.Web.Controllers new OkResult() : new BadRequestObjectResult("Could not delete Comment"); } - - [HttpDelete] - [Route("Comment")] - public async Task DeleteComment(Guid id, [FromHeader] string authorization) - { - if (!await this._postService.ValidateJwtForComment(id, authorization)) - return new UnauthorizedResult(); - - return await this._postService.DeleteComment(id) ? - new OkResult() : - new BadRequestObjectResult("Could not delete Comment"); - } #endregion } } diff --git a/src/DevHive.Web/Models/Comment/CreateCommentWebModel.cs b/src/DevHive.Web/Models/Comment/CreateCommentWebModel.cs new file mode 100644 index 0000000..8b2bf8d --- /dev/null +++ b/src/DevHive.Web/Models/Comment/CreateCommentWebModel.cs @@ -0,0 +1,17 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + +namespace DevHive.Web.Models.Comment +{ + public class CreateCommentWebModel + { + [NotNull] + [Required] + public Guid PostId { get; set; } + + [NotNull] + [Required] + public string Message { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Comment/ReadCommentWebModel.cs b/src/DevHive.Web/Models/Comment/ReadCommentWebModel.cs new file mode 100644 index 0000000..4d3aff7 --- /dev/null +++ b/src/DevHive.Web/Models/Comment/ReadCommentWebModel.cs @@ -0,0 +1,21 @@ +using System; + +namespace DevHive.Web.Models.Comment +{ + public class ReadCommentWebModel + { + public Guid CommentId { get; set; } + + public Guid PostId { get; set; } + + public string IssuerFirstName { get; set; } + + public string IssuerLastName { get; set; } + + public string IssuerUsername { get; set; } + + public string Message { get; set; } + + public DateTime TimeCreated { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Comment/UpdateCommentWebModel.cs b/src/DevHive.Web/Models/Comment/UpdateCommentWebModel.cs new file mode 100644 index 0000000..b5d7970 --- /dev/null +++ b/src/DevHive.Web/Models/Comment/UpdateCommentWebModel.cs @@ -0,0 +1,13 @@ +using System; + +namespace DevHive.Web.Models.Comment +{ + public class UpdateCommentWebModel + { + public Guid CommentId { get; set; } + + public Guid PostId { get; set; } + + public string NewMessage { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Feed/ReadPageWebModel.cs b/src/DevHive.Web/Models/Feed/ReadPageWebModel.cs index 40d29c9..839aaa6 100644 --- a/src/DevHive.Web/Models/Feed/ReadPageWebModel.cs +++ b/src/DevHive.Web/Models/Feed/ReadPageWebModel.cs @@ -1,5 +1,5 @@ using System.Collections.Generic; -using DevHive.Web.Models.Post.Post; +using DevHive.Web.Models.Post; namespace DevHive.Web.Controllers { diff --git a/src/DevHive.Web/Models/Post/Comment/CreateCommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/CreateCommentWebModel.cs deleted file mode 100644 index 85c67bf..0000000 --- a/src/DevHive.Web/Models/Post/Comment/CreateCommentWebModel.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; -using System.Diagnostics.CodeAnalysis; - -namespace DevHive.Web.Models.Post.Comment -{ - public class CreateCommentWebModel - { - [NotNull] - [Required] - public Guid PostId { get; set; } - - [NotNull] - [Required] - public string Message { get; set; } - } -} diff --git a/src/DevHive.Web/Models/Post/Comment/ReadCommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/ReadCommentWebModel.cs deleted file mode 100644 index 5320c3c..0000000 --- a/src/DevHive.Web/Models/Post/Comment/ReadCommentWebModel.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; - -namespace DevHive.Web.Models.Post.Comment -{ - public class ReadCommentWebModel - { - public Guid CommentId { get; set; } - - public Guid PostId { get; set; } - - public string IssuerFirstName { get; set; } - - public string IssuerLastName { get; set; } - - public string IssuerUsername { get; set; } - - public string Message { get; set; } - - public DateTime TimeCreated { get; set; } - } -} diff --git a/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs deleted file mode 100644 index cb1c60a..0000000 --- a/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; - -namespace DevHive.Web.Models.Post.Comment -{ - public class UpdateCommentWebModel - { - public Guid CommentId { get; set; } - - public Guid PostId { get; set; } - - public string NewMessage { get; set; } - } -} diff --git a/src/DevHive.Web/Models/Post/CreatePostWebModel.cs b/src/DevHive.Web/Models/Post/CreatePostWebModel.cs new file mode 100644 index 0000000..256055a --- /dev/null +++ b/src/DevHive.Web/Models/Post/CreatePostWebModel.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; +using Microsoft.AspNetCore.Http; + +namespace DevHive.Web.Models.Post +{ + public class CreatePostWebModel + { + [NotNull] + [Required] + public string Message { get; set; } + + public List Files { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs deleted file mode 100644 index e35a813..0000000 --- a/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Diagnostics.CodeAnalysis; -using Microsoft.AspNetCore.Http; - -namespace DevHive.Web.Models.Post.Post -{ - public class CreatePostWebModel - { - [NotNull] - [Required] - public string Message { get; set; } - - public List Files { get; set; } - } -} diff --git a/src/DevHive.Web/Models/Post/Post/ReadPostWebModel.cs b/src/DevHive.Web/Models/Post/Post/ReadPostWebModel.cs deleted file mode 100644 index 5d4da31..0000000 --- a/src/DevHive.Web/Models/Post/Post/ReadPostWebModel.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using System.Collections.Generic; -using DevHive.Web.Models.Post.Comment; -using Microsoft.AspNetCore.Http; - -namespace DevHive.Web.Models.Post.Post -{ - public class ReadPostWebModel - { - public Guid PostId { get; set; } - - public string CreatorFirstName { get; set; } - - public string CreatorLastName { get; set; } - - public string CreatorUsername { get; set; } - - public string Message { get; set; } - - public DateTime TimeCreated { get; set; } - - public List Comments { get; set; } - - public List Files { get; set; } - } -} diff --git a/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs deleted file mode 100644 index ac84d2c..0000000 --- a/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Diagnostics.CodeAnalysis; -using Microsoft.AspNetCore.Http; - -namespace DevHive.Web.Models.Post.Post -{ - public class UpdatePostWebModel - { - [Required] - [NotNull] - public Guid PostId { get; set; } - - [NotNull] - [Required] - public string NewMessage { get; set; } - - public List Files { get; set; } = new(); - } -} diff --git a/src/DevHive.Web/Models/Post/ReadPostWebModel.cs b/src/DevHive.Web/Models/Post/ReadPostWebModel.cs new file mode 100644 index 0000000..1d2669e --- /dev/null +++ b/src/DevHive.Web/Models/Post/ReadPostWebModel.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using DevHive.Web.Models.Comment; +using Microsoft.AspNetCore.Http; + +namespace DevHive.Web.Models.Post +{ + public class ReadPostWebModel + { + public Guid PostId { get; set; } + + public string CreatorFirstName { get; set; } + + public string CreatorLastName { get; set; } + + public string CreatorUsername { get; set; } + + public string Message { get; set; } + + public DateTime TimeCreated { get; set; } + + public List Comments { get; set; } + + public List Files { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Post/UpdatePostWebModel.cs b/src/DevHive.Web/Models/Post/UpdatePostWebModel.cs new file mode 100644 index 0000000..a0c9b61 --- /dev/null +++ b/src/DevHive.Web/Models/Post/UpdatePostWebModel.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; +using Microsoft.AspNetCore.Http; + +namespace DevHive.Web.Models.Post +{ + public class UpdatePostWebModel + { + [Required] + [NotNull] + public Guid PostId { get; set; } + + [NotNull] + [Required] + public string NewMessage { get; set; } + + public List Files { get; set; } = new(); + } +} -- cgit v1.2.3 From b41f887712ef8f2f0b602da3042261a78c5f492a Mon Sep 17 00:00:00 2001 From: transtrike Date: Mon, 1 Feb 2021 16:03:38 +0200 Subject: Commented out implementation of Rating; Bug fixes --- src/DevHive.Data/DevHiveContext.cs | 36 +- src/DevHive.Data/Interfaces/Models/IPost.cs | 2 +- src/DevHive.Data/Interfaces/Models/IRating.cs | 8 +- .../Interfaces/Repositories/IRatingRepository.cs | 4 +- .../20210201105546_Rating_Implemented.Designer.cs | 575 +++++++++++++++++++ .../20210201105546_Rating_Implemented.cs | 45 ++ ...201110523_Rating_Init_Configuration.Designer.cs | 597 ++++++++++++++++++++ .../20210201110523_Rating_Init_Configuration.cs | 77 +++ .../20210201112614_Rating_Rename.Designer.cs | 597 ++++++++++++++++++++ .../Migrations/20210201112614_Rating_Rename.cs | 78 +++ .../20210201113039_Rating_Rename_2.Designer.cs | 597 ++++++++++++++++++++ .../Migrations/20210201113039_Rating_Rename_2.cs | 97 ++++ .../20210201113809_Rating_Many-To-Many.Designer.cs | 611 ++++++++++++++++++++ .../20210201113809_Rating_Many-To-Many.cs | 45 ++ ...0913_Rating_Many-To-Many_Configured.Designer.cs | 614 +++++++++++++++++++++ ...0210201120913_Rating_Many-To-Many_Configured.cs | 25 + .../20210201140246_Rating_Frozen.Designer.cs | 601 ++++++++++++++++++++ .../Migrations/20210201140246_Rating_Frozen.cs | 137 +++++ .../Migrations/DevHiveContextModelSnapshot.cs | 80 ++- src/DevHive.Data/Models/Post.cs | 4 +- src/DevHive.Data/Models/Rating.cs | 9 +- src/DevHive.Data/Models/User.cs | 2 + src/DevHive.Data/RelationModels/RatedPosts.cs | 18 + src/DevHive.Data/RelationModels/UserFriends.cs | 2 + src/DevHive.Data/RelationModels/UserRate.cs | 16 + src/DevHive.Data/Repositories/BaseRepository.cs | 10 +- src/DevHive.Data/Repositories/CommentRepository.cs | 2 +- src/DevHive.Data/Repositories/PostRepository.cs | 8 +- src/DevHive.Data/Repositories/RatingRepository.cs | 19 +- src/DevHive.Data/Repositories/RoleRepository.cs | 2 +- src/DevHive.Data/Repositories/UserRepository.cs | 2 +- .../Configurations/Mapping/RatingMappings.cs | 5 +- src/DevHive.Services/Interfaces/IRateService.cs | 14 + .../Post/Rating/ReadPostRatingServiceModel.cs | 15 + .../Models/Post/Rating/ReadRatingServiceModel.cs | 13 - src/DevHive.Services/Services/RateService.cs | 80 +++ src/DevHive.Services/Services/RatingService.cs | 54 -- .../Extensions/ConfigureDependencyInjection.cs | 1 + .../Configurations/Mapping/RatingMappings.cs | 16 + src/DevHive.Web/Controllers/RateController.cs | 40 ++ .../Models/Post/Rating/RatePostWebModel.cs | 11 + .../Models/Post/Rating/ReadPostRatingWebModel.cs | 15 + 42 files changed, 5056 insertions(+), 128 deletions(-) create mode 100644 src/DevHive.Data/Migrations/20210201105546_Rating_Implemented.Designer.cs create mode 100644 src/DevHive.Data/Migrations/20210201105546_Rating_Implemented.cs create mode 100644 src/DevHive.Data/Migrations/20210201110523_Rating_Init_Configuration.Designer.cs create mode 100644 src/DevHive.Data/Migrations/20210201110523_Rating_Init_Configuration.cs create mode 100644 src/DevHive.Data/Migrations/20210201112614_Rating_Rename.Designer.cs create mode 100644 src/DevHive.Data/Migrations/20210201112614_Rating_Rename.cs create mode 100644 src/DevHive.Data/Migrations/20210201113039_Rating_Rename_2.Designer.cs create mode 100644 src/DevHive.Data/Migrations/20210201113039_Rating_Rename_2.cs create mode 100644 src/DevHive.Data/Migrations/20210201113809_Rating_Many-To-Many.Designer.cs create mode 100644 src/DevHive.Data/Migrations/20210201113809_Rating_Many-To-Many.cs create mode 100644 src/DevHive.Data/Migrations/20210201120913_Rating_Many-To-Many_Configured.Designer.cs create mode 100644 src/DevHive.Data/Migrations/20210201120913_Rating_Many-To-Many_Configured.cs create mode 100644 src/DevHive.Data/Migrations/20210201140246_Rating_Frozen.Designer.cs create mode 100644 src/DevHive.Data/Migrations/20210201140246_Rating_Frozen.cs create mode 100644 src/DevHive.Data/RelationModels/RatedPosts.cs create mode 100644 src/DevHive.Data/RelationModels/UserRate.cs create mode 100644 src/DevHive.Services/Interfaces/IRateService.cs create mode 100644 src/DevHive.Services/Models/Post/Rating/ReadPostRatingServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Post/Rating/ReadRatingServiceModel.cs create mode 100644 src/DevHive.Services/Services/RateService.cs delete mode 100644 src/DevHive.Services/Services/RatingService.cs create mode 100644 src/DevHive.Web/Configurations/Mapping/RatingMappings.cs create mode 100644 src/DevHive.Web/Controllers/RateController.cs create mode 100644 src/DevHive.Web/Models/Post/Rating/RatePostWebModel.cs create mode 100644 src/DevHive.Web/Models/Post/Rating/ReadPostRatingWebModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Data/DevHiveContext.cs b/src/DevHive.Data/DevHiveContext.cs index e7c606f..993ddb6 100644 --- a/src/DevHive.Data/DevHiveContext.cs +++ b/src/DevHive.Data/DevHiveContext.cs @@ -17,6 +17,8 @@ namespace DevHive.Data public DbSet Comments { get; set; } public DbSet UserFriends { get; set; } public DbSet Rating { get; set; } + public DbSet RatedPost { get; set; } + public DbSet UserRate { get; set; } protected override void OnModelCreating(ModelBuilder builder) { @@ -73,10 +75,6 @@ namespace DevHive.Data .HasMany(x => x.Comments) .WithOne(x => x.Post); - builder.Entity() - .HasOne(x => x.Rating) - .WithOne(x => x.Post); - /* Comment */ builder.Entity() .HasOne(x => x.Post) @@ -86,6 +84,36 @@ namespace DevHive.Data .HasOne(x => x.Creator) .WithMany(x => x.Comments); + /* Rating */ + builder.Entity() + .HasKey(x => x.Id); + + // builder.Entity() + // .HasOne(x => x.Post) + // .WithOne(x => x.Rating) + // .HasForeignKey(x => x.RatingId); + + // builder.Entity() + // .HasOne(x => x.Rating) + // .WithOne(x => x.Post); + + // builder.Entity() + // .HasMany(x => x.UsersThatRated); + + // /* User Rated Posts */ + builder.Entity() + .HasKey(x => new { x.UserId, x.PostId }); + + // builder.Entity() + // .HasOne(x => x.User) + // .WithMany(x => x.RatedPosts); + + // builder.Entity() + // .HasOne(x => x.Post); + + // builder.Entity() + // .HasMany(x => x.RatedPosts); + base.OnModelCreating(builder); } } diff --git a/src/DevHive.Data/Interfaces/Models/IPost.cs b/src/DevHive.Data/Interfaces/Models/IPost.cs index 65c0fb4..5031a05 100644 --- a/src/DevHive.Data/Interfaces/Models/IPost.cs +++ b/src/DevHive.Data/Interfaces/Models/IPost.cs @@ -14,7 +14,7 @@ namespace DevHive.Data.Interfaces.Models List Comments { get; set; } - Rating Rating { get; set; } + // Rating Rating { get; set; } List FileUrls { get; set; } } diff --git a/src/DevHive.Data/Interfaces/Models/IRating.cs b/src/DevHive.Data/Interfaces/Models/IRating.cs index 4604a75..d1b968f 100644 --- a/src/DevHive.Data/Interfaces/Models/IRating.cs +++ b/src/DevHive.Data/Interfaces/Models/IRating.cs @@ -1,14 +1,14 @@ -using DevHive.Data.Interfaces.Models; +using System.Collections.Generic; using DevHive.Data.Models; namespace DevHive.Data.Interfaces.Models { public interface IRating : IModel { - Post Post { get; set; } + // Post Post { get; set; } - int Likes { get; set; } + int Rate { get; set; } - int Dislikes { get; set; } + // HashSet UsersThatRated { get; set; } } } diff --git a/src/DevHive.Data/Interfaces/Repositories/IRatingRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IRatingRepository.cs index de4e3f2..7b99e0e 100644 --- a/src/DevHive.Data/Interfaces/Repositories/IRatingRepository.cs +++ b/src/DevHive.Data/Interfaces/Repositories/IRatingRepository.cs @@ -8,8 +8,6 @@ namespace DevHive.Data.Interfaces.Repositories public interface IRatingRepository : IRepository { Task GetByPostId(Guid postId); - Task> GetRating(Guid postId); - - Task HasUserRatedThisPost(Guid userId, Guid postId); + Task GetRating(Guid postId); } } diff --git a/src/DevHive.Data/Migrations/20210201105546_Rating_Implemented.Designer.cs b/src/DevHive.Data/Migrations/20210201105546_Rating_Implemented.Designer.cs new file mode 100644 index 0000000..00e4fbd --- /dev/null +++ b/src/DevHive.Data/Migrations/20210201105546_Rating_Implemented.Designer.cs @@ -0,0 +1,575 @@ +// +using System; +using System.Collections.Generic; +using DevHive.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace DevHive.Data.Migrations +{ + [DbContext(typeof(DevHiveContext))] + [Migration("20210201105546_Rating_Implemented")] + partial class Rating_Implemented + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseIdentityByDefaultColumns() + .HasAnnotation("Relational:MaxIdentifierLength", 63) + .HasAnnotation("ProductVersion", "5.0.1"); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatorId") + .HasColumnType("uuid"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.HasIndex("CreatorId"); + + b.HasIndex("PostId"); + + b.ToTable("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Language", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Languages"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatorId") + .HasColumnType("uuid"); + + b.Property>("FileUrls") + .HasColumnType("text[]"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("RatingId") + .HasColumnType("uuid"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.HasIndex("CreatorId"); + + b.HasIndex("RatingId") + .IsUnique(); + + b.ToTable("Posts"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Rating", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Dislikes") + .HasColumnType("integer"); + + b.Property("Likes") + .HasColumnType("integer"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Rating"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Technology", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Technologies"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AccessFailedCount") + .HasColumnType("integer"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("EmailConfirmed") + .HasColumnType("boolean"); + + b.Property("FirstName") + .HasColumnType("text"); + + b.Property("LastName") + .HasColumnType("text"); + + b.Property("LockoutEnabled") + .HasColumnType("boolean"); + + b.Property("LockoutEnd") + .HasColumnType("timestamp with time zone"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("PasswordHash") + .HasColumnType("text"); + + b.Property("PhoneNumber") + .HasColumnType("text"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("boolean"); + + b.Property("ProfilePictureUrl") + .HasColumnType("text"); + + b.Property("SecurityStamp") + .HasColumnType("text"); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.HasIndex("UserName") + .IsUnique(); + + b.ToTable("AspNetUsers"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserFriends", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("FriendId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "FriendId"); + + b.HasIndex("FriendId"); + + b.ToTable("UserFriends"); + }); + + modelBuilder.Entity("LanguageUser", b => + { + b.Property("LanguagesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("LanguagesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("LanguageUser"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("ProviderKey") + .HasColumnType("text"); + + b.Property("ProviderDisplayName") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.Property("RolesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("RolesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("RoleUser"); + }); + + modelBuilder.Entity("TechnologyUser", b => + { + b.Property("TechnologiesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("TechnologiesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("TechnologyUser"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.HasOne("DevHive.Data.Models.User", "Creator") + .WithMany("Comments") + .HasForeignKey("CreatorId"); + + b.HasOne("DevHive.Data.Models.Post", "Post") + .WithMany("Comments") + .HasForeignKey("PostId"); + + b.Navigation("Creator"); + + b.Navigation("Post"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.HasOne("DevHive.Data.Models.User", "Creator") + .WithMany("Posts") + .HasForeignKey("CreatorId"); + + b.HasOne("DevHive.Data.Models.Rating", "Rating") + .WithOne("Post") + .HasForeignKey("DevHive.Data.Models.Post", "RatingId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Creator"); + + b.Navigation("Rating"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Rating", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany("RatedPosts") + .HasForeignKey("UserId"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserFriends", b => + { + b.HasOne("DevHive.Data.Models.User", "Friend") + .WithMany() + .HasForeignKey("FriendId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany("Friends") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Friend"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("LanguageUser", b => + { + b.HasOne("DevHive.Data.Models.Language", null) + .WithMany() + .HasForeignKey("LanguagesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RolesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("TechnologyUser", b => + { + b.HasOne("DevHive.Data.Models.Technology", null) + .WithMany() + .HasForeignKey("TechnologiesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Navigation("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Rating", b => + { + b.Navigation("Post"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Navigation("Comments"); + + b.Navigation("Friends"); + + b.Navigation("Posts"); + + b.Navigation("RatedPosts"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/DevHive.Data/Migrations/20210201105546_Rating_Implemented.cs b/src/DevHive.Data/Migrations/20210201105546_Rating_Implemented.cs new file mode 100644 index 0000000..ae7ef8e --- /dev/null +++ b/src/DevHive.Data/Migrations/20210201105546_Rating_Implemented.cs @@ -0,0 +1,45 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace DevHive.Data.Migrations +{ + public partial class Rating_Implemented : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "UserId", + table: "Rating", + type: "uuid", + nullable: true); + + migrationBuilder.CreateIndex( + name: "IX_Rating_UserId", + table: "Rating", + column: "UserId"); + + migrationBuilder.AddForeignKey( + name: "FK_Rating_AspNetUsers_UserId", + table: "Rating", + column: "UserId", + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Rating_AspNetUsers_UserId", + table: "Rating"); + + migrationBuilder.DropIndex( + name: "IX_Rating_UserId", + table: "Rating"); + + migrationBuilder.DropColumn( + name: "UserId", + table: "Rating"); + } + } +} diff --git a/src/DevHive.Data/Migrations/20210201110523_Rating_Init_Configuration.Designer.cs b/src/DevHive.Data/Migrations/20210201110523_Rating_Init_Configuration.Designer.cs new file mode 100644 index 0000000..0aea5ce --- /dev/null +++ b/src/DevHive.Data/Migrations/20210201110523_Rating_Init_Configuration.Designer.cs @@ -0,0 +1,597 @@ +// +using System; +using System.Collections.Generic; +using DevHive.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace DevHive.Data.Migrations +{ + [DbContext(typeof(DevHiveContext))] + [Migration("20210201110523_Rating_Init_Configuration")] + partial class Rating_Init_Configuration + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseIdentityByDefaultColumns() + .HasAnnotation("Relational:MaxIdentifierLength", 63) + .HasAnnotation("ProductVersion", "5.0.1"); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatorId") + .HasColumnType("uuid"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.HasIndex("CreatorId"); + + b.HasIndex("PostId"); + + b.ToTable("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Language", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Languages"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatorId") + .HasColumnType("uuid"); + + b.Property>("FileUrls") + .HasColumnType("text[]"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("RatingId") + .HasColumnType("uuid"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.HasIndex("CreatorId"); + + b.HasIndex("RatingId") + .IsUnique(); + + b.ToTable("Posts"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Rating", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Dislikes") + .HasColumnType("integer"); + + b.Property("Likes") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("Rating"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Technology", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Technologies"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AccessFailedCount") + .HasColumnType("integer"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("EmailConfirmed") + .HasColumnType("boolean"); + + b.Property("FirstName") + .HasColumnType("text"); + + b.Property("LastName") + .HasColumnType("text"); + + b.Property("LockoutEnabled") + .HasColumnType("boolean"); + + b.Property("LockoutEnd") + .HasColumnType("timestamp with time zone"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("PasswordHash") + .HasColumnType("text"); + + b.Property("PhoneNumber") + .HasColumnType("text"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("boolean"); + + b.Property("ProfilePictureUrl") + .HasColumnType("text"); + + b.Property("SecurityStamp") + .HasColumnType("text"); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.HasIndex("UserName") + .IsUnique(); + + b.ToTable("AspNetUsers"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.RatedPosts", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "PostId"); + + b.HasIndex("PostId"); + + b.ToTable("RatedPosts"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserFriends", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("FriendId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "FriendId"); + + b.HasIndex("FriendId"); + + b.ToTable("UserFriends"); + }); + + modelBuilder.Entity("LanguageUser", b => + { + b.Property("LanguagesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("LanguagesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("LanguageUser"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("ProviderKey") + .HasColumnType("text"); + + b.Property("ProviderDisplayName") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.Property("RolesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("RolesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("RoleUser"); + }); + + modelBuilder.Entity("TechnologyUser", b => + { + b.Property("TechnologiesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("TechnologiesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("TechnologyUser"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.HasOne("DevHive.Data.Models.User", "Creator") + .WithMany("Comments") + .HasForeignKey("CreatorId"); + + b.HasOne("DevHive.Data.Models.Post", "Post") + .WithMany("Comments") + .HasForeignKey("PostId"); + + b.Navigation("Creator"); + + b.Navigation("Post"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.HasOne("DevHive.Data.Models.User", "Creator") + .WithMany("Posts") + .HasForeignKey("CreatorId"); + + b.HasOne("DevHive.Data.Models.Rating", "Rating") + .WithOne("Post") + .HasForeignKey("DevHive.Data.Models.Post", "RatingId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Creator"); + + b.Navigation("Rating"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.RatedPosts", b => + { + b.HasOne("DevHive.Data.Models.Post", "Post") + .WithMany() + .HasForeignKey("PostId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany("RatedPosts") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Post"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserFriends", b => + { + b.HasOne("DevHive.Data.Models.User", "Friend") + .WithMany() + .HasForeignKey("FriendId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany("Friends") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Friend"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("LanguageUser", b => + { + b.HasOne("DevHive.Data.Models.Language", null) + .WithMany() + .HasForeignKey("LanguagesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RolesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("TechnologyUser", b => + { + b.HasOne("DevHive.Data.Models.Technology", null) + .WithMany() + .HasForeignKey("TechnologiesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Navigation("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Rating", b => + { + b.Navigation("Post"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Navigation("Comments"); + + b.Navigation("Friends"); + + b.Navigation("Posts"); + + b.Navigation("RatedPosts"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/DevHive.Data/Migrations/20210201110523_Rating_Init_Configuration.cs b/src/DevHive.Data/Migrations/20210201110523_Rating_Init_Configuration.cs new file mode 100644 index 0000000..a5a4a72 --- /dev/null +++ b/src/DevHive.Data/Migrations/20210201110523_Rating_Init_Configuration.cs @@ -0,0 +1,77 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace DevHive.Data.Migrations +{ + public partial class Rating_Init_Configuration : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Rating_AspNetUsers_UserId", + table: "Rating"); + + migrationBuilder.DropIndex( + name: "IX_Rating_UserId", + table: "Rating"); + + migrationBuilder.DropColumn( + name: "UserId", + table: "Rating"); + + migrationBuilder.CreateTable( + name: "RatedPosts", + columns: table => new + { + UserId = table.Column(type: "uuid", nullable: false), + PostId = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_RatedPosts", x => new { x.UserId, x.PostId }); + table.ForeignKey( + name: "FK_RatedPosts_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_RatedPosts_Posts_PostId", + column: x => x.PostId, + principalTable: "Posts", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_RatedPosts_PostId", + table: "RatedPosts", + column: "PostId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "RatedPosts"); + + migrationBuilder.AddColumn( + name: "UserId", + table: "Rating", + type: "uuid", + nullable: true); + + migrationBuilder.CreateIndex( + name: "IX_Rating_UserId", + table: "Rating", + column: "UserId"); + + migrationBuilder.AddForeignKey( + name: "FK_Rating_AspNetUsers_UserId", + table: "Rating", + column: "UserId", + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + } + } +} diff --git a/src/DevHive.Data/Migrations/20210201112614_Rating_Rename.Designer.cs b/src/DevHive.Data/Migrations/20210201112614_Rating_Rename.Designer.cs new file mode 100644 index 0000000..a64baa1 --- /dev/null +++ b/src/DevHive.Data/Migrations/20210201112614_Rating_Rename.Designer.cs @@ -0,0 +1,597 @@ +// +using System; +using System.Collections.Generic; +using DevHive.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace DevHive.Data.Migrations +{ + [DbContext(typeof(DevHiveContext))] + [Migration("20210201112614_Rating_Rename")] + partial class Rating_Rename + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseIdentityByDefaultColumns() + .HasAnnotation("Relational:MaxIdentifierLength", 63) + .HasAnnotation("ProductVersion", "5.0.1"); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatorId") + .HasColumnType("uuid"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.HasIndex("CreatorId"); + + b.HasIndex("PostId"); + + b.ToTable("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Language", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Languages"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatorId") + .HasColumnType("uuid"); + + b.Property>("FileUrls") + .HasColumnType("text[]"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("RatingId") + .HasColumnType("uuid"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.HasIndex("CreatorId"); + + b.HasIndex("RatingId") + .IsUnique(); + + b.ToTable("Posts"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Rating", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Dislikes") + .HasColumnType("integer"); + + b.Property("Likes") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("Rating"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Technology", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Technologies"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AccessFailedCount") + .HasColumnType("integer"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("EmailConfirmed") + .HasColumnType("boolean"); + + b.Property("FirstName") + .HasColumnType("text"); + + b.Property("LastName") + .HasColumnType("text"); + + b.Property("LockoutEnabled") + .HasColumnType("boolean"); + + b.Property("LockoutEnd") + .HasColumnType("timestamp with time zone"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("PasswordHash") + .HasColumnType("text"); + + b.Property("PhoneNumber") + .HasColumnType("text"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("boolean"); + + b.Property("ProfilePictureUrl") + .HasColumnType("text"); + + b.Property("SecurityStamp") + .HasColumnType("text"); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.HasIndex("UserName") + .IsUnique(); + + b.ToTable("AspNetUsers"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.RatedPost", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "PostId"); + + b.HasIndex("PostId"); + + b.ToTable("RatedPost"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserFriends", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("FriendId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "FriendId"); + + b.HasIndex("FriendId"); + + b.ToTable("UserFriends"); + }); + + modelBuilder.Entity("LanguageUser", b => + { + b.Property("LanguagesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("LanguagesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("LanguageUser"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("ProviderKey") + .HasColumnType("text"); + + b.Property("ProviderDisplayName") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.Property("RolesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("RolesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("RoleUser"); + }); + + modelBuilder.Entity("TechnologyUser", b => + { + b.Property("TechnologiesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("TechnologiesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("TechnologyUser"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.HasOne("DevHive.Data.Models.User", "Creator") + .WithMany("Comments") + .HasForeignKey("CreatorId"); + + b.HasOne("DevHive.Data.Models.Post", "Post") + .WithMany("Comments") + .HasForeignKey("PostId"); + + b.Navigation("Creator"); + + b.Navigation("Post"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.HasOne("DevHive.Data.Models.User", "Creator") + .WithMany("Posts") + .HasForeignKey("CreatorId"); + + b.HasOne("DevHive.Data.Models.Rating", "Rating") + .WithOne("Post") + .HasForeignKey("DevHive.Data.Models.Post", "RatingId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Creator"); + + b.Navigation("Rating"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.RatedPost", b => + { + b.HasOne("DevHive.Data.Models.Post", "Post") + .WithMany() + .HasForeignKey("PostId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany("RatedPosts") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Post"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserFriends", b => + { + b.HasOne("DevHive.Data.Models.User", "Friend") + .WithMany() + .HasForeignKey("FriendId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany("Friends") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Friend"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("LanguageUser", b => + { + b.HasOne("DevHive.Data.Models.Language", null) + .WithMany() + .HasForeignKey("LanguagesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RolesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("TechnologyUser", b => + { + b.HasOne("DevHive.Data.Models.Technology", null) + .WithMany() + .HasForeignKey("TechnologiesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Navigation("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Rating", b => + { + b.Navigation("Post"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Navigation("Comments"); + + b.Navigation("Friends"); + + b.Navigation("Posts"); + + b.Navigation("RatedPosts"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/DevHive.Data/Migrations/20210201112614_Rating_Rename.cs b/src/DevHive.Data/Migrations/20210201112614_Rating_Rename.cs new file mode 100644 index 0000000..1fb4743 --- /dev/null +++ b/src/DevHive.Data/Migrations/20210201112614_Rating_Rename.cs @@ -0,0 +1,78 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace DevHive.Data.Migrations +{ + public partial class Rating_Rename : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "RatedPosts"); + + migrationBuilder.CreateTable( + name: "RatedPost", + columns: table => new + { + UserId = table.Column(type: "uuid", nullable: false), + PostId = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_RatedPost", x => new { x.UserId, x.PostId }); + table.ForeignKey( + name: "FK_RatedPost_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_RatedPost_Posts_PostId", + column: x => x.PostId, + principalTable: "Posts", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_RatedPost_PostId", + table: "RatedPost", + column: "PostId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "RatedPost"); + + migrationBuilder.CreateTable( + name: "RatedPosts", + columns: table => new + { + UserId = table.Column(type: "uuid", nullable: false), + PostId = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_RatedPosts", x => new { x.UserId, x.PostId }); + table.ForeignKey( + name: "FK_RatedPosts_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_RatedPosts_Posts_PostId", + column: x => x.PostId, + principalTable: "Posts", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_RatedPosts_PostId", + table: "RatedPosts", + column: "PostId"); + } + } +} diff --git a/src/DevHive.Data/Migrations/20210201113039_Rating_Rename_2.Designer.cs b/src/DevHive.Data/Migrations/20210201113039_Rating_Rename_2.Designer.cs new file mode 100644 index 0000000..82e08a5 --- /dev/null +++ b/src/DevHive.Data/Migrations/20210201113039_Rating_Rename_2.Designer.cs @@ -0,0 +1,597 @@ +// +using System; +using System.Collections.Generic; +using DevHive.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace DevHive.Data.Migrations +{ + [DbContext(typeof(DevHiveContext))] + [Migration("20210201113039_Rating_Rename_2")] + partial class Rating_Rename_2 + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseIdentityByDefaultColumns() + .HasAnnotation("Relational:MaxIdentifierLength", 63) + .HasAnnotation("ProductVersion", "5.0.1"); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatorId") + .HasColumnType("uuid"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.HasIndex("CreatorId"); + + b.HasIndex("PostId"); + + b.ToTable("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Language", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Languages"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatorId") + .HasColumnType("uuid"); + + b.Property>("FileUrls") + .HasColumnType("text[]"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("RatingId") + .HasColumnType("uuid"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.HasIndex("CreatorId"); + + b.HasIndex("RatingId") + .IsUnique(); + + b.ToTable("Posts"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Rating", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Dislikes") + .HasColumnType("integer"); + + b.Property("Likes") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("Rating"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Technology", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Technologies"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AccessFailedCount") + .HasColumnType("integer"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("EmailConfirmed") + .HasColumnType("boolean"); + + b.Property("FirstName") + .HasColumnType("text"); + + b.Property("LastName") + .HasColumnType("text"); + + b.Property("LockoutEnabled") + .HasColumnType("boolean"); + + b.Property("LockoutEnd") + .HasColumnType("timestamp with time zone"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("PasswordHash") + .HasColumnType("text"); + + b.Property("PhoneNumber") + .HasColumnType("text"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("boolean"); + + b.Property("ProfilePictureUrl") + .HasColumnType("text"); + + b.Property("SecurityStamp") + .HasColumnType("text"); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.HasIndex("UserName") + .IsUnique(); + + b.ToTable("AspNetUsers"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.RatedPost", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "PostId"); + + b.HasIndex("PostId"); + + b.ToTable("RatedPosts"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserFriends", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("FriendId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "FriendId"); + + b.HasIndex("FriendId"); + + b.ToTable("UserFriends"); + }); + + modelBuilder.Entity("LanguageUser", b => + { + b.Property("LanguagesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("LanguagesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("LanguageUser"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("ProviderKey") + .HasColumnType("text"); + + b.Property("ProviderDisplayName") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.Property("RolesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("RolesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("RoleUser"); + }); + + modelBuilder.Entity("TechnologyUser", b => + { + b.Property("TechnologiesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("TechnologiesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("TechnologyUser"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.HasOne("DevHive.Data.Models.User", "Creator") + .WithMany("Comments") + .HasForeignKey("CreatorId"); + + b.HasOne("DevHive.Data.Models.Post", "Post") + .WithMany("Comments") + .HasForeignKey("PostId"); + + b.Navigation("Creator"); + + b.Navigation("Post"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.HasOne("DevHive.Data.Models.User", "Creator") + .WithMany("Posts") + .HasForeignKey("CreatorId"); + + b.HasOne("DevHive.Data.Models.Rating", "Rating") + .WithOne("Post") + .HasForeignKey("DevHive.Data.Models.Post", "RatingId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Creator"); + + b.Navigation("Rating"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.RatedPost", b => + { + b.HasOne("DevHive.Data.Models.Post", "Post") + .WithMany() + .HasForeignKey("PostId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany("RatedPosts") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Post"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserFriends", b => + { + b.HasOne("DevHive.Data.Models.User", "Friend") + .WithMany() + .HasForeignKey("FriendId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany("Friends") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Friend"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("LanguageUser", b => + { + b.HasOne("DevHive.Data.Models.Language", null) + .WithMany() + .HasForeignKey("LanguagesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RolesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("TechnologyUser", b => + { + b.HasOne("DevHive.Data.Models.Technology", null) + .WithMany() + .HasForeignKey("TechnologiesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Navigation("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Rating", b => + { + b.Navigation("Post"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Navigation("Comments"); + + b.Navigation("Friends"); + + b.Navigation("Posts"); + + b.Navigation("RatedPosts"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/DevHive.Data/Migrations/20210201113039_Rating_Rename_2.cs b/src/DevHive.Data/Migrations/20210201113039_Rating_Rename_2.cs new file mode 100644 index 0000000..9dc87cf --- /dev/null +++ b/src/DevHive.Data/Migrations/20210201113039_Rating_Rename_2.cs @@ -0,0 +1,97 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace DevHive.Data.Migrations +{ + public partial class Rating_Rename_2 : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_RatedPost_AspNetUsers_UserId", + table: "RatedPost"); + + migrationBuilder.DropForeignKey( + name: "FK_RatedPost_Posts_PostId", + table: "RatedPost"); + + migrationBuilder.DropPrimaryKey( + name: "PK_RatedPost", + table: "RatedPost"); + + migrationBuilder.RenameTable( + name: "RatedPost", + newName: "RatedPosts"); + + migrationBuilder.RenameIndex( + name: "IX_RatedPost_PostId", + table: "RatedPosts", + newName: "IX_RatedPosts_PostId"); + + migrationBuilder.AddPrimaryKey( + name: "PK_RatedPosts", + table: "RatedPosts", + columns: new[] { "UserId", "PostId" }); + + migrationBuilder.AddForeignKey( + name: "FK_RatedPosts_AspNetUsers_UserId", + table: "RatedPosts", + column: "UserId", + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_RatedPosts_Posts_PostId", + table: "RatedPosts", + column: "PostId", + principalTable: "Posts", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_RatedPosts_AspNetUsers_UserId", + table: "RatedPosts"); + + migrationBuilder.DropForeignKey( + name: "FK_RatedPosts_Posts_PostId", + table: "RatedPosts"); + + migrationBuilder.DropPrimaryKey( + name: "PK_RatedPosts", + table: "RatedPosts"); + + migrationBuilder.RenameTable( + name: "RatedPosts", + newName: "RatedPost"); + + migrationBuilder.RenameIndex( + name: "IX_RatedPosts_PostId", + table: "RatedPost", + newName: "IX_RatedPost_PostId"); + + migrationBuilder.AddPrimaryKey( + name: "PK_RatedPost", + table: "RatedPost", + columns: new[] { "UserId", "PostId" }); + + migrationBuilder.AddForeignKey( + name: "FK_RatedPost_AspNetUsers_UserId", + table: "RatedPost", + column: "UserId", + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_RatedPost_Posts_PostId", + table: "RatedPost", + column: "PostId", + principalTable: "Posts", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + } +} diff --git a/src/DevHive.Data/Migrations/20210201113809_Rating_Many-To-Many.Designer.cs b/src/DevHive.Data/Migrations/20210201113809_Rating_Many-To-Many.Designer.cs new file mode 100644 index 0000000..849715a --- /dev/null +++ b/src/DevHive.Data/Migrations/20210201113809_Rating_Many-To-Many.Designer.cs @@ -0,0 +1,611 @@ +// +using System; +using System.Collections.Generic; +using DevHive.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace DevHive.Data.Migrations +{ + [DbContext(typeof(DevHiveContext))] + [Migration("20210201113809_Rating_Many-To-Many")] + partial class Rating_ManyToMany + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseIdentityByDefaultColumns() + .HasAnnotation("Relational:MaxIdentifierLength", 63) + .HasAnnotation("ProductVersion", "5.0.1"); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatorId") + .HasColumnType("uuid"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.HasIndex("CreatorId"); + + b.HasIndex("PostId"); + + b.ToTable("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Language", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Languages"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatorId") + .HasColumnType("uuid"); + + b.Property>("FileUrls") + .HasColumnType("text[]"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("RatingId") + .HasColumnType("uuid"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.HasIndex("CreatorId"); + + b.HasIndex("RatingId") + .IsUnique(); + + b.ToTable("Posts"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Rating", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Dislikes") + .HasColumnType("integer"); + + b.Property("Likes") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("Rating"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Technology", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Technologies"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AccessFailedCount") + .HasColumnType("integer"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("EmailConfirmed") + .HasColumnType("boolean"); + + b.Property("FirstName") + .HasColumnType("text"); + + b.Property("LastName") + .HasColumnType("text"); + + b.Property("LockoutEnabled") + .HasColumnType("boolean"); + + b.Property("LockoutEnd") + .HasColumnType("timestamp with time zone"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("PasswordHash") + .HasColumnType("text"); + + b.Property("PhoneNumber") + .HasColumnType("text"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("boolean"); + + b.Property("ProfilePictureUrl") + .HasColumnType("text"); + + b.Property("RatingId") + .HasColumnType("uuid"); + + b.Property("SecurityStamp") + .HasColumnType("text"); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.HasIndex("RatingId"); + + b.HasIndex("UserName") + .IsUnique(); + + b.ToTable("AspNetUsers"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.RatedPost", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "PostId"); + + b.HasIndex("PostId"); + + b.ToTable("RatedPosts"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserFriends", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("FriendId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "FriendId"); + + b.HasIndex("FriendId"); + + b.ToTable("UserFriends"); + }); + + modelBuilder.Entity("LanguageUser", b => + { + b.Property("LanguagesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("LanguagesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("LanguageUser"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("ProviderKey") + .HasColumnType("text"); + + b.Property("ProviderDisplayName") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.Property("RolesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("RolesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("RoleUser"); + }); + + modelBuilder.Entity("TechnologyUser", b => + { + b.Property("TechnologiesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("TechnologiesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("TechnologyUser"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.HasOne("DevHive.Data.Models.User", "Creator") + .WithMany("Comments") + .HasForeignKey("CreatorId"); + + b.HasOne("DevHive.Data.Models.Post", "Post") + .WithMany("Comments") + .HasForeignKey("PostId"); + + b.Navigation("Creator"); + + b.Navigation("Post"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.HasOne("DevHive.Data.Models.User", "Creator") + .WithMany("Posts") + .HasForeignKey("CreatorId"); + + b.HasOne("DevHive.Data.Models.Rating", "Rating") + .WithOne("Post") + .HasForeignKey("DevHive.Data.Models.Post", "RatingId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Creator"); + + b.Navigation("Rating"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.HasOne("DevHive.Data.Models.Rating", null) + .WithMany("UsersThatRated") + .HasForeignKey("RatingId"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.RatedPost", b => + { + b.HasOne("DevHive.Data.Models.Post", "Post") + .WithMany() + .HasForeignKey("PostId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany("RatedPosts") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Post"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserFriends", b => + { + b.HasOne("DevHive.Data.Models.User", "Friend") + .WithMany() + .HasForeignKey("FriendId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany("Friends") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Friend"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("LanguageUser", b => + { + b.HasOne("DevHive.Data.Models.Language", null) + .WithMany() + .HasForeignKey("LanguagesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RolesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("TechnologyUser", b => + { + b.HasOne("DevHive.Data.Models.Technology", null) + .WithMany() + .HasForeignKey("TechnologiesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Navigation("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Rating", b => + { + b.Navigation("Post"); + + b.Navigation("UsersThatRated"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Navigation("Comments"); + + b.Navigation("Friends"); + + b.Navigation("Posts"); + + b.Navigation("RatedPosts"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/DevHive.Data/Migrations/20210201113809_Rating_Many-To-Many.cs b/src/DevHive.Data/Migrations/20210201113809_Rating_Many-To-Many.cs new file mode 100644 index 0000000..4ec0bd7 --- /dev/null +++ b/src/DevHive.Data/Migrations/20210201113809_Rating_Many-To-Many.cs @@ -0,0 +1,45 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace DevHive.Data.Migrations +{ + public partial class Rating_ManyToMany : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "RatingId", + table: "AspNetUsers", + type: "uuid", + nullable: true); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUsers_RatingId", + table: "AspNetUsers", + column: "RatingId"); + + migrationBuilder.AddForeignKey( + name: "FK_AspNetUsers_Rating_RatingId", + table: "AspNetUsers", + column: "RatingId", + principalTable: "Rating", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_AspNetUsers_Rating_RatingId", + table: "AspNetUsers"); + + migrationBuilder.DropIndex( + name: "IX_AspNetUsers_RatingId", + table: "AspNetUsers"); + + migrationBuilder.DropColumn( + name: "RatingId", + table: "AspNetUsers"); + } + } +} diff --git a/src/DevHive.Data/Migrations/20210201120913_Rating_Many-To-Many_Configured.Designer.cs b/src/DevHive.Data/Migrations/20210201120913_Rating_Many-To-Many_Configured.Designer.cs new file mode 100644 index 0000000..e31576f --- /dev/null +++ b/src/DevHive.Data/Migrations/20210201120913_Rating_Many-To-Many_Configured.Designer.cs @@ -0,0 +1,614 @@ +// +using System; +using System.Collections.Generic; +using DevHive.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace DevHive.Data.Migrations +{ + [DbContext(typeof(DevHiveContext))] + [Migration("20210201120913_Rating_Many-To-Many_Configured")] + partial class Rating_ManyToMany_Configured + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseIdentityByDefaultColumns() + .HasAnnotation("Relational:MaxIdentifierLength", 63) + .HasAnnotation("ProductVersion", "5.0.1"); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatorId") + .HasColumnType("uuid"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.HasIndex("CreatorId"); + + b.HasIndex("PostId"); + + b.ToTable("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Language", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Languages"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatorId") + .HasColumnType("uuid"); + + b.Property>("FileUrls") + .HasColumnType("text[]"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("RatingId") + .HasColumnType("uuid"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.HasIndex("CreatorId"); + + b.HasIndex("RatingId") + .IsUnique(); + + b.ToTable("Posts"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Rating", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Dislikes") + .HasColumnType("integer"); + + b.Property("Likes") + .HasColumnType("integer"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.ToTable("Rating"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Technology", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Technologies"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AccessFailedCount") + .HasColumnType("integer"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("EmailConfirmed") + .HasColumnType("boolean"); + + b.Property("FirstName") + .HasColumnType("text"); + + b.Property("LastName") + .HasColumnType("text"); + + b.Property("LockoutEnabled") + .HasColumnType("boolean"); + + b.Property("LockoutEnd") + .HasColumnType("timestamp with time zone"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("PasswordHash") + .HasColumnType("text"); + + b.Property("PhoneNumber") + .HasColumnType("text"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("boolean"); + + b.Property("ProfilePictureUrl") + .HasColumnType("text"); + + b.Property("RatingId") + .HasColumnType("uuid"); + + b.Property("SecurityStamp") + .HasColumnType("text"); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.HasIndex("RatingId"); + + b.HasIndex("UserName") + .IsUnique(); + + b.ToTable("AspNetUsers"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.RatedPost", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "PostId"); + + b.HasIndex("PostId"); + + b.ToTable("RatedPosts"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserFriends", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("FriendId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "FriendId"); + + b.HasIndex("FriendId"); + + b.ToTable("UserFriends"); + }); + + modelBuilder.Entity("LanguageUser", b => + { + b.Property("LanguagesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("LanguagesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("LanguageUser"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("ProviderKey") + .HasColumnType("text"); + + b.Property("ProviderDisplayName") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.Property("RolesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("RolesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("RoleUser"); + }); + + modelBuilder.Entity("TechnologyUser", b => + { + b.Property("TechnologiesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("TechnologiesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("TechnologyUser"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.HasOne("DevHive.Data.Models.User", "Creator") + .WithMany("Comments") + .HasForeignKey("CreatorId"); + + b.HasOne("DevHive.Data.Models.Post", "Post") + .WithMany("Comments") + .HasForeignKey("PostId"); + + b.Navigation("Creator"); + + b.Navigation("Post"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.HasOne("DevHive.Data.Models.User", "Creator") + .WithMany("Posts") + .HasForeignKey("CreatorId"); + + b.HasOne("DevHive.Data.Models.Rating", "Rating") + .WithOne("Post") + .HasForeignKey("DevHive.Data.Models.Post", "RatingId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Creator"); + + b.Navigation("Rating"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.HasOne("DevHive.Data.Models.Rating", null) + .WithMany("UsersThatRated") + .HasForeignKey("RatingId"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.RatedPost", b => + { + b.HasOne("DevHive.Data.Models.Post", "Post") + .WithMany() + .HasForeignKey("PostId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany("RatedPosts") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Post"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserFriends", b => + { + b.HasOne("DevHive.Data.Models.User", "Friend") + .WithMany() + .HasForeignKey("FriendId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany("Friends") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Friend"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("LanguageUser", b => + { + b.HasOne("DevHive.Data.Models.Language", null) + .WithMany() + .HasForeignKey("LanguagesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RolesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("TechnologyUser", b => + { + b.HasOne("DevHive.Data.Models.Technology", null) + .WithMany() + .HasForeignKey("TechnologiesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Navigation("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Rating", b => + { + b.Navigation("Post"); + + b.Navigation("UsersThatRated"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Navigation("Comments"); + + b.Navigation("Friends"); + + b.Navigation("Posts"); + + b.Navigation("RatedPosts"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/DevHive.Data/Migrations/20210201120913_Rating_Many-To-Many_Configured.cs b/src/DevHive.Data/Migrations/20210201120913_Rating_Many-To-Many_Configured.cs new file mode 100644 index 0000000..7800d3a --- /dev/null +++ b/src/DevHive.Data/Migrations/20210201120913_Rating_Many-To-Many_Configured.cs @@ -0,0 +1,25 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace DevHive.Data.Migrations +{ + public partial class Rating_ManyToMany_Configured : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "PostId", + table: "Rating", + type: "uuid", + nullable: false, + defaultValue: new Guid("00000000-0000-0000-0000-000000000000")); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "PostId", + table: "Rating"); + } + } +} diff --git a/src/DevHive.Data/Migrations/20210201140246_Rating_Frozen.Designer.cs b/src/DevHive.Data/Migrations/20210201140246_Rating_Frozen.Designer.cs new file mode 100644 index 0000000..17c80e0 --- /dev/null +++ b/src/DevHive.Data/Migrations/20210201140246_Rating_Frozen.Designer.cs @@ -0,0 +1,601 @@ +// +using System; +using System.Collections.Generic; +using DevHive.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace DevHive.Data.Migrations +{ + [DbContext(typeof(DevHiveContext))] + [Migration("20210201140246_Rating_Frozen")] + partial class Rating_Frozen + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseIdentityByDefaultColumns() + .HasAnnotation("Relational:MaxIdentifierLength", 63) + .HasAnnotation("ProductVersion", "5.0.1"); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatorId") + .HasColumnType("uuid"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.HasIndex("CreatorId"); + + b.HasIndex("PostId"); + + b.ToTable("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Language", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Languages"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatorId") + .HasColumnType("uuid"); + + b.Property>("FileUrls") + .HasColumnType("text[]"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.HasIndex("CreatorId"); + + b.ToTable("Posts"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Rating", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Rate") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("Rating"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Technology", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Technologies"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AccessFailedCount") + .HasColumnType("integer"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("EmailConfirmed") + .HasColumnType("boolean"); + + b.Property("FirstName") + .HasColumnType("text"); + + b.Property("LastName") + .HasColumnType("text"); + + b.Property("LockoutEnabled") + .HasColumnType("boolean"); + + b.Property("LockoutEnd") + .HasColumnType("timestamp with time zone"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("PasswordHash") + .HasColumnType("text"); + + b.Property("PhoneNumber") + .HasColumnType("text"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("boolean"); + + b.Property("ProfilePictureUrl") + .HasColumnType("text"); + + b.Property("SecurityStamp") + .HasColumnType("text"); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.HasIndex("UserName") + .IsUnique(); + + b.ToTable("AspNetUsers"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.RatedPost", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "PostId"); + + b.HasIndex("PostId"); + + b.ToTable("RatedPosts"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserFriends", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("FriendId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "FriendId"); + + b.HasIndex("FriendId"); + + b.ToTable("UserFriends"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserRate", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Rate") + .HasColumnType("boolean"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("UserRates"); + }); + + modelBuilder.Entity("LanguageUser", b => + { + b.Property("LanguagesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("LanguagesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("LanguageUser"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("ProviderKey") + .HasColumnType("text"); + + b.Property("ProviderDisplayName") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.Property("RolesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("RolesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("RoleUser"); + }); + + modelBuilder.Entity("TechnologyUser", b => + { + b.Property("TechnologiesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("TechnologiesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("TechnologyUser"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.HasOne("DevHive.Data.Models.User", "Creator") + .WithMany("Comments") + .HasForeignKey("CreatorId"); + + b.HasOne("DevHive.Data.Models.Post", "Post") + .WithMany("Comments") + .HasForeignKey("PostId"); + + b.Navigation("Creator"); + + b.Navigation("Post"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.HasOne("DevHive.Data.Models.User", "Creator") + .WithMany("Posts") + .HasForeignKey("CreatorId"); + + b.Navigation("Creator"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.RatedPost", b => + { + b.HasOne("DevHive.Data.Models.Post", "Post") + .WithMany() + .HasForeignKey("PostId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Post"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserFriends", b => + { + b.HasOne("DevHive.Data.Models.User", "Friend") + .WithMany() + .HasForeignKey("FriendId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany("Friends") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Friend"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserRate", b => + { + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany() + .HasForeignKey("UserId"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("LanguageUser", b => + { + b.HasOne("DevHive.Data.Models.Language", null) + .WithMany() + .HasForeignKey("LanguagesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RolesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("TechnologyUser", b => + { + b.HasOne("DevHive.Data.Models.Technology", null) + .WithMany() + .HasForeignKey("TechnologiesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Navigation("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Navigation("Comments"); + + b.Navigation("Friends"); + + b.Navigation("Posts"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/DevHive.Data/Migrations/20210201140246_Rating_Frozen.cs b/src/DevHive.Data/Migrations/20210201140246_Rating_Frozen.cs new file mode 100644 index 0000000..49b0809 --- /dev/null +++ b/src/DevHive.Data/Migrations/20210201140246_Rating_Frozen.cs @@ -0,0 +1,137 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace DevHive.Data.Migrations +{ + public partial class Rating_Frozen : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_AspNetUsers_Rating_RatingId", + table: "AspNetUsers"); + + migrationBuilder.DropForeignKey( + name: "FK_Posts_Rating_RatingId", + table: "Posts"); + + migrationBuilder.DropIndex( + name: "IX_Posts_RatingId", + table: "Posts"); + + migrationBuilder.DropIndex( + name: "IX_AspNetUsers_RatingId", + table: "AspNetUsers"); + + migrationBuilder.DropColumn( + name: "Dislikes", + table: "Rating"); + + migrationBuilder.DropColumn( + name: "PostId", + table: "Rating"); + + migrationBuilder.DropColumn( + name: "RatingId", + table: "Posts"); + + migrationBuilder.DropColumn( + name: "RatingId", + table: "AspNetUsers"); + + migrationBuilder.RenameColumn( + name: "Likes", + table: "Rating", + newName: "Rate"); + + migrationBuilder.CreateTable( + name: "UserRates", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + UserId = table.Column(type: "uuid", nullable: true), + Rate = table.Column(type: "boolean", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_UserRates", x => x.Id); + table.ForeignKey( + name: "FK_UserRates_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateIndex( + name: "IX_UserRates_UserId", + table: "UserRates", + column: "UserId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "UserRates"); + + migrationBuilder.RenameColumn( + name: "Rate", + table: "Rating", + newName: "Likes"); + + migrationBuilder.AddColumn( + name: "Dislikes", + table: "Rating", + type: "integer", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "PostId", + table: "Rating", + type: "uuid", + nullable: false, + defaultValue: new Guid("00000000-0000-0000-0000-000000000000")); + + migrationBuilder.AddColumn( + name: "RatingId", + table: "Posts", + type: "uuid", + nullable: false, + defaultValue: new Guid("00000000-0000-0000-0000-000000000000")); + + migrationBuilder.AddColumn( + name: "RatingId", + table: "AspNetUsers", + type: "uuid", + nullable: true); + + migrationBuilder.CreateIndex( + name: "IX_Posts_RatingId", + table: "Posts", + column: "RatingId", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUsers_RatingId", + table: "AspNetUsers", + column: "RatingId"); + + migrationBuilder.AddForeignKey( + name: "FK_AspNetUsers_Rating_RatingId", + table: "AspNetUsers", + column: "RatingId", + principalTable: "Rating", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Posts_Rating_RatingId", + table: "Posts", + column: "RatingId", + principalTable: "Rating", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + } +} diff --git a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs index 064fb26..435adb4 100644 --- a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs +++ b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs @@ -76,9 +76,6 @@ namespace DevHive.Data.Migrations b.Property("Message") .HasColumnType("text"); - b.Property("RatingId") - .HasColumnType("uuid"); - b.Property("TimeCreated") .HasColumnType("timestamp without time zone"); @@ -86,9 +83,6 @@ namespace DevHive.Data.Migrations b.HasIndex("CreatorId"); - b.HasIndex("RatingId") - .IsUnique(); - b.ToTable("Posts"); }); @@ -98,10 +92,7 @@ namespace DevHive.Data.Migrations .ValueGeneratedOnAdd() .HasColumnType("uuid"); - b.Property("Dislikes") - .HasColumnType("integer"); - - b.Property("Likes") + b.Property("Rate") .HasColumnType("integer"); b.HasKey("Id"); @@ -227,6 +218,21 @@ namespace DevHive.Data.Migrations b.ToTable("AspNetUsers"); }); + modelBuilder.Entity("DevHive.Data.RelationModels.RatedPost", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "PostId"); + + b.HasIndex("PostId"); + + b.ToTable("RatedPosts"); + }); + modelBuilder.Entity("DevHive.Data.RelationModels.UserFriends", b => { b.Property("UserId") @@ -242,6 +248,25 @@ namespace DevHive.Data.Migrations b.ToTable("UserFriends"); }); + modelBuilder.Entity("DevHive.Data.RelationModels.UserRate", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Rate") + .HasColumnType("boolean"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("UserRates"); + }); + modelBuilder.Entity("LanguageUser", b => { b.Property("LanguagesId") @@ -409,15 +434,26 @@ namespace DevHive.Data.Migrations .WithMany("Posts") .HasForeignKey("CreatorId"); - b.HasOne("DevHive.Data.Models.Rating", "Rating") - .WithOne("Post") - .HasForeignKey("DevHive.Data.Models.Post", "RatingId") + b.Navigation("Creator"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.RatedPost", b => + { + b.HasOne("DevHive.Data.Models.Post", "Post") + .WithMany() + .HasForeignKey("PostId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.Navigation("Creator"); + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); - b.Navigation("Rating"); + b.Navigation("Post"); + + b.Navigation("User"); }); modelBuilder.Entity("DevHive.Data.RelationModels.UserFriends", b => @@ -439,6 +475,15 @@ namespace DevHive.Data.Migrations b.Navigation("User"); }); + modelBuilder.Entity("DevHive.Data.RelationModels.UserRate", b => + { + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany() + .HasForeignKey("UserId"); + + b.Navigation("User"); + }); + modelBuilder.Entity("LanguageUser", b => { b.HasOne("DevHive.Data.Models.Language", null) @@ -540,11 +585,6 @@ namespace DevHive.Data.Migrations b.Navigation("Comments"); }); - modelBuilder.Entity("DevHive.Data.Models.Rating", b => - { - b.Navigation("Post"); - }); - modelBuilder.Entity("DevHive.Data.Models.User", b => { b.Navigation("Comments"); diff --git a/src/DevHive.Data/Models/Post.cs b/src/DevHive.Data/Models/Post.cs index bb22576..8fb3e91 100644 --- a/src/DevHive.Data/Models/Post.cs +++ b/src/DevHive.Data/Models/Post.cs @@ -18,8 +18,8 @@ namespace DevHive.Data.Models public List Comments { get; set; } = new(); - public Guid RatingId { get; set; } - public Rating Rating { get; set; } = new(); + // public Guid RatingId { get; set; } + // public Rating Rating { get; set; } = new(); public List FileUrls { get; set; } = new(); } diff --git a/src/DevHive.Data/Models/Rating.cs b/src/DevHive.Data/Models/Rating.cs index 8ebde9a..d07692d 100644 --- a/src/DevHive.Data/Models/Rating.cs +++ b/src/DevHive.Data/Models/Rating.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using DevHive.Data.Interfaces.Models; +using DevHive.Data.RelationModels; namespace DevHive.Data.Models { @@ -7,10 +9,11 @@ namespace DevHive.Data.Models { public Guid Id { get; set; } - public Post Post { get; set; } + // public Guid PostId { get; set; } + // public Post Post { get; set; } - public int Likes { get; set; } + public int Rate { get; set; } - public int Dislikes { get; set; } + // public HashSet UsersThatRated { get; set; } = new(); } } diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs index da18c2f..44f3612 100644 --- a/src/DevHive.Data/Models/User.cs +++ b/src/DevHive.Data/Models/User.cs @@ -27,5 +27,7 @@ namespace DevHive.Data.Models public HashSet Posts { get; set; } = new(); public HashSet Comments { get; set; } = new(); + + // public HashSet RatedPosts { get; set; } = new(); } } diff --git a/src/DevHive.Data/RelationModels/RatedPosts.cs b/src/DevHive.Data/RelationModels/RatedPosts.cs new file mode 100644 index 0000000..7001d92 --- /dev/null +++ b/src/DevHive.Data/RelationModels/RatedPosts.cs @@ -0,0 +1,18 @@ +using System; +using System.ComponentModel.DataAnnotations.Schema; +using System.Reflection.Metadata.Ecma335; +using DevHive.Data.Models; +using Microsoft.EntityFrameworkCore; + +namespace DevHive.Data.RelationModels +{ + [Table("RatedPosts")] + public class RatedPost + { + public Guid UserId { get; set; } + public User User { get; set; } + + public Guid PostId { get; set; } + public Post Post { get; set; } + } +} diff --git a/src/DevHive.Data/RelationModels/UserFriends.cs b/src/DevHive.Data/RelationModels/UserFriends.cs index 485d6ec..31bdee2 100644 --- a/src/DevHive.Data/RelationModels/UserFriends.cs +++ b/src/DevHive.Data/RelationModels/UserFriends.cs @@ -1,9 +1,11 @@ using System; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using DevHive.Data.Models; namespace DevHive.Data.RelationModels { + [Table("UserFriends")] public class UserFriends { public Guid UserId { get; set; } diff --git a/src/DevHive.Data/RelationModels/UserRate.cs b/src/DevHive.Data/RelationModels/UserRate.cs new file mode 100644 index 0000000..06e9ab5 --- /dev/null +++ b/src/DevHive.Data/RelationModels/UserRate.cs @@ -0,0 +1,16 @@ +using System; +using System.ComponentModel.DataAnnotations.Schema; +using DevHive.Data.Models; + +namespace DevHive.Data.RelationModels +{ + [Table("UserRates")] + public class UserRate + { + public Guid Id { get; set; } + + public User User { get; set; } + + public bool Rate { get; set; } + } +} diff --git a/src/DevHive.Data/Repositories/BaseRepository.cs b/src/DevHive.Data/Repositories/BaseRepository.cs index cac802e..ece372e 100644 --- a/src/DevHive.Data/Repositories/BaseRepository.cs +++ b/src/DevHive.Data/Repositories/BaseRepository.cs @@ -21,7 +21,7 @@ namespace DevHive.Data.Repositories .Set() .AddAsync(entity); - return await this.SaveChangesAsync(_context); + return await this.SaveChangesAsync(); } public virtual async Task GetByIdAsync(Guid id) @@ -39,19 +39,19 @@ namespace DevHive.Data.Repositories entry.State = EntityState.Modified; - return await this.SaveChangesAsync(_context); + return await this.SaveChangesAsync(); } public virtual async Task DeleteAsync(TEntity entity) { this._context.Remove(entity); - return await this.SaveChangesAsync(_context); + return await this.SaveChangesAsync(); } - public virtual async Task SaveChangesAsync(DbContext context) + public virtual async Task SaveChangesAsync() { - int result = await context.SaveChangesAsync(); + int result = await _context.SaveChangesAsync(); return result >= 1; } diff --git a/src/DevHive.Data/Repositories/CommentRepository.cs b/src/DevHive.Data/Repositories/CommentRepository.cs index 8ddc628..1560c97 100644 --- a/src/DevHive.Data/Repositories/CommentRepository.cs +++ b/src/DevHive.Data/Repositories/CommentRepository.cs @@ -43,7 +43,7 @@ namespace DevHive.Data.Repositories .CurrentValues .SetValues(newEntity); - return await this.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(); } #endregion diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index f9f2475..31eb7d0 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -24,7 +24,7 @@ namespace DevHive.Data.Repositories return await this._context.Posts .Include(x => x.Comments) .Include(x => x.Creator) - .Include(x => x.Rating) + // .Include(x => x.Rating) .FirstOrDefaultAsync(x => x.Id == id); } @@ -45,7 +45,7 @@ namespace DevHive.Data.Repositories public override async Task EditAsync(Guid id, Post newEntity) { Post post = await this.GetByIdAsync(id); - var ratingId = post.RatingId; + // var ratingId = post.Rating.Id; this._context .Entry(post) @@ -60,11 +60,11 @@ namespace DevHive.Data.Repositories foreach(var comment in newEntity.Comments) post.Comments.Add(comment); - post.RatingId = ratingId; + // post.Rating.Id = ratingId; this._context.Entry(post).State = EntityState.Modified; - return await this.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(); } #endregion diff --git a/src/DevHive.Data/Repositories/RatingRepository.cs b/src/DevHive.Data/Repositories/RatingRepository.cs index 43fe90d..d676f27 100644 --- a/src/DevHive.Data/Repositories/RatingRepository.cs +++ b/src/DevHive.Data/Repositories/RatingRepository.cs @@ -9,29 +9,26 @@ namespace DevHive.Data.Repositories public class RatingRepository : BaseRepository, IRatingRepository { private readonly DevHiveContext _context; + private readonly IPostRepository _postRepository; - public RatingRepository(DevHiveContext context) + public RatingRepository(DevHiveContext context, IPostRepository postRepository) : base(context) { this._context = context; + this._postRepository = postRepository; } public async Task GetByPostId(Guid postId) { - return await this._context.Rating - .FirstOrDefaultAsync(x => x.Post.Id == postId); - } - - public async Task> GetRating(Guid postId) - { - Rating rating = await this.GetByPostId(postId); - - return new Tuple(rating.Likes, rating.Dislikes); + throw new NotImplementedException(); + // return await this._context.Rating + // .FirstOrDefaultAsync(x => x.Post.Id == postId); } - public async Task HasUserRatedThisPost(Guid userId, Guid postId) + public async Task GetRating(Guid postId) { throw new NotImplementedException(); + // return (await this.GetByPostId(postId)).Rate; } } } diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs index 2eeb382..441efef 100644 --- a/src/DevHive.Data/Repositories/RoleRepository.cs +++ b/src/DevHive.Data/Repositories/RoleRepository.cs @@ -33,7 +33,7 @@ namespace DevHive.Data.Repositories .CurrentValues .SetValues(newEntity); - return await this.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(); } #region Validations diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 6ff2ffa..4bf919e 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -95,7 +95,7 @@ namespace DevHive.Data.Repositories this._context.Entry(user).State = EntityState.Modified; - return await this.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(); } #endregion diff --git a/src/DevHive.Services/Configurations/Mapping/RatingMappings.cs b/src/DevHive.Services/Configurations/Mapping/RatingMappings.cs index 5da1b0d..1dbb7b4 100644 --- a/src/DevHive.Services/Configurations/Mapping/RatingMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RatingMappings.cs @@ -1,4 +1,6 @@ using AutoMapper; +using DevHive.Data.Models; +using DevHive.Services.Models.Post.Rating; namespace DevHive.Services.Configurations.Mapping { @@ -6,7 +8,8 @@ namespace DevHive.Services.Configurations.Mapping { public RatingMappings() { - + // CreateMap() + // .ForMember(dest => dest.PostId, src => src.MapFrom(p => p.Post.Id)); } } } diff --git a/src/DevHive.Services/Interfaces/IRateService.cs b/src/DevHive.Services/Interfaces/IRateService.cs new file mode 100644 index 0000000..359ef55 --- /dev/null +++ b/src/DevHive.Services/Interfaces/IRateService.cs @@ -0,0 +1,14 @@ +using System; +using System.Threading.Tasks; +using DevHive.Data.Models; +using DevHive.Services.Models.Post.Rating; + +namespace DevHive.Services.Interfaces +{ + public interface IRateService + { + Task RatePost(RatePostServiceModel ratePostServiceModel); + + bool HasUserRatedThisPost(User user, Post post); + } +} diff --git a/src/DevHive.Services/Models/Post/Rating/ReadPostRatingServiceModel.cs b/src/DevHive.Services/Models/Post/Rating/ReadPostRatingServiceModel.cs new file mode 100644 index 0000000..8c73aaf --- /dev/null +++ b/src/DevHive.Services/Models/Post/Rating/ReadPostRatingServiceModel.cs @@ -0,0 +1,15 @@ +using System; + +namespace DevHive.Services.Models.Post.Rating +{ + public class ReadPostRatingServiceModel + { + public Guid Id { get; set; } + + public Guid PostId { get; set; } + + public int Likes { get; set; } + + public int Dislikes { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Post/Rating/ReadRatingServiceModel.cs b/src/DevHive.Services/Models/Post/Rating/ReadRatingServiceModel.cs deleted file mode 100644 index b071e74..0000000 --- a/src/DevHive.Services/Models/Post/Rating/ReadRatingServiceModel.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Post.Rating -{ - public class ReadRatingServiceModel - { - public Guid PostId { get; set; } - - public int Likes { get; set; } - - public int Dislikes { get; set; } - } -} diff --git a/src/DevHive.Services/Services/RateService.cs b/src/DevHive.Services/Services/RateService.cs new file mode 100644 index 0000000..204c550 --- /dev/null +++ b/src/DevHive.Services/Services/RateService.cs @@ -0,0 +1,80 @@ +using System; +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.Post.Rating; + +namespace DevHive.Services.Services +{ + public class RateService : IRateService + { + private readonly IPostRepository _postRepository; + private readonly IUserRepository _userRepository; + private readonly IRatingRepository _ratingRepository; + private readonly IMapper _mapper; + + public RateService(IPostRepository postRepository, IRatingRepository ratingRepository, IUserRepository userRepository, IMapper mapper) + { + this._postRepository = postRepository; + this._ratingRepository = ratingRepository; + this._userRepository = userRepository; + this._mapper = mapper; + } + + public async Task RatePost(RatePostServiceModel ratePostServiceModel) + { + throw new NotImplementedException(); + // if (!await this._postRepository.DoesPostExist(ratePostServiceModel.PostId)) + // throw new ArgumentException("Post does not exist!"); + + // if (!await this._userRepository.DoesUserExistAsync(ratePostServiceModel.UserId)) + // throw new ArgumentException("User does not exist!"); + + // Post post = await this._postRepository.GetByIdAsync(ratePostServiceModel.PostId); + // User user = await this._userRepository.GetByIdAsync(ratePostServiceModel.UserId); + + // if (this.HasUserRatedThisPost(user, post)) + // throw new ArgumentException("You can't rate the same post more then one(duh, amigo)"); + + // this.Rate(user, post, ratePostServiceModel.Liked); + + // bool success = await this._ratingRepository.EditAsync(post.Rating.Id, post.Rating); + // if (!success) + // throw new InvalidOperationException("Unable to rate the post!"); + + // Rating newRating = await this._ratingRepository.GetByIdAsync(post.Rating.Id); + // return this._mapper.Map(newRating); + } + + public async Task RemoveUserRateFromPost(Guid userId, Guid postId) + { + throw new NotImplementedException(); + // Post post = await this._postRepository.GetByIdAsync(postId); + // User user = await this._userRepository.GetByIdAsync(userId); + + // if (!this.HasUserRatedThisPost(user, post)) + // throw new ArgumentException("You haven't rated this post, lmao!"); + } + + public bool HasUserRatedThisPost(User user, Post post) + { + throw new NotImplementedException(); + // return post.Rating.UsersThatRated + // .Any(x => x.Id == user.Id); + } + + private void Rate(User user, Post post, bool liked) + { + throw new NotImplementedException(); + // if (liked) + // post.Rating.Rate++; + // else + // post.Rating.Rate--; + + // post.Rating.UsersThatRated.Add(user); + } + } +} diff --git a/src/DevHive.Services/Services/RatingService.cs b/src/DevHive.Services/Services/RatingService.cs deleted file mode 100644 index 2c5a6b6..0000000 --- a/src/DevHive.Services/Services/RatingService.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Diagnostics; -using System.Threading.Tasks; -using AutoMapper; -using DevHive.Data.Interfaces.Repositories; -using DevHive.Data.Models; -using DevHive.Services.Models.Post.Rating; -using Microsoft.CodeAnalysis.CSharp.Syntax; - -namespace DevHive.Services.Services -{ - public class RatingService - { - private readonly IPostRepository _postRepository; - private readonly IRatingRepository _ratingRepository; - private readonly IMapper _mapper; - - public RatingService(IPostRepository postRepository, IRatingRepository ratingRepository, IMapper mapper) - { - this._postRepository = postRepository; - this._ratingRepository = ratingRepository; - this._mapper = mapper; - } - - public async Task RatePost(RatePostServiceModel ratePostServiceModel) - { - if (!await this._postRepository.DoesPostExist(ratePostServiceModel.PostId)) - throw new ArgumentNullException("Post does not exist!"); - - if (!await this._ratingRepository.HasUserRatedThisPost(ratePostServiceModel.UserId, ratePostServiceModel.PostId)) - throw new ArgumentException("You can't rate the same post more then one(duh, amigo)"); - - Post post = await this._postRepository.GetByIdAsync(ratePostServiceModel.PostId); - - Rating rating = post.Rating; - if (ratePostServiceModel.Liked) - rating.Likes++; - else - rating.Dislikes++; - - bool success = await this._ratingRepository.EditAsync(rating.Id, rating); - if (!success) - throw new InvalidOperationException("Unable to rate the post!"); - - Rating newRating = await this._ratingRepository.GetByIdAsync(rating.Id); - return this._mapper.Map(newRating); - } - - public async Task RemoveUserRateFromPost(Guid userId, Guid postId) - { - throw new NotImplementedException(); - } - } -} diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs index 5c0d378..88f21d4 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs @@ -32,6 +32,7 @@ namespace DevHive.Web.Configurations.Extensions cloudName: configuration.GetSection("Cloud").GetSection("cloudName").Value, apiKey: configuration.GetSection("Cloud").GetSection("apiKey").Value, apiSecret: configuration.GetSection("Cloud").GetSection("apiSecret").Value)); + services.AddTransient(); } } } diff --git a/src/DevHive.Web/Configurations/Mapping/RatingMappings.cs b/src/DevHive.Web/Configurations/Mapping/RatingMappings.cs new file mode 100644 index 0000000..4e071de --- /dev/null +++ b/src/DevHive.Web/Configurations/Mapping/RatingMappings.cs @@ -0,0 +1,16 @@ +using AutoMapper; +using DevHive.Services.Models.Post.Rating; +using DevHive.Web.Models.Post.Rating; + +namespace DevHive.Web.Configurations.Mapping +{ + public class RatingMappings : Profile + { + public RatingMappings() + { + CreateMap(); + + CreateMap(); + } + } +} diff --git a/src/DevHive.Web/Controllers/RateController.cs b/src/DevHive.Web/Controllers/RateController.cs new file mode 100644 index 0000000..68b859b --- /dev/null +++ b/src/DevHive.Web/Controllers/RateController.cs @@ -0,0 +1,40 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Post.Rating; +using DevHive.Web.Models.Post.Rating; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class RateController + { + private readonly IRateService _rateService; + private readonly IUserService _userService; + private readonly IMapper _mapper; + + public RateController(IRateService rateService, IUserService userService, IMapper mapper) + { + this._rateService = rateService; + this._userService = userService; + this._mapper = mapper; + } + + [HttpPost] + [Authorize(Roles = "Admin,User")] + public async Task RatePost(Guid userId, [FromBody] RatePostWebModel ratePostWebModel, [FromHeader] string authorization) + { + RatePostServiceModel ratePostServiceModel = this._mapper.Map(ratePostWebModel); + ratePostServiceModel.UserId = userId; + + ReadPostRatingServiceModel readPostRatingServiceModel = await this._rateService.RatePost(ratePostServiceModel); + ReadPostRatingWebModel readPostRatingWebModel = this._mapper.Map(readPostRatingServiceModel); + + return new OkObjectResult(readPostRatingWebModel); + } + } +} diff --git a/src/DevHive.Web/Models/Post/Rating/RatePostWebModel.cs b/src/DevHive.Web/Models/Post/Rating/RatePostWebModel.cs new file mode 100644 index 0000000..5f0e58f --- /dev/null +++ b/src/DevHive.Web/Models/Post/Rating/RatePostWebModel.cs @@ -0,0 +1,11 @@ +using System; + +namespace DevHive.Web.Models.Post.Rating +{ + public class RatePostWebModel + { + public Guid PostId { get; set; } + + public bool Liked { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Post/Rating/ReadPostRatingWebModel.cs b/src/DevHive.Web/Models/Post/Rating/ReadPostRatingWebModel.cs new file mode 100644 index 0000000..a551fb8 --- /dev/null +++ b/src/DevHive.Web/Models/Post/Rating/ReadPostRatingWebModel.cs @@ -0,0 +1,15 @@ +using System; + +namespace DevHive.Web.Models.Post.Rating +{ + public class ReadPostRatingWebModel + { + public Guid Id { get; set; } + + public Guid PostId { get; set; } + + public int Likes { get; set; } + + public int Dislikes { get; set; } + } +} -- cgit v1.2.3 From 01ad75fa5a871a0c9f8cd0c5291312286ae4d52d Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 3 Feb 2021 10:22:37 +0200 Subject: Implemented profile picture table functionality; added models and interfaces for profile picture; added ability for user layers to update the profile picture; added migrations; updated mappings --- src/DevHive.Data/DevHiveContext.cs | 5 + .../Interfaces/Models/IProfilePicture.cs | 13 + src/DevHive.Data/Interfaces/Models/IUser.cs | 2 +- .../Interfaces/Repositories/IUserRepository.cs | 1 + .../20210203071720_ProfilePicture.Designer.cs | 633 +++++++++++++++++++++ .../Migrations/20210203071720_ProfilePicture.cs | 52 ++ .../Migrations/DevHiveContextModelSnapshot.cs | 36 +- src/DevHive.Data/Models/ProfilePicture.cs | 14 + src/DevHive.Data/Models/User.cs | 2 +- src/DevHive.Data/Repositories/UserRepository.cs | 10 + .../Configurations/Mapping/UserMappings.cs | 6 +- src/DevHive.Services/Interfaces/IUserService.cs | 1 + .../Identity/User/ProfilePictureServiceModel.cs | 7 + .../User/UpdateProfilePictureServiceModel.cs | 12 + .../Models/Identity/User/UpdateUserServiceModel.cs | 2 + .../Models/Identity/User/UserServiceModel.cs | 2 + src/DevHive.Services/Services/UserService.cs | 29 +- .../Configurations/Mapping/UserMappings.cs | 3 + src/DevHive.Web/Controllers/UserController.cs | 17 + .../Models/Identity/User/ProfilePictureWebModel.cs | 7 + .../Identity/User/UpdateProfilePictureWebModel.cs | 9 + .../Models/Identity/User/UserWebModel.cs | 2 + 22 files changed, 857 insertions(+), 8 deletions(-) create mode 100644 src/DevHive.Data/Interfaces/Models/IProfilePicture.cs create mode 100644 src/DevHive.Data/Migrations/20210203071720_ProfilePicture.Designer.cs create mode 100644 src/DevHive.Data/Migrations/20210203071720_ProfilePicture.cs create mode 100644 src/DevHive.Data/Models/ProfilePicture.cs create mode 100644 src/DevHive.Services/Models/Identity/User/ProfilePictureServiceModel.cs create mode 100644 src/DevHive.Services/Models/Identity/User/UpdateProfilePictureServiceModel.cs create mode 100644 src/DevHive.Web/Models/Identity/User/ProfilePictureWebModel.cs create mode 100644 src/DevHive.Web/Models/Identity/User/UpdateProfilePictureWebModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Data/DevHiveContext.cs b/src/DevHive.Data/DevHiveContext.cs index 417de7f..b3ebd73 100644 --- a/src/DevHive.Data/DevHiveContext.cs +++ b/src/DevHive.Data/DevHiveContext.cs @@ -27,6 +27,11 @@ namespace DevHive.Data .HasIndex(x => x.UserName) .IsUnique(); + builder.Entity() + .HasOne(x => x.ProfilePicture) + .WithOne(x => x.User) + .HasForeignKey(x => x.UserId); + /* Roles */ builder.Entity() .HasMany(x => x.Roles) diff --git a/src/DevHive.Data/Interfaces/Models/IProfilePicture.cs b/src/DevHive.Data/Interfaces/Models/IProfilePicture.cs new file mode 100644 index 0000000..c3fcbea --- /dev/null +++ b/src/DevHive.Data/Interfaces/Models/IProfilePicture.cs @@ -0,0 +1,13 @@ +using System; +using DevHive.Data.Models; + +namespace DevHive.Data.Interfaces.Models +{ + public interface IProfilePicture : IModel + { + Guid UserId { get; set; } + User User { get; set; } + + string PictureURL { get; set; } + } +} diff --git a/src/DevHive.Data/Interfaces/Models/IUser.cs b/src/DevHive.Data/Interfaces/Models/IUser.cs index eb262fd..fcd741c 100644 --- a/src/DevHive.Data/Interfaces/Models/IUser.cs +++ b/src/DevHive.Data/Interfaces/Models/IUser.cs @@ -10,7 +10,7 @@ namespace DevHive.Data.Interfaces.Models string LastName { get; set; } - string ProfilePictureUrl { get; set; } + ProfilePicture ProfilePicture { get; set; } HashSet Languages { get; set; } diff --git a/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs index 4346e9c..5b6ab9e 100644 --- a/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs +++ b/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs @@ -11,6 +11,7 @@ namespace DevHive.Data.Interfaces.Repositories //Read Task GetByUsernameAsync(string username); IEnumerable QueryAll(); + Task UpdateProfilePicture(Guid userId, string pictureUrl); //Validations Task DoesEmailExistAsync(string email); diff --git a/src/DevHive.Data/Migrations/20210203071720_ProfilePicture.Designer.cs b/src/DevHive.Data/Migrations/20210203071720_ProfilePicture.Designer.cs new file mode 100644 index 0000000..b8dbd8e --- /dev/null +++ b/src/DevHive.Data/Migrations/20210203071720_ProfilePicture.Designer.cs @@ -0,0 +1,633 @@ +// +using System; +using System.Collections.Generic; +using DevHive.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace DevHive.Data.Migrations +{ + [DbContext(typeof(DevHiveContext))] + [Migration("20210203071720_ProfilePicture")] + partial class ProfilePicture + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseIdentityByDefaultColumns() + .HasAnnotation("Relational:MaxIdentifierLength", 63) + .HasAnnotation("ProductVersion", "5.0.1"); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatorId") + .HasColumnType("uuid"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.HasIndex("CreatorId"); + + b.HasIndex("PostId"); + + b.ToTable("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Language", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Languages"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatorId") + .HasColumnType("uuid"); + + b.Property>("FileUrls") + .HasColumnType("text[]"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.HasIndex("CreatorId"); + + b.ToTable("Posts"); + }); + + modelBuilder.Entity("DevHive.Data.Models.ProfilePicture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("PictureURL") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId") + .IsUnique(); + + b.ToTable("ProfilePicture"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Rating", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Rate") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("Rating"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Technology", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Technologies"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AccessFailedCount") + .HasColumnType("integer"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("EmailConfirmed") + .HasColumnType("boolean"); + + b.Property("FirstName") + .HasColumnType("text"); + + b.Property("LastName") + .HasColumnType("text"); + + b.Property("LockoutEnabled") + .HasColumnType("boolean"); + + b.Property("LockoutEnd") + .HasColumnType("timestamp with time zone"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("PasswordHash") + .HasColumnType("text"); + + b.Property("PhoneNumber") + .HasColumnType("text"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("boolean"); + + b.Property("SecurityStamp") + .HasColumnType("text"); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.HasIndex("UserName") + .IsUnique(); + + b.ToTable("AspNetUsers"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.RatedPost", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "PostId"); + + b.HasIndex("PostId"); + + b.ToTable("RatedPosts"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserFriend", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("FriendId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "FriendId"); + + b.HasIndex("FriendId"); + + b.ToTable("UserFriends"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserRate", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Rate") + .HasColumnType("boolean"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("UserRates"); + }); + + modelBuilder.Entity("LanguageUser", b => + { + b.Property("LanguagesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("LanguagesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("LanguageUser"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("ProviderKey") + .HasColumnType("text"); + + b.Property("ProviderDisplayName") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.Property("RolesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("RolesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("RoleUser"); + }); + + modelBuilder.Entity("TechnologyUser", b => + { + b.Property("TechnologiesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("TechnologiesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("TechnologyUser"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.HasOne("DevHive.Data.Models.User", "Creator") + .WithMany("Comments") + .HasForeignKey("CreatorId"); + + b.HasOne("DevHive.Data.Models.Post", "Post") + .WithMany("Comments") + .HasForeignKey("PostId"); + + b.Navigation("Creator"); + + b.Navigation("Post"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.HasOne("DevHive.Data.Models.User", "Creator") + .WithMany("Posts") + .HasForeignKey("CreatorId"); + + b.Navigation("Creator"); + }); + + modelBuilder.Entity("DevHive.Data.Models.ProfilePicture", b => + { + b.HasOne("DevHive.Data.Models.User", "User") + .WithOne("ProfilePicture") + .HasForeignKey("DevHive.Data.Models.ProfilePicture", "UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.RatedPost", b => + { + b.HasOne("DevHive.Data.Models.Post", "Post") + .WithMany() + .HasForeignKey("PostId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Post"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserFriend", b => + { + b.HasOne("DevHive.Data.Models.User", "Friend") + .WithMany("FriendsOf") + .HasForeignKey("FriendId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany("MyFriends") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Friend"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserRate", b => + { + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany() + .HasForeignKey("UserId"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("LanguageUser", b => + { + b.HasOne("DevHive.Data.Models.Language", null) + .WithMany() + .HasForeignKey("LanguagesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RolesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("TechnologyUser", b => + { + b.HasOne("DevHive.Data.Models.Technology", null) + .WithMany() + .HasForeignKey("TechnologiesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Navigation("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Navigation("Comments"); + + b.Navigation("FriendsOf"); + + b.Navigation("MyFriends"); + + b.Navigation("Posts"); + + b.Navigation("ProfilePicture"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/DevHive.Data/Migrations/20210203071720_ProfilePicture.cs b/src/DevHive.Data/Migrations/20210203071720_ProfilePicture.cs new file mode 100644 index 0000000..1b0c2c6 --- /dev/null +++ b/src/DevHive.Data/Migrations/20210203071720_ProfilePicture.cs @@ -0,0 +1,52 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace DevHive.Data.Migrations +{ + public partial class ProfilePicture : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "ProfilePictureUrl", + table: "AspNetUsers"); + + migrationBuilder.CreateTable( + name: "ProfilePicture", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + UserId = table.Column(type: "uuid", nullable: false), + PictureURL = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_ProfilePicture", x => x.Id); + table.ForeignKey( + name: "FK_ProfilePicture_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ProfilePicture_UserId", + table: "ProfilePicture", + column: "UserId", + unique: true); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ProfilePicture"); + + migrationBuilder.AddColumn( + name: "ProfilePictureUrl", + table: "AspNetUsers", + type: "text", + nullable: true); + } + } +} diff --git a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs index 96cabad..0450670 100644 --- a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs +++ b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs @@ -86,6 +86,26 @@ namespace DevHive.Data.Migrations b.ToTable("Posts"); }); + modelBuilder.Entity("DevHive.Data.Models.ProfilePicture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("PictureURL") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId") + .IsUnique(); + + b.ToTable("ProfilePicture"); + }); + modelBuilder.Entity("DevHive.Data.Models.Rating", b => { b.Property("Id") @@ -190,9 +210,6 @@ namespace DevHive.Data.Migrations b.Property("PhoneNumberConfirmed") .HasColumnType("boolean"); - b.Property("ProfilePictureUrl") - .HasColumnType("text"); - b.Property("SecurityStamp") .HasColumnType("text"); @@ -437,6 +454,17 @@ namespace DevHive.Data.Migrations b.Navigation("Creator"); }); + modelBuilder.Entity("DevHive.Data.Models.ProfilePicture", b => + { + b.HasOne("DevHive.Data.Models.User", "User") + .WithOne("ProfilePicture") + .HasForeignKey("DevHive.Data.Models.ProfilePicture", "UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + modelBuilder.Entity("DevHive.Data.RelationModels.RatedPost", b => { b.HasOne("DevHive.Data.Models.Post", "Post") @@ -594,6 +622,8 @@ namespace DevHive.Data.Migrations b.Navigation("MyFriends"); b.Navigation("Posts"); + + b.Navigation("ProfilePicture"); }); #pragma warning restore 612, 618 } diff --git a/src/DevHive.Data/Models/ProfilePicture.cs b/src/DevHive.Data/Models/ProfilePicture.cs new file mode 100644 index 0000000..e2ef04b --- /dev/null +++ b/src/DevHive.Data/Models/ProfilePicture.cs @@ -0,0 +1,14 @@ +using System; + +namespace DevHive.Data.Models +{ + public class ProfilePicture + { + public Guid Id { get; set; } + + public Guid UserId { get; set; } + public User User { get; set; } + + public string PictureURL { get; set; } + } +} diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs index 1c365e4..d73f989 100644 --- a/src/DevHive.Data/Models/User.cs +++ b/src/DevHive.Data/Models/User.cs @@ -14,7 +14,7 @@ namespace DevHive.Data.Models public string LastName { get; set; } - public string ProfilePictureUrl { get; set; } + public ProfilePicture ProfilePicture { get; set; } public HashSet Languages { get; set; } = new(); diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index b46198c..466b123 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -38,6 +38,7 @@ namespace DevHive.Data.Repositories .Include(x => x.Languages) .Include(x => x.Technologies) .Include(x => x.Posts) + .Include(x => x.ProfilePicture) .FirstOrDefaultAsync(x => x.Id == id); } @@ -48,6 +49,7 @@ namespace DevHive.Data.Repositories .Include(x => x.Languages) .Include(x => x.Technologies) .Include(x => x.Posts) + .Include(x => x.ProfilePicture) .FirstOrDefaultAsync(x => x.UserName == username); } #endregion @@ -93,6 +95,14 @@ namespace DevHive.Data.Repositories return await this.SaveChangesAsync(); } + + public async Task UpdateProfilePicture(Guid userId, string pictureUrl) { + User user = await this.GetByIdAsync(userId); + + user.ProfilePicture.PictureURL = pictureUrl; + + return await this.SaveChangesAsync(); + } #endregion #region Validations diff --git a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs index 5223d84..6922cd7 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs @@ -22,10 +22,12 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap() - .ForMember(dest => dest.Friends, src => src.MapFrom(p => p.MyFriends)); + .ForMember(dest => dest.Friends, src => src.MapFrom(p => p.MyFriends)) + .ForMember(dest => dest.ProfilePictureURL, src => src.MapFrom(p => p.ProfilePicture.PictureURL)); // .ForMember(dest => dest.Friends, src => src.MapFrom(p => p.Friends)); CreateMap() - .ForMember(x => x.Password, opt => opt.Ignore()); + .ForMember(x => x.Password, opt => opt.Ignore()) + .ForMember(dest => dest.ProfilePictureURL, src => src.MapFrom(p => p.ProfilePicture.PictureURL)); CreateMap(); CreateMap() diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs index b701e4a..9e2b4e3 100644 --- a/src/DevHive.Services/Interfaces/IUserService.cs +++ b/src/DevHive.Services/Interfaces/IUserService.cs @@ -14,6 +14,7 @@ namespace DevHive.Services.Interfaces Task GetUserById(Guid id); Task UpdateUser(UpdateUserServiceModel updateModel); + Task UpdateProfilePicture(UpdateProfilePictureServiceModel updateProfilePictureServiceModel); Task DeleteUser(Guid id); diff --git a/src/DevHive.Services/Models/Identity/User/ProfilePictureServiceModel.cs b/src/DevHive.Services/Models/Identity/User/ProfilePictureServiceModel.cs new file mode 100644 index 0000000..ad81057 --- /dev/null +++ b/src/DevHive.Services/Models/Identity/User/ProfilePictureServiceModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Services.Models.Identity.User +{ + public class ProfilePictureServiceModel + { + public string ProfilePictureURL { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/User/UpdateProfilePictureServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateProfilePictureServiceModel.cs new file mode 100644 index 0000000..8563953 --- /dev/null +++ b/src/DevHive.Services/Models/Identity/User/UpdateProfilePictureServiceModel.cs @@ -0,0 +1,12 @@ +using System; +using Microsoft.AspNetCore.Http; + +namespace DevHive.Services.Models.Identity.User +{ + public class UpdateProfilePictureServiceModel + { + public Guid UserId { get; set; } + + public IFormFile Picture { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs index 5b197e4..b4e4400 100644 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs @@ -12,6 +12,8 @@ namespace DevHive.Services.Models.Identity.User public string Password { get; set; } + public string ProfilePictureURL { get; set; } + public HashSet Roles { get; set; } = new HashSet(); public HashSet Friends { get; set; } = new HashSet(); diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index 5bf58ec..ac7bba2 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -8,6 +8,8 @@ namespace DevHive.Services.Models.Identity.User { public class UserServiceModel : BaseUserServiceModel { + public string ProfilePictureURL { get; set; } + public HashSet Roles { get; set; } = new(); public HashSet Friends { get; set; } = new(); diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 09b56c1..d8c4ee5 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -15,6 +15,7 @@ using DevHive.Data.Interfaces.Repositories; using System.Linq; using DevHive.Common.Models.Misc; using DevHive.Data.RelationModels; +using Microsoft.AspNetCore.Http; namespace DevHive.Services.Services { @@ -26,13 +27,15 @@ namespace DevHive.Services.Services private readonly ITechnologyRepository _technologyRepository; private readonly IMapper _userMapper; private readonly JWTOptions _jwtOptions; + private readonly ICloudService _cloudService; public UserService(IUserRepository userRepository, ILanguageRepository languageRepository, IRoleRepository roleRepository, ITechnologyRepository technologyRepository, IMapper mapper, - JWTOptions jwtOptions) + JWTOptions jwtOptions, + ICloudService cloudService) { this._userRepository = userRepository; this._roleRepository = roleRepository; @@ -40,6 +43,7 @@ namespace DevHive.Services.Services this._jwtOptions = jwtOptions; this._languageRepository = languageRepository; this._technologyRepository = technologyRepository; + this._cloudService = cloudService; } #region Authentication @@ -66,6 +70,7 @@ namespace DevHive.Services.Services User user = this._userMapper.Map(registerModel); user.PasswordHash = PasswordModifications.GeneratePasswordHash(registerModel.Password); + user.ProfilePicture = new ProfilePicture() { PictureURL = String.Empty }; // Make sure the default role exists //TODO: Move when project starts @@ -119,6 +124,28 @@ namespace DevHive.Services.Services return this._userMapper.Map( await this._userRepository.GetByIdAsync(user.Id)); } + + public async Task UpdateProfilePicture(UpdateProfilePictureServiceModel updateProfilePictureServiceModel) + { + User user = await this._userRepository.GetByIdAsync(updateProfilePictureServiceModel.UserId); + + if (!String.IsNullOrEmpty(user.ProfilePicture.PictureURL)) + { + bool success = await _cloudService.RemoveFilesFromCloud(new List { user.ProfilePicture.PictureURL }); + if (!success) + throw new InvalidCastException("Could not delete old profile picture!"); + } + + string fileUrl = (await this._cloudService.UploadFilesToCloud(new List { updateProfilePictureServiceModel.Picture }))[0] ?? + throw new ArgumentNullException("Unable to upload profile picture to cloud"); + + bool successful = await this._userRepository.UpdateProfilePicture(updateProfilePictureServiceModel.UserId, fileUrl); + + if (!successful) + throw new InvalidOperationException("Unable to change profile picture!"); + + return new ProfilePictureServiceModel() { ProfilePictureURL = fileUrl }; + } #endregion #region Delete diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index 1b26cc9..f58e7ca 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -23,6 +23,9 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); CreateMap(); } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index fdf317c..109bbaa 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -94,6 +94,23 @@ namespace DevHive.Web.Controllers return new AcceptedResult("UpdateUser", userWebModel); } + + [HttpPut] + [Route("ProfilePicture")] + [Authorize(Roles = "User,Admin")] + public async Task UpdateProfilePicture(Guid userId, [FromForm] UpdateProfilePictureWebModel updateProfilePictureWebModel, [FromHeader] string authorization) + { + if (!await this._userService.ValidJWT(userId, authorization)) + return new UnauthorizedResult(); + + UpdateProfilePictureServiceModel updateProfilePictureServiceModel = this._userMapper.Map(updateProfilePictureWebModel); + updateProfilePictureServiceModel.UserId = userId; + + ProfilePictureServiceModel profilePictureServiceModel = await this._userService.UpdateProfilePicture(updateProfilePictureServiceModel); + ProfilePictureWebModel profilePictureWebModel = this._userMapper.Map(profilePictureServiceModel); + + return new AcceptedResult("UpdateProfilePicture", profilePictureWebModel); + } #endregion #region Delete diff --git a/src/DevHive.Web/Models/Identity/User/ProfilePictureWebModel.cs b/src/DevHive.Web/Models/Identity/User/ProfilePictureWebModel.cs new file mode 100644 index 0000000..9fb1516 --- /dev/null +++ b/src/DevHive.Web/Models/Identity/User/ProfilePictureWebModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Web.Models.Identity.User +{ + public class ProfilePictureWebModel + { + public string ProfilePictureURL { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Identity/User/UpdateProfilePictureWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateProfilePictureWebModel.cs new file mode 100644 index 0000000..6efe968 --- /dev/null +++ b/src/DevHive.Web/Models/Identity/User/UpdateProfilePictureWebModel.cs @@ -0,0 +1,9 @@ +using Microsoft.AspNetCore.Http; + +namespace DevHive.Web.Models.Identity.User +{ + public class UpdateProfilePictureWebModel + { + public IFormFile Picture { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 1a5484a..7ab8cca 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -10,6 +10,8 @@ namespace DevHive.Web.Models.Identity.User { public class UserWebModel : BaseUserWebModel { + public string ProfilePictureURL { get; set; } + [NotNull] [Required] public HashSet Roles { get; set; } = new(); -- cgit v1.2.3 From 5ece5a07e8bb693e4f80f8ced977c4cfa8f770ce Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Thu, 4 Feb 2021 20:32:49 +0200 Subject: Adding FeedController tests --- .../DevHive.Web.Tests/FeedController.Tests.cs | 98 ++++++++++++++++++++++ .../TechnologyController.Tests.cs | 2 + .../Configurations/Mapping/FeedMappings.cs | 2 +- src/DevHive.Web/Controllers/FeedController.cs | 1 + src/DevHive.Web/Models/Feed/ReadPageWebModel.cs | 2 +- 5 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 src/DevHive.Tests/DevHive.Web.Tests/FeedController.Tests.cs (limited to 'src/DevHive.Web/Configurations/Mapping') diff --git a/src/DevHive.Tests/DevHive.Web.Tests/FeedController.Tests.cs b/src/DevHive.Tests/DevHive.Web.Tests/FeedController.Tests.cs new file mode 100644 index 0000000..01f67e5 --- /dev/null +++ b/src/DevHive.Tests/DevHive.Web.Tests/FeedController.Tests.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Interfaces; +using DevHive.Services.Models; +using DevHive.Web.Controllers; +using DevHive.Web.Models.Comment; +using DevHive.Web.Models.Feed; +using DevHive.Web.Models.Post; +using Microsoft.AspNetCore.Mvc; +using Moq; +using NUnit.Framework; + +namespace DevHive.Web.Tests +{ + [TestFixture] + public class FeedControllerTests + { + private Mock FeedServiceMock { get; set; } + private Mock MapperMock { get; set; } + private FeedController FeedController { get; set; } + + #region SetUp + [SetUp] + public void SetUp() + { + this.FeedServiceMock = new Mock(); + this.MapperMock = new Mock(); + this.FeedController = new FeedController(this.FeedServiceMock.Object, this.MapperMock.Object); + } + #endregion + + #region GetPosts + [Test] + public async Task GetPosts_ReturnsOkObjectResultWithCorrectReadPageWebModel_WhenPostsExist() + { + GetPageWebModel getPageWebModel = new GetPageWebModel { }; + GetPageServiceModel getPageServiceModel = new GetPageServiceModel { }; + ReadPageServiceModel readPageServiceModel = new ReadPageServiceModel { }; + ReadPageWebModel readPageWebModel = new ReadPageWebModel + { + Posts = new List + { + new ReadPostWebModel(), + new ReadPostWebModel(), + new ReadPostWebModel() + } + }; + + this.FeedServiceMock.Setup(p => p.GetPage(It.IsAny())).Returns(Task.FromResult(readPageServiceModel)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(getPageServiceModel); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readPageWebModel); + + IActionResult result = await this.FeedController.GetPosts(Guid.Empty, getPageWebModel); + + Assert.IsInstanceOf(result); + + OkObjectResult okObjectResult = result as OkObjectResult; + ReadPageWebModel resultModel = okObjectResult.Value as Models.Comment.ReadPageWebModel; + + Assert.AreEqual(3, resultModel.Posts.Count); + } + #endregion + + #region GetUserPosts + [Test] + public async Task GetUserPosts_GetsPostsOfUser_WhenTheyExist() + { + GetPageWebModel getPageWebModel = new GetPageWebModel { }; + GetPageServiceModel getPageServiceModel = new GetPageServiceModel { }; + ReadPageServiceModel readPageServiceModel = new ReadPageServiceModel { }; + ReadPageWebModel readPageWebModel = new ReadPageWebModel + { + Posts = new List + { + new ReadPostWebModel(), + new ReadPostWebModel(), + new ReadPostWebModel() + } + }; + + this.FeedServiceMock.Setup(p => p.GetUserPage(It.IsAny())).Returns(Task.FromResult(readPageServiceModel)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(getPageServiceModel); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readPageWebModel); + + IActionResult result = await this.FeedController.GetUserPosts(null, getPageWebModel); + + Assert.IsInstanceOf(result); + + OkObjectResult okObjectResult = result as OkObjectResult; + ReadPageWebModel resultModel = okObjectResult.Value as Models.Comment.ReadPageWebModel; + + Assert.AreEqual(3, resultModel.Posts.Count); + } + #endregion + } +} diff --git a/src/DevHive.Tests/DevHive.Web.Tests/TechnologyController.Tests.cs b/src/DevHive.Tests/DevHive.Web.Tests/TechnologyController.Tests.cs index d56a602..164bcbf 100644 --- a/src/DevHive.Tests/DevHive.Web.Tests/TechnologyController.Tests.cs +++ b/src/DevHive.Tests/DevHive.Web.Tests/TechnologyController.Tests.cs @@ -21,6 +21,7 @@ namespace DevHive.Web.Tests private Mock MapperMock { get; set; } private TechnologyController TechnologyController { get; set; } + #region SetUp [SetUp] public void SetUp() { @@ -28,6 +29,7 @@ namespace DevHive.Web.Tests this.MapperMock = new Mock(); this.TechnologyController = new TechnologyController(this.TechnologyServiceMock.Object, this.MapperMock.Object); } + #endregion #region Create [Test] diff --git a/src/DevHive.Web/Configurations/Mapping/FeedMappings.cs b/src/DevHive.Web/Configurations/Mapping/FeedMappings.cs index 159582d..0909f6d 100644 --- a/src/DevHive.Web/Configurations/Mapping/FeedMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/FeedMappings.cs @@ -1,6 +1,6 @@ using AutoMapper; using DevHive.Services.Models; -using DevHive.Web.Controllers; +using DevHive.Web.Models.Comment; using DevHive.Web.Models.Feed; namespace DevHive.Web.Configurations.Mapping diff --git a/src/DevHive.Web/Controllers/FeedController.cs b/src/DevHive.Web/Controllers/FeedController.cs index 2f14cf3..abca3e4 100644 --- a/src/DevHive.Web/Controllers/FeedController.cs +++ b/src/DevHive.Web/Controllers/FeedController.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using AutoMapper; using DevHive.Services.Interfaces; using DevHive.Services.Models; +using DevHive.Web.Models.Comment; using DevHive.Web.Models.Feed; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; diff --git a/src/DevHive.Web/Models/Feed/ReadPageWebModel.cs b/src/DevHive.Web/Models/Feed/ReadPageWebModel.cs index 839aaa6..f429313 100644 --- a/src/DevHive.Web/Models/Feed/ReadPageWebModel.cs +++ b/src/DevHive.Web/Models/Feed/ReadPageWebModel.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using DevHive.Web.Models.Post; -namespace DevHive.Web.Controllers +namespace DevHive.Web.Models.Comment { public class ReadPageWebModel { -- cgit v1.2.3