From 32027f7384372d5eea6e16bf62af535c654508e1 Mon Sep 17 00:00:00 2001 From: eee4 <41441600+eee4@users.noreply.github.com> Date: Sat, 31 May 2025 18:19:15 +0200 Subject: [PATCH] feat: add first volunteer skill endpoint (add_skill) along with dtos --- WebApp/DTOs/SingleSkillDto.cs | 8 ++++++ WebApp/DTOs/SkillSummaryDto.cs | 9 ++++++ WebApp/Endpoints/AuthEndpoints.cs | 37 +++++++++++++++++++++++++ WebApp/Mapping/SkillMapping.cs | 25 +++++++++++++++++ WebApp/Mapping/VolunteerSkillMapping.cs | 17 ++++++++++++ 5 files changed, 96 insertions(+) create mode 100644 WebApp/DTOs/SingleSkillDto.cs create mode 100644 WebApp/DTOs/SkillSummaryDto.cs create mode 100644 WebApp/Mapping/SkillMapping.cs create mode 100644 WebApp/Mapping/VolunteerSkillMapping.cs diff --git a/WebApp/DTOs/SingleSkillDto.cs b/WebApp/DTOs/SingleSkillDto.cs new file mode 100644 index 0000000..a6d5d47 --- /dev/null +++ b/WebApp/DTOs/SingleSkillDto.cs @@ -0,0 +1,8 @@ +using System.ComponentModel.DataAnnotations; + +namespace WebApp.DTOs; + +public record class SingleSkillDto +( + [Required] int Skill +); diff --git a/WebApp/DTOs/SkillSummaryDto.cs b/WebApp/DTOs/SkillSummaryDto.cs new file mode 100644 index 0000000..597d39b --- /dev/null +++ b/WebApp/DTOs/SkillSummaryDto.cs @@ -0,0 +1,9 @@ +using System.ComponentModel.DataAnnotations; + +namespace WebApp.DTOs; + +public record class SkillSummaryDto +( + [Required] int SkillId, + [Required] string SkillName +); diff --git a/WebApp/Endpoints/AuthEndpoints.cs b/WebApp/Endpoints/AuthEndpoints.cs index 2363ce7..f4aecd2 100644 --- a/WebApp/Endpoints/AuthEndpoints.cs +++ b/WebApp/Endpoints/AuthEndpoints.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.EntityFrameworkCore; +using System.Runtime.Intrinsics.Arm; using System.Security.Cryptography; using System.Text; using WebApp.Data; @@ -123,6 +124,42 @@ namespace WebApp.Endpoints }); + group.MapPost("/add_skill", async (SingleSkillDto dto, HttpContext httpContext, ApplicationDbContext context, GeneralUseHelpers guh) => + { + // Uzyskaj użytkownika z tokenu + Token? token = await guh.GetTokenFromHTTPContext(httpContext); + User? user = await guh.GetUserFromToken(token); + + // Tylko wolontariusze powinno móc dodawać swoje skille + if (user == null || user.IsOrganisation) { + return Results.Json(new { message = "Unauthorized" }, statusCode: 401); + } + + // Szukamy skilla w bazie o ID takim, jak w otrzymanym DTO + Skill? skill = await context.Skills.FindAsync(dto.Skill); + if (skill is null) + { + return Results.Json(new { message = "Skill not found" }, statusCode: 404); + } + + // Sprawdzamy, czy ten użytkownik nie ma już takiego skilla. Jeżeli ma, nie ma sensu dodawać go kilkukrotnie. + VolunteerSkill? vs = await context.VolunteerSkills.FirstOrDefaultAsync(v => v.UserId == user.UserId && v.SkillId == dto.Skill); + if (vs is null) + { + // Nie ma - zatem musimy dodać nowy VolunteerSkill do bazy + VolunteerSkill newVs = dto.ToVolunteerSkillEntity(user.UserId); + context.VolunteerSkills.Add(newVs); + await context.SaveChangesAsync(); + + } else + { + // Ma - (ta para UserId <-> SkillId już istnieje w bazie) użytkownik już ma ten skill + return Results.Json(new { message = "User already has this skill" }, statusCode: 400); + } + + return Results.Json(new { message = "Skill added successfully!" }, statusCode: 201); + }); + return group; } diff --git a/WebApp/Mapping/SkillMapping.cs b/WebApp/Mapping/SkillMapping.cs new file mode 100644 index 0000000..fb96056 --- /dev/null +++ b/WebApp/Mapping/SkillMapping.cs @@ -0,0 +1,25 @@ +using WebApp.DTOs; +using WebApp.Entities; + +namespace WebApp.Mapping +{ + public static class SkillMapping + { + public static Skill ToSkillEntity(this SingleSkillDto SSDto, string name) + { + return new Skill() + { + SkillId = SSDto.Skill, + Name = name + }; + } + + public static SkillSummaryDto ToSkillSummaryDto(this Skill s) + { + return new SkillSummaryDto( + s.SkillId, + s.Name + ); + } + } +} diff --git a/WebApp/Mapping/VolunteerSkillMapping.cs b/WebApp/Mapping/VolunteerSkillMapping.cs new file mode 100644 index 0000000..a92198a --- /dev/null +++ b/WebApp/Mapping/VolunteerSkillMapping.cs @@ -0,0 +1,17 @@ +using WebApp.DTOs; +using WebApp.Entities; + +namespace WebApp.Mapping +{ + public static class VolunteerSkillMapping + { + public static VolunteerSkill ToVolunteerSkillEntity(this SingleSkillDto SSDto, int uid) + { + return new VolunteerSkill() + { + UserId = uid, + SkillId = SSDto.Skill, + }; + } + } +}