diff options
| author | transtrike <transtrike@gmail.com> | 2020-12-11 18:41:14 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2020-12-11 18:41:14 +0200 |
| commit | 29b2a82d7ef2613b3e56eba7ed959243a98ae92d (patch) | |
| tree | 7da289cfd800d17024d5c0370582594ef4dcb0ea | |
| parent | 6ca4c43b3192a70347ba98bd88c5aab71327ecc9 (diff) | |
| download | DevHive-29b2a82d7ef2613b3e56eba7ed959243a98ae92d.tar DevHive-29b2a82d7ef2613b3e56eba7ed959243a98ae92d.tar.gz DevHive-29b2a82d7ef2613b3e56eba7ed959243a98ae92d.zip | |
Changed returns to IActionResult; Id from UserDTO removed
| -rw-r--r-- | API/Controllers/UserController.cs | 3 | ||||
| -rw-r--r-- | API/Database/DbRepository.cs | 2 | ||||
| -rw-r--r-- | API/Database/UserDbRepository.cs | 7 | ||||
| -rw-r--r-- | API/Service/UserService.cs | 16 | ||||
| -rw-r--r-- | Data/Models/DTOs/UserDTO.cs | 1 |
5 files changed, 14 insertions, 15 deletions
diff --git a/API/Controllers/UserController.cs b/API/Controllers/UserController.cs index 7e75e3e..fdb1c44 100644 --- a/API/Controllers/UserController.cs +++ b/API/Controllers/UserController.cs @@ -1,11 +1,8 @@ -using System.Net; -using System.Net.Http; using System.Threading.Tasks; using API.Database; using API.Service; using AutoMapper; using Microsoft.AspNetCore.Mvc; -using Data.Models.Classes; using Data.Models.DTOs; namespace API.Controllers diff --git a/API/Database/DbRepository.cs b/API/Database/DbRepository.cs index 786062c..aae5791 100644 --- a/API/Database/DbRepository.cs +++ b/API/Database/DbRepository.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using Data.Models.Interfaces.Database; using Microsoft.EntityFrameworkCore; using Data.Models.Classes; +using System.Diagnostics; namespace API.Database { @@ -48,6 +49,7 @@ namespace API.Database public async Task EditAsync(object id, TEntity newEntity) { TEntity entity = await FindByIdAsync(id); + typeof(TEntity).GetProperty("Id").SetValue(newEntity, id); this._context.Entry(entity) .CurrentValues diff --git a/API/Database/UserDbRepository.cs b/API/Database/UserDbRepository.cs index 2aa1b55..b8bf8e4 100644 --- a/API/Database/UserDbRepository.cs +++ b/API/Database/UserDbRepository.cs @@ -28,5 +28,12 @@ namespace API.Database return this._dbRepository.DbSet .Any(x => x.Id == id); } + + public bool HasThisUsername(int id, string username) + { + return this._dbRepository.DbSet + .Any(x => x.Id == id && + x.UserName == username); + } } } diff --git a/API/Service/UserService.cs b/API/Service/UserService.cs index 880b9c6..3c3b390 100644 --- a/API/Service/UserService.cs +++ b/API/Service/UserService.cs @@ -1,15 +1,8 @@ -using System.Net; using System.Threading.Tasks; using API.Database; using AutoMapper; using Data.Models.Classes; using Data.Models.DTOs; -using Newtonsoft.Json; -using System.Web.Http; -using System.Net.Http; -using Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionTranslators.Internal; -using System; -using Microsoft.AspNetCore.Mvc.Routing; using Microsoft.AspNetCore.Mvc; namespace API.Service @@ -33,7 +26,7 @@ namespace API.Service User user = this._userMapper.Map<User>(userDTO); await this._userDbRepository.AddAsync(user); - return new OkObjectResult("User created."); + return new CreatedResult("CreateUser", user); } public async Task<IActionResult> GetUserById(int id) @@ -51,13 +44,14 @@ namespace API.Service if (!this._userDbRepository.DoesUserExist(id)) return new NotFoundObjectResult("User does not exist!"); - if (this._userDbRepository.DoesUsernameExist(userDTO.UserName)) + if (!this._userDbRepository.HasThisUsername(id, userDTO.UserName) + && this._userDbRepository.DoesUsernameExist(userDTO.UserName)) return new BadRequestObjectResult("Username already exists!"); User user = this._userMapper.Map<User>(userDTO); await this._userDbRepository.EditAsync(id, user); - return new OkObjectResult("User updated."); + return new AcceptedResult("UpdateUser", user); } public async Task<IActionResult> DeleteUser(int id) @@ -67,7 +61,7 @@ namespace API.Service await this._userDbRepository.DeleteAsync(id); - return new OkObjectResult("User deleted successfully."); + return new OkResult(); } } } diff --git a/Data/Models/DTOs/UserDTO.cs b/Data/Models/DTOs/UserDTO.cs index d0b8642..d6d3d15 100644 --- a/Data/Models/DTOs/UserDTO.cs +++ b/Data/Models/DTOs/UserDTO.cs @@ -2,7 +2,6 @@ namespace Data.Models.DTOs { public class UserDTO { - public int Id { get; set; } public string UserName { get; set; } public string Email { get; set; } public string FirstName { get; set; } |
