From 5b4d524c47fa91a2e2138f0a724ee270572b59e3 Mon Sep 17 00:00:00 2001 From: Lisoveliy Date: Fri, 11 Jul 2025 00:10:00 +0300 Subject: [PATCH] chore: cleanup --- JOBot.Backend/Services/gRPC/UserService.cs | 57 +++++++--------------- 1 file changed, 18 insertions(+), 39 deletions(-) diff --git a/JOBot.Backend/Services/gRPC/UserService.cs b/JOBot.Backend/Services/gRPC/UserService.cs index d2f3b60..f694431 100644 --- a/JOBot.Backend/Services/gRPC/UserService.cs +++ b/JOBot.Backend/Services/gRPC/UserService.cs @@ -6,39 +6,28 @@ using Microsoft.EntityFrameworkCore; using User = JOBot.Backend.DAL.Models.User; namespace JOBot.Backend.Services.gRPC; + public class UserService(AppDbContext dbContext) : Proto.User.UserBase { - /// /// Create user /// /// /// /// Status of operation (fail if user exists) - public override async Task Register( - RegisterRequest request, - ServerCallContext _) + public override async Task Register(RegisterRequest request, ServerCallContext _) { - if(!await dbContext.Users - .AnyAsync(x => x.UserId == request.UserId)) - { - dbContext.Users.Add(new User - { - UserId = request.UserId, - Username = !string.IsNullOrEmpty(request.Username) ? request.Username : null - }); - - await dbContext.SaveChangesAsync(); - return new RegisterResponse - { - Success = true - }; - } + if (await dbContext.Users.AnyAsync(x => x.UserId == request.UserId)) + return new RegisterResponse { Success = false }; - return new RegisterResponse + dbContext.Users.Add(new User { - Success = false - }; + UserId = request.UserId, + Username = !string.IsNullOrEmpty(request.Username) ? request.Username : null + }); + await dbContext.SaveChangesAsync(); + + return new RegisterResponse { Success = true }; } /// @@ -47,13 +36,11 @@ public class UserService(AppDbContext dbContext) : Proto.User.UserBase /// /// /// User, or throws RPC exception if not found - public override async Task GetUser( - GetUserRequest request, - ServerCallContext _) + public override async Task GetUser(GetUserRequest request, ServerCallContext _) { var user = await dbContext.Users.FirstOrDefaultAsync(x => x.UserId == request.UserId); - ValidateUserFound(user); - + ThrowIfUserNotFound(user); + 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); if (user == null) - { - return new AcceptEulaResponse - { - Success = false - }; - } + return new AcceptEulaResponse { Success = false }; user.Eula = request.EulaAccepted; await dbContext.SaveChangesAsync(); - - return new AcceptEulaResponse - { - Success = true - }; + + return new AcceptEulaResponse { Success = true }; } /// @@ -88,7 +67,7 @@ public class UserService(AppDbContext dbContext) : Proto.User.UserBase /// /// /// - private void ValidateUserFound(User? user) + private static void ThrowIfUserNotFound(User? user) { if (user == null) throw new RpcException(new Status(StatusCode.NotFound, "User not found"));