Added endpoint for registering a user locally after successful logon from Auth0

This commit is contained in:
Carl Tibule
2023-01-25 22:47:05 -06:00
parent e0b38beff6
commit 406bd9a0d1
16 changed files with 267 additions and 47 deletions

View File

@ -1,9 +0,0 @@
using System;
namespace YABA.Service
{
public class Class1
{
}
}

View File

@ -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>();
}
}
}

View 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;
}
}
}

View 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);
}
}

View 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;
}
}
}

View File

@ -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>