Skip to content

Commit

Permalink
feat: finished backend API and database schema
Browse files Browse the repository at this point in the history
Signed-off-by: Type-32 <[email protected]>
  • Loading branch information
Type-32 committed Sep 29, 2024
1 parent bafb8a8 commit 7841904
Show file tree
Hide file tree
Showing 14 changed files with 403 additions and 10 deletions.
21 changes: 21 additions & 0 deletions app/pages/galleries/index.vue
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>
73 changes: 72 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"devDependencies": {
"@types/minio": "^7.1.1",
"nuxt": "^3.13.1",
"nuxt-file-storage": "^0.2.6"
"nuxt-file-storage": "^0.2.6",
"prisma": "^5.20.0"
}
}
45 changes: 45 additions & 0 deletions server/api/v1/categories.get.ts
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,
}))
}
}
})
35 changes: 35 additions & 0 deletions server/api/v1/category/delete.post.ts
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;
});
43 changes: 43 additions & 0 deletions server/api/v1/category/edit.post.ts
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;
});
31 changes: 31 additions & 0 deletions server/api/v1/category/new.post.ts
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;
});
2 changes: 1 addition & 1 deletion server/api/v1/galleries.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default defineEventHandler(async (event) => {
const query = getQuery(event)
try {
let pageIndex: number = 0
let pageAmount: number = 9
let pageAmount: number = 90
if (query.pagination != null && query.paginatePerPage != null) {
pageIndex = parseInt(query.pagination as any)
pageAmount = parseInt(query.paginatePerPage as any)
Expand Down
35 changes: 35 additions & 0 deletions server/api/v1/gallery/delete.post.ts
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;
});
13 changes: 9 additions & 4 deletions server/api/v1/gallery/edit.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,24 @@ export default defineEventHandler(async (event) => {
return sendError(event, createError({ statusCode: 403, statusMessage: 'Unauthorized; Please re-login.'}));
}

let edit = await prisma.gallery.update({
let {data: edit, error} = await prisma.gallery.update({
where: {
id: body.id,
},
data: {
updatedAt: new Date().toISOString(),
name: body.name,
published: body.published
published: body.published,
medias: {
connect: body.medias.map(mediaId => ({
mediaId: mediaId,
})),
},
}
})

if (!edit)
return sendError(event, createError({statusCode: 401, statusMessage: 'Article not found' }));
if (error)
return sendError(event, createError({statusCode: 401, statusMessage: 'Gallery not found' }));

return edit;
});
29 changes: 29 additions & 0 deletions server/api/v1/gallery/get-slug.get.ts
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,
}))
}
}
})
Loading

0 comments on commit 7841904

Please sign in to comment.