diff options
| author | transtrike <transtrike@gmail.com> | 2020-12-11 00:39:23 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2020-12-11 00:39:23 +0200 |
| commit | edefd4a53ad8bb5318e8c6fc347f04ef3ac2886c (patch) | |
| tree | a49dcb7a139aa52bc4948243916399e28ff1c601 | |
| parent | fcdecc38741dc8e3adb41897ee294aa6d1384128 (diff) | |
| download | DevHive-edefd4a53ad8bb5318e8c6fc347f04ef3ac2886c.tar DevHive-edefd4a53ad8bb5318e8c6fc347f04ef3ac2886c.tar.gz DevHive-edefd4a53ad8bb5318e8c6fc347f04ef3ac2886c.zip | |
Fixed Validations in UserService. Removed unnessessary code. Swagger not working
| -rw-r--r-- | API/Database/DbRepository.cs | 12 | ||||
| -rw-r--r-- | API/Database/UserDbRepository.cs | 32 | ||||
| -rw-r--r-- | API/Service/UserService.cs | 58 | ||||
| -rw-r--r-- | API/Startup.cs | 1 |
4 files changed, 53 insertions, 50 deletions
diff --git a/API/Database/DbRepository.cs b/API/Database/DbRepository.cs index 184adb4..a24be36 100644 --- a/API/Database/DbRepository.cs +++ b/API/Database/DbRepository.cs @@ -11,6 +11,7 @@ namespace API.Database where TEntity : class { private readonly DbContext _context; + public DbRepository(DbContext context) { _context = context; @@ -34,23 +35,18 @@ namespace API.Database .FindAsync(id); } - public IEnumerable<TEntity> Query() + public IEnumerable<TEntity> Query(int count) { 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) { - //Set the Id property to the given id TEntity entity = await FindByIdAsync(id); this._context.Entry(entity) @@ -69,5 +65,7 @@ namespace API.Database await this._context.SaveChangesAsync(); } + + public DbSet<TEntity> DbSet => this._context.Set<TEntity>(); } } diff --git a/API/Database/UserDbRepository.cs b/API/Database/UserDbRepository.cs new file mode 100644 index 0000000..a4a3ac0 --- /dev/null +++ b/API/Database/UserDbRepository.cs @@ -0,0 +1,32 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Models.Interfaces.Database; +using Microsoft.EntityFrameworkCore; +using Models.Classes; + +namespace API.Database +{ + public class UserDbRepository : DbRepository<User> + { + private readonly DbRepository<User> _dbRepository; + + public UserDbRepository(DbContext context) + : base (context) + { + this._dbRepository = new DbRepository<User>(context); + } + + public bool DoesUsernameExist(string username) + { + return this._dbRepository.DbSet + .Any(x => x.UserName == username); + } + + public bool DoesUserExist(int id) + { + return this._dbRepository.DbSet + .Any(x => x.Id == id); + } + } +}
\ No newline at end of file diff --git a/API/Service/UserService.cs b/API/Service/UserService.cs index b86687d..632e96d 100644 --- a/API/Service/UserService.cs +++ b/API/Service/UserService.cs @@ -1,50 +1,39 @@ using System.Net; -using System.Net.Http; -using System.Net.Http.Json; -using System.Text.Json.Serialization; using System.Threading.Tasks; using API.Database; using AutoMapper; -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 { public class UserService { - private readonly DbRepository<User> _dbRepository; + private readonly UserDbRepository _userDbRepository; private readonly IMapper _userMapper; public UserService(DevHiveContext context, IMapper mapper) { - this._dbRepository = new DbRepository<User>(context); + this._userDbRepository = new UserDbRepository(context); this._userMapper = mapper; } public async Task<HttpStatusCode> CreateUser(UserDTO userDTO) { - IEnumerable<User> allUsers = this._dbRepository.Query(); - - foreach (var currUser in allUsers) - { - if (currUser.UserName == userDTO.UserName) - return HttpStatusCode.Forbidden; - } + if(this._userDbRepository.DoesUsernameExist(userDTO.UserName)) + return HttpStatusCode.Forbidden; User user = this._userMapper.Map<User>(userDTO); - await this._dbRepository.AddAsync(user); + await this._userDbRepository.AddAsync(user); return HttpStatusCode.OK; } public async Task<string> GetUserById(int id) { - User user = await this._dbRepository.FindByIdAsync(id) ?? + User user = await this._userDbRepository.FindByIdAsync(id) ?? throw new HttpResponseException(HttpStatusCode.NotFound); return JsonConvert.SerializeObject(user); @@ -52,39 +41,24 @@ namespace API.Service 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) + if (!this._userDbRepository.DoesUserExist(id)) return HttpStatusCode.NotFound; + if(this._userDbRepository.DoesUsernameExist(userDTO.UserName)) + return HttpStatusCode.Forbidden; + User user = this._userMapper.Map<User>(userDTO); - await this._dbRepository.EditAsync(id, user); + await this._userDbRepository.EditAsync(id, user); + return HttpStatusCode.OK; } public async Task<HttpStatusCode> DeleteUser(int 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; - } + if (!this._userDbRepository.DoesUserExist(id)) + return HttpStatusCode.Forbidden; + + await this._userDbRepository.DeleteAsync(id); return HttpStatusCode.OK; } diff --git a/API/Startup.cs b/API/Startup.cs index 399feca..796f13a 100644 --- a/API/Startup.cs +++ b/API/Startup.cs @@ -10,7 +10,6 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Models.Classes;
-using Models.DTOs;
namespace API
{
|
