mirror of
https://github.com/GCMatters/hermes.git
synced 2026-02-04 13:40:13 +01:00
Added "add/delete/get skill"
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Runtime.Intrinsics.Arm;
|
||||
using System.Runtime.Intrinsics.Arm;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WebApp.Data;
|
||||
using WebApp.DTOs;
|
||||
using WebApp.Entities;
|
||||
@@ -160,6 +161,73 @@ namespace WebApp.Endpoints
|
||||
return Results.Json(new { message = "Skill added successfully!" }, statusCode: 201);
|
||||
});
|
||||
|
||||
|
||||
group.MapDelete("/delete_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 powinien móc usuwac 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 ma już taki skill. Jeżeli nie ma, nie ma sensu usuwac go kilkukrotnie.
|
||||
VolunteerSkill? vs = await context.VolunteerSkills.FirstOrDefaultAsync(v => v.UserId == user.UserId && v.SkillId == dto.Skill);
|
||||
if (vs is not null)
|
||||
{
|
||||
// Nie ma - zatem musimy dodać nowy VolunteerSkill do bazy
|
||||
VolunteerSkill newVs = dto.ToVolunteerSkillEntity(user.UserId);
|
||||
|
||||
|
||||
await context.VolunteerSkills.Where(v => v.SkillId == dto.Skill)
|
||||
.ExecuteDeleteAsync();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// Ma - (ta para UserId <-> SkillId już istnieje w bazie) użytkownik już ma ten skill
|
||||
return Results.Json(new { message = "User already has deleted this skill" }, statusCode: 400);
|
||||
}
|
||||
|
||||
return Results.Json(new { message = "Skill deleted successfully!" }, statusCode: 201);
|
||||
});
|
||||
|
||||
group.MapGet("/get_skills", 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);
|
||||
|
||||
// Sprawdź, czy użytkownik istnieje i nie jest organizacją
|
||||
if (user == null || user.IsOrganisation)
|
||||
{
|
||||
return Results.Json(new { message = "Unauthorized" }, statusCode: 401);
|
||||
}
|
||||
|
||||
// Pobierz skille wolontariusza
|
||||
var skills = await context.VolunteerSkills
|
||||
.Where(vs => vs.UserId == user.UserId)
|
||||
.Include(vs => vs.Skill)
|
||||
.Select(vs => new
|
||||
{
|
||||
skillId = vs.Skill.SkillId,
|
||||
skillName = vs.Skill.Name
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
return Results.Json(skills);
|
||||
});
|
||||
|
||||
return group;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user