Skip to content

Commit

Permalink
chore: adding api routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Type-32 committed Sep 27, 2024
1 parent 661aacf commit 54386a8
Show file tree
Hide file tree
Showing 11 changed files with 4,881 additions and 3,978 deletions.
2 changes: 1 addition & 1 deletion app/composables/useCategories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import type {Category} from "@prisma/client";

export const useCategories = () => {
const getCategories = async () =>{
return useFetch<Category[]>('/api/v1/galleries')
return useFetch<Category[]>('/api/v1/categories')
}
}
7 changes: 7 additions & 0 deletions app/composables/useGalleries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type {Gallery} from "@prisma/client";

export const useGalleries = () => {
const getGalleries = async () =>{
return useFetch<Gallery[]>('/api/v1/galleries')
}
}
8,653 changes: 4,696 additions & 3,957 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@nuxt/ui-pro": "^1.3.2",
"@nuxtjs/mdc": "^0.8.3",
"@pinia/nuxt": "^0.5.1",
"@prisma/client": "^5.15.1",
"@prisma/client": "^5.19.1",
"@tailwindcss/typography": "^0.5.13",
"@tiptap/extension-image": "^2.4.0",
"@tiptap/extension-youtube": "^2.4.0",
Expand All @@ -33,7 +33,7 @@
},
"devDependencies": {
"@types/minio": "^7.1.1",
"nuxt": "^3.12.3",
"nuxt": "^3.13.1",
"nuxt-file-storage": "^0.2.6"
}
}
116 changes: 116 additions & 0 deletions prisma/migrations/20240911120215_nightly/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
-- CreateTable
CREATE TABLE "Match" (
"id" SERIAL NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"home_score" INTEGER NOT NULL DEFAULT 0,
"guest_score" INTEGER NOT NULL DEFAULT 0,
"homeTeamId" INTEGER NOT NULL,
"guestTeamId" INTEGER NOT NULL,

CONSTRAINT "Match_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Team" (
"id" SERIAL NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"team_name" TEXT NOT NULL,
"team_icon" TEXT,
"team_desc" TEXT DEFAULT '',
"team_abbv" TEXT DEFAULT '',

CONSTRAINT "Team_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Article" (
"id" SERIAL NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"title" VARCHAR(255) NOT NULL,
"slug" VARCHAR(255) NOT NULL,
"description" VARCHAR(255),
"content" TEXT,
"published" BOOLEAN NOT NULL DEFAULT false,
"banner" TEXT,

CONSTRAINT "Article_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "User" (
"id" SERIAL NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"username" TEXT NOT NULL,
"password" TEXT NOT NULL,

CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Category" (
"id" SERIAL NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"name" TEXT NOT NULL,

CONSTRAINT "Category_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Gallery" (
"id" SERIAL NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"name" TEXT NOT NULL,
"categoryId" INTEGER,

CONSTRAINT "Gallery_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Media" (
"id" SERIAL NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"fileName" TEXT NOT NULL,
"url" TEXT NOT NULL,
"directory" TEXT NOT NULL,
"pseudoDirectory" TEXT NOT NULL DEFAULT '/',
"isFolder" BOOLEAN NOT NULL DEFAULT false,
"galleryId" INTEGER,

CONSTRAINT "Media_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Token" (
"id" SERIAL NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"uuid" TEXT NOT NULL,
"userId" INTEGER NOT NULL,

CONSTRAINT "Token_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "Token_uuid_key" ON "Token"("uuid");

-- AddForeignKey
ALTER TABLE "Match" ADD CONSTRAINT "Match_homeTeamId_fkey" FOREIGN KEY ("homeTeamId") REFERENCES "Team"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Match" ADD CONSTRAINT "Match_guestTeamId_fkey" FOREIGN KEY ("guestTeamId") REFERENCES "Team"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Gallery" ADD CONSTRAINT "Gallery_categoryId_fkey" FOREIGN KEY ("categoryId") REFERENCES "Category"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Media" ADD CONSTRAINT "Media_galleryId_fkey" FOREIGN KEY ("galleryId") REFERENCES "Gallery"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Token" ADD CONSTRAINT "Token_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
2 changes: 2 additions & 0 deletions prisma/migrations/20240911121141_nightly2/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Gallery" ADD COLUMN "published" BOOLEAN NOT NULL DEFAULT false;
27 changes: 27 additions & 0 deletions prisma/migrations/20240911121728_nightly3/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Warnings:
- You are about to drop the column `galleryId` on the `Media` table. All the data in the column will be lost.
*/
-- DropForeignKey
ALTER TABLE "Media" DROP CONSTRAINT "Media_galleryId_fkey";

-- AlterTable
ALTER TABLE "Media" DROP COLUMN "galleryId";

-- CreateTable
CREATE TABLE "MediaOnGalleries" (
"galleryId" INTEGER NOT NULL,
"mediaId" INTEGER NOT NULL,
"assignedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"assignedBy" TEXT NOT NULL,

CONSTRAINT "MediaOnGalleries_pkey" PRIMARY KEY ("galleryId","mediaId")
);

-- AddForeignKey
ALTER TABLE "MediaOnGalleries" ADD CONSTRAINT "MediaOnGalleries_galleryId_fkey" FOREIGN KEY ("galleryId") REFERENCES "Gallery"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "MediaOnGalleries" ADD CONSTRAINT "MediaOnGalleries_mediaId_fkey" FOREIGN KEY ("mediaId") REFERENCES "Media"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
3 changes: 3 additions & 0 deletions prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
23 changes: 17 additions & 6 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ model Gallery {
updatedAt DateTime @default(now()) @updatedAt
name String
medias Media[]
Category Category? @relation(fields: [categoryId], references: [id])
published Boolean @default(false)
Category Category? @relation(fields: [categoryId], references: [id])
categoryId Int?
medias MediaOnGalleries[]
}

model Media {
Expand All @@ -83,10 +84,20 @@ model Media {
fileName String
url String
directory String
pseudoDirectory String @default("/")
isFolder Boolean @default(false)
Gallery Gallery? @relation(fields: [galleryId], references: [id])
galleryId Int?
pseudoDirectory String @default("/")
isFolder Boolean @default(false)
galleries MediaOnGalleries[]
}

model MediaOnGalleries {
gallery Gallery @relation(fields: [galleryId], references: [id])
galleryId Int // relation scalar field (used in the `@relation` attribute above)
media Media @relation(fields: [mediaId], references: [id])
mediaId Int // relation scalar field (used in the `@relation` attribute above)
assignedAt DateTime @default(now())
assignedBy String
@@id([galleryId, mediaId])
}

model Token {
Expand Down
19 changes: 8 additions & 11 deletions server/api/v1/gallery/edit.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
import { v4 as uuidv4 } from 'uuid';
//@ts-ignore
import jwt from 'jsonwebtoken';
import { setCookie } from 'h3';
import {Media, PrismaClient} from '@prisma/client'
import useServerAuth from "~/composables/useServerAuth";
const prisma = new PrismaClient()

export default defineEventHandler(async (event) => {
const body = await readBody<{name: string, medias: Media[]}>(event);
const body = await readBody<{ id: number, name: string, published: boolean, medias: number[]}>(event);
const header = getHeader(event, 'Authorization')

if (!body.name || !body.medias || !header) {
return sendError(event, createError({ statusCode: 400, statusMessage: 'Requires full parameters or headers' }));
if (!body.id || !body.name || !body.published || !body.medias || !header) {
return sendError(event, createError({ statusCode: 400, statusMessage: 'Cannot access without authorization header.' }));
}

const auth = useServerAuth(event, header)
Expand All @@ -22,16 +21,14 @@ export default defineEventHandler(async (event) => {
return sendError(event, createError({ statusCode: 403, statusMessage: 'Unauthorized; Please re-login.'}));
}

let edit = await prisma.article.update({
let edit = await prisma.gallery.update({
where: {
id: body.id as number,
id: body.id,
},
data: {
title: body.title,
description: body.description,
content: body.content,
published: body.published,
updatedAt: new Date().toISOString()
updatedAt: new Date().toISOString(),
name: body.name,
published: body.published
}
})

Expand Down
3 changes: 2 additions & 1 deletion server/api/v1/gallery/new.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export default defineEventHandler(async (event) => {

let data = await prisma.gallery.create({
data: {
name: body.name as string
name: body.name as string,
published: false
}
})

Expand Down

0 comments on commit 54386a8

Please sign in to comment.