mirror of
https://github.com/GCMatters/hermes.git
synced 2026-02-04 21:50:12 +01:00
Compare commits
2 Commits
26635b4e88
...
MyBranchKa
| Author | SHA1 | Date | |
|---|---|---|---|
| 426288d728 | |||
| 72fbfe982f |
@@ -1,11 +0,0 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
|
||||||
using WebApp.Entities;
|
|
||||||
|
|
||||||
namespace WebApp.DTOs;
|
|
||||||
|
|
||||||
// Output values in JSON file
|
|
||||||
public record class EventRegistrationDto(
|
|
||||||
int EventId,
|
|
||||||
int UserId,
|
|
||||||
DateTime RegisteredAt
|
|
||||||
);
|
|
||||||
@@ -18,6 +18,7 @@ namespace WebApp.Endpoints
|
|||||||
var group = app.MapGroup("api/auth")
|
var group = app.MapGroup("api/auth")
|
||||||
.WithParameterValidation();
|
.WithParameterValidation();
|
||||||
|
|
||||||
|
// POST /api/auth/login
|
||||||
group.MapPost("/login", async (LoginDto dto, ApplicationDbContext context, GeneralUseHelpers guh) =>
|
group.MapPost("/login", async (LoginDto dto, ApplicationDbContext context, GeneralUseHelpers guh) =>
|
||||||
{
|
{
|
||||||
var user = await context.WebUsers.FirstOrDefaultAsync(u => u.Email == dto.Email);
|
var user = await context.WebUsers.FirstOrDefaultAsync(u => u.Email == dto.Email);
|
||||||
@@ -38,6 +39,7 @@ namespace WebApp.Endpoints
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// POST /api/auth/logout
|
||||||
group.MapPost("/logout", async (HttpContext httpContext, GeneralUseHelpers guh) =>
|
group.MapPost("/logout", async (HttpContext httpContext, GeneralUseHelpers guh) =>
|
||||||
{
|
{
|
||||||
var token = await guh.GetTokenFromHTTPContext(httpContext);
|
var token = await guh.GetTokenFromHTTPContext(httpContext);
|
||||||
@@ -54,6 +56,7 @@ namespace WebApp.Endpoints
|
|||||||
return Results.Ok(new { success = true });
|
return Results.Ok(new { success = true });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// GET /api/auth/my_account
|
||||||
group.MapGet("/my_account", async (HttpContext httpContext, GeneralUseHelpers guh) =>
|
group.MapGet("/my_account", async (HttpContext httpContext, GeneralUseHelpers guh) =>
|
||||||
{
|
{
|
||||||
var token = await guh.GetTokenFromHTTPContext(httpContext);
|
var token = await guh.GetTokenFromHTTPContext(httpContext);
|
||||||
@@ -77,6 +80,7 @@ namespace WebApp.Endpoints
|
|||||||
})
|
})
|
||||||
.WithName(GetUserEndpointName);
|
.WithName(GetUserEndpointName);
|
||||||
|
|
||||||
|
// GET /api/auth/my_events
|
||||||
group.MapGet("/my_events", async (HttpContext httpContext, GeneralUseHelpers guh, ApplicationDbContext context) =>
|
group.MapGet("/my_events", async (HttpContext httpContext, GeneralUseHelpers guh, ApplicationDbContext context) =>
|
||||||
{
|
{
|
||||||
var token = await guh.GetTokenFromHTTPContext(httpContext);
|
var token = await guh.GetTokenFromHTTPContext(httpContext);
|
||||||
@@ -95,16 +99,9 @@ namespace WebApp.Endpoints
|
|||||||
|
|
||||||
if(!user.IsOrganisation)
|
if(!user.IsOrganisation)
|
||||||
{
|
{
|
||||||
|
var events = await context.EventRegistrations
|
||||||
var eventIds = await context.EventRegistrations
|
|
||||||
.Where(er => er.UserId == user.UserId)
|
.Where(er => er.UserId == user.UserId)
|
||||||
.Select(er => er.EventId)
|
.Select(er => er.Event.ToEventSummaryNoErDto())
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
var events = await context.Events
|
|
||||||
.Where(e => eventIds.Contains(e.EventId))
|
|
||||||
.Include(e => e.Organisation)
|
|
||||||
.Select(e => e.ToEventSummaryDto())
|
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
return Results.Ok(events);
|
return Results.Ok(events);
|
||||||
@@ -129,13 +126,14 @@ namespace WebApp.Endpoints
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// POST /api/auth/add_skill
|
||||||
group.MapPost("/add_skill", async (SingleSkillDto dto, HttpContext httpContext, ApplicationDbContext context, GeneralUseHelpers guh) =>
|
group.MapPost("/add_skill", async (SingleSkillDto dto, HttpContext httpContext, ApplicationDbContext context, GeneralUseHelpers guh) =>
|
||||||
{
|
{
|
||||||
// Uzyskaj użytkownika z tokenu
|
// Uzyskaj użytkownika z tokenu
|
||||||
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);
|
||||||
}
|
}
|
||||||
@@ -165,14 +163,14 @@ namespace WebApp.Endpoints
|
|||||||
return Results.Json(new { message = "Skill added successfully!" }, statusCode: 201);
|
return Results.Json(new { message = "Skill added successfully!" }, statusCode: 201);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// POST /api/auth/remove_skill
|
||||||
group.MapPost("/remove_skill", async (SingleSkillDto dto, HttpContext httpContext, ApplicationDbContext context, GeneralUseHelpers guh) =>
|
group.MapPost("/remove_skill", async (SingleSkillDto dto, HttpContext httpContext, ApplicationDbContext context, GeneralUseHelpers guh) =>
|
||||||
{
|
{
|
||||||
// Uzyskaj użytkownika z tokenu
|
// Uzyskaj użytkownika z tokenu
|
||||||
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);
|
||||||
@@ -185,13 +183,12 @@ 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();
|
||||||
@@ -199,14 +196,15 @@ namespace WebApp.Endpoints
|
|||||||
}
|
}
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Results.Json(new { message = "Skill deleted successfully!" }, statusCode: 201);
|
return Results.Json(new { message = "Skill deleted successfully!" }, statusCode: 201);
|
||||||
});
|
});
|
||||||
|
|
||||||
group.MapGet("/get_skills", async (HttpContext httpContext, ApplicationDbContext context, GeneralUseHelpers guh) =>
|
// GET /api/auth/skills
|
||||||
|
group.MapGet("/skills", async (HttpContext httpContext, ApplicationDbContext context, GeneralUseHelpers guh) =>
|
||||||
{
|
{
|
||||||
// Uzyskaj użytkownika z tokenu
|
// Uzyskaj użytkownika z tokenu
|
||||||
Token? token = await guh.GetTokenFromHTTPContext(httpContext);
|
Token? token = await guh.GetTokenFromHTTPContext(httpContext);
|
||||||
|
|||||||
@@ -1,135 +0,0 @@
|
|||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.Http.HttpResults;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using System.Security.Cryptography;
|
|
||||||
using WebApp.Data;
|
|
||||||
using WebApp.DTOs;
|
|
||||||
using WebApp.Entities;
|
|
||||||
using WebApp.Mapping;
|
|
||||||
|
|
||||||
namespace WebApp.Endpoints
|
|
||||||
{
|
|
||||||
public static class EventsRegistrationEndpoints
|
|
||||||
{
|
|
||||||
const string GetEventEndpointRegistrationName = "GetEventRegistration";
|
|
||||||
|
|
||||||
public static RouteGroupBuilder MapEventsRegistrationEndpoints(this WebApplication app)
|
|
||||||
{
|
|
||||||
var group = app.MapGroup("api/events")
|
|
||||||
.WithParameterValidation();
|
|
||||||
|
|
||||||
// POST /api/events/join/{id}
|
|
||||||
group.MapPost("/join/{id}",
|
|
||||||
async (int id, ApplicationDbContext dbContext, HttpContext httpContext, GeneralUseHelpers guhf) =>
|
|
||||||
{
|
|
||||||
Event? Eve = await dbContext.Events.FindAsync(id);
|
|
||||||
if (Eve is null)
|
|
||||||
return Results.Json(new { success = false, error_msg = "Event not found." });
|
|
||||||
|
|
||||||
Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
|
|
||||||
User? user = await guhf.GetUserFromToken(token);
|
|
||||||
|
|
||||||
if (user is null || user.IsOrganisation)
|
|
||||||
return Results.Json(new { success = false, error_msg = "Unauthorized or organisations cannot register for events." });
|
|
||||||
|
|
||||||
if (await dbContext.EventRegistrations.AnyAsync(er => er.UserId == user.UserId && er.EventId == id))
|
|
||||||
return Results.Json(new { success = false, error_msg = "You are already registered for this event." });
|
|
||||||
|
|
||||||
if (Eve.EventDate < DateTime.UtcNow)
|
|
||||||
return Results.Json(new { success = false, error_msg = "This event has already ended." });
|
|
||||||
|
|
||||||
EventRegistration registration = new EventRegistration
|
|
||||||
{
|
|
||||||
UserId = user.UserId,
|
|
||||||
EventId = id,
|
|
||||||
RegisteredAt = DateTime.UtcNow
|
|
||||||
};
|
|
||||||
dbContext.EventRegistrations.Add(registration);
|
|
||||||
await dbContext.SaveChangesAsync();
|
|
||||||
|
|
||||||
return Results.Json(new { success = true });
|
|
||||||
});
|
|
||||||
|
|
||||||
// POST /api/events/leave/{id}
|
|
||||||
group.MapPost("/leave/{id}",
|
|
||||||
async (int id, ApplicationDbContext dbContext, HttpContext httpContext, GeneralUseHelpers guhf) =>
|
|
||||||
{
|
|
||||||
Event? Eve = await dbContext.Events.FindAsync(id);
|
|
||||||
if (Eve is null)
|
|
||||||
return Results.Json(new { success = false, error_msg = "Event not found." });
|
|
||||||
|
|
||||||
Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
|
|
||||||
User? user = await guhf.GetUserFromToken(token);
|
|
||||||
|
|
||||||
if (user is null)
|
|
||||||
return Results.Json(new { success = false, error_msg = "Unauthorized." });
|
|
||||||
|
|
||||||
if (!await dbContext.EventRegistrations.AnyAsync(er => er.UserId == user.UserId && er.EventId == id))
|
|
||||||
return Results.Json(new { success = false, error_msg = "You are not registered for this event." });
|
|
||||||
|
|
||||||
if (Eve.EventDate < DateTime.UtcNow)
|
|
||||||
return Results.Json(new { success = false, error_msg = "This event has already ended." });
|
|
||||||
|
|
||||||
EventRegistration? registration = await dbContext.EventRegistrations
|
|
||||||
.FirstOrDefaultAsync(er => er.UserId == user.UserId && er.EventId == id);
|
|
||||||
|
|
||||||
dbContext.EventRegistrations.Remove(registration);
|
|
||||||
await dbContext.SaveChangesAsync();
|
|
||||||
|
|
||||||
return Results.Json(new { success = true });
|
|
||||||
});
|
|
||||||
|
|
||||||
// GET /api/events/registrations/{id}
|
|
||||||
group.MapGet("/registrations/{id}",
|
|
||||||
async (int id, ApplicationDbContext dbContext, HttpContext httpContext, GeneralUseHelpers guhf) =>
|
|
||||||
{
|
|
||||||
Event? Eve = await dbContext.Events.FindAsync(id);
|
|
||||||
if (Eve is null)
|
|
||||||
return Results.Json(new { success = false, error_msg = "Event not found." });
|
|
||||||
|
|
||||||
Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
|
|
||||||
Organisation? org = await guhf.GetOrganisationFromToken(token);
|
|
||||||
if (org is null || org.OrganisationId != Eve.OrganisationId)
|
|
||||||
return Results.Json(new { success = false, error_msg = "Unauthorized." });
|
|
||||||
|
|
||||||
var registrations = await dbContext.EventRegistrations
|
|
||||||
.Where(er => er.EventId == id)
|
|
||||||
.Select(er => er.ToEventRegistrationDto())
|
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
return Results.Json(new
|
|
||||||
{
|
|
||||||
success = true,
|
|
||||||
registrations
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// POST /api/events/remove/{id}/{userId}
|
|
||||||
group.MapPost("/remove/{id}/{userId}",
|
|
||||||
async (int id, int userId, ApplicationDbContext dbContext, HttpContext httpContext, GeneralUseHelpers guhf) =>
|
|
||||||
{
|
|
||||||
Event? Eve = await dbContext.Events.FindAsync(id);
|
|
||||||
if (Eve is null)
|
|
||||||
return Results.Json(new { success = false, error_msg = "Event not found." });
|
|
||||||
|
|
||||||
Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
|
|
||||||
Organisation? org = await guhf.GetOrganisationFromToken(token);
|
|
||||||
if (org is null || org.OrganisationId != Eve.OrganisationId)
|
|
||||||
return Results.Json(new { success = false, error_msg = "Unauthorized." });
|
|
||||||
|
|
||||||
EventRegistration? registration = await dbContext.EventRegistrations
|
|
||||||
.FirstOrDefaultAsync(er => er.UserId == userId && er.EventId == id);
|
|
||||||
|
|
||||||
if (registration is null)
|
|
||||||
return Results.Json(new { success = false, error_msg = "Registration not found." });
|
|
||||||
|
|
||||||
dbContext.EventRegistrations.Remove(registration);
|
|
||||||
await dbContext.SaveChangesAsync();
|
|
||||||
|
|
||||||
return Results.Json(new { success = true });
|
|
||||||
});
|
|
||||||
|
|
||||||
return group;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using WebApp.Data;
|
using WebApp.Data;
|
||||||
using WebApp.DTOs;
|
using WebApp.DTOs;
|
||||||
@@ -134,7 +134,7 @@ namespace WebApp.Endpoints
|
|||||||
// Uzyskaj organizację z tokenu
|
// Uzyskaj organizację z tokenu
|
||||||
Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
|
Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
|
||||||
Organisation? org = await guhf.GetOrganisationFromToken(token);
|
Organisation? org = await guhf.GetOrganisationFromToken(token);
|
||||||
if (org is null) return Results.StatusCode(403);
|
if (org is null) return Results.Unauthorized();
|
||||||
|
|
||||||
// Sprawdź, czy organizacja ma prawo
|
// Sprawdź, czy organizacja ma prawo
|
||||||
// do usunięcia tego (EventId = id) eventu.
|
// do usunięcia tego (EventId = id) eventu.
|
||||||
@@ -160,7 +160,6 @@ namespace WebApp.Endpoints
|
|||||||
Organisation? org = await guhf.GetOrganisationFromToken(token);
|
Organisation? org = await guhf.GetOrganisationFromToken(token);
|
||||||
List<EventSummaryDto> SearchResults = [];
|
List<EventSummaryDto> SearchResults = [];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
List<Event> AllEvents = await dbContext.Events.ToListAsync();
|
List<Event> AllEvents = await dbContext.Events.ToListAsync();
|
||||||
if (sort is null || sort.ToUpper() != "ASC")
|
if (sort is null || sort.ToUpper() != "ASC")
|
||||||
@@ -223,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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
26
WebApp/Endpoints/SkillsEndpoints.cs
Normal file
26
WebApp/Endpoints/SkillsEndpoints.cs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using WebApp.Data;
|
||||||
|
using WebApp.Mapping;
|
||||||
|
|
||||||
|
namespace WebApp.Endpoints;
|
||||||
|
|
||||||
|
public static class SkillsEndpoints
|
||||||
|
{
|
||||||
|
const string GetSkillEndpointName = "GetSkill";
|
||||||
|
|
||||||
|
public static RouteGroupBuilder MapSkillsEndpoints(this WebApplication app)
|
||||||
|
{
|
||||||
|
var group = app.MapGroup("api/skills").WithParameterValidation();
|
||||||
|
|
||||||
|
// GET /skills
|
||||||
|
group.MapGet("/",
|
||||||
|
async (ApplicationDbContext dbContext) =>
|
||||||
|
await dbContext.Skills
|
||||||
|
.OrderBy(Sk => Sk.SkillId)
|
||||||
|
.Select(Sk => Sk.ToSkillSummaryDto()) // SkillSummaryDto
|
||||||
|
.AsNoTracking()
|
||||||
|
.ToListAsync());
|
||||||
|
|
||||||
|
return group;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
using WebApp.DTOs;
|
|
||||||
using WebApp.Entities;
|
|
||||||
|
|
||||||
namespace WebApp.Mapping
|
|
||||||
{
|
|
||||||
public static class EventRegistrationMapping
|
|
||||||
{
|
|
||||||
public static EventRegistrationDto ToEventRegistrationDto(this EventRegistration er)
|
|
||||||
{
|
|
||||||
return new EventRegistrationDto(
|
|
||||||
er.EventId,
|
|
||||||
er.UserId,
|
|
||||||
er.RegisteredAt
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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,17 +1,16 @@
|
|||||||
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)
|
return new VolunteerSkill()
|
||||||
{
|
{
|
||||||
return new VolunteerSkill()
|
UserId = uid,
|
||||||
{
|
SkillId = SSDto.Skill,
|
||||||
UserId = uid,
|
};
|
||||||
SkillId = SSDto.Skill,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,6 +53,6 @@ app.UseRouting(); // Enables routing to match incoming request to endpoints
|
|||||||
app.MapEventsEndpoints();
|
app.MapEventsEndpoints();
|
||||||
app.MapOrganizationsEndpoints();
|
app.MapOrganizationsEndpoints();
|
||||||
app.MapAuthEndpoints();
|
app.MapAuthEndpoints();
|
||||||
app.MapEventsRegistrationEndpoints();
|
app.MapSkillsEndpoints();
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { getEvent, getMyAccount, unhideElementById, getMyRegisteredEventIds } from './generalUseHelpers.js';
|
import { getEvent, getMyAccount, unhideElementById } from './generalUseHelpers.js';
|
||||||
|
|
||||||
const queryString = window.location.search;
|
const queryString = window.location.search;
|
||||||
const urlParams = new URLSearchParams(queryString);
|
const urlParams = new URLSearchParams(queryString);
|
||||||
@@ -9,8 +9,6 @@ document.addEventListener("DOMContentLoaded", async () => {
|
|||||||
var container = document.getElementById("mainContainer");
|
var container = document.getElementById("mainContainer");
|
||||||
const modifyBtn = document.getElementById("editBtn");
|
const modifyBtn = document.getElementById("editBtn");
|
||||||
const removeBtn = document.getElementById("removeBtn");
|
const removeBtn = document.getElementById("removeBtn");
|
||||||
const applyBtn = document.getElementById("applyBtn");
|
|
||||||
const leaveBtn = document.getElementById("leaveBtn");
|
|
||||||
var org_id: number = -1;
|
var org_id: number = -1;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -30,11 +28,11 @@ document.addEventListener("DOMContentLoaded", async () => {
|
|||||||
try {
|
try {
|
||||||
if (eventId) thisEvent = await getEvent(eventId);
|
if (eventId) thisEvent = await getEvent(eventId);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (container !== null) container.innerHTML = `<p class="text-danger">To wydarzenie nie istnieje! <a href="/" style="color:#2898BD;">Powr<EFBFBD>t -></a></p>`;
|
if (container !== null) container.innerHTML = `<p class="text-danger">To wydarzenie nie istnieje! <a href="/" style="color:#2898BD;">Powrót -></a></p>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (thisEvent == null) {
|
if (thisEvent == null) {
|
||||||
if (container !== null) container.innerHTML = `<p class="text-danger">Błąd we wczytywaniu wydarzenia. <a href="/" style="color:#2898BD;">Powrót -></a></p>`;
|
if (container !== null) container.innerHTML = `<p class="text-danger">B³¹d we wczytywaniu wydarzenia. <a href="/" style="color:#2898BD;">Powrót -></a></p>`;
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
const titleText = document.getElementById( "titleText") as HTMLElement;
|
const titleText = document.getElementById( "titleText") as HTMLElement;
|
||||||
@@ -53,20 +51,13 @@ document.addEventListener("DOMContentLoaded", async () => {
|
|||||||
organizerText.innerHTML = "Organized by: " + thisEvent.organisationName;
|
organizerText.innerHTML = "Organized by: " + thisEvent.organisationName;
|
||||||
|
|
||||||
if (org_id == thisEvent.organisationId) {
|
if (org_id == thisEvent.organisationId) {
|
||||||
// Użytkownik jest organizacją, która
|
// U¿ytkownik jest organizacj¹, która
|
||||||
// stworzyła to wydarzenie
|
// stworzy³a to wydarzenie
|
||||||
unhideElementById(document, "editBtn");
|
unhideElementById(document, "editBtn");
|
||||||
unhideElementById(document, "removeBtn");
|
unhideElementById(document, "removeBtn");
|
||||||
} else if (org_id == -1) {
|
} else if (org_id == -1) {
|
||||||
// Użytkownik jest wolontariuszem
|
// U¿ytkownik jest wolontariuszem
|
||||||
const registeredIds = await getMyRegisteredEventIds();
|
unhideElementById(document, "applyBtn");
|
||||||
const isRegistered = registeredIds.includes(Number(eventId));
|
|
||||||
|
|
||||||
if (isRegistered) {
|
|
||||||
unhideElementById(document, "leaveBtn");
|
|
||||||
} else {
|
|
||||||
unhideElementById(document, "applyBtn");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unhideElementById(document, "mainContainer");
|
unhideElementById(document, "mainContainer");
|
||||||
@@ -85,7 +76,7 @@ document.addEventListener("DOMContentLoaded", async () => {
|
|||||||
if (!confirmed) return;
|
if (!confirmed) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Wysyła żądanie DELETE do API
|
// Wysy³a ¿¹danie DELETE do API
|
||||||
const response = await fetch(`/api/events/${eventId}`, {
|
const response = await fetch(`/api/events/${eventId}`, {
|
||||||
method: "DELETE"
|
method: "DELETE"
|
||||||
});
|
});
|
||||||
@@ -103,59 +94,4 @@ document.addEventListener("DOMContentLoaded", async () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (applyBtn) {
|
});
|
||||||
applyBtn.addEventListener("click", async (e) => {
|
|
||||||
try {
|
|
||||||
const response = await fetch(`/api/events/join/${eventId}`, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json"
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const result: {
|
|
||||||
success: boolean;
|
|
||||||
error_msg?: string;
|
|
||||||
} = await response.json();
|
|
||||||
|
|
||||||
if (result.success) {
|
|
||||||
window.location.href = `/view.html?event=${eventId}`;
|
|
||||||
} else {
|
|
||||||
alert(`Error: ${result.error_msg ?? "Unknown error occurred."}`);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Failed to apply:", error);
|
|
||||||
alert("Failed to apply.");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (leaveBtn) {
|
|
||||||
leaveBtn.addEventListener("click", async (e) => {
|
|
||||||
try {
|
|
||||||
const response = await fetch(`/api/events/leave/${eventId}`, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json"
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const result: {
|
|
||||||
success: boolean;
|
|
||||||
error_msg?: string;
|
|
||||||
} = await response.json();
|
|
||||||
|
|
||||||
if (result.success) {
|
|
||||||
window.location.href = `/view.html?event=${eventId}`;
|
|
||||||
} else {
|
|
||||||
alert(`Error: ${result.error_msg ?? "Unknown error occurred."}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Failed to leave:", error)
|
|
||||||
alert("Failed to leave.")
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
@@ -36,20 +36,9 @@ export async function getEvent(id: string): Promise<EventData> {
|
|||||||
export async function getMyAccount(): Promise<MyAccount> {
|
export async function getMyAccount(): Promise<MyAccount> {
|
||||||
const res = await fetch("/api/auth/my_account");
|
const res = await fetch("/api/auth/my_account");
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
throw Error("U<EFBFBD>ytkownik niezalogowany!");
|
throw Error("U¿ytkownik niezalogowany!");
|
||||||
}
|
}
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getMyRegisteredEventIds(): Promise<number[]> {
|
|
||||||
const res = await fetch("/api/auth/my_events");
|
|
||||||
|
|
||||||
if (!res.ok) {
|
|
||||||
throw Error("Użytkownik niezalogowany!");
|
|
||||||
}
|
|
||||||
|
|
||||||
const events = await res.json();
|
|
||||||
|
|
||||||
return events.map((event: { eventId: number }) => event.eventId);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|||||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
import { getEvent, getMyAccount, unhideElementById, getMyRegisteredEventIds } from './generalUseHelpers.js';
|
import { getEvent, getMyAccount, unhideElementById } from './generalUseHelpers.js';
|
||||||
const queryString = window.location.search;
|
const queryString = window.location.search;
|
||||||
const urlParams = new URLSearchParams(queryString);
|
const urlParams = new URLSearchParams(queryString);
|
||||||
const eventId = urlParams.get('event');
|
const eventId = urlParams.get('event');
|
||||||
@@ -15,8 +15,6 @@ document.addEventListener("DOMContentLoaded", () => __awaiter(void 0, void 0, vo
|
|||||||
var container = document.getElementById("mainContainer");
|
var container = document.getElementById("mainContainer");
|
||||||
const modifyBtn = document.getElementById("editBtn");
|
const modifyBtn = document.getElementById("editBtn");
|
||||||
const removeBtn = document.getElementById("removeBtn");
|
const removeBtn = document.getElementById("removeBtn");
|
||||||
const applyBtn = document.getElementById("applyBtn");
|
|
||||||
const leaveBtn = document.getElementById("leaveBtn");
|
|
||||||
var org_id = -1;
|
var org_id = -1;
|
||||||
try {
|
try {
|
||||||
var user = yield getMyAccount();
|
var user = yield getMyAccount();
|
||||||
@@ -38,7 +36,7 @@ document.addEventListener("DOMContentLoaded", () => __awaiter(void 0, void 0, vo
|
|||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
if (container !== null)
|
if (container !== null)
|
||||||
container.innerHTML = `<p class="text-danger">To wydarzenie nie istnieje! <a href="/" style="color:#2898BD;">Powr<EFBFBD>t -></a></p>`;
|
container.innerHTML = `<p class="text-danger">To wydarzenie nie istnieje! <a href="/" style="color:#2898BD;">Powrót -></a></p>`;
|
||||||
}
|
}
|
||||||
if (thisEvent == null) {
|
if (thisEvent == null) {
|
||||||
if (container !== null)
|
if (container !== null)
|
||||||
@@ -65,14 +63,7 @@ document.addEventListener("DOMContentLoaded", () => __awaiter(void 0, void 0, vo
|
|||||||
}
|
}
|
||||||
else if (org_id == -1) {
|
else if (org_id == -1) {
|
||||||
// Użytkownik jest wolontariuszem
|
// Użytkownik jest wolontariuszem
|
||||||
const registeredIds = yield getMyRegisteredEventIds();
|
unhideElementById(document, "applyBtn");
|
||||||
const isRegistered = registeredIds.includes(Number(eventId));
|
|
||||||
if (isRegistered) {
|
|
||||||
unhideElementById(document, "leaveBtn");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
unhideElementById(document, "applyBtn");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
unhideElementById(document, "mainContainer");
|
unhideElementById(document, "mainContainer");
|
||||||
}
|
}
|
||||||
@@ -105,52 +96,4 @@ document.addEventListener("DOMContentLoaded", () => __awaiter(void 0, void 0, vo
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
if (applyBtn) {
|
|
||||||
applyBtn.addEventListener("click", (e) => __awaiter(void 0, void 0, void 0, function* () {
|
|
||||||
var _b;
|
|
||||||
try {
|
|
||||||
const response = yield fetch(`/api/events/join/${eventId}`, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json"
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const result = yield response.json();
|
|
||||||
if (result.success) {
|
|
||||||
window.location.href = `/view.html?event=${eventId}`;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
alert(`Error: ${(_b = result.error_msg) !== null && _b !== void 0 ? _b : "Unknown error occurred."}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (error) {
|
|
||||||
console.error("Failed to apply:", error);
|
|
||||||
alert("Failed to apply.");
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
if (leaveBtn) {
|
|
||||||
leaveBtn.addEventListener("click", (e) => __awaiter(void 0, void 0, void 0, function* () {
|
|
||||||
var _c;
|
|
||||||
try {
|
|
||||||
const response = yield fetch(`/api/events/leave/${eventId}`, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json"
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const result = yield response.json();
|
|
||||||
if (result.success) {
|
|
||||||
window.location.href = `/view.html?event=${eventId}`;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
alert(`Error: ${(_c = result.error_msg) !== null && _c !== void 0 ? _c : "Unknown error occurred."}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (error) {
|
|
||||||
console.error("Failed to leave:", error);
|
|
||||||
alert("Failed to leave.");
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}));
|
}));
|
||||||
|
|||||||
@@ -35,13 +35,3 @@ export function getMyAccount() {
|
|||||||
return data;
|
return data;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
export function getMyRegisteredEventIds() {
|
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
|
||||||
const res = yield fetch("/api/auth/my_events");
|
|
||||||
if (!res.ok) {
|
|
||||||
throw Error("Użytkownik niezalogowany!");
|
|
||||||
}
|
|
||||||
const events = yield res.json();
|
|
||||||
return events.map((event) => event.eventId);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
@@ -68,12 +68,16 @@
|
|||||||
<input type="password" id="password" class="form-control input-field" required />
|
<input type="password" id="password" class="form-control input-field" required />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<br/>
|
<br />
|
||||||
|
|
||||||
<button id="logInBtn" class="button" type="submit">
|
<button id="logInBtn" class="button" type="submit">
|
||||||
<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>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="pl">
|
<html lang="pl">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
@@ -64,7 +64,6 @@
|
|||||||
<h4 id="descText"></h4><br />
|
<h4 id="descText"></h4><br />
|
||||||
|
|
||||||
<button id="applyBtn" class="button hidden-before-load"><span>Apply</span><span>⮞</span></button>
|
<button id="applyBtn" class="button hidden-before-load"><span>Apply</span><span>⮞</span></button>
|
||||||
<button id="leaveBtn" class="button hidden-before-load"><span>Leave</span><span>⮞</span></button>
|
|
||||||
<button id="editBtn" class="button hidden-before-load"><span>Modify</span><span>⮞</span></button>
|
<button id="editBtn" class="button hidden-before-load"><span>Modify</span><span>⮞</span></button>
|
||||||
<button id="removeBtn" class="button hidden-before-load" style="background-color: red;"><span>Remove permanently</span><span>⮞</span></button>
|
<button id="removeBtn" class="button hidden-before-load" style="background-color: red;"><span>Remove permanently</span><span>⮞</span></button>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user