Initial version
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using ProtoBuf.Grpc;
|
||||
using EonaCat.gRPC.Api.Helpers;
|
||||
using EonaCat.gRPC.Proto;
|
||||
|
||||
namespace EonaCat.gRPC.Api.Services;
|
||||
|
||||
public class AuthenticationHandler : IAuthenticationService
|
||||
{
|
||||
private readonly IOptions<AppSettings> _appSettings;
|
||||
|
||||
public AuthenticationHandler(IOptions<AppSettings> appSettings)
|
||||
{
|
||||
_appSettings = appSettings;
|
||||
}
|
||||
|
||||
public Task<AuthenticationResponse> Authenticate(AuthenticationRequest request, CallContext context = default)
|
||||
{
|
||||
var authenticationResponse = JwtAuthenticationManager.Authenticate(_appSettings, request);
|
||||
if (authenticationResponse == null)
|
||||
throw new RpcException(new Status(StatusCode.Unauthenticated, "Invalid ProtoUserResponse Credentials"));
|
||||
return Task.FromResult(authenticationResponse);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using EonaCat.gRPC.Core.Interfaces.Services;
|
||||
using EonaCat.gRPC.Proto;
|
||||
using EonaCat.Mapper;
|
||||
|
||||
namespace EonaCat.gRPC.Api.Services;
|
||||
|
||||
public class UserHandler : IProtoUserService
|
||||
{
|
||||
private readonly IUserService _userService;
|
||||
|
||||
public UserHandler(IUserService userService)
|
||||
{
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
public async ValueTask<BaseResponse<string>> Create(UserCreateRequest userCreateRequest)
|
||||
{
|
||||
var user = CustomMapper.Map<UserCreateRequest, UserEntity>(userCreateRequest);
|
||||
var response = await _userService.CreateUser(user);
|
||||
return BaseResponse<string>.Created(response.ToString());
|
||||
}
|
||||
|
||||
public async Task<BaseResponse<List<UserResponse>?>> GetAsync()
|
||||
{
|
||||
var users = await _userService.GetAsync();
|
||||
var mapped = CustomMapper.Map<List<UserEntity>, List<UserResponse>>(users.ToList());
|
||||
return BaseResponse<List<UserResponse>?>.Ok(mapped);
|
||||
}
|
||||
|
||||
public async Task<BaseResponse<UserResponse?>> GetByIdAsync(string id)
|
||||
{
|
||||
if (string.IsNullOrEmpty(id) || !long.TryParse(id, out _))
|
||||
return BaseResponse<UserResponse?>.Failed(null, message: "Invalid ID: It is either empty or not a valid long.");
|
||||
var user = await _userService.GetAsync(Convert.ToInt64(id));
|
||||
var mapped = CustomMapper.Map<UserEntity, UserResponse>(user);
|
||||
return BaseResponse<UserResponse>.Ok(mapped);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user