diff --git a/JOBot.Backend/DAL/Context/AppDbContext.cs b/JOBot.Backend/DAL/Context/AppDbContext.cs index a60473f..a8d79bb 100644 --- a/JOBot.Backend/DAL/Context/AppDbContext.cs +++ b/JOBot.Backend/DAL/Context/AppDbContext.cs @@ -8,4 +8,12 @@ class AppDbContext : DbContext public DbSet Users { get; set; } public AppDbContext(DbContextOptions options) : base(options) { } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + modelBuilder.Entity() + .HasAlternateKey(b => b.TelegramId); + } } \ No newline at end of file diff --git a/JOBot.Backend/DAL/Models/User.cs b/JOBot.Backend/DAL/Models/User.cs index 5ce5d36..4767238 100644 --- a/JOBot.Backend/DAL/Models/User.cs +++ b/JOBot.Backend/DAL/Models/User.cs @@ -1,9 +1,15 @@ +using System.ComponentModel.DataAnnotations; +using Microsoft.EntityFrameworkCore; + namespace JOBot.Backend.DAL.Models; +[PrimaryKey(nameof(Id))] public class User { public Guid Id { get; set; } - public long TelegramId { get; set; } + + [Key] + public required long TelegramId { get; set; } public string? Username { get; set; } public DateTime CreatedAt { get; set; } = DateTime.Now; } \ No newline at end of file diff --git a/JOBot.Backend/Services/gRPC/UserService.cs b/JOBot.Backend/Services/gRPC/UserService.cs index 3743a03..c265578 100644 --- a/JOBot.Backend/Services/gRPC/UserService.cs +++ b/JOBot.Backend/Services/gRPC/UserService.cs @@ -1,19 +1,36 @@ -namespace JOBot.Backend.Services.gRPC; using Grpc.Core; using JOBot.Proto; +using JOBot.Backend.DAL.Context; -class UserService : User.UserBase +using Models = JOBot.Backend.DAL.Models; + +namespace JOBot.Backend.Services.gRPC; +class UserService(AppDbContext dbContext) : User.UserBase { + public override Task Register( RegisterRequest request, ServerCallContext context) - { + if(!dbContext.Users.Where(x => x.TelegramId == request.UserId) + .Any()) + { + dbContext.Users.Add(new Models.User() + { + TelegramId = request.UserId, + Username = !string.IsNullOrEmpty(request.Username) ? request.Username : null + }); + + return Task.FromResult(new RegisterResponse + { + Success = true + }); + } return Task.FromResult(new RegisterResponse { - UserId = request.UserId + Success = false }); } } \ No newline at end of file diff --git a/Proto/user.proto b/Proto/user.proto index ee50850..8fa07db 100644 --- a/Proto/user.proto +++ b/Proto/user.proto @@ -11,5 +11,5 @@ message RegisterRequest{ } message RegisterResponse{ - int64 user_id = 1; + bool success = 1; } \ No newline at end of file