-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: finished backend API and database schema
Signed-off-by: Type-32 <[email protected]>
- Loading branch information
Showing
14 changed files
with
403 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<script setup lang="ts"> | ||
import MainLayout from "~/layouts/MainLayout.vue"; | ||
</script> | ||
|
||
<template> | ||
<MainLayout> | ||
<UContainer> | ||
<UPage> | ||
<UPageHeader | ||
title="Galleries" | ||
description="A section of where we put collections of media at." | ||
/> | ||
</UPage> | ||
</UContainer> | ||
</MainLayout> | ||
</template> | ||
|
||
<style scoped> | ||
</style> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { PrismaClient } from '@prisma/client' | ||
const prisma = new PrismaClient() | ||
export default defineEventHandler(async (event) => { | ||
const query = getQuery(event) | ||
try { | ||
let pageIndex: number = 0 | ||
let pageAmount: number = 90 | ||
if (query.pagination != null && query.paginatePerPage != null) { | ||
pageIndex = parseInt(query.pagination as any) | ||
pageAmount = parseInt(query.paginatePerPage as any) | ||
} | ||
let data = null | ||
if (query.ids != null) { | ||
const queryIds = Array.isArray(query.ids) | ||
? query.ids.map(Number) | ||
: [Number(query.ids)]; | ||
data = await prisma.category.findMany({ | ||
where: { | ||
id: { | ||
in: Array.from(queryIds as any[] as number[], Number) | ||
} | ||
} | ||
}) | ||
} else { | ||
data = await prisma.category.findMany({ | ||
select: { | ||
id: true, | ||
name: true, | ||
galleries: true, | ||
} | ||
}) | ||
} | ||
|
||
return data | ||
} catch (error: any) { | ||
if (error) { | ||
console.log(`${event.toString()} -> Error at ${error.message}`) | ||
|
||
return sendError(event, createError({ | ||
status: error.code as any as number, | ||
message: error.message, | ||
})) | ||
} | ||
} | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//@ts-ignore | ||
import { v4 as uuidv4 } from 'uuid'; | ||
//@ts-ignore | ||
import jwt from 'jsonwebtoken'; | ||
import { setCookie } from 'h3'; | ||
import { PrismaClient } from '@prisma/client' | ||
import useAuth from "~/composables/useAuth"; | ||
import useServerAuth from "~/composables/useServerAuth"; | ||
const prisma = new PrismaClient() | ||
|
||
export default defineEventHandler(async (event) => { | ||
const body = await readBody(event); | ||
|
||
if(!body.id) | ||
return sendError(event, createError({statusCode: 400, statusMessage: 'Not enough parameters.'})) | ||
|
||
const header = getHeader(event, 'Authorization') | ||
const auth = useServerAuth(event, header as any as string) | ||
|
||
const isServerAuthenticated = await auth.validateServerToken() | ||
if (!isServerAuthenticated) { | ||
return sendError(event, createError({ statusCode: 403, statusMessage: 'Unauthorized; Please re-login.'})); | ||
} | ||
|
||
let data = await prisma.gallery.delete({ | ||
where: { | ||
id: body.id as number, | ||
} | ||
}) | ||
|
||
if (!data) | ||
return sendError(event, createError({statusCode: 401, statusMessage: 'Gallery not found' })); | ||
|
||
return data; | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//@ts-ignore | ||
import { v4 as uuidv4 } from 'uuid'; | ||
//@ts-ignore | ||
import jwt from 'jsonwebtoken'; | ||
import {Media, PrismaClient} from '@prisma/client' | ||
import useServerAuth from "~/composables/useServerAuth"; | ||
const prisma = new PrismaClient() | ||
|
||
export default defineEventHandler(async (event) => { | ||
const body = await readBody<{ id: number, name: string, galleries: number[]}>(event); | ||
const header = getHeader(event, 'Authorization') | ||
|
||
if (!body.id || !body.name || !body.galleries || !header) { | ||
return sendError(event, createError({ statusCode: 400, statusMessage: 'Cannot access without authorization header.' })); | ||
} | ||
|
||
const auth = useServerAuth(event, header) | ||
|
||
const isServerAuthenticated = await auth.validateServerToken() | ||
if (!isServerAuthenticated) { | ||
return sendError(event, createError({ statusCode: 403, statusMessage: 'Unauthorized; Please re-login.'})); | ||
} | ||
|
||
let {error} = await prisma.category.update({ | ||
where: { | ||
id: body.id, | ||
}, | ||
data: { | ||
updatedAt: new Date().toISOString(), | ||
name: body.name, | ||
galleries: { | ||
connect: galleryIds.map(galleryId => ({ | ||
id: galleryId, | ||
})), | ||
}, | ||
} | ||
}) | ||
|
||
if (error) | ||
return sendError(event, createError({statusCode: 401, statusMessage: 'Gallery not found' })); | ||
|
||
return edit; | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {PrismaClient} from '@prisma/client' | ||
import useServerAuth from "~/composables/useServerAuth"; | ||
const prisma = new PrismaClient() | ||
|
||
export default defineEventHandler(async (event) => { | ||
const body = await readBody<{name: string}>(event); | ||
const header = getHeader(event, 'Authorization') | ||
console.log(header) | ||
|
||
if (!body.name || !header) { | ||
return sendError(event, createError({ statusCode: 400, statusMessage: 'Requires full parameters or headers' })); | ||
} | ||
|
||
const auth = useServerAuth(event, header) | ||
|
||
const isServerAuthenticated = await auth.validateServerToken() | ||
if (!isServerAuthenticated) { | ||
return sendError(event, createError({ statusCode: 403, statusMessage: 'Unauthorized; Please re-login.'})); | ||
} | ||
|
||
let {data, error} = await prisma.category.create({ | ||
data: { | ||
name: body.name as string | ||
} | ||
}) | ||
|
||
if (error) | ||
return sendError(event, createError({statusCode: 401, statusMessage: 'Unsuccessful creation. Try again later.' })); | ||
|
||
return data; | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//@ts-ignore | ||
import { v4 as uuidv4 } from 'uuid'; | ||
//@ts-ignore | ||
import jwt from 'jsonwebtoken'; | ||
import { setCookie } from 'h3'; | ||
import { PrismaClient } from '@prisma/client' | ||
import useAuth from "~/composables/useAuth"; | ||
import useServerAuth from "~/composables/useServerAuth"; | ||
const prisma = new PrismaClient() | ||
|
||
export default defineEventHandler(async (event) => { | ||
const body = await readBody(event); | ||
|
||
if(!body.id) | ||
return sendError(event, createError({statusCode: 400, statusMessage: 'Not enough parameters.'})) | ||
|
||
const header = getHeader(event, 'Authorization') | ||
const auth = useServerAuth(event, header as any as string) | ||
|
||
const isServerAuthenticated = await auth.validateServerToken() | ||
if (!isServerAuthenticated) { | ||
return sendError(event, createError({ statusCode: 403, statusMessage: 'Unauthorized; Please re-login.'})); | ||
} | ||
|
||
let {data, error} = await prisma.gallery.delete({ | ||
where: { | ||
id: body.id as number, | ||
} | ||
}) | ||
|
||
if (error) | ||
return sendError(event, createError({statusCode: 401, statusMessage: 'Gallery not found' })); | ||
|
||
return data; | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { PrismaClient } from '@prisma/client' | ||
const prisma = new PrismaClient() | ||
export default defineEventHandler(async (event) => { | ||
const query = getQuery(event) | ||
|
||
try { | ||
let data = null | ||
if (query.slug != null) { | ||
data = await prisma.gallery.findFirst({ | ||
where: { | ||
id: query.id as any as number | ||
} | ||
}) | ||
} else { | ||
return null | ||
} | ||
|
||
return data | ||
} catch (error: any) { | ||
if (error) { | ||
console.log(`${event.toString()} -> Error at ${error.message}`) | ||
|
||
return sendError(event, createError({ | ||
status: error.code as any as number, | ||
message: error.message, | ||
})) | ||
} | ||
} | ||
}) |
Oops, something went wrong.