-
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: add audio bulk, tweaks api response (#9)
* feat: add audio bulk, tweaks api response * chore: remove useless
- Loading branch information
Showing
11 changed files
with
110 additions
and
33 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
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,39 @@ | ||
import { PrismaClient, type Prisma, type Audio } from "@prisma/client"; | ||
|
||
async function createManyAudios(audios: Audio[]): Promise<void> { | ||
const prisma = new PrismaClient(); | ||
try { | ||
await prisma.$transaction( | ||
audios.map((audio) => | ||
prisma.audio.create({ | ||
data: { ...audio }, | ||
}), | ||
), | ||
); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
} | ||
|
||
export async function POST(req: Request): Promise<Response> { | ||
const prisma = new PrismaClient(); | ||
const { audios } = (await req.json()) as { | ||
audios: Prisma.AudioGetPayload<{ include: { material: true } }>[]; | ||
}; | ||
if ( | ||
Array.isArray(audios) && | ||
audios.length !== 0 && | ||
audios.every((audio) => audio.url && audio.detail) | ||
) { | ||
try { | ||
await createManyAudios(audios).finally(() => { | ||
void prisma.$disconnect(); | ||
}); | ||
return new Response(JSON.stringify({ status: 0, data: {} })); | ||
} catch (error) { | ||
return new Response(error, { status: 500 }); | ||
} | ||
} else { | ||
return new Response("Unexpected input types", { status: 500 }); | ||
} | ||
} |
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
Binary file not shown.
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,2 @@ | ||
-- AlterTable | ||
ALTER TABLE "Material" ADD COLUMN "audioIds" TEXT; |
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,20 @@ | ||
/* | ||
Warnings: | ||
- You are about to drop the column `audioIds` on the `Material` table. All the data in the column will be lost. | ||
*/ | ||
-- RedefineTables | ||
PRAGMA foreign_keys=OFF; | ||
CREATE TABLE "new_Material" ( | ||
"id" TEXT NOT NULL PRIMARY KEY, | ||
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" DATETIME NOT NULL, | ||
"contentList" TEXT NOT NULL, | ||
"imageList" TEXT | ||
); | ||
INSERT INTO "new_Material" ("contentList", "createdAt", "id", "imageList", "updatedAt") SELECT "contentList", "createdAt", "id", "imageList", "updatedAt" FROM "Material"; | ||
DROP TABLE "Material"; | ||
ALTER TABLE "new_Material" RENAME TO "Material"; | ||
PRAGMA foreign_key_check; | ||
PRAGMA foreign_keys=ON; |
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,2 @@ | ||
-- DropIndex | ||
DROP INDEX "Audio_materialId_key"; |
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