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 /API/Service | |
| parent | fcdecc38741dc8e3adb41897ee294aa6d1384128 (diff) | |
| download | DevHive-edefd4a53ad8bb5318e8c6fc347f04ef3ac2886c.tar DevHive-edefd4a53ad8bb5318e8c6fc347f04ef3ac2886c.tar.gz DevHive-edefd4a53ad8bb5318e8c6fc347f04ef3ac2886c.zip | |
Fixed Validations in UserService. Removed unnessessary code. Swagger not working
Diffstat (limited to 'API/Service')
| -rw-r--r-- | API/Service/UserService.cs | 58 |
1 files changed, 16 insertions, 42 deletions
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; } |
