add deleteStory function, fix schema to allow it

This commit is contained in:
andrzej 2024-06-19 18:01:42 +02:00
parent f454f6739e
commit 7b68a7451e
11 changed files with 139 additions and 29 deletions

Binary file not shown.

BIN
prisma/dev.db-journal Normal file

Binary file not shown.

View File

@ -0,0 +1,19 @@
-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Sub" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"storyId" INTEGER,
"pubId" INTEGER,
"submitted" TEXT NOT NULL,
"responded" TEXT,
"responseId" INTEGER,
CONSTRAINT "Sub_storyId_fkey" FOREIGN KEY ("storyId") REFERENCES "Story" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT "Sub_pubId_fkey" FOREIGN KEY ("pubId") REFERENCES "Pub" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT "Sub_responseId_fkey" FOREIGN KEY ("responseId") REFERENCES "Response" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
INSERT INTO "new_Sub" ("id", "pubId", "responded", "responseId", "storyId", "submitted") SELECT "id", "pubId", "responded", "responseId", "storyId", "submitted" FROM "Sub";
DROP TABLE "Sub";
ALTER TABLE "new_Sub" RENAME TO "Sub";
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;

View File

@ -0,0 +1,19 @@
/*
Warnings:
- You are about to drop the column `deleted` on the `Story` table. All the data in the column will be lost.
*/
-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Story" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"word_count" INTEGER NOT NULL,
"title" TEXT NOT NULL
);
INSERT INTO "new_Story" ("id", "title", "word_count") SELECT "id", "title", "word_count" FROM "Story";
DROP TABLE "Story";
ALTER TABLE "new_Story" RENAME TO "Story";
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;

View File

@ -0,0 +1,20 @@
/*
Warnings:
- You are about to drop the column `deleted` on the `Pub` table. All the data in the column will be lost.
*/
-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Pub" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"title" TEXT NOT NULL,
"link" TEXT NOT NULL DEFAULT '',
"query_after_days" INTEGER NOT NULL
);
INSERT INTO "new_Pub" ("id", "link", "query_after_days", "title") SELECT "id", "link", "query_after_days", "title" FROM "Pub";
DROP TABLE "Pub";
ALTER TABLE "new_Pub" RENAME TO "Pub";
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;

View File

@ -14,7 +14,6 @@ model Story {
id Int @id @default(autoincrement())
word_count Int
title String
deleted Int @default(0)
subs Sub[]
genres Genre[]
}
@ -24,7 +23,6 @@ model Pub {
title String
link String @default("")
query_after_days Int
deleted Int @default(0)
subs Sub[]
genres Genre[]
}
@ -44,12 +42,12 @@ model Response {
model Sub {
id Int @id @default(autoincrement())
story Story @relation(fields: [storyId], references: [id])
storyId Int
pub Pub @relation(fields: [pubId], references: [id])
pubId Int
story Story? @relation(fields: [storyId], references: [id])
storyId Int?
pub Pub? @relation(fields: [pubId], references: [id])
pubId Int?
submitted String
responded String
response Response @relation(fields: [responseId], references: [id])
responseId Int
responded String?
response Response? @relation(fields: [responseId], references: [id])
responseId Int?
}

10
src/app/lib/del.ts Normal file
View File

@ -0,0 +1,10 @@
"use server"
import prisma from "./db";
export async function deleteStory(id) {
console.log(`id: ${id}`)
const res = await prisma.story.delete({
where: { id }
})
console.log(res)
}

View File

@ -4,11 +4,14 @@ import { StoryWithGenres } from "./page"
import { ArrowUpDown, MoreHorizontal } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Badge } from "@/components/ui/badge"
import { CircleX } from "lucide-react"
import { deleteStory } from "app/lib/del"
const columnHelper = createColumnHelper<StoryWithGenres>()
export const columns: ColumnDef<StoryWithGenres>[] = [
{
accessorKey: "title",
@ -40,29 +43,22 @@ export const columns: ColumnDef<StoryWithGenres>[] = [
},
enableColumnFilter: false
},
// {
// accessorFn: row => {
// let unpacked = ""
// for (let i = 0; i < row.genres.length; i++) {
// unpacked = unpacked + " " + row.genres[i].name
// }
// return unpacked
// },
// header: "Genres"
//
//
// },
columnHelper.accessor("genres", {
cell: props => {
const genres = props.getValue()
.map(e => <Badge>{e.name}</Badge>)
return genres
}
})
// {
// accessorKey: "deleted",
// header: "Deleted"
// },
}),
{
id: "actions",
header: "Actions",
cell: ({ row }) => {
return <Button variant="ghost"
onClick={() => { deleteStory(row.original.id) }}>
<CircleX /></Button>
}
}
]

View File

@ -31,6 +31,7 @@ import {
TableHeader,
TableRow,
} from "@/components/ui/table"
import { EyeIcon } from "lucide-react"
interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[]
@ -105,7 +106,7 @@ export function DataTable<TData, TValue>({
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline" className="justify-self-end">
Show/hide
<EyeIcon />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">

View File

@ -907,6 +907,10 @@ body {
gap: 0.25rem;
}
.gap-2 {
gap: 0.5rem;
}
.space-x-1 > :not([hidden]) ~ :not([hidden]) {
--tw-space-x-reverse: 0;
margin-right: calc(0.25rem * var(--tw-space-x-reverse));
@ -967,6 +971,18 @@ body {
margin-bottom: calc(2rem * var(--tw-space-y-reverse));
}
.justify-self-start {
justify-self: start;
}
.justify-self-end {
justify-self: end;
}
.justify-self-center {
justify-self: center;
}
.overflow-auto {
overflow: auto;
}
@ -987,6 +1003,10 @@ body {
border-radius: calc(var(--radius) - 4px);
}
.rounded-full {
border-radius: 9999px;
}
.border {
border-width: 1px;
}
@ -1011,6 +1031,10 @@ body {
border-color: hsl(var(--primary));
}
.border-transparent {
border-color: transparent;
}
.bg-accent {
background-color: hsl(var(--accent));
}
@ -1121,6 +1145,21 @@ body {
padding-bottom: 1rem;
}
.px-2\.5 {
padding-left: 0.625rem;
padding-right: 0.625rem;
}
.py-0 {
padding-top: 0px;
padding-bottom: 0px;
}
.py-0\.5 {
padding-top: 0.125rem;
padding-bottom: 0.125rem;
}
.pl-3 {
padding-left: 0.75rem;
}
@ -1400,6 +1439,14 @@ body {
background-color: hsl(var(--secondary) / 0.8);
}
.hover\:bg-destructive\/80:hover {
background-color: hsl(var(--destructive) / 0.8);
}
.hover\:bg-primary\/80:hover {
background-color: hsl(var(--primary) / 0.8);
}
.hover\:text-accent-foreground:hover {
color: hsl(var(--accent-foreground));
}