#10 feat: implemented return of user info and eula accept logic #13

Merged
Lisoveliy merged 2 commits from 10 into main 2025-07-10 23:10:35 +02:00
Showing only changes of commit 5b4d524c47 - Show all commits

View File

@ -6,39 +6,28 @@ using Microsoft.EntityFrameworkCore;
using User = JOBot.Backend.DAL.Models.User; using User = JOBot.Backend.DAL.Models.User;
namespace JOBot.Backend.Services.gRPC; namespace JOBot.Backend.Services.gRPC;
public class UserService(AppDbContext dbContext) : Proto.User.UserBase public class UserService(AppDbContext dbContext) : Proto.User.UserBase
{ {
/// <summary> /// <summary>
/// Create user /// Create user
/// </summary> /// </summary>
/// <param name="request"></param> /// <param name="request"></param>
/// <param name="_"></param> /// <param name="_"></param>
/// <returns>Status of operation (fail if user exists)</returns> /// <returns>Status of operation (fail if user exists)</returns>
public override async Task<RegisterResponse> Register( public override async Task<RegisterResponse> Register(RegisterRequest request, ServerCallContext _)
RegisterRequest request,
ServerCallContext _)
{
if(!await dbContext.Users
.AnyAsync(x => x.UserId == request.UserId))
{ {
if (await dbContext.Users.AnyAsync(x => x.UserId == request.UserId))
return new RegisterResponse { Success = false };
dbContext.Users.Add(new User dbContext.Users.Add(new User
{ {
UserId = request.UserId, UserId = request.UserId,
Username = !string.IsNullOrEmpty(request.Username) ? request.Username : null Username = !string.IsNullOrEmpty(request.Username) ? request.Username : null
}); });
await dbContext.SaveChangesAsync(); await dbContext.SaveChangesAsync();
return new RegisterResponse
{
Success = true
};
}
return new RegisterResponse return new RegisterResponse { Success = true };
{
Success = false
};
} }
/// <summary> /// <summary>
@ -47,12 +36,10 @@ public class UserService(AppDbContext dbContext) : Proto.User.UserBase
/// <param name="request"></param> /// <param name="request"></param>
/// <param name="_"></param> /// <param name="_"></param>
/// <returns>User, or throws RPC exception if not found</returns> /// <returns>User, or throws RPC exception if not found</returns>
public override async Task<GetUserResponse> GetUser( public override async Task<GetUserResponse> GetUser(GetUserRequest request, ServerCallContext _)
GetUserRequest request,
ServerCallContext _)
{ {
var user = await dbContext.Users.FirstOrDefaultAsync(x => x.UserId == request.UserId); var user = await dbContext.Users.FirstOrDefaultAsync(x => x.UserId == request.UserId);
ValidateUserFound(user); ThrowIfUserNotFound(user);
return user!.MapToResponse(); return user!.MapToResponse();
} }
@ -67,20 +54,12 @@ public class UserService(AppDbContext dbContext) : Proto.User.UserBase
{ {
var user = await dbContext.Users.FirstOrDefaultAsync(x => x.UserId == request.UserId); var user = await dbContext.Users.FirstOrDefaultAsync(x => x.UserId == request.UserId);
if (user == null) if (user == null)
{ return new AcceptEulaResponse { Success = false };
return new AcceptEulaResponse
{
Success = false
};
}
user.Eula = request.EulaAccepted; user.Eula = request.EulaAccepted;
await dbContext.SaveChangesAsync(); await dbContext.SaveChangesAsync();
return new AcceptEulaResponse return new AcceptEulaResponse { Success = true };
{
Success = true
};
} }
/// <summary> /// <summary>
@ -88,7 +67,7 @@ public class UserService(AppDbContext dbContext) : Proto.User.UserBase
/// </summary> /// </summary>
/// <param name="user"></param> /// <param name="user"></param>
/// <exception cref="RpcException"></exception> /// <exception cref="RpcException"></exception>
private void ValidateUserFound(User? user) private static void ThrowIfUserNotFound(User? user)
{ {
if (user == null) if (user == null)
throw new RpcException(new Status(StatusCode.NotFound, "User not found")); throw new RpcException(new Status(StatusCode.NotFound, "User not found"));