feat: implemented return of user info and eula accept logic
This commit is contained in:
parent
252adf0fc7
commit
4a7810f571
1
.gitignore
vendored
1
.gitignore
vendored
@ -406,3 +406,4 @@ FodyWeavers.xsd
|
||||
# Secrets
|
||||
JOBot.TClient/appsettings.json
|
||||
/.idea/.idea.JOBot/Docker/compose.generated.override.yml
|
||||
/.idea/.idea.JOBot/Docker/compose.dev.generated.override.yml
|
||||
|
@ -1,19 +1,17 @@
|
||||
namespace JOBot.Backend.DAL.Context;
|
||||
|
||||
using JOBot.Backend.DAL.Models;
|
||||
using Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
public class AppDbContext : DbContext
|
||||
public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
|
||||
{
|
||||
public DbSet<User> Users { get; set; }
|
||||
|
||||
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
modelBuilder.Entity<User>()
|
||||
.HasAlternateKey(b => b.TelegramId);
|
||||
.HasAlternateKey(b => b.UserId);
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using JOBot.Proto;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace JOBot.Backend.DAL.Models;
|
||||
@ -9,10 +10,30 @@ public class User
|
||||
public Guid Id { get; set; }
|
||||
|
||||
[Key]
|
||||
public required long TelegramId { get; set; }
|
||||
[MaxLength(50)]
|
||||
public required long UserId { get; set; }
|
||||
[MaxLength(255)]
|
||||
public string? Username { get; set; }
|
||||
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
||||
|
||||
public string? HeadHunterResumeUrl { get; set; }
|
||||
[MaxLength(255)] public string? AccessToken { get; set; } = null;
|
||||
[MaxLength(255)] public string? RefreshToken { get; set; } = null;
|
||||
|
||||
public bool Eula { get; set; } = false;
|
||||
[MaxLength(255)] public string? CvUrl { get; set; } = null;
|
||||
}
|
||||
|
||||
//TODO: Негоже это маппинги в DAL ложить
|
||||
public static class UserMap
|
||||
{
|
||||
public static GetUserResponse MapToResponse(this User user)
|
||||
{
|
||||
return new GetUserResponse
|
||||
{
|
||||
UserId = user.UserId,
|
||||
Username = user.Username,
|
||||
Eula = user.Eula,
|
||||
IsLogged = user.RefreshToken != null,
|
||||
CVUrl = user.CvUrl
|
||||
};
|
||||
}
|
||||
}
|
@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
namespace JOBot.Backend.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(AppDbContext))]
|
||||
[Migration("20250710125338_Initial")]
|
||||
[Migration("20250710203327_Initial")]
|
||||
partial class Initial
|
||||
{
|
||||
/// <inheritdoc />
|
||||
@ -31,22 +31,34 @@ namespace JOBot.Backend.Data.Migrations
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("AccessToken")
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<string>("HeadHunterResumeUrl")
|
||||
.HasColumnType("text");
|
||||
b.Property<string>("CvUrl")
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)");
|
||||
|
||||
b.Property<long>("TelegramId")
|
||||
b.Property<bool>("Eula")
|
||||
.HasColumnType("boolean");
|
||||
|
||||
b.Property<string>("RefreshToken")
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)");
|
||||
|
||||
b.Property<long>("UserId")
|
||||
.HasColumnType("bigint");
|
||||
|
||||
b.Property<string>("Username")
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("character varying(50)");
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasAlternateKey("TelegramId");
|
||||
b.HasAlternateKey("UserId");
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
@ -16,15 +16,18 @@ namespace JOBot.Backend.Data.Migrations
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
TelegramId = table.Column<long>(type: "bigint", nullable: false),
|
||||
Username = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
|
||||
UserId = table.Column<long>(type: "bigint", nullable: false),
|
||||
Username = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: true),
|
||||
CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
|
||||
HeadHunterResumeUrl = table.Column<string>(type: "text", nullable: true)
|
||||
AccessToken = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: true),
|
||||
RefreshToken = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: true),
|
||||
Eula = table.Column<bool>(type: "boolean", nullable: false),
|
||||
CvUrl = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Users", x => x.Id);
|
||||
table.UniqueConstraint("AK_Users_TelegramId", x => x.TelegramId);
|
||||
table.UniqueConstraint("AK_Users_UserId", x => x.UserId);
|
||||
});
|
||||
}
|
||||
|
@ -28,22 +28,34 @@ namespace JOBot.Backend.Data.Migrations
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("AccessToken")
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<string>("HeadHunterResumeUrl")
|
||||
.HasColumnType("text");
|
||||
b.Property<string>("CvUrl")
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)");
|
||||
|
||||
b.Property<long>("TelegramId")
|
||||
b.Property<bool>("Eula")
|
||||
.HasColumnType("boolean");
|
||||
|
||||
b.Property<string>("RefreshToken")
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)");
|
||||
|
||||
b.Property<long>("UserId")
|
||||
.HasColumnType("bigint");
|
||||
|
||||
b.Property<string>("Username")
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("character varying(50)");
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasAlternateKey("TelegramId");
|
||||
b.HasAlternateKey("UserId");
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
@ -10,4 +10,5 @@ WORKDIR /app
|
||||
COPY --from=build /app .
|
||||
EXPOSE 5001
|
||||
EXPOSE 5000
|
||||
ENV ASPNETCORE_ENVIRONMENT Staging
|
||||
ENTRYPOINT ["dotnet", "JOBot.Backend.dll"]
|
@ -9,6 +9,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Google.Protobuf" Version="3.30.2" />
|
||||
<PackageReference Include="Grpc.AspNetCore" Version="2.71.0" />
|
||||
<PackageReference Include="Grpc.AspNetCore.Server.Reflection" Version="2.71.0" />
|
||||
<PackageReference Include="Grpc.Tools" Version="2.71.0">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
@ -24,4 +25,8 @@
|
||||
<Protobuf Include="..\Proto\*" GrpcServices="Server"></Protobuf>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Data\Migrations\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -1,43 +1,96 @@
|
||||
using Grpc.Core;
|
||||
using JOBot.Proto;
|
||||
using JOBot.Backend.DAL.Context;
|
||||
|
||||
using Models = JOBot.Backend.DAL.Models;
|
||||
using JOBot.Backend.DAL.Models;
|
||||
using JOBot.Proto;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using User = JOBot.Backend.DAL.Models.User;
|
||||
|
||||
namespace JOBot.Backend.Services.gRPC;
|
||||
public class UserService(AppDbContext dbContext) : User.UserBase
|
||||
public class UserService(AppDbContext dbContext) : Proto.User.UserBase
|
||||
{
|
||||
|
||||
public override Task<RegisterResponse> Register(
|
||||
/// <summary>
|
||||
/// Create user
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="_"></param>
|
||||
/// <returns>Status of operation (fail if user exists)</returns>
|
||||
public override async Task<RegisterResponse> Register(
|
||||
RegisterRequest request,
|
||||
ServerCallContext context)
|
||||
ServerCallContext _)
|
||||
{
|
||||
if(!dbContext.Users
|
||||
.Any(x => x.TelegramId == request.UserId))
|
||||
if(!await dbContext.Users
|
||||
.AnyAsync(x => x.UserId == request.UserId))
|
||||
{
|
||||
dbContext.Users.Add(new Models.User
|
||||
dbContext.Users.Add(new User
|
||||
{
|
||||
TelegramId = request.UserId,
|
||||
UserId = request.UserId,
|
||||
Username = !string.IsNullOrEmpty(request.Username) ? request.Username : null
|
||||
});
|
||||
|
||||
dbContext.SaveChanges();
|
||||
return Task.FromResult(new RegisterResponse
|
||||
await dbContext.SaveChangesAsync();
|
||||
return new RegisterResponse
|
||||
{
|
||||
Success = true
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
return Task.FromResult(new RegisterResponse
|
||||
return new RegisterResponse
|
||||
{
|
||||
Success = false
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
public override Task<GetUserResponse> GetUser(
|
||||
/// <summary>
|
||||
/// Get user for client
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="_"></param>
|
||||
/// <returns>User, or throws RPC exception if not found</returns>
|
||||
public override async Task<GetUserResponse> GetUser(
|
||||
GetUserRequest request,
|
||||
ServerCallContext context)
|
||||
ServerCallContext _)
|
||||
{
|
||||
return null;
|
||||
var user = await dbContext.Users.FirstOrDefaultAsync(x => x.UserId == request.UserId);
|
||||
ValidateUserFound(user);
|
||||
|
||||
return user!.MapToResponse();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Accept EULA for user
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="_"></param>
|
||||
/// <returns>Status of operation</returns>
|
||||
public override async Task<AcceptEulaResponse> AcceptEula(AcceptEulaRequest request, ServerCallContext _)
|
||||
{
|
||||
var user = await dbContext.Users.FirstOrDefaultAsync(x => x.UserId == request.UserId);
|
||||
if (user == null)
|
||||
{
|
||||
return new AcceptEulaResponse
|
||||
{
|
||||
Success = false
|
||||
};
|
||||
}
|
||||
|
||||
user.Eula = request.EulaAccepted;
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
return new AcceptEulaResponse
|
||||
{
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throw RPCException if user not found
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <exception cref="RpcException"></exception>
|
||||
private void ValidateUserFound(User? user)
|
||||
{
|
||||
if (user == null)
|
||||
throw new RpcException(new Status(StatusCode.NotFound, "User not found"));
|
||||
}
|
||||
}
|
@ -15,13 +15,16 @@ public class Startup
|
||||
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddGrpc();
|
||||
services.AddGrpc();
|
||||
services.AddGrpcReflection();
|
||||
|
||||
services.AddDbContext<AppDbContext>(options =>
|
||||
options.UseNpgsql(Configuration.GetConnectionString("PostgreSQL")));
|
||||
}
|
||||
|
||||
public void Configure(WebApplication app, IWebHostEnvironment env)
|
||||
{
|
||||
app.MapGrpcReflectionService().AllowAnonymous();
|
||||
app.MapGrpcService<UserService>();
|
||||
}
|
||||
}
|
@ -4,5 +4,8 @@
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
"PostgreSQL": "Host=localhost;Port=5432;Database=jobot;Username=postgres;Password=LocalDbPass"
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,6 @@
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"ConnectionStrings": {
|
||||
"PostgreSQL": "Host=localhost;Port=5432;Database=jobot_test;Username=postgres;Password=LocalDbPass"
|
||||
"PostgreSQL": "Host=postgres;Port=5432;Database=jobot_test;Username=postgres;Password=LocalDbPass"
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ option csharp_namespace = "JOBot.Proto";
|
||||
service User {
|
||||
rpc Register (RegisterRequest) returns (RegisterResponse);
|
||||
rpc GetUser (GetUserRequest) returns (GetUserResponse);
|
||||
rpc AcceptEula (AcceptEulaRequest) returns (AcceptEulaResponse);
|
||||
}
|
||||
|
||||
import "google/protobuf/wrappers.proto";
|
||||
@ -22,5 +23,18 @@ message GetUserRequest {
|
||||
}
|
||||
|
||||
message GetUserResponse {
|
||||
bool logged_to_hh = 1;
|
||||
int64 user_id = 1;
|
||||
google.protobuf.StringValue username = 2;
|
||||
bool eula = 3;
|
||||
bool is_logged = 4;
|
||||
google.protobuf.StringValue CVUrl = 5;
|
||||
}
|
||||
|
||||
message AcceptEulaRequest {
|
||||
int64 user_id = 1;
|
||||
bool eula_accepted = 2;
|
||||
}
|
||||
|
||||
message AcceptEulaResponse{
|
||||
bool success = 1;
|
||||
}
|
37
compose.dev.yml
Normal file
37
compose.dev.yml
Normal file
@ -0,0 +1,37 @@
|
||||
version: '3.8'
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:15
|
||||
environment:
|
||||
POSTGRES_PASSWORD: LocalDbPass
|
||||
POSTGRES_DB: jobot
|
||||
ports:
|
||||
- "5432:5432"
|
||||
volumes:
|
||||
- ./.docker/postgres_data:/var/lib/postgresql/data
|
||||
networks:
|
||||
- jobot
|
||||
|
||||
backend:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: JOBot.Backend/Dockerfile
|
||||
depends_on:
|
||||
- postgres
|
||||
ports:
|
||||
- "5001:5001"
|
||||
networks:
|
||||
- jobot
|
||||
|
||||
bot:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: JOBot.TClient/Dockerfile
|
||||
depends_on:
|
||||
- backend
|
||||
networks:
|
||||
- jobot
|
||||
|
||||
networks:
|
||||
jobot:
|
@ -6,8 +6,6 @@ services:
|
||||
environment:
|
||||
POSTGRES_PASSWORD: LocalDbPass
|
||||
POSTGRES_DB: jobot
|
||||
ports:
|
||||
- "5432:5432"
|
||||
volumes:
|
||||
- ./.docker/postgres_data:/var/lib/postgresql/data
|
||||
networks:
|
||||
|
Loading…
x
Reference in New Issue
Block a user