3 Commits

Author SHA1 Message Date
32027f7384 feat: add first volunteer skill endpoint (add_skill) along with dtos 2025-05-31 18:19:15 +02:00
AleksDw
2a8fff39c9 Fix logout so it deletes token in database 2025-05-31 14:37:06 +02:00
AleksDw
b194819b6e Add login/logout in every page 2025-05-31 14:21:02 +02:00
9 changed files with 137 additions and 22 deletions

View File

@@ -0,0 +1,8 @@
using System.ComponentModel.DataAnnotations;
namespace WebApp.DTOs;
public record class SingleSkillDto
(
[Required] int Skill
);

View File

@@ -0,0 +1,9 @@
using System.ComponentModel.DataAnnotations;
namespace WebApp.DTOs;
public record class SkillSummaryDto
(
[Required] int SkillId,
[Required] string SkillName
);

View File

@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.Runtime.Intrinsics.Arm;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using WebApp.Data; 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; return group;
} }

View 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
);
}
}
}

View 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,
};
}
}
}

View File

@@ -4,18 +4,17 @@ function deleteCookie(name: string): void {
document.cookie = `${name}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`; document.cookie = `${name}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`;
} }
function logoutUser(): void { async function logoutUser(): Promise<void> {
// Inform backend to remove cookie if necessary await fetch("/api/auth/logout", {
fetch('/api/logout', { method: "POST",
method: 'POST', headers: {
credentials: 'include', "Content-Type": "application/json",
}).catch((err) => console.warn('Logout request failed:', err)); },
});
// Clear the auth cookie deleteCookie('token');
deleteCookie('token');
// Redirect to login page window.location.href = "/index.html";
window.location.href = 'index.html';
} }
function redirectToLogin(): void { function redirectToLogin(): void {
@@ -46,7 +45,10 @@ function setupAuthUI(): void {
if (logoutBtn) { if (logoutBtn) {
logoutBtn.classList.toggle('d-none', !isAuthenticated); logoutBtn.classList.toggle('d-none', !isAuthenticated);
logoutBtn.addEventListener('click', logoutUser); logoutBtn.addEventListener('click', (e) => {
e.preventDefault();
logoutUser();
});
} }
} }

View File

@@ -80,6 +80,8 @@
<script type="module" src="/js/eventCreate.js"></script> <script type="module" src="/js/eventCreate.js"></script>
<script type="module" src="/js/generalUseHelpers.js"></script> <script type="module" src="/js/generalUseHelpers.js"></script>
<script type="module" src="/js/auth.js"></script>
</body> </body>

View File

@@ -1,18 +1,28 @@
"use strict"; "use strict";
// /js/auth.ts // /js/auth.ts
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
function deleteCookie(name) { function deleteCookie(name) {
document.cookie = `${name}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`; document.cookie = `${name}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`;
} }
function logoutUser() { function logoutUser() {
// Inform backend to remove cookie if necessary return __awaiter(this, void 0, void 0, function* () {
fetch('/api/logout', { yield fetch("/api/auth/logout", {
method: 'POST', method: "POST",
credentials: 'include', headers: {
}).catch((err) => console.warn('Logout request failed:', err)); "Content-Type": "application/json",
// Clear the auth cookie },
deleteCookie('token'); });
// Redirect to login page deleteCookie('token');
window.location.href = 'index.html'; window.location.href = "/index.html";
});
} }
function redirectToLogin() { function redirectToLogin() {
window.location.href = 'login.html'; window.location.href = 'login.html';
@@ -36,7 +46,10 @@ function setupAuthUI() {
} }
if (logoutBtn) { if (logoutBtn) {
logoutBtn.classList.toggle('d-none', !isAuthenticated); logoutBtn.classList.toggle('d-none', !isAuthenticated);
logoutBtn.addEventListener('click', logoutUser); logoutBtn.addEventListener('click', (e) => {
e.preventDefault();
logoutUser();
});
} }
} }
// Initialize on load // Initialize on load

View File

@@ -61,7 +61,7 @@
<h2 id="locationText">Place: 127.0.0.1</h2> <h2 id="locationText">Place: 127.0.0.1</h2>
<h2 id="dateText">When: now or never!</h2> <h2 id="dateText">When: now or never!</h2>
<h3>Description:</h3> <h3>Description:</h3>
<h4 id="descText"></h4><br/> <h4 id="descText"></h4><br />
<button id="applyBtn" class="button hidden-before-load"><span>Apply</span><span>&#11166;</span></button> <button id="applyBtn" class="button hidden-before-load"><span>Apply</span><span>&#11166;</span></button>
<button id="editBtn" class="button hidden-before-load"><span>Modify</span><span>&#11166;</span></button> <button id="editBtn" class="button hidden-before-load"><span>Modify</span><span>&#11166;</span></button>
@@ -71,6 +71,8 @@
<script type="module" src="/js/eventView.js"></script> <script type="module" src="/js/eventView.js"></script>
<script type="module" src="/js/generalUseHelpers.js"></script> <script type="module" src="/js/generalUseHelpers.js"></script>
<script type="module" src="/js/auth.js"></script>
</body> </body>