Added SignalR into project
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AIHubContext_00601_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F731dbe51d65849048244493644b992cd78910_003Fda_003Fd14ae6ee_003FIHubContext_00601_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ARepositoryBaseOfT_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E2_003Fresharper_002Dhost_003FSourcesCache_003F5023b5be698d783ffe9f42b2e944a85a7a66b61bc5e19c76c591036343fd16_003FRepositoryBaseOfT_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>
|
||||
@@ -15,6 +15,7 @@
|
||||
<PackageReference Include="FastEndpoints.Security" Version="7.2.0" />
|
||||
<PackageReference Include="FastEndpoints.Swagger" Version="7.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.24"/>
|
||||
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.2.9" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.20" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.20">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
|
||||
@@ -36,7 +36,12 @@ public class GetGroupRankingEndpoint(
|
||||
foreach (UserGroup member in group.UserGroups!)
|
||||
{
|
||||
if (member.Proof is null) member.Score -= 2;
|
||||
|
||||
if (!member.VotedProofId.HasValue) continue;
|
||||
User? votedUser = await usersRepository.SingleOrDefaultAsync(new GetUserByIdSpec(member.VotedProofId.Value), ct);
|
||||
if (votedUser is not null) votedUser.Score++;
|
||||
}
|
||||
await usersRepository.SaveChangesAsync(ct);
|
||||
await userGroupsRepository.SaveChangesAsync(ct);
|
||||
|
||||
List<GetGroupRankingDto> groupScore = await userGroupsRepository.ProjectToListAsync<GetGroupRankingDto>(new GetGroupRankSpec(req.Id), ct);
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
using BeReadyBackend.Models;
|
||||
using BeReadyBackend.Hubs;
|
||||
using BeReadyBackend.Models;
|
||||
using BeReadyBackend.Repositories;
|
||||
using BeReadyBackend.Services;
|
||||
using BeReadyBackend.Specifications.Groups;
|
||||
using FastEndpoints;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace BeReadyBackend.Endpoints.Groups;
|
||||
|
||||
@@ -12,7 +14,7 @@ public class UserProofRequest
|
||||
public string? Proof { get; set; }
|
||||
}
|
||||
|
||||
public class PatchGroupUserProofEndpoint(UserGroupsRepository userGroupsRepository, UserService userService) : Endpoint<UserProofRequest>
|
||||
public class PatchGroupUserProofEndpoint(UserGroupsRepository userGroupsRepository, UserService userService, IHubContext<GroupHub> hubContext) : Endpoint<UserProofRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -35,7 +37,7 @@ public class PatchGroupUserProofEndpoint(UserGroupsRepository userGroupsReposito
|
||||
await Send.StringAsync("Vous avez déjà déposé une photo", 400, cancellation: ct);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (member.Group!.IsFinished)
|
||||
{
|
||||
await Send.StringAsync("Ce défi est terminé", 400, cancellation: ct);
|
||||
@@ -44,6 +46,9 @@ public class PatchGroupUserProofEndpoint(UserGroupsRepository userGroupsReposito
|
||||
|
||||
member.Proof = req.Proof;
|
||||
await userGroupsRepository.SaveChangesAsync(ct);
|
||||
|
||||
await hubContext.Clients.Group($"group-{req.GroupId}").SendAsync("ReceiveProof", req.Proof, cancellationToken: ct);
|
||||
|
||||
await Send.OkAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
using BeReadyBackend.Models;
|
||||
using BeReadyBackend.Hubs;
|
||||
using BeReadyBackend.Models;
|
||||
using BeReadyBackend.Repositories;
|
||||
using BeReadyBackend.Services;
|
||||
using BeReadyBackend.Specifications.Groups;
|
||||
using FastEndpoints;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Group = BeReadyBackend.Models.Group;
|
||||
|
||||
namespace BeReadyBackend.Endpoints.Groups;
|
||||
@@ -12,20 +14,21 @@ public class GroupVoteRequest
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class StartVoteEndpoint(UserGroupsRepository userGroupsRepository, GroupsRepository groupsRepository, UserService userService) : Endpoint<GroupVoteRequest>
|
||||
public class StartVoteEndpoint(UserGroupsRepository userGroupsRepository, GroupsRepository groupsRepository, UserService userService, IHubContext<GroupHub> hubContext)
|
||||
: Endpoint<GroupVoteRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/Groups/{@Id}/Vote/", x => new { x.Id });
|
||||
}
|
||||
|
||||
|
||||
public override async Task HandleAsync(GroupVoteRequest req, CancellationToken ct)
|
||||
{
|
||||
Group? group = await groupsRepository.SingleOrDefaultAsync(new GetGroupByIdSpec(req.Id), ct);
|
||||
|
||||
|
||||
if (group is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -33,23 +36,23 @@ public class StartVoteEndpoint(UserGroupsRepository userGroupsRepository, Groups
|
||||
|
||||
if (userGroup?.Grade != "Admin")
|
||||
{
|
||||
await Send.StringAsync("Vous ne pouvez pas lancer le vote", 400, cancellation: ct);
|
||||
await Send.StringAsync("Vous ne pouvez pas lancer le vote", 400, cancellation: ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (group.IsFinished)
|
||||
{
|
||||
await Send.StringAsync("Le défi est terminé", 400, cancellation: ct);
|
||||
await Send.StringAsync("Le défi est terminé", 400, cancellation: ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (group.CreationDate.AddHours(group.Duration) > DateTime.Now)
|
||||
{
|
||||
await Send.StringAsync("Le défi n'est pas terminé", 400, cancellation: ct);
|
||||
await Send.StringAsync("Le défi n'est pas terminé", 400, cancellation: ct);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
await hubContext.Clients.Group($"group-{req.Id}").SendAsync("StartVote", cancellationToken: ct);
|
||||
await Send.OkAsync(ct);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
|
||||
namespace BeReadyBackend.Hubs;
|
||||
|
||||
public class GroupHub : Hub
|
||||
{
|
||||
public async Task SendProofToGroup(int groupId, string proofUrl)
|
||||
{
|
||||
await Clients.Group($"group-{groupId}").SendAsync("ReceiveProof", proofUrl);
|
||||
}
|
||||
|
||||
public async Task SendMessageToGroup(int groupId, string message)
|
||||
{
|
||||
await Clients.Group($"group-{groupId}").SendAsync("ReceiveMessage", message);
|
||||
}
|
||||
|
||||
public async Task StartVoteIntoGroup(int groupId)
|
||||
{
|
||||
await Clients.Group($"group-{groupId}").SendAsync("StartVote");
|
||||
}
|
||||
|
||||
public override async Task OnConnectedAsync()
|
||||
{
|
||||
HttpContext? httpContext = Context.GetHttpContext();
|
||||
if (httpContext!.Request.Query.TryGetValue("groupId", out StringValues groupId))
|
||||
{
|
||||
await Groups.AddToGroupAsync(Context.ConnectionId, $"group-{groupId}");
|
||||
}
|
||||
await base.OnConnectedAsync();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using AutoMapper;
|
||||
using AutoMapper.EquivalencyExpression;
|
||||
using BeReadyBackend;
|
||||
using BeReadyBackend.Hubs;
|
||||
using BeReadyBackend.MappingProfiles;
|
||||
using FastEndpoints;
|
||||
using FastEndpoints.Swagger;
|
||||
@@ -27,10 +28,7 @@ builder.Services
|
||||
.WithExposedHeaders(HeaderNames.ContentDisposition);
|
||||
});
|
||||
})
|
||||
.SwaggerDocument(options =>
|
||||
{
|
||||
options.ShortSchemaNames = true;
|
||||
});
|
||||
.SwaggerDocument(options => { options.ShortSchemaNames = true; });
|
||||
|
||||
builder.Services.AddDbContext<BeReadyDbContext>();
|
||||
|
||||
@@ -45,6 +43,8 @@ builder.Services.AddScoped<UserGroupsRepository>();
|
||||
builder.Services.AddScoped<UserRandomChallengesRepository>();
|
||||
builder.Services.AddScoped<UsersRepository>();
|
||||
|
||||
builder.Services.AddSignalR();
|
||||
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
|
||||
builder.Services.AddScoped<UserService>();
|
||||
@@ -70,6 +70,8 @@ app.UseAuthentication()
|
||||
})
|
||||
.UseSwaggerGen();
|
||||
|
||||
app.MapHub<GroupHub>("/groupHub");
|
||||
|
||||
// app.UseHttpsRedirection();
|
||||
|
||||
// app.UseCors();
|
||||
|
||||
Reference in New Issue
Block a user