Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { formatToUTCDate } from '$lib/utils/date';
import type { fetchType } from '$lib/utils/types';
export async function getStudiesAPI(fetch: fetchType): Promise<any[]> {
const response = await fetch('/api/studies');
if (!response.ok) return [];
return await response.json();
}
export async function getStudyAPI(fetch: fetchType, id: number): Promise<any | undefined> {
const response = await fetch(`/api/studies/${id}`);
if (!response.ok) return;
return await response.json();
}
export async function deleteStudyAPI(fetch: fetchType, id: number): Promise<boolean> {
const response = await fetch(`/api/studies/${id}`, {
method: 'DELETE'
});
return response.ok;
}
export async function createStudyAPI(
fetch: fetchType,
title: string,
description: string,
startDate: Date,
endDate: Date,
chatDuration: number
): Promise<number | null> {
const response = await fetch('/api/studies', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title,
description,
start_date: formatToUTCDate(startDate),
end_date: formatToUTCDate(endDate),
chat_duration: chatDuration
})
});
if (!response.ok) return null;
return parseInt(await response.text());
}
export async function patchStudyAPI(
fetch: fetchType,
study_id: number,
data: any
): Promise<boolean> {
const response = await fetch(`/api/studies/${study_id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
return response.ok;
}
export async function addUserToStudyAPI(
fetch: fetchType,
study_id: number,
user_id: number
): Promise<boolean> {
const response = await fetch(`/api/studies/${study_id}/users/${user_id}`, {
method: 'POST'
});
return response.ok;
}
export async function removeUserToStudyAPI(
fetch: fetchType,
study_id: number,
user_id: number
): Promise<boolean> {
const response = await fetch(`/api/studies/${study_id}/users/${user_id}`, {
method: 'DELETE'
});
return response.ok;
}
export async function createTestTypingAPI(
fetch: fetchType,
entries: typingEntry[],
code: string
): Promise<number | null> {
const response = await fetch(`/api/studies/typing`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ entries, code })
});
if (!response.ok) return null;
return parseInt(await response.text());
}