From 0b01340e885841218f2cd15bdd63f47cf801524b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Wed, 19 May 2021 19:39:46 +0200 Subject: [PATCH] user mgmt done --- .../V2/HanyadikHetVanController.cs | 9 ++--- HanyadikHetVan/DTO/UserDTO.cs | 14 ++++++++ HanyadikHetVan/Profiles/UserProfile.cs | 14 ++++++++ .../Services/HanyadikHetVanEntityService.cs | 4 +-- HanyadikHetVan/Services/UserService.cs | 36 +++++++++++++++++++ HanyadikHetVan/Startup.cs | 1 - 6 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 HanyadikHetVan/DTO/UserDTO.cs create mode 100644 HanyadikHetVan/Profiles/UserProfile.cs create mode 100644 HanyadikHetVan/Services/UserService.cs diff --git a/HanyadikHetVan/Controllers/V2/HanyadikHetVanController.cs b/HanyadikHetVan/Controllers/V2/HanyadikHetVanController.cs index cd9be2c..7d0fa21 100644 --- a/HanyadikHetVan/Controllers/V2/HanyadikHetVanController.cs +++ b/HanyadikHetVan/Controllers/V2/HanyadikHetVanController.cs @@ -1,8 +1,6 @@ -using HanyadikHetVan.Data.Entities; -using HanyadikHetVan.Services; +using HanyadikHetVan.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using System; using System.Security.Claims; @@ -16,12 +14,10 @@ namespace HanyadikHetVan.Controllers.V2 public class HanyadikHetVanController : Controller { private readonly HanyadikHetVanEntityService _hanyadikHetVanEntityService; - private readonly UserManager _userManager; - public HanyadikHetVanController(HanyadikHetVanEntityService hanyadikHetVanEntityService, UserManager userManager) + public HanyadikHetVanController(HanyadikHetVanEntityService hanyadikHetVanEntityService) { _hanyadikHetVanEntityService = hanyadikHetVanEntityService ?? throw new ArgumentNullException(nameof(hanyadikHetVanEntityService)); - _userManager = userManager ?? throw new ArgumentNullException(nameof(userManager)); } [HttpGet("{weeklytimespanId}")] @@ -56,7 +52,6 @@ namespace HanyadikHetVan.Controllers.V2 } } - [HttpGet("my")] [Authorize] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(int))] diff --git a/HanyadikHetVan/DTO/UserDTO.cs b/HanyadikHetVan/DTO/UserDTO.cs new file mode 100644 index 0000000..711738e --- /dev/null +++ b/HanyadikHetVan/DTO/UserDTO.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace HanyadikHetVan.DTO +{ + public class UserDTO + { + public string Email { get; set; } + + public string Password { get; set; } + } +} diff --git a/HanyadikHetVan/Profiles/UserProfile.cs b/HanyadikHetVan/Profiles/UserProfile.cs new file mode 100644 index 0000000..029e828 --- /dev/null +++ b/HanyadikHetVan/Profiles/UserProfile.cs @@ -0,0 +1,14 @@ +using AutoMapper; +using HanyadikHetVan.Data.Entities; +using HanyadikHetVan.DTO; + +namespace HanyadikHetVan.Profiles +{ + public class UserProfile : Profile + { + public UserProfile() + { + CreateMap(); + } + } +} diff --git a/HanyadikHetVan/Services/HanyadikHetVanEntityService.cs b/HanyadikHetVan/Services/HanyadikHetVanEntityService.cs index 4317689..f673633 100644 --- a/HanyadikHetVan/Services/HanyadikHetVanEntityService.cs +++ b/HanyadikHetVan/Services/HanyadikHetVanEntityService.cs @@ -22,14 +22,14 @@ namespace HanyadikHetVan.Services public async Task GetDefaultWeek() { - var latest = await _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).LastAsync(); + var latest = await _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).OrderBy(x => x.Id).LastAsync(); var latestDto = _mapper.Map(latest); return latestDto.CurrentWeek; } public async Task GetUserDefaultWeek(string userId) { - var latest = await _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).Where(x => x.UserId == userId).LastAsync(); + var latest = await _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).Where(x => x.UserId == userId).OrderBy(x => x.Id).LastAsync(); var latestDto = _mapper.Map(latest); return latestDto.CurrentWeek; } diff --git a/HanyadikHetVan/Services/UserService.cs b/HanyadikHetVan/Services/UserService.cs new file mode 100644 index 0000000..d5eead4 --- /dev/null +++ b/HanyadikHetVan/Services/UserService.cs @@ -0,0 +1,36 @@ +using HanyadikHetVan.Data.Entities; +using HanyadikHetVan.DTO; +using Microsoft.AspNetCore.Identity; +using System; +using System.Threading.Tasks; + +namespace HanyadikHetVan.Services +{ + public class UserService + { + private readonly UserManager _userManager; + + public UserService(UserManager userManager) + { + _userManager = userManager ?? throw new ArgumentNullException(nameof(userManager)); + } + public async Task CreateUser(string email, string password) + { + var newUser = new User + { + UserName = email, + Email = email, + EmailConfirmed = true, + + }; + + var result = await _userManager.CreateAsync(newUser,password); + if (!result.Succeeded) + { + throw new Exception("Failed to create user."); + } + + return new UserDTO() { Email = email }; + } + } +} diff --git a/HanyadikHetVan/Startup.cs b/HanyadikHetVan/Startup.cs index 24dbdb1..b4b9b81 100644 --- a/HanyadikHetVan/Startup.cs +++ b/HanyadikHetVan/Startup.cs @@ -104,7 +104,6 @@ namespace HanyadikHetVan { c.SwaggerEndpoint("v1/swagger.json", "Original API"); c.SwaggerEndpoint("v2/swagger.json", "Homework API"); - c.OAuthClientId("admin"); }); } else