From b9e8b4e7b8d8fc2c6b5d21370e4ec389e4d2c02a Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 10 Dec 2020 17:13:52 +0200 Subject: Trying to implement AutoMapper --- API/API.csproj | 2 ++ API/Controllers/UserController.cs | 5 +++-- API/Database/DbRepository.cs | 1 + API/Service/UserService.cs | 9 ++++++--- API/Startup.cs | 31 +++++++++++++++++++++++++++++-- API/appsettings.json | 2 +- 6 files changed, 42 insertions(+), 8 deletions(-) (limited to 'API') diff --git a/API/API.csproj b/API/API.csproj index f596657..663d7ed 100644 --- a/API/API.csproj +++ b/API/API.csproj @@ -12,6 +12,8 @@ + + diff --git a/API/Controllers/UserController.cs b/API/Controllers/UserController.cs index 22555c6..e8a58b8 100644 --- a/API/Controllers/UserController.cs +++ b/API/Controllers/UserController.cs @@ -3,6 +3,7 @@ using System.Net.Http; using System.Threading.Tasks; using API.Database; using API.Service; +using AutoMapper; using Microsoft.AspNetCore.Mvc; using Models.Classes; using Models.DTOs; @@ -15,9 +16,9 @@ namespace API.Controllers { private readonly UserService _service; - public UserController(DevHiveContext context) + public UserController(DevHiveContext context, IMapper mapper) { - this._service = new UserService(context); + this._service = new UserService(context, mapper); } //Create diff --git a/API/Database/DbRepository.cs b/API/Database/DbRepository.cs index ea1593a..acb91dc 100644 --- a/API/Database/DbRepository.cs +++ b/API/Database/DbRepository.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using Models.Interfaces.Database; using Microsoft.EntityFrameworkCore; +using Models.Classes; namespace API.Database { diff --git a/API/Service/UserService.cs b/API/Service/UserService.cs index 0bb53c0..06e2dbe 100644 --- a/API/Service/UserService.cs +++ b/API/Service/UserService.cs @@ -4,6 +4,7 @@ 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; @@ -14,18 +15,20 @@ namespace API.Service public class UserService { private readonly DbRepository _dbRepository; + private readonly IMapper _mapper; - public UserService(DevHiveContext context) + public UserService(DevHiveContext context, IMapper mapper) { this._dbRepository = new DbRepository(context); + this._mapper = mapper; } public async Task CreateUser(UserDTO userDTO) { //TODO: MAKE VALIDATIONS OF PROPER REQUEST - User user = Mapper.UserDtoToUser(userDTO); - await this._dbRepository.AddAsync(user); + //User user = this._mapper.Map(userDTO); + //await this._dbRepository.AddAsync(user); return HttpStatusCode.OK; } diff --git a/API/Startup.cs b/API/Startup.cs index 6167993..6d95c1a 100644 --- a/API/Startup.cs +++ b/API/Startup.cs @@ -1,4 +1,6 @@ +using System; using API.Database; +using AutoMapper; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Identity; @@ -8,6 +10,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; using Models.Classes; +using Models.DTOs; namespace API { @@ -25,7 +28,7 @@ namespace API { services.AddControllers(); - services.AddDbContext(options => + services.AddDbContext(options => options.UseNpgsql(Configuration.GetConnectionString("DEV"))) .AddAuthentication() .AddJwtBearer(); @@ -45,6 +48,8 @@ namespace API { c.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1" }); }); + + services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -71,6 +76,28 @@ namespace API { endpoints.MapControllers(); }); + + IMapper configuration = new MapperConfiguration(cfg => + { + //cfg.DestinationMemberNamingConvention = new ExactMatchNamingConvention(); + + cfg.CreateMap(); + cfg.CreateMap() + .ForMember(x => x.AccessFailedCount, o => o.Ignore()) + .ForMember(x => x.ConcurrencyStamp, o => o.Ignore()) + .ForMember(x => x.EmailConfirmed, o => o.Ignore()) + .ForMember(x => x.Id, o => o.Ignore()) + .ForMember(x => x.LockoutEnabled, o => o.Ignore()) + .ForMember(x => x.LockoutEnd, o => o.Ignore()) + .ForMember(x => x.NormalizedEmail, o => o.Ignore()) + .ForMember(x => x.NormalizedUserName, o => o.Ignore()) + .ForMember(x => x.PasswordHash, o => o.Ignore()) + .ForMember(x => x.PhoneNumber, o => o.Ignore()) + .ForMember(x => x.PhoneNumberConfirmed, o => o.Ignore()) + .ForMember(x => x.ProfilePicture, o => o.Ignore()) + .ForMember(x => x.SecurityStamp, o => o.Ignore()) + .ForMember(x => x.TwoFactorEnabled, o => o.Ignore()); + }).CreateMapper(); } - } +} } diff --git a/API/appsettings.json b/API/appsettings.json index b13eb27..31c8109 100644 --- a/API/appsettings.json +++ b/API/appsettings.json @@ -1,5 +1,5 @@ { - "ConnectionString" : { + "ConnectionStrings" : { "DEV": "Server=localhost;Port=5432;Database=API;User Id=postgres;Password=;" }, "Logging": { -- cgit v1.2.3