mirror of
https://github.com/GCMatters/hermes.git
synced 2026-02-04 13:40:13 +01:00
feat: enable adding relevant skills to events
This commit is contained in:
@@ -133,7 +133,7 @@ namespace WebApp.Endpoints
|
|||||||
Token? token = await guh.GetTokenFromHTTPContext(httpContext);
|
Token? token = await guh.GetTokenFromHTTPContext(httpContext);
|
||||||
User? user = await guh.GetUserFromToken(token);
|
User? user = await guh.GetUserFromToken(token);
|
||||||
|
|
||||||
// Tylko wolontariusze powinno móc dodawać swoje skille
|
// Tylko wolontariusze powinni móc dodawać swoje skille
|
||||||
if (user == null || user.IsOrganisation) {
|
if (user == null || user.IsOrganisation) {
|
||||||
return Results.Json(new { message = "Unauthorized" }, statusCode: 401);
|
return Results.Json(new { message = "Unauthorized" }, statusCode: 401);
|
||||||
}
|
}
|
||||||
@@ -170,7 +170,7 @@ namespace WebApp.Endpoints
|
|||||||
Token? token = await guh.GetTokenFromHTTPContext(httpContext);
|
Token? token = await guh.GetTokenFromHTTPContext(httpContext);
|
||||||
User? user = await guh.GetUserFromToken(token);
|
User? user = await guh.GetUserFromToken(token);
|
||||||
|
|
||||||
// Tylko wolontariusze powinien móc usuwac swoje skille
|
// Tylko wolontariusze powinni móc usuwać swoje skille
|
||||||
if (user == null || user.IsOrganisation)
|
if (user == null || user.IsOrganisation)
|
||||||
{
|
{
|
||||||
return Results.Json(new { message = "Unauthorized" }, statusCode: 401);
|
return Results.Json(new { message = "Unauthorized" }, statusCode: 401);
|
||||||
@@ -183,21 +183,20 @@ namespace WebApp.Endpoints
|
|||||||
return Results.Json(new { message = "Skill not found" }, statusCode: 404);
|
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.
|
// Sprawdzamy, czy ten użytkownik ma już taki skill. Jeżeli nie ma, to nie ma sensu usuwać czegoś, czego nie ma.
|
||||||
VolunteerSkill? vs = await context.VolunteerSkills.FirstOrDefaultAsync(v => v.UserId == user.UserId && v.SkillId == dto.Skill);
|
VolunteerSkill? vs = await context.VolunteerSkills.FirstOrDefaultAsync(v => v.UserId == user.UserId && v.SkillId == dto.Skill);
|
||||||
if (vs is not null)
|
if (vs is not null)
|
||||||
{
|
{
|
||||||
// Nie ma - zatem musimy dodać nowy VolunteerSkill do bazy
|
// Ma - zatem musimy usunąć otrzymany VolunteerSkill z bazy
|
||||||
VolunteerSkill newVs = dto.ToVolunteerSkillEntity(user.UserId);
|
VolunteerSkill newVs = dto.ToVolunteerSkillEntity(user.UserId);
|
||||||
|
|
||||||
|
|
||||||
await context.VolunteerSkills.Where(v => v.SkillId == dto.Skill)
|
await context.VolunteerSkills.Where(v => v.SkillId == dto.Skill)
|
||||||
.ExecuteDeleteAsync();
|
.ExecuteDeleteAsync();
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Ma - (ta para UserId <-> SkillId już istnieje w bazie) użytkownik już ma ten skill
|
// Nie ma - (ta para UserId <-> SkillId nie istnieje w bazie). Zwracamy błąd.
|
||||||
return Results.Json(new { message = "You don't have this skill" }, statusCode: 400);
|
return Results.Json(new { message = "You don't have this skill" }, statusCode: 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -222,6 +222,91 @@ namespace WebApp.Endpoints
|
|||||||
return Results.Ok(SearchResults);
|
return Results.Ok(SearchResults);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// POST /events/1/add_skill
|
||||||
|
group.MapPost("/{id}/add_skill/",
|
||||||
|
async (int id, SingleSkillDto dto, ApplicationDbContext dbContext, HttpContext httpContext, GeneralUseHelpers guhf) =>
|
||||||
|
{
|
||||||
|
Event? Eve = await dbContext.Events.FindAsync(id);
|
||||||
|
|
||||||
|
if (Eve is null) return Results.Json(new { message = "Event not found" }, statusCode: 404);
|
||||||
|
|
||||||
|
// Sprawdź, czy token należy do organizacji, a jeżeli tak, to do której.
|
||||||
|
Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
|
||||||
|
Organisation? org = await guhf.GetOrganisationFromToken(token);
|
||||||
|
|
||||||
|
// Jeśli token należy do organizacji, która utworzyła to wydarzenie,
|
||||||
|
// to zwróć także EventRegistrations. W przeciwnym razie usuń to pole
|
||||||
|
// przed jego wysłaniem!
|
||||||
|
if (org is null || org.OrganisationId != Eve.OrganisationId) return Results.Unauthorized();
|
||||||
|
|
||||||
|
// Szukamy skilla w bazie o ID takim, jak w otrzymanym DTO
|
||||||
|
Skill? skill = await dbContext.Skills.FindAsync(dto.Skill);
|
||||||
|
if (skill is null)
|
||||||
|
{
|
||||||
|
return Results.Json(new { message = "Skill not found" }, statusCode: 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sprawdzamy, czy to wydarzenie nie ma już takiego skilla. Jeżeli ma, nie ma sensu dodawać go kilkukrotnie.
|
||||||
|
EventSkill? es = await dbContext.EventSkills.FirstOrDefaultAsync(e => e.EventId == id && e.SkillId == dto.Skill);
|
||||||
|
if (es is null)
|
||||||
|
{
|
||||||
|
// Nie ma - zatem musimy dodać nowy EventSkill do bazy
|
||||||
|
EventSkill newEs = dto.ToEventSkillEntity(Eve.EventId);
|
||||||
|
dbContext.EventSkills.Add(newEs);
|
||||||
|
await dbContext.SaveChangesAsync();
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Ma - (ta para EventId <-> SkillId już istnieje w bazie); ten Event posiada już ten skill
|
||||||
|
return Results.Json(new { message = "Skill already assinged to this event!" }, statusCode: 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Results.Json(new { message = "Skill added to event successfully!" }, statusCode: 201);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
// POST /events/1/renive_skill
|
||||||
|
group.MapPost("/{id}/remove_skill/",
|
||||||
|
async (int id, SingleSkillDto dto, ApplicationDbContext dbContext, HttpContext httpContext, GeneralUseHelpers guhf) =>
|
||||||
|
{
|
||||||
|
Event? Eve = await dbContext.Events.FindAsync(id);
|
||||||
|
|
||||||
|
if (Eve is null) return Results.Json(new { message = "Event not found" }, statusCode: 404);
|
||||||
|
|
||||||
|
// Sprawdź, czy token należy do organizacji, a jeżeli tak, to do której.
|
||||||
|
Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
|
||||||
|
Organisation? org = await guhf.GetOrganisationFromToken(token);
|
||||||
|
|
||||||
|
// Jeśli token należy do organizacji, która utworzyła to wydarzenie,
|
||||||
|
// to zwróć także EventRegistrations. W przeciwnym razie usuń to pole
|
||||||
|
// przed jego wysłaniem!
|
||||||
|
if (org is null || org.OrganisationId != Eve.OrganisationId) return Results.Unauthorized();
|
||||||
|
|
||||||
|
// Szukamy skilla w bazie o ID takim, jak w otrzymanym DTO
|
||||||
|
Skill? skill = await dbContext.Skills.FindAsync(dto.Skill);
|
||||||
|
if (skill is null)
|
||||||
|
{
|
||||||
|
return Results.Json(new { message = "Skill not found" }, statusCode: 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sprawdzamy, czy to wydarzenie nie ma już takiego skilla. Jeżeli nie ma, to nie ma sensu kasować czegoś, czego nie ma.
|
||||||
|
EventSkill? es = await dbContext.EventSkills.FirstOrDefaultAsync(e => e.EventId == id && e.SkillId == dto.Skill);
|
||||||
|
if (es is not null)
|
||||||
|
{
|
||||||
|
// Ma - zatem musimy usunąć ten EventSkill z bazy
|
||||||
|
await dbContext.EventSkills.Where(e => e.SkillId == dto.Skill)
|
||||||
|
.ExecuteDeleteAsync();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Nie ma - (ta para EventId <-> SkillId nie istnieje w bazie); ten Event nie posiada tego skill'a
|
||||||
|
return Results.Json(new { message = "This skill isn't assinged to this event!" }, statusCode: 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Results.Json(new { message = "Skill removed from event successfully!" }, statusCode: 201);
|
||||||
|
});
|
||||||
|
|
||||||
return group;
|
return group;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
WebApp/Mapping/EventSkillMapping.cs
Normal file
16
WebApp/Mapping/EventSkillMapping.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
using WebApp.DTOs;
|
||||||
|
using WebApp.Entities;
|
||||||
|
|
||||||
|
namespace WebApp.Mapping;
|
||||||
|
|
||||||
|
public static class EventSkillMapping
|
||||||
|
{
|
||||||
|
public static EventSkill ToEventSkillEntity(this SingleSkillDto SSDto, int eid)
|
||||||
|
{
|
||||||
|
return new EventSkill()
|
||||||
|
{
|
||||||
|
EventId = eid,
|
||||||
|
SkillId = SSDto.Skill,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
using WebApp.DTOs;
|
using WebApp.DTOs;
|
||||||
using WebApp.Entities;
|
using WebApp.Entities;
|
||||||
|
|
||||||
namespace WebApp.Mapping
|
namespace WebApp.Mapping;
|
||||||
{
|
|
||||||
public static class VolunteerSkillMapping
|
public static class VolunteerSkillMapping
|
||||||
{
|
{
|
||||||
public static VolunteerSkill ToVolunteerSkillEntity(this SingleSkillDto SSDto, int uid)
|
public static VolunteerSkill ToVolunteerSkillEntity(this SingleSkillDto SSDto, int uid)
|
||||||
@@ -14,4 +14,3 @@ namespace WebApp.Mapping
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
@@ -74,6 +74,10 @@
|
|||||||
<span>Log in</span>
|
<span>Log in</span>
|
||||||
<span>⮞</span>
|
<span>⮞</span>
|
||||||
</button>
|
</button>
|
||||||
|
<button id="signUpBtn" class="button" type="button" onclick="alert('Coming soon!')">
|
||||||
|
<span>Sign up</span>
|
||||||
|
<span>⮞</span>
|
||||||
|
</button>
|
||||||
<p id="message" style="color: red;"></p>
|
<p id="message" style="color: red;"></p>
|
||||||
|
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
Reference in New Issue
Block a user