51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
import "server-only";
|
|
|
|
import { db } from "@/db";
|
|
import { cdn, shareTexts } from "@/db/schema";
|
|
import {
|
|
createShareAtomically,
|
|
type CreateShareInput,
|
|
} from "@/lib/share/create-core";
|
|
|
|
export type { ShareAuthor } from "@/lib/share/create-core";
|
|
|
|
export async function createShare(input: CreateShareInput) {
|
|
return createShareAtomically(input, {
|
|
transaction: (callback) => db.transaction(async (tx) => callback({
|
|
async insertImage(image, ownerDiscordId) {
|
|
const [record] = await tx
|
|
.insert(cdn)
|
|
.values({
|
|
data: image.bytes,
|
|
name: image.name,
|
|
type: image.type,
|
|
size: image.size,
|
|
ownerDiscordId,
|
|
})
|
|
.returning({ id: cdn.id });
|
|
return record.id;
|
|
},
|
|
async insertShare(share, imageCdnId) {
|
|
const [record] = await tx
|
|
.insert(shareTexts)
|
|
.values({
|
|
content: share.content,
|
|
description: share.description,
|
|
imageCdnId,
|
|
source: share.source,
|
|
authorId: share.author.userId,
|
|
authorDiscordId: share.author.discordId,
|
|
authorName: share.author.displayName,
|
|
authorAvatarUrl: share.author.avatarUrl,
|
|
})
|
|
.returning({
|
|
id: shareTexts.id,
|
|
imageCdnId: shareTexts.imageCdnId,
|
|
createdAt: shareTexts.createdAt,
|
|
});
|
|
return record;
|
|
},
|
|
})),
|
|
});
|
|
}
|