Added endpoint for registering a user locally after successful logon from Auth0
This commit is contained in:
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user