aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--API/API.csproj2
-rw-r--r--API/Controllers/UserController.cs5
-rw-r--r--API/Database/DbRepository.cs1
-rw-r--r--API/Service/UserService.cs9
-rw-r--r--API/Startup.cs31
-rw-r--r--API/appsettings.json2
-rw-r--r--Models/Classes/UserMapper.cs25
-rw-r--r--Models/DTOs/UserDTO.cs3
8 files changed, 44 insertions, 34 deletions
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 @@
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.1"/>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3"/>
+ <PackageReference Include="AutoMapper" Version="10.1.1"/>
+ <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.0"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Models\Models.csproj"/>
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<User> _dbRepository;
+ private readonly IMapper _mapper;
- public UserService(DevHiveContext context)
+ public UserService(DevHiveContext context, IMapper mapper)
{
this._dbRepository = new DbRepository<User>(context);
+ this._mapper = mapper;
}
public async Task<HttpStatusCode> 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, User>(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<DevHiveContext>(options =>
+ services.AddDbContext<DevHiveContext>(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<User, UserDTO>();
+ cfg.CreateMap<UserDTO, User>()
+ .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": {
diff --git a/Models/Classes/UserMapper.cs b/Models/Classes/UserMapper.cs
deleted file mode 100644
index 3969818..0000000
--- a/Models/Classes/UserMapper.cs
+++ /dev/null
@@ -1,25 +0,0 @@
-using Models.DTOs;
-using System.Reflection;
-
-namespace Models.Classes
-{
- public static class Mapper
- {
- /// <summary>
- /// Mapps UserDTO to a User
- /// </summary>
- /// <param name="userDTO">UserDTO that is going to be mapped</param>
- /// <returns>Mapped User</returns>
- public static User UserDtoToUser(UserDTO userDTO)
- {
- User user = new User();
- foreach(PropertyInfo property in user.GetType().GetProperties())
- {
- var neshto = property.GetValue(userDTO, null);
- property.SetValue(user, neshto, null);
- }
-
- return user;
- }
- }
-}
diff --git a/Models/DTOs/UserDTO.cs b/Models/DTOs/UserDTO.cs
index 24ddc5f..60e98c2 100644
--- a/Models/DTOs/UserDTO.cs
+++ b/Models/DTOs/UserDTO.cs
@@ -2,7 +2,8 @@ namespace Models.DTOs
{
public class UserDTO
{
- public string Username { get; set; }
+ public string UserName { get; set; }
+ public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}