From 08823de4749ec83e48fe314686452ce4a4b5eefe Mon Sep 17 00:00:00 2001 From: Carl Tibule Date: Thu, 26 Jan 2023 18:56:22 -0600 Subject: [PATCH] Added AutoMapper for User related objects --- YABA.API/Controllers/UsersController.cs | 11 +++++---- YABA.API/Program.cs | 14 +++++++++++ YABA.API/Settings/AutoMapperProfile.cs | 14 +++++++++++ YABA.Common/DTOs/UserDTO.cs | 12 ++++++++++ .../Configuration/AutoMapperProfile.cs | 14 +++++++++++ YABA.Service/DTO/UserDTO.cs | 23 ------------------- YABA.Service/Interfaces/IUserService.cs | 5 +--- YABA.Service/UserService.cs | 13 +++++++---- YABA.Service/YABA.Service.csproj | 3 ++- 9 files changed, 72 insertions(+), 37 deletions(-) create mode 100644 YABA.API/Settings/AutoMapperProfile.cs create mode 100644 YABA.Common/DTOs/UserDTO.cs create mode 100644 YABA.Service/Configuration/AutoMapperProfile.cs delete mode 100644 YABA.Service/DTO/UserDTO.cs diff --git a/YABA.API/Controllers/UsersController.cs b/YABA.API/Controllers/UsersController.cs index 7625d6a..d3d17bb 100644 --- a/YABA.API/Controllers/UsersController.cs +++ b/YABA.API/Controllers/UsersController.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Authorization; +using AutoMapper; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Net; @@ -12,14 +13,16 @@ namespace YABA.API.Controllers [Authorize, Route("api/v{version:apiVersion}/[controller]")] public class UsersController : ControllerBase { + private readonly IMapper _mapper; private readonly IUserService _userService; - public UsersController(IUserService userService) + public UsersController(IMapper mapper, IUserService userService) { + _mapper = mapper; _userService = userService; } - [HttpPost("Register")] + [HttpPost] [ProducesResponseType(typeof(UserResponse), (int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.NotFound)] [ProducesResponseType((int)HttpStatusCode.NoContent)] @@ -34,7 +37,7 @@ namespace YABA.API.Controllers if (isRegistered) return NoContent(); var registedUser = _userService.RegisterUser(authProviderId); - return Ok(new UserResponse(registedUser)); + return Ok(_mapper.Map(registedUser)); } } } diff --git a/YABA.API/Program.cs b/YABA.API/Program.cs index 6add65e..83d6b16 100644 --- a/YABA.API/Program.cs +++ b/YABA.API/Program.cs @@ -1,3 +1,4 @@ +using AutoMapper; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Versioning; @@ -5,6 +6,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; using System.Security.Claims; +using YABA.API.Middlewares; using YABA.API.Settings; using YABA.API.Settings.Swashbuckle; using YABA.Data.Configuration; @@ -46,6 +48,15 @@ builder.Services.AddServiceProjectDependencyInjectionConfiguration(configuration builder.Services.AddDataProjectDependencyInjectionConfiguration(configuration); builder.Services.AddControllers().AddNewtonsoftJson(); +// Add AutoMapper profiles +var mapperConfiguration = new MapperConfiguration(mapperConfiguration => +{ + mapperConfiguration.AddProfile(new YABA.API.Settings.AutoMapperProfile()); + mapperConfiguration.AddProfile(new YABA.Service.Configuration.AutoMapperProfile()); +}); + +IMapper mapper = mapperConfiguration.CreateMapper(); +builder.Services.AddSingleton(mapper); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); @@ -88,4 +99,7 @@ app.UseAuthorization(); app.MapControllers(); +// Add custom middlewares +app.UseMiddleware(); + app.Run(); diff --git a/YABA.API/Settings/AutoMapperProfile.cs b/YABA.API/Settings/AutoMapperProfile.cs new file mode 100644 index 0000000..53cc77a --- /dev/null +++ b/YABA.API/Settings/AutoMapperProfile.cs @@ -0,0 +1,14 @@ +using AutoMapper; +using YABA.API.ViewModels; +using YABA.Common.DTOs; + +namespace YABA.API.Settings +{ + public class AutoMapperProfile : Profile + { + public AutoMapperProfile() + { + CreateMap(); + } + } +} diff --git a/YABA.Common/DTOs/UserDTO.cs b/YABA.Common/DTOs/UserDTO.cs new file mode 100644 index 0000000..6918aa9 --- /dev/null +++ b/YABA.Common/DTOs/UserDTO.cs @@ -0,0 +1,12 @@ +using System; + +namespace YABA.Common.DTOs +{ + public class UserDTO + { + public int Id { get; set; } + public bool IsDeleted { get; set; } + public DateTimeOffset CreatedOn { get; set; } + public DateTimeOffset LastModified { get; set; } + } +} diff --git a/YABA.Service/Configuration/AutoMapperProfile.cs b/YABA.Service/Configuration/AutoMapperProfile.cs new file mode 100644 index 0000000..641e1e3 --- /dev/null +++ b/YABA.Service/Configuration/AutoMapperProfile.cs @@ -0,0 +1,14 @@ +using AutoMapper; +using YABA.Common.DTOs; +using YABA.Models; + +namespace YABA.Service.Configuration +{ + public class AutoMapperProfile : Profile + { + public AutoMapperProfile() + { + CreateMap(); + } + } +} diff --git a/YABA.Service/DTO/UserDTO.cs b/YABA.Service/DTO/UserDTO.cs deleted file mode 100644 index cfb3786..0000000 --- a/YABA.Service/DTO/UserDTO.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using YABA.Models; - -namespace YABA.Service.DTO -{ - public class UserDTO - { - public int Id { get; set; } - public bool IsDeleted { get; set; } - public DateTimeOffset CreatedOn { get; set; } - public DateTimeOffset LastModified { get; set; } - - public UserDTO(User value) - { - Id = value.Id; - IsDeleted = value.IsDeleted; - CreatedOn = value.CreatedOn; - LastModified = value.LastModified; - } - } -} diff --git a/YABA.Service/Interfaces/IUserService.cs b/YABA.Service/Interfaces/IUserService.cs index 56871a5..6764766 100644 --- a/YABA.Service/Interfaces/IUserService.cs +++ b/YABA.Service/Interfaces/IUserService.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; -using YABA.Service.DTO; +using YABA.Common.DTOs; namespace YABA.Service.Interfaces { diff --git a/YABA.Service/UserService.cs b/YABA.Service/UserService.cs index 337c07f..3269525 100644 --- a/YABA.Service/UserService.cs +++ b/YABA.Service/UserService.cs @@ -1,7 +1,8 @@ -using System.Linq; +using AutoMapper; +using System.Linq; +using YABA.Common.DTOs; using YABA.Data.Context; using YABA.Models; -using YABA.Service.DTO; using YABA.Service.Interfaces; namespace YABA.Service @@ -10,11 +11,13 @@ namespace YABA.Service { private readonly YABAReadOnlyContext _roContext; private readonly YABAReadWriteContext _context; + private readonly IMapper _mapper; - public UserService (YABAReadOnlyContext roContext, YABAReadWriteContext context) + public UserService (YABAReadOnlyContext roContext, YABAReadWriteContext context, IMapper mapper) { _roContext = roContext; _context = context; + _mapper = mapper; } public bool IsUserRegistered(string authProviderId) @@ -27,7 +30,7 @@ namespace YABA.Service if(IsUserRegistered(authProviderId)) { var user = _roContext.Users.FirstOrDefault(x => x.Auth0Id == authProviderId); - return new UserDTO(user); + return _mapper.Map(user); } var userToRegister = new User @@ -36,7 +39,7 @@ namespace YABA.Service }; var registedUser = _context.Users.Add(userToRegister); - return _context.SaveChanges() > 0 ? new UserDTO(registedUser.Entity) : null; + return _context.SaveChanges() > 0 ? _mapper.Map(registedUser.Entity) : null; } public int GetUserId(string authProviderId) diff --git a/YABA.Service/YABA.Service.csproj b/YABA.Service/YABA.Service.csproj index 7785b1b..3cf02c9 100644 --- a/YABA.Service/YABA.Service.csproj +++ b/YABA.Service/YABA.Service.csproj @@ -1,4 +1,4 @@ - + netstandard2.1 @@ -6,6 +6,7 @@ +