159 lines
4.9 KiB
TypeScript
159 lines
4.9 KiB
TypeScript
import type {Gottesdienst, GottesdienstGroup, Mark} from "@/models/models";
|
|
import {Auth} from "@/services/auth";
|
|
|
|
|
|
const API_ENDPOINT = import.meta.env.MODE == "development"
|
|
? "http://localhost:8080/api"
|
|
: window.location.origin + "/api"
|
|
|
|
|
|
export async function api(endpoint: string, method: string = "GET", body?: any ) {
|
|
let isJson = (typeof body == "object")
|
|
return fetch(API_ENDPOINT + endpoint, {
|
|
method: method,
|
|
body: isJson ? JSON.stringify(body) : body,
|
|
headers: {
|
|
Accept: "application/json",
|
|
"Content-Type": (isJson ? "application/json" : "text/plain"),
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Authorization": "Bearer " + Auth.getToken()
|
|
}
|
|
})
|
|
}
|
|
|
|
function setToken(token: string) {
|
|
let expires = new Date((new Date()).getTime() + (1000*60*60*24*5)).toUTCString()
|
|
document.cookie = `token=${token};expires=${expires};HTTPOnly`
|
|
}
|
|
|
|
function getToken(): string | null {
|
|
return ""
|
|
}
|
|
|
|
export class UnauthorizedError extends Error {
|
|
constructor() {
|
|
super("UnauthorizedError");
|
|
}
|
|
}
|
|
|
|
export namespace API {
|
|
|
|
export async function getPlan(id: number) {
|
|
return api(`/plan?id=${id}`).then(res => {
|
|
if(res.status == 401) {
|
|
throw new UnauthorizedError()
|
|
}
|
|
return res.json()
|
|
}).then(data => {
|
|
return {
|
|
gottesdienste: data.gottesdienste,
|
|
ministranten: data.ministranten,
|
|
marks: data.marks
|
|
}
|
|
})
|
|
}
|
|
|
|
export async function getPlans(): Promise<GottesdienstGroup[]> {
|
|
return api("/groups").then(res => res.json())
|
|
}
|
|
|
|
export async function createPlan(from, to) {
|
|
return api("/groups", "POST", {
|
|
id: -1,
|
|
from,
|
|
to
|
|
}).then(res => res.json())
|
|
}
|
|
|
|
export async function deletePlan(id) {
|
|
return api("/groups?id=" + id, "DELETE").then(res => res.json())
|
|
}
|
|
|
|
export async function updatePlan(group) {
|
|
return api("/groups", "PATCH", group).then(res => res.json())
|
|
}
|
|
|
|
function formatGottesdienste(data: any): Array<Gottesdienst>{
|
|
return data.map(json => {
|
|
json["date"] = new Date(json["date"])
|
|
json["attendance"] = new Date(json["attendance"])
|
|
return json as Gottesdienst
|
|
}) as Array<Gottesdienst>
|
|
}
|
|
|
|
export async function addGottesdienst(
|
|
name: string = "",
|
|
date: Date,
|
|
attendance: Date = new Date(date.getTime() - 1000*60*30),
|
|
planId: number
|
|
){
|
|
return api("/gottesdienste", "PUT", {
|
|
id: 0, name, date: date.getTime(), attendance: attendance.getTime(), planId
|
|
}).then(res => res.json())
|
|
.then(json => {
|
|
return {
|
|
id: json.id, name, date, attendance, planId
|
|
}
|
|
})
|
|
}
|
|
|
|
export async function addGottesdienstNew(gottesdienst: Gottesdienst) {
|
|
return api("/gottesdienste", "PUT", {
|
|
...gottesdienst,
|
|
date: gottesdienst.date.getTime(),
|
|
attendance: gottesdienst.attendance.getTime()
|
|
}).then(data => data.json())
|
|
}
|
|
|
|
export async function updateGottesdienst(gottesdienst: Gottesdienst) {
|
|
return api("/gottesdienste", "PATCH", {
|
|
...gottesdienst,
|
|
date: gottesdienst.date.getTime(),
|
|
attendance: gottesdienst.attendance.getTime()
|
|
}).then(res => res.status == 200)
|
|
}
|
|
|
|
export async function getMinistranten() {
|
|
return api("/ministranten", "GET").then(res => res.json())
|
|
}
|
|
|
|
export async function deleteGottesdienst(id) {
|
|
return api("/gottesdienste?id=" + id, "DELETE")
|
|
.then(data => data.status == 200)
|
|
}
|
|
export async function deleteMinistrant(id) {
|
|
return api("/ministranten?id=" + id, "DELETE")
|
|
.then(data => data.status == 200)
|
|
}
|
|
|
|
export async function createMinistrant(ministrant) {
|
|
return api("/ministranten", "PUT", ministrant).then(data => data.json())
|
|
}
|
|
export async function updateMinistrant(ministrant) {
|
|
return api("/ministranten", "PATCH", ministrant)
|
|
.then(data => data.status == 200)
|
|
}
|
|
|
|
export async function setMarks(marks: Mark[]): Promise<boolean> {
|
|
return api("/marks", "PATCH", marks)
|
|
.then(res => Promise.resolve(res.status == 200))
|
|
}
|
|
|
|
export async function resetPassword(username: String): Promise<any> {
|
|
return api("/auth/reset", "POST", { username })
|
|
.then(res => res.json())
|
|
}
|
|
|
|
export async function updatePassword(username: String, newPassword: string): Promise<any> {
|
|
return api("/auth/update", "PUT", { username, newPassword })
|
|
.then(res => res.json())
|
|
}
|
|
|
|
export function getZelebrationsplanParsingUrl(planId: number) {
|
|
return API_ENDPOINT + "/gottesdienste/parseZelebrationsplan?id=" + planId
|
|
}
|
|
|
|
}
|
|
|
|
|