mirror of
https://github.com/GCMatters/hermes.git
synced 2026-02-04 13:40:13 +01:00
feat: add first volunteer skill endpoint (add_skill) along with dtos
This commit is contained in:
8
WebApp/DTOs/SingleSkillDto.cs
Normal file
8
WebApp/DTOs/SingleSkillDto.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace WebApp.DTOs;
|
||||
|
||||
public record class SingleSkillDto
|
||||
(
|
||||
[Required] int Skill
|
||||
);
|
||||
9
WebApp/DTOs/SkillSummaryDto.cs
Normal file
9
WebApp/DTOs/SkillSummaryDto.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace WebApp.DTOs;
|
||||
|
||||
public record class SkillSummaryDto
|
||||
(
|
||||
[Required] int SkillId,
|
||||
[Required] string SkillName
|
||||
);
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
25
WebApp/Mapping/SkillMapping.cs
Normal file
25
WebApp/Mapping/SkillMapping.cs
Normal file
@@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
17
WebApp/Mapping/VolunteerSkillMapping.cs
Normal file
17
WebApp/Mapping/VolunteerSkillMapping.cs
Normal file
@@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user