-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from ssciwr/use_new_frontend_api
Use new auto-generated client API in frontend admin interface
- Loading branch information
Showing
22 changed files
with
342 additions
and
607 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,321 +1,28 @@ | ||
import { milestoneGroups, userQuestions } from '$lib/stores/adminStore'; | ||
import { getMilestoneGroupsAdmin, getUserQuestionsAdmin } from '$lib/client/services.gen'; | ||
|
||
export async function refreshMilestoneGroups() { | ||
console.log('refreshMilestoneGroups...'); | ||
try { | ||
const res = await fetch(`${import.meta.env.VITE_MONDEY_API_URL}/admin/milestone-groups/`, { | ||
method: 'GET', | ||
credentials: 'include', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json' | ||
} | ||
}); | ||
const json = await res.json(); | ||
console.log(json); | ||
if (res.status === 200) { | ||
milestoneGroups.set(json); | ||
} else { | ||
console.log('Failed to get MilestoneGroups'); | ||
milestoneGroups.set([]); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
const { data, error } = await getMilestoneGroupsAdmin(); | ||
if (error || data == undefined) { | ||
console.log('Failed to get MilestoneGroups'); | ||
milestoneGroups.set([]); | ||
} | ||
} | ||
|
||
export async function newMilestoneGroup() { | ||
console.log('newMilestoneGroup...'); | ||
try { | ||
const res = await fetch(`${import.meta.env.VITE_MONDEY_API_URL}/admin/milestone-groups/`, { | ||
method: 'POST', | ||
credentials: 'include', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json' | ||
} | ||
}); | ||
if (res.status === 200) { | ||
const newGroup = await res.json(); | ||
console.log(newGroup); | ||
await refreshMilestoneGroups(); | ||
return newGroup; | ||
} else { | ||
console.log('Failed to create new MilestoneGroup'); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
return null; | ||
} | ||
|
||
export async function updateMilestoneGroup(milestoneGroup) { | ||
console.log('updateMilestoneGroup...'); | ||
console.log(milestoneGroup); | ||
try { | ||
const res = await fetch(`${import.meta.env.VITE_MONDEY_API_URL}/admin/milestone-groups/`, { | ||
method: 'PUT', | ||
credentials: 'include', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json' | ||
}, | ||
body: JSON.stringify(milestoneGroup) | ||
}); | ||
if (res.status === 200) { | ||
const updatedGroup = await res.json(); | ||
console.log(updatedGroup); | ||
await refreshMilestoneGroups(); | ||
return updatedGroup; | ||
} else { | ||
console.log('Failed to create new MilestoneGroup'); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
return null; | ||
} | ||
|
||
export async function uploadMilestoneGroupImage(milestoneGroupId: number, file) { | ||
console.log('uploadMilestoneGroupImage...'); | ||
try { | ||
const formData = new FormData(); | ||
formData.append('file', file); | ||
const res = await fetch( | ||
`${import.meta.env.VITE_MONDEY_API_URL}/admin/milestone-group-images/${milestoneGroupId}`, | ||
{ | ||
method: 'PUT', | ||
credentials: 'include', | ||
body: formData | ||
} | ||
); | ||
console.log(await res.json()); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
|
||
export async function deleteMilestoneGroup(milestoneGroupId: number | null) { | ||
try { | ||
const res = await fetch( | ||
`${import.meta.env.VITE_MONDEY_API_URL}/admin/milestone-groups/${milestoneGroupId}`, | ||
{ | ||
method: 'DELETE', | ||
credentials: 'include', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json' | ||
} | ||
} | ||
); | ||
const json = await res.json(); | ||
console.log(json); | ||
if (res.status === 200) { | ||
console.log(`Deleted MilestoneGroup with id ${milestoneGroupId}.`); | ||
await refreshMilestoneGroups(); | ||
} else { | ||
console.log(`Error deleting MilestoneGroup with id ${milestoneGroupId}.`); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} else { | ||
milestoneGroups.set(data); | ||
} | ||
} | ||
|
||
export function milestoneGroupImageUrl(id: number) { | ||
return `${import.meta.env.VITE_MONDEY_API_URL}/static/mg${id}.jpg`; | ||
} | ||
|
||
export async function newMilestone(milestoneGroupId: number) { | ||
console.log('newMilestone...'); | ||
try { | ||
const res = await fetch( | ||
`${import.meta.env.VITE_MONDEY_API_URL}/admin/milestones/${milestoneGroupId}`, | ||
{ | ||
method: 'POST', | ||
credentials: 'include', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json' | ||
} | ||
} | ||
); | ||
if (res.status === 200) { | ||
const newMilestone = await res.json(); | ||
console.log(newMilestone); | ||
await refreshMilestoneGroups(); | ||
return newMilestone; | ||
} else { | ||
console.log('Failed to create new Milestone'); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
return null; | ||
} | ||
|
||
export async function updateMilestone(milestone) { | ||
console.log('updateMilestone...'); | ||
console.log(milestone); | ||
try { | ||
const res = await fetch(`${import.meta.env.VITE_MONDEY_API_URL}/admin/milestones/`, { | ||
method: 'PUT', | ||
credentials: 'include', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json' | ||
}, | ||
body: JSON.stringify(milestone) | ||
}); | ||
if (res.status === 200) { | ||
const updatedMilestone = await res.json(); | ||
console.log(updatedMilestone); | ||
return updatedMilestone; | ||
} else { | ||
console.log('Failed to update Milestone'); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
return null; | ||
} | ||
|
||
export async function uploadMilestoneImage(milestoneId: number, file) { | ||
console.log('uploadMilestoneImage...'); | ||
try { | ||
const formData = new FormData(); | ||
formData.append('file', file); | ||
const res = await fetch( | ||
`${import.meta.env.VITE_MONDEY_API_URL}/admin/milestone-images/${milestoneId}`, | ||
{ | ||
method: 'POST', | ||
credentials: 'include', | ||
body: formData | ||
} | ||
); | ||
console.log(await res.json()); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
|
||
export async function deleteMilestone(milestoneId: number | null) { | ||
try { | ||
const res = await fetch( | ||
`${import.meta.env.VITE_MONDEY_API_URL}/admin/milestones/${milestoneId}`, | ||
{ | ||
method: 'DELETE', | ||
credentials: 'include', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json' | ||
} | ||
} | ||
); | ||
const json = await res.json(); | ||
console.log(json); | ||
if (res.status === 200) { | ||
console.log(`Deleted Milestone with id ${milestoneId}.`); | ||
await refreshMilestoneGroups(); | ||
} else { | ||
console.log(`Error deleting Milestone with id ${milestoneId}.`); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
|
||
export async function refreshUserQuestions() { | ||
console.log('refreshQuestions...'); | ||
try { | ||
const res = await fetch(`${import.meta.env.VITE_MONDEY_API_URL}/admin/user-questions/`, { | ||
method: 'GET', | ||
credentials: 'include', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json' | ||
} | ||
}); | ||
const json = await res.json(); | ||
console.log(json); | ||
if (res.status === 200) { | ||
userQuestions.set(json); | ||
} else { | ||
console.log('Failed to get UserQuestions'); | ||
userQuestions.set([]); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
const { data, error } = await getUserQuestionsAdmin(); | ||
if (error || data === undefined) { | ||
console.log('Failed to get UserQuestions'); | ||
userQuestions.set([]); | ||
} else { | ||
userQuestions.set(data); | ||
} | ||
} | ||
|
||
export async function newUserQuestion() { | ||
try { | ||
const res = await fetch(`${import.meta.env.VITE_MONDEY_API_URL}/admin/user-questions/`, { | ||
method: 'POST', | ||
credentials: 'include', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
}); | ||
if (res.status === 200) { | ||
const newUserQuestion = await res.json(); | ||
console.log(newUserQuestion); | ||
await refreshUserQuestions(); | ||
return newUserQuestion; | ||
} else { | ||
console.log('Failed to create new Question'); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
|
||
export async function updateUserQuestion(userQuestion) { | ||
console.log('updateUserQuestion...'); | ||
console.log(userQuestion); | ||
try { | ||
const res = await fetch(`${import.meta.env.VITE_MONDEY_API_URL}/admin/user-questions/`, { | ||
method: 'PUT', | ||
credentials: 'include', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json' | ||
}, | ||
body: JSON.stringify(userQuestion) | ||
}); | ||
if (res.status === 200) { | ||
const updatedUserQuestion = await res.json(); | ||
console.log(updatedUserQuestion); | ||
await refreshUserQuestions(); | ||
return updatedUserQuestion; | ||
} else { | ||
console.log('Failed to create new UserQuestion'); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
return null; | ||
} | ||
|
||
export async function deleteUserQuestion(id: number) { | ||
try { | ||
const res = await fetch(`${import.meta.env.VITE_MONDEY_API_URL}/admin/user-questions/${id}`, { | ||
method: 'DELETE', | ||
credentials: 'include', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json' | ||
} | ||
}); | ||
if (res.status === 200) { | ||
await refreshUserQuestions(); | ||
} else { | ||
console.log('Failed to delete Question'); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.