mirror of
https://github.com/danbulant/Portfolio
synced 2026-05-24 12:35:31 +00:00
feat: admin API
This commit is contained in:
parent
87c04c8602
commit
2d7ed46cf3
3 changed files with 99 additions and 0 deletions
79
frontend/src/lib/@api/admin.ts
Normal file
79
frontend/src/lib/@api/admin.ts
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
import type { AdminLogin } from "$lib/stores/admin";
|
||||
import type { CandidateData, CandidatePreview, CreateCandidate, CreateCandidateLogin } from "$lib/stores/candidate";
|
||||
import axios from "axios";
|
||||
import { API_URL, errorHandler, type Fetch } from ".";
|
||||
|
||||
|
||||
// Login as admin /admin/login
|
||||
export const apiLogin = async (data: AdminLogin): Promise<number> => {
|
||||
try {
|
||||
const res = await axios.post(API_URL + '/admin/login', data, { withCredentials: true });
|
||||
return data.adminId;
|
||||
} catch (e: any) {
|
||||
throw errorHandler(e, 'Login failed');
|
||||
}
|
||||
};
|
||||
|
||||
// Create new candidate /admin/create
|
||||
// return created candidate's applicationId, personalIdNumber and password
|
||||
export const createCandidate = async (data: CreateCandidate): Promise<CreateCandidateLogin> => {
|
||||
try {
|
||||
const res = await axios.post(API_URL + '/admin/create', data, { withCredentials: true });
|
||||
return res.data;
|
||||
} catch (e: any) {
|
||||
throw errorHandler(e, 'Candidate creation failed');
|
||||
}
|
||||
}
|
||||
|
||||
// Reset candidate password /admin/candidate/{id}/reset_password
|
||||
export const resetCandidatePassword = async (id: number): Promise<string> => {
|
||||
try {
|
||||
const res = await axios.post(API_URL + '/admin/candidate/' + id + '/reset_password',
|
||||
{},
|
||||
{ withCredentials: true }
|
||||
);
|
||||
return res.data;
|
||||
} catch (e: any) {
|
||||
throw errorHandler(e, 'Candidate creation failed');
|
||||
}
|
||||
}
|
||||
|
||||
// SSR compatible
|
||||
// List all candidates /admin/list/candidates
|
||||
export const apiListCandidates = async (fetchSsr?: Fetch): Promise<[CandidatePreview]> => {
|
||||
try {
|
||||
if (fetchSsr) {
|
||||
const res = await fetchSsr(API_URL + '/admin/list/candidates', {
|
||||
method: 'GET',
|
||||
credentials: 'include'
|
||||
});
|
||||
return await res.json();
|
||||
}
|
||||
const res = await axios.get(API_URL + '/admin/list/candidates', {
|
||||
withCredentials: true
|
||||
});
|
||||
return res.data;
|
||||
} catch (e: any) {
|
||||
throw errorHandler(e, 'Failed to fetch submission progress');
|
||||
}
|
||||
};
|
||||
|
||||
// SSR compatible
|
||||
// Get candidate data /admin/candidate/{id}
|
||||
export const apiFetchCandidate = async (id: number, fetchSsr?: Fetch): Promise<CandidateData> => {
|
||||
try {
|
||||
if (fetchSsr) {
|
||||
const res = await fetchSsr(API_URL + '/admin/candidate/' + id, {
|
||||
method: 'GET',
|
||||
credentials: 'include'
|
||||
});
|
||||
return await res.json();
|
||||
}
|
||||
const res = await axios.get(API_URL + '/admin/candidate/' + id, {
|
||||
withCredentials: true
|
||||
});
|
||||
return res.data;
|
||||
} catch (e: any) {
|
||||
throw errorHandler(e, 'Failed to fetch candidate data');
|
||||
}
|
||||
}
|
||||
4
frontend/src/lib/stores/admin.ts
Normal file
4
frontend/src/lib/stores/admin.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
export interface AdminLogin {
|
||||
adminId: number;
|
||||
password: string;
|
||||
}
|
||||
|
|
@ -18,9 +18,25 @@ export interface CandidateData {
|
|||
parentEmail?: string;
|
||||
}
|
||||
|
||||
export interface CandidatePreview {
|
||||
applicationId: number;
|
||||
name: string;
|
||||
surname: string;
|
||||
study: string;
|
||||
}
|
||||
|
||||
export interface CandidateLogin {
|
||||
applicationId: number;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export interface CreateCandidate {
|
||||
applicationId: number;
|
||||
personalIdNumber: string;
|
||||
}
|
||||
|
||||
export interface CreateCandidateLogin extends CreateCandidate {
|
||||
password: string;
|
||||
}
|
||||
|
||||
export const candidateData = writable<CandidateData>({});
|
||||
|
|
|
|||
Loading…
Reference in a new issue