From 2bb1f730c99c5aa9fd0ce208f91c4db1614bdf7b Mon Sep 17 00:00:00 2001 From: gokhoal Date: Thu, 11 Jun 2026 00:19:32 +0200 Subject: [PATCH] Creatediscussions private et group --- .../CreateGroupDiscussionEndpoint.cs | 75 +++++++++++++++++ .../CreatePrivateDiscussionEndpoint.cs | 83 +++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 Knots/Endpoints/Discussion/CreateGroupDiscussionEndpoint.cs create mode 100644 Knots/Endpoints/Discussion/CreatePrivateDiscussionEndpoint.cs diff --git a/Knots/Endpoints/Discussion/CreateGroupDiscussionEndpoint.cs b/Knots/Endpoints/Discussion/CreateGroupDiscussionEndpoint.cs new file mode 100644 index 0000000..b8a0201 --- /dev/null +++ b/Knots/Endpoints/Discussion/CreateGroupDiscussionEndpoint.cs @@ -0,0 +1,75 @@ +using FastEndpoints; +using Knots.DTO.Discussion; +using Knots.Models; +using Microsoft.EntityFrameworkCore; +using System.Security.Claims; + +namespace Knots.Endpoints.Discussion; + +public class CreateGroupDiscussionEndpoint(KnotsDbContext db) + : Endpoint +{ + public override void Configure() + { + Post("/discussions/group"); + } + + public override async Task HandleAsync(CreateGroupDiscussionRequest req, CancellationToken ct) + { + int currentUserId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!); + + + if (req.Usernames == null || req.Usernames.Count == 0) + { + await SendErrorsAsync(400, ct); + return; + } + + + List targets = await db.Users + .Where(u => req.Usernames.Contains(u.Username!)) + .ToListAsync(ct); + + if (targets.Count != req.Usernames.Count) + { + await SendNotFoundAsync(ct); // un ou plusieurs utilisateurs introuvables + return; + } + + if (targets.Any(t => t.Id == currentUserId)) + { + await SendErrorsAsync(400, ct); // pas de discussion avec soi-même + return; + } + + + List members = targets + .Select(t => new UserDiscussion { UserId = t.Id }) + .ToList(); + + members.Add(new UserDiscussion { UserId = currentUserId }); + + Models.Discussion discussion = new() + { + IsGroup = true, + UserDiscussions = members + }; + + db.Discussions.Add(discussion); + await db.SaveChangesAsync(ct); + + await SendOkAsync(new GetDiscussionDto + { + Id = discussion.Id, + IsGroup = true, + Name = req.GroupName, + MembersCount = members.Count + }, ct); + } +} + +public class CreateGroupDiscussionRequest +{ + public string GroupName { get; set; } = ""; + public List Usernames { get; set; } = []; +} \ No newline at end of file diff --git a/Knots/Endpoints/Discussion/CreatePrivateDiscussionEndpoint.cs b/Knots/Endpoints/Discussion/CreatePrivateDiscussionEndpoint.cs new file mode 100644 index 0000000..3c43958 --- /dev/null +++ b/Knots/Endpoints/Discussion/CreatePrivateDiscussionEndpoint.cs @@ -0,0 +1,83 @@ +using FastEndpoints; +using Knots.DTO.Discussion; +using Knots.Models; +using Microsoft.EntityFrameworkCore; +using System.Security.Claims; + +namespace Knots.Endpoints.Discussion; + +public class CreatePrivateDiscussionEndpoint(KnotsDbContext db) + : Endpoint +{ + public override void Configure() + { + Post("/discussions/private"); + } + + public override async Task HandleAsync(CreatePrivateDiscussionRequest req, CancellationToken ct) + { + int currentUserId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!); + + // 1. retrouver l'utilisateur cible par son nom + Models.User? target = await db.Users + .SingleOrDefaultAsync(u => u.Username == req.Username, ct); + + if (target is null) + { + await SendNotFoundAsync(ct); // utilisateur introuvable + return; + } + + if (target.Id == currentUserId) + { + await SendErrorsAsync(400, ct); // pas de discussion avec soi-même + return; + } + + // 2. vérifier qu'une discussion privée entre les deux n'existe pas déjà + Models.Discussion? existing = await db.Discussions + .Where(d => d.GroupId == null + && d.UserDiscussions.Any(ud => ud.UserId == currentUserId) + && d.UserDiscussions.Any(ud => ud.UserId == target.Id)) + .FirstOrDefaultAsync(ct); + + if (existing is not null) + { + await SendOkAsync(new GetDiscussionDto + { + Id = existing.Id, + IsGroup = false, + Name = target.Username!, + MembersCount = null + }, ct); + return; + } + + // 3. créer la discussion + les deux participants + Models.Discussion discussion = new() + { + IsGroup = false, + UserDiscussions = + [ + new UserDiscussion { UserId = currentUserId }, + new UserDiscussion { UserId = target.Id } + ] + }; + + db.Discussions.Add(discussion); + await db.SaveChangesAsync(ct); + + await SendOkAsync(new GetDiscussionDto + { + Id = discussion.Id, + IsGroup = false, + Name = target.Username!, + MembersCount = null + }, ct); + } +} + +public class CreatePrivateDiscussionRequest +{ + public string Username { get; set; } = ""; +} \ No newline at end of file