Added endpoint for registering a user locally after successful logon from Auth0
This commit is contained in:
@ -1,9 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace YABA.Service
|
||||
{
|
||||
public class Class1
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using YABA.Service.Interfaces;
|
||||
|
||||
namespace YABA.Service.Configuration
|
||||
{
|
||||
@ -7,7 +8,7 @@ namespace YABA.Service.Configuration
|
||||
{
|
||||
public static void AddServiceProjectDependencyInjectionConfiguration(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
|
||||
services.AddScoped<IUserService, UserService>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
23
YABA.Service/DTO/UserDTO.cs
Normal file
23
YABA.Service/DTO/UserDTO.cs
Normal file
@ -0,0 +1,23 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
13
YABA.Service/Interfaces/IUserService.cs
Normal file
13
YABA.Service/Interfaces/IUserService.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using YABA.Service.DTO;
|
||||
|
||||
namespace YABA.Service.Interfaces
|
||||
{
|
||||
public interface IUserService
|
||||
{
|
||||
public bool IsUserRegistered(string authProviderId);
|
||||
public UserDTO RegisterUser(string authProviderId);
|
||||
}
|
||||
}
|
||||
42
YABA.Service/UserService.cs
Normal file
42
YABA.Service/UserService.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using System.Linq;
|
||||
using YABA.Data.Context;
|
||||
using YABA.Models;
|
||||
using YABA.Service.DTO;
|
||||
using YABA.Service.Interfaces;
|
||||
|
||||
namespace YABA.Service
|
||||
{
|
||||
public class UserService : IUserService
|
||||
{
|
||||
private readonly YABAReadOnlyContext _roContext;
|
||||
private readonly YABAReadWriteContext _context;
|
||||
|
||||
public UserService (YABAReadOnlyContext roContext, YABAReadWriteContext context)
|
||||
{
|
||||
_roContext = roContext;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public bool IsUserRegistered(string authProviderId)
|
||||
{
|
||||
return _roContext.Users.Any(x => x.Auth0Id == authProviderId);
|
||||
}
|
||||
|
||||
public UserDTO RegisterUser(string authProviderId)
|
||||
{
|
||||
if(IsUserRegistered(authProviderId))
|
||||
{
|
||||
var user = _roContext.Users.FirstOrDefault(x => x.Auth0Id == authProviderId);
|
||||
return new UserDTO(user);
|
||||
}
|
||||
|
||||
var userToRegister = new User
|
||||
{
|
||||
Auth0Id = authProviderId
|
||||
};
|
||||
|
||||
var registedUser = _context.Users.Add(userToRegister);
|
||||
return _context.SaveChanges() > 0 ? new UserDTO(registedUser.Entity) : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -10,4 +10,10 @@
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\YABA.Common\YABA.Common.csproj" />
|
||||
<ProjectReference Include="..\YABA.Data\YABA.Data.csproj" />
|
||||
<ProjectReference Include="..\YABA.Models\YABA.Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user