mirror of
https://github.com/Govor-team/Govor.git
synced 2026-09-17 16:22:49 +00:00
3c785d5c89
Migrate codebase to use SmartRes Result<T, Error> (and Unit) across services and interfaces, replacing previous Result usage and updating many method signatures and implementations. Add ResultExtensions to convert SmartRes results to ASP.NET ActionResult and refactor AuthController to use functional Bind/Tap chains for registration/login flows. Upgrade projects to .NET 10 and bump related NuGet packages; add libs/SmartRes.dll and project references. Simplify DbContext registration to use Npgsql only with retry policies and enable detailed logging. Update Swagger/launch settings and other minor fixes (AutoMapper registration change, whitespace/exception handling, and removal of the Govor.ConsoleClient files).
60 lines
1.6 KiB
C#
60 lines
1.6 KiB
C#
using Govor.Application.Exceptions.InvitesService;
|
|
using Govor.Domain;
|
|
using Govor.Domain.Common;
|
|
using Govor.Domain.Models;
|
|
using Govor.Domain.Models.Users;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SmartRes;
|
|
|
|
namespace Govor.Application.Authentication;
|
|
|
|
public class InvitesService : IInvitesService
|
|
{
|
|
private readonly GovorDbContext _context;
|
|
|
|
public InvitesService(GovorDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<string> GetRoleNameAsync(User user)
|
|
{
|
|
return await GetRoleNameAsync(user.InviteId);
|
|
}
|
|
|
|
public async Task<string> GetRoleNameAsync(Guid sessionId)
|
|
{
|
|
var invitation = await _context.Invitations.FirstOrDefaultAsync(s => s.Id == sessionId);
|
|
|
|
if (invitation == null)
|
|
return "User";
|
|
|
|
return invitation.IsAdmin ? "Admin" : "User";
|
|
}
|
|
|
|
public async Task<Result<Invitation, Error>> ValidateAsync(string inviteCode)
|
|
{
|
|
var invite = await _context.Invitations
|
|
.Include(s => s.Users)
|
|
.FirstOrDefaultAsync(s => s.Code == inviteCode);
|
|
|
|
if (invite == null)
|
|
return Result.Failure<Invitation>(Error.NotFound("Auth.LinkNotFount","Invitation not found."));
|
|
|
|
if (invite.EndDate < DateTime.Now || invite.MaxParticipants <= invite.Users.Count)
|
|
{
|
|
invite.IsActive = false;
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Result.Failure<Invitation>(
|
|
Error.Failure(
|
|
"Auth.InviteLinkInvalid", $"Invite link invalid: {inviteCode}"
|
|
)
|
|
);
|
|
}
|
|
|
|
return invite;
|
|
}
|
|
}
|
|
|