aboutsummaryrefslogtreecommitdiff
path: root/API
diff options
context:
space:
mode:
authortranstrike <transtrike@gmail.com>2020-12-11 18:41:14 +0200
committertranstrike <transtrike@gmail.com>2020-12-11 18:41:14 +0200
commit29b2a82d7ef2613b3e56eba7ed959243a98ae92d (patch)
tree7da289cfd800d17024d5c0370582594ef4dcb0ea /API
parent6ca4c43b3192a70347ba98bd88c5aab71327ecc9 (diff)
downloadDevHive-29b2a82d7ef2613b3e56eba7ed959243a98ae92d.tar
DevHive-29b2a82d7ef2613b3e56eba7ed959243a98ae92d.tar.gz
DevHive-29b2a82d7ef2613b3e56eba7ed959243a98ae92d.zip
Changed returns to IActionResult; Id from UserDTO removed
Diffstat (limited to 'API')
-rw-r--r--API/Controllers/UserController.cs3
-rw-r--r--API/Database/DbRepository.cs2
-rw-r--r--API/Database/UserDbRepository.cs7
-rw-r--r--API/Service/UserService.cs16
4 files changed, 14 insertions, 14 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();
}
}
}