diff --git a/WebApp/Endpoints/AuthEndpoints.cs b/WebApp/Endpoints/AuthEndpoints.cs index 3c6dc1e..6e019ad 100644 --- a/WebApp/Endpoints/AuthEndpoints.cs +++ b/WebApp/Endpoints/AuthEndpoints.cs @@ -133,7 +133,7 @@ namespace WebApp.Endpoints Token? token = await guh.GetTokenFromHTTPContext(httpContext); 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) { return Results.Json(new { message = "Unauthorized" }, statusCode: 401); } @@ -170,7 +170,7 @@ namespace WebApp.Endpoints Token? token = await guh.GetTokenFromHTTPContext(httpContext); 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) { return Results.Json(new { message = "Unauthorized" }, statusCode: 401); @@ -183,13 +183,12 @@ namespace WebApp.Endpoints 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); 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); - await context.VolunteerSkills.Where(v => v.SkillId == dto.Skill) .ExecuteDeleteAsync(); @@ -197,7 +196,7 @@ namespace WebApp.Endpoints } 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); } diff --git a/WebApp/Endpoints/EventsEndpoints.cs b/WebApp/Endpoints/EventsEndpoints.cs index 87af920..cd099ff 100644 --- a/WebApp/Endpoints/EventsEndpoints.cs +++ b/WebApp/Endpoints/EventsEndpoints.cs @@ -222,6 +222,91 @@ namespace WebApp.Endpoints 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; } } diff --git a/WebApp/Mapping/EventSkillMapping.cs b/WebApp/Mapping/EventSkillMapping.cs new file mode 100644 index 0000000..2dc0e2f --- /dev/null +++ b/WebApp/Mapping/EventSkillMapping.cs @@ -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, + }; + } +} diff --git a/WebApp/Mapping/VolunteerSkillMapping.cs b/WebApp/Mapping/VolunteerSkillMapping.cs index a92198a..1ac2c37 100644 --- a/WebApp/Mapping/VolunteerSkillMapping.cs +++ b/WebApp/Mapping/VolunteerSkillMapping.cs @@ -1,17 +1,16 @@ -using WebApp.DTOs; +using WebApp.DTOs; 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) + return new VolunteerSkill() { - return new VolunteerSkill() - { - UserId = uid, - SkillId = SSDto.Skill, - }; - } + UserId = uid, + SkillId = SSDto.Skill, + }; } } diff --git a/WebApp/wwwroot/login.html b/WebApp/wwwroot/login.html index 7bea6e5..be138df 100644 --- a/WebApp/wwwroot/login.html +++ b/WebApp/wwwroot/login.html @@ -1,4 +1,4 @@ - + @@ -68,12 +68,16 @@ -
+
+