From edefd4a53ad8bb5318e8c6fc347f04ef3ac2886c Mon Sep 17 00:00:00 2001 From: transtrike Date: Fri, 11 Dec 2020 00:39:23 +0200 Subject: Fixed Validations in UserService. Removed unnessessary code. Swagger not working --- API/Service/UserService.cs | 58 +++++++++++++--------------------------------- 1 file changed, 16 insertions(+), 42 deletions(-) (limited to 'API/Service/UserService.cs') 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 _dbRepository; + private readonly UserDbRepository _userDbRepository; private readonly IMapper _userMapper; public UserService(DevHiveContext context, IMapper mapper) { - this._dbRepository = new DbRepository(context); + this._userDbRepository = new UserDbRepository(context); this._userMapper = mapper; } public async Task CreateUser(UserDTO userDTO) { - IEnumerable 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(userDTO); - await this._dbRepository.AddAsync(user); + await this._userDbRepository.AddAsync(user); return HttpStatusCode.OK; } public async Task 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 UpdateUser(int id, UserDTO userDTO) { - IEnumerable 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(userDTO); - await this._dbRepository.EditAsync(id, user); + await this._userDbRepository.EditAsync(id, user); + return HttpStatusCode.OK; } public async Task 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; } -- cgit v1.2.3