Added AutoMapper for User related objects
This commit is contained in:
14
YABA.Service/Configuration/AutoMapperProfile.cs
Normal file
14
YABA.Service/Configuration/AutoMapperProfile.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using AutoMapper;
|
||||
using YABA.Common.DTOs;
|
||||
using YABA.Models;
|
||||
|
||||
namespace YABA.Service.Configuration
|
||||
{
|
||||
public class AutoMapperProfile : Profile
|
||||
{
|
||||
public AutoMapperProfile()
|
||||
{
|
||||
CreateMap<User, UserDTO>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
{
|
||||
|
||||
@ -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<UserDTO>(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<UserDTO>(registedUser.Entity) : null;
|
||||
}
|
||||
|
||||
public int GetUserId(string authProviderId)
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
@ -6,6 +6,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="12.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user