diff options
| author | Syndamia <kamen.d.mladenov@protonmail.com> | 2020-12-10 21:23:46 +0200 |
|---|---|---|
| committer | Syndamia <kamen.d.mladenov@protonmail.com> | 2020-12-10 21:23:46 +0200 |
| commit | fcdecc38741dc8e3adb41897ee294aa6d1384128 (patch) | |
| tree | 683eea600faa7ce9d265d04aad325c6e23968447 | |
| parent | 0bd15454f29e30438b28ca0e27887de6b765fb11 (diff) | |
| download | DevHive-fcdecc38741dc8e3adb41897ee294aa6d1384128.tar DevHive-fcdecc38741dc8e3adb41897ee294aa6d1384128.tar.gz DevHive-fcdecc38741dc8e3adb41897ee294aa6d1384128.zip | |
Added data validations of UserService requests
| -rw-r--r-- | API/API.csproj | 23 | ||||
| -rw-r--r-- | API/Database/DbRepository.cs | 10 | ||||
| -rw-r--r-- | API/Service/UserService.cs | 45 |
3 files changed, 59 insertions, 19 deletions
diff --git a/API/API.csproj b/API/API.csproj index 663d7ed..3232fb9 100644 --- a/API/API.csproj +++ b/API/API.csproj @@ -4,18 +4,19 @@ <RootNamespace>API</RootNamespace> </PropertyGroup> <ItemGroup> - <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.0" NoWarn="NU1605"/> - <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.0" NoWarn="NU1605"/> - <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3"/> - <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.1"/> - <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.0"/> - <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.0"/> - <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.1"/> - <PackageReference Include="Newtonsoft.Json" Version="12.0.3"/> - <PackageReference Include="AutoMapper" Version="10.1.1"/> - <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.0"/> + <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.0" NoWarn="NU1605" /> + <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.0" NoWarn="NU1605" /> + <PackageReference Include="Microsoft.AspNetCore.Mvc.WebApiCompatShim" Version="2.2.0" /> + <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" /> + <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.1" /> + <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.0" /> + <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.0" /> + <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.1" /> + <PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> + <PackageReference Include="AutoMapper" Version="10.1.1" /> + <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.0" /> </ItemGroup> <ItemGroup> - <ProjectReference Include="..\Models\Models.csproj"/> + <ProjectReference Include="..\Models\Models.csproj" /> </ItemGroup> </Project>
\ No newline at end of file diff --git a/API/Database/DbRepository.cs b/API/Database/DbRepository.cs index acb91dc..184adb4 100644 --- a/API/Database/DbRepository.cs +++ b/API/Database/DbRepository.cs @@ -34,15 +34,19 @@ namespace API.Database .FindAsync(id); } - public IEnumerable<TEntity> Query(int count) + public IEnumerable<TEntity> Query() { return this._context .Set<TEntity>() .AsNoTracking() - .Take(count) .AsEnumerable(); } + public IEnumerable<TEntity> Query(int count) + { + return this.Query().Take(count); + } + //Update public async Task EditAsync(object id, TEntity newEntity) { @@ -66,4 +70,4 @@ namespace API.Database await this._context.SaveChangesAsync(); } } -}
\ No newline at end of file +} diff --git a/API/Service/UserService.cs b/API/Service/UserService.cs index c673fac..b86687d 100644 --- a/API/Service/UserService.cs +++ b/API/Service/UserService.cs @@ -9,6 +9,9 @@ using Microsoft.AspNetCore.Mvc; using Models.Classes; using Models.DTOs; using Newtonsoft.Json; +using System; +using System.Web.Http; +using System.Collections.Generic; namespace API.Service { @@ -25,7 +28,13 @@ namespace API.Service public async Task<HttpStatusCode> CreateUser(UserDTO userDTO) { - //TODO: MAKE VALIDATIONS OF PROPER REQUEST + IEnumerable<User> allUsers = this._dbRepository.Query(); + + foreach (var currUser in allUsers) + { + if (currUser.UserName == userDTO.UserName) + return HttpStatusCode.Forbidden; + } User user = this._userMapper.Map<User>(userDTO); await this._dbRepository.AddAsync(user); @@ -35,22 +44,48 @@ namespace API.Service public async Task<string> GetUserById(int id) { - User user = await this._dbRepository.FindByIdAsync(id); + User user = await this._dbRepository.FindByIdAsync(id) ?? + throw new HttpResponseException(HttpStatusCode.NotFound); + return JsonConvert.SerializeObject(user); } public async Task<HttpStatusCode> UpdateUser(int id, UserDTO userDTO) { + IEnumerable<User> allUsers = this._dbRepository.Query(); + + bool userExists = false; + foreach (var currUser in allUsers) + { + if (currUser.Id == userDTO.Id) + { + userExists = true; + continue; + } + + if (currUser.UserName == userDTO.UserName) + return HttpStatusCode.Forbidden; + } + + if (!userExists) + return HttpStatusCode.NotFound; + User user = this._userMapper.Map<User>(userDTO); await this._dbRepository.EditAsync(id, user); - return HttpStatusCode.OK; } public async Task<HttpStatusCode> DeleteUser(int id) { - await this._dbRepository.DeleteAsync(id); - + try // This skips having to query the database and check if the user doesn't exist + { + await this._dbRepository.DeleteAsync(id); + } + catch (ArgumentNullException) + { + return HttpStatusCode.NotFound; + } + return HttpStatusCode.OK; } } |
