Browse Source

feat: add ideas API with CRUD operations and database schema

- Implemented GET endpoint for listing ideas with pagination.
- Implemented POST endpoint for creating new ideas with validation.
- Added service functions for listing and creating ideas, including random color assignment.
- Created database schema for ideas in the migration files.
- Updated migration journal to reflect new versioning and changes.
acas
npmrun 1 month ago
parent
commit
aa3a011e9f
  1. 2
      .env.example
  2. 207
      app/components/index/IdeaCard.vue
  3. 498
      app/pages/index/index.vue
  4. 0
      db.sqlite
  5. BIN
      packages/drizzle-pkg/db.sqlite
  6. 18
      packages/drizzle-pkg/lib/schema/content.ts
  7. 1
      packages/drizzle-pkg/migrations/0007_add_card_content.sql
  8. 4
      packages/drizzle-pkg/migrations/0008_magical_black_knight.sql
  9. 9
      packages/drizzle-pkg/migrations/0009_ideas.sql
  10. 1
      packages/drizzle-pkg/migrations/0010_broken_machine_man.sql
  11. 1303
      packages/drizzle-pkg/migrations/meta/0008_snapshot.json
  12. 1364
      packages/drizzle-pkg/migrations/meta/0010_snapshot.json
  13. 20
      packages/drizzle-pkg/migrations/meta/_journal.json
  14. 10
      server/api/ideas/index.get.ts
  15. 18
      server/api/ideas/index.post.ts
  16. 106
      server/service/ideas/index.ts

2
.env.example

@ -1,7 +1,6 @@
DATABASE_URL=file:./db.sqlite
STATIC_DIR=static
UPLOAD_SUBDIR=upload
NITRO_PORT=3399
SCHEDULER_MAX_CONCURRENCY=5
SCHEDULER_LOG_RETENTION_DAYS=30
BOOTSTRAP_ADMIN_USERNAME=admin
@ -11,4 +10,5 @@ GITHUB_CLIENT_SECRET=your_github_client_secret
GITEA_CLIENT_ID=your_gitea_client_id
GITEA_CLIENT_SECRET=your_gitea_client_secret
GITEA_URL=https://gitea.com
NITRO_PORT=3399
APP_URL=http://localhost:3399

207
app/components/index/IdeaCard.vue

@ -0,0 +1,207 @@
<script setup lang="ts">
export interface IdeaItem {
id: number
author: string | null
content: string
platform: string | null
color: string | null
createdAt: string
}
const props = defineProps<{
idea: IdeaItem
index: number
focused: boolean
}>()
const emit = defineEmits<{
select: [id: number]
}>()
const cardColor = computed(() => props.idea.color || '#cc785c')
// Convert hex to lighter variant for background
function hexToRgba(hex: string, alpha: number): string {
const r = parseInt(hex.slice(1, 3), 16)
const g = parseInt(hex.slice(3, 5), 16)
const b = parseInt(hex.slice(5, 7), 16)
return `rgba(${r}, ${g}, ${b}, ${alpha})`
}
const bgColor = computed(() => hexToRgba(cardColor.value, 0.12))
const borderColor = computed(() => hexToRgba(cardColor.value, 0.3))
const accentDot = computed(() => cardColor.value)
// Random subtle rotation for sticky-note feel
const tilt = computed(() => {
// Use id as seed for consistent rotation
const seed = props.idea.id * 7 + 3
return (seed % 5) - 2 // -2 to +2 degrees
})
const formattedDate = computed(() => {
const d = new Date(props.idea.createdAt)
return d.toLocaleDateString('zh-CN', { month: 'short', day: 'numeric' })
})
</script>
<template>
<div
:class="['idea-card', { 'idea-card--focused': focused }]"
:style="{
'--card-bg': bgColor,
'--card-border': borderColor,
'--card-accent': accentDot,
'--card-tilt': tilt + 'deg',
'--card-delay': index * 60 + 'ms',
}"
@click="emit('select', idea.id)"
>
<div class="card-inner">
<div class="card-accent-dot" />
<span v-if="idea.platform" class="card-platform">{{ idea.platform }}</span>
<p class="card-content">{{ idea.content }}</p>
<div class="card-footer">
<span class="card-author">{{ idea.author || '匿名' }}</span>
<span class="card-date">{{ formattedDate }}</span>
</div>
</div>
</div>
</template>
<style scoped>
.idea-card {
--card-bg: rgba(204, 120, 92, 0.12);
--card-border: rgba(204, 120, 92, 0.3);
--card-accent: #cc785c;
--card-tilt: 0deg;
--card-delay: 0ms;
background: var(--card-bg);
border: 1px solid var(--card-border);
border-radius: 12px;
cursor: pointer;
transform: rotate(var(--card-tilt));
transition: all 0.35s var(--ease-out-quart, cubic-bezier(0.25, 1, 0.5, 1));
animation: card-enter 0.5s var(--ease-out-expo, cubic-bezier(0.16, 1, 0.3, 1)) both;
animation-delay: var(--card-delay);
position: relative;
overflow: hidden;
}
.idea-card::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: linear-gradient(135deg, var(--card-accent) 0%, transparent 60%);
opacity: 0;
transition: opacity 0.35s ease;
border-radius: 12px;
z-index: 0;
}
.idea-card:hover {
transform: rotate(0deg) translateY(-4px) scale(1.03);
border-color: var(--card-accent);
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
}
.idea-card:hover::before {
opacity: 0.06;
}
.idea-card--focused {
transform: rotate(0deg) scale(1.02);
border-color: var(--card-accent);
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.12);
z-index: 10;
background: var(--card-bg);
}
.idea-card--focused::before {
opacity: 0.1;
}
.card-inner {
position: relative;
z-index: 1;
padding: 20px;
display: flex;
flex-direction: column;
min-height: 120px;
}
.card-accent-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--card-accent);
margin-bottom: 12px;
opacity: 0.8;
}
.card-platform {
display: inline-block;
align-self: flex-start;
padding: 2px 10px;
font-size: 11px;
font-weight: 600;
color: var(--card-accent);
background: var(--card-bg);
border: 1px solid var(--card-border);
border-radius: 12px;
margin-bottom: 10px;
letter-spacing: 0.02em;
}
.card-content {
font-size: 15px;
line-height: 1.6;
color: var(--color-body, #3d3d3a);
margin: 0 0 14px;
flex: 1;
display: -webkit-box;
-webkit-line-clamp: 4;
-webkit-box-orient: vertical;
overflow: hidden;
word-break: break-word;
}
.card-footer {
display: flex;
justify-content: space-between;
align-items: center;
font-size: 12px;
}
.card-author {
color: var(--card-accent);
font-weight: 600;
letter-spacing: 0.02em;
}
.card-date {
color: var(--color-muted-soft, #8e8b82);
}
@keyframes card-enter {
from {
opacity: 0;
transform: rotate(var(--card-tilt)) translateY(20px) scale(0.95);
}
to {
opacity: 1;
transform: rotate(var(--card-tilt)) translateY(0) scale(1);
}
}
@media (prefers-reduced-motion: reduce) {
.idea-card {
animation: none;
transition: none;
}
}
</style>

498
app/pages/index/index.vue

@ -2,57 +2,499 @@
definePageMeta({
layout: 'home',
})
const { $toast } = useNuxtApp()
// Form state
const author = ref('')
const content = ref('')
const platform = ref<string | null>(null)
const submitting = ref(false)
// Platform options
const platforms = ['网站', 'Windows 应用', 'App', '小程序', '插件/扩展', '命令行工具', '其他']
// Ideas list
const page = ref(1)
const { data, refresh, pending } = await useFetch('/api/ideas', {
query: computed(() => ({ page: page.value, pageSize: 30 })),
})
const ideas = computed(() => (data.value as any)?.data?.items ?? [])
const hasMore = computed(() => (data.value as any)?.data?.hasMore ?? false)
// Detail dialog
const selectedIdea = ref<any>(null)
const showDetail = ref(false)
function openDetail(idea: any) {
selectedIdea.value = idea
showDetail.value = true
}
function closeDetail() {
showDetail.value = false
}
// Submit
async function submitIdea() {
const trimmedAuthor = author.value.trim()
const trimmedContent = content.value.trim()
if (!trimmedContent) {
$toast.warning('请输入你的想法')
return
}
submitting.value = true
try {
await $fetch('/api/ideas', {
method: 'POST',
body: { author: trimmedAuthor, content: trimmedContent, platform: platform.value },
})
author.value = ''
content.value = ''
platform.value = null
$toast.success('想法已发布 ✨')
await refresh()
} catch (e: any) {
$toast.error(e?.data?.message || '发布失败,请重试')
} finally {
submitting.value = false
}
}
// Load more
async function loadMore() {
page.value++
await refresh()
}
</script>
<template>
<div class="home-page">
<div class="hero">
<h1 class="hero-title">欢迎来到 Dash</h1>
<p class="hero-sub">一个收集分享和交流的地方</p>
</div>
<div class="home-split">
<!-- Left: Form Panel -->
<aside class="form-panel">
<div class="form-sticky">
<div class="form-header">
<h2 class="form-title">想开发什么</h2>
<p class="form-desc">每一个点子都值得被看见</p>
</div>
<div class="form-body">
<!-- Platform selector -->
<div class="field">
<label class="field-label">目标平台</label>
<div class="platform-chips">
<button
v-for="p in platforms"
:key="p"
type="button"
:class="['platform-chip', { 'platform-chip--active': platform === p }]"
:disabled="submitting"
@click="platform = platform === p ? null : p"
>
{{ p }}
</button>
</div>
</div>
<div class="field">
<label class="field-label" for="author-input">你的昵称</label>
<input
id="author-input"
v-model="author"
type="text"
class="field-input"
placeholder="怎么称呼你?(选填)"
maxlength="30"
:disabled="submitting"
/>
</div>
<div class="field">
<label class="field-label" for="content-input">你的想法</label>
<textarea
id="content-input"
v-model="content"
class="field-textarea"
placeholder="写下你的点子、灵感或想法..."
rows="4"
maxlength="500"
:disabled="submitting"
/>
<span class="field-count">{{ content.length }}/500</span>
</div>
<BoButton
type="primary"
size="large"
:loading="submitting"
class="submit-btn"
@click="submitIdea"
>
发布想法
</BoButton>
</div>
</div>
</aside>
<!-- Right: Card Collection -->
<main class="cards-panel">
<!-- Empty state -->
<div v-if="!pending && ideas.length === 0" class="empty-state">
<div class="empty-icon">💡</div>
<h3 class="empty-title">还没有想法</h3>
<p class="empty-desc">成为第一个分享的人吧</p>
</div>
<!-- Loading -->
<div v-if="pending && ideas.length === 0" class="loading-state">
<div class="loading-spinner" />
<p>加载中...</p>
</div>
<!-- Cards grid -->
<div v-if="ideas.length > 0" class="cards-grid">
<IndexIdeaCard
v-for="(idea, idx) in ideas"
:key="idea.id"
:idea="idea"
:index="idx"
:focused="selectedIdea?.id === idea.id"
@select="openDetail(idea)"
/>
</div>
<!-- Load more -->
<div v-if="hasMore" class="load-more-wrap">
<BoButton
type="secondary"
size="medium"
:loading="pending"
@click="loadMore"
>
加载更多
</BoButton>
</div>
</main>
</div>
<!-- Detail Dialog -->
<BoDialog v-model:show="showDetail" @close="closeDetail">
<div v-if="selectedIdea" class="detail-card" :style="{ '--detail-accent': selectedIdea.color || '#cc785c' }">
<div class="detail-accent-bar" />
<div class="detail-body">
<div class="detail-meta">
<span class="detail-author">{{ selectedIdea.author || '匿名' }}</span>
<span v-if="selectedIdea.platform" class="detail-platform">{{ selectedIdea.platform }}</span>
<span class="detail-date">{{ new Date(selectedIdea.createdAt).toLocaleDateString('zh-CN', { year: 'numeric', month: 'long', day: 'numeric' }) }}</span>
</div>
<p class="detail-content">{{ selectedIdea.content }}</p>
<div class="detail-footer">
<BoButton type="secondary" size="small" @click="closeDetail">关闭</BoButton>
</div>
</div>
</div>
</BoDialog>
</template>
<style scoped>
.home-page {
/* ── Layout ── */
.home-split {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
min-height: calc(100vh - 64px);
padding: 48px 24px;
}
.hero {
text-align: center;
max-width: 600px;
/* ── Left Form Panel ── */
.form-panel {
width: 380px;
flex-shrink: 0;
border-right: 1px solid var(--color-hairline, #e6dfd8);
background: var(--color-canvas, #faf9f5);
}
.hero-title {
font-family: 'Abril Fatface', Georgia, serif;
font-size: 48px;
.form-sticky {
position: sticky;
top: 64px;
padding: 40px 32px;
}
.form-header {
margin-bottom: 32px;
}
.form-title {
font-family: var(--font-display, 'Abril Fatface', Georgia, serif);
font-size: 26px;
font-weight: 400;
color: var(--color-ink, #141413);
letter-spacing: -1px;
margin: 0 0 16px;
line-height: 1.15;
margin: 0 0 6px;
letter-spacing: -0.5px;
}
.hero-sub {
font-size: 18px;
.form-desc {
font-size: 14px;
color: var(--color-muted, #6c6a64);
margin: 0;
font-weight: 400;
letter-spacing: 0;
}
@media (max-width: 640px) {
.hero-title {
font-size: 34px;
/* ── Fields ── */
.form-body {
display: flex;
flex-direction: column;
gap: 20px;
}
.field {
display: flex;
flex-direction: column;
gap: 6px;
position: relative;
}
.field-label {
font-size: 13px;
font-weight: 600;
color: var(--color-body-strong, #252523);
letter-spacing: 0.02em;
}
.field-input,
.field-textarea {
width: 100%;
padding: 10px 14px;
font-size: 14px;
font-family: var(--font-body, Inter, sans-serif);
color: var(--color-body, #3d3d3a);
background: var(--color-surface-soft, #f5f0e8);
border: 1px solid var(--color-hairline, #e6dfd8);
border-radius: 8px;
outline: none;
transition: border-color 0.2s ease, box-shadow 0.2s ease;
resize: vertical;
box-sizing: border-box;
}
.field-input:focus,
.field-textarea:focus {
border-color: var(--color-primary, #cc785c);
box-shadow: 0 0 0 3px rgba(204, 120, 92, 0.1);
}
.field-textarea {
min-height: 100px;
line-height: 1.6;
}
.field-count {
position: absolute;
bottom: -18px;
right: 0;
font-size: 11px;
color: var(--color-muted-soft, #8e8b82);
}
/* ── Platform Chips ── */
.platform-chips {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.platform-chip {
padding: 6px 14px;
font-size: 13px;
font-family: var(--font-body, Inter, sans-serif);
color: var(--color-body, #3d3d3a);
background: var(--color-surface-soft, #f5f0e8);
border: 1px solid var(--color-hairline, #e6dfd8);
border-radius: 20px;
cursor: pointer;
transition: all 0.2s ease;
outline: none;
}
.platform-chip:hover {
border-color: var(--color-primary, #cc785c);
color: var(--color-primary, #cc785c);
}
.platform-chip--active {
background: var(--color-primary, #cc785c);
border-color: var(--color-primary, #cc785c);
color: #fff;
}
.platform-chip:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.submit-btn {
margin-top: 4px;
width: 100%;
}
/* ── Right Cards Panel ── */
.cards-panel {
flex: 1;
padding: 40px 36px;
overflow-y: auto;
background: linear-gradient(
180deg,
var(--color-canvas, #faf9f5) 0%,
var(--color-surface-soft, #f5f0e8) 100%
);
}
/* ── Cards Grid ── */
.cards-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
gap: 20px;
}
/* ── Empty State ── */
.empty-state,
.loading-state {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 300px;
color: var(--color-muted, #6c6a64);
}
.empty-icon {
font-size: 48px;
margin-bottom: 12px;
}
.empty-title {
font-size: 18px;
color: var(--color-body, #3d3d3a);
margin: 0 0 6px;
}
.empty-desc {
font-size: 14px;
margin: 0;
}
/* ── Loading ── */
.loading-spinner {
width: 32px;
height: 32px;
border: 3px solid var(--color-hairline, #e6dfd8);
border-top-color: var(--color-primary, #cc785c);
border-radius: 50%;
animation: spin 0.8s linear infinite;
margin-bottom: 12px;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
/* ── Load More ── */
.load-more-wrap {
display: flex;
justify-content: center;
margin-top: 36px;
padding-bottom: 40px;
}
</style>
<style>
/* ── Detail Dialog (non-scoped for Teleport) ── */
.detail-card {
background: var(--color-canvas, #faf9f5);
border-radius: 16px;
overflow: hidden;
max-width: 520px;
width: 90vw;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
}
.detail-accent-bar {
height: 4px;
background: var(--detail-accent, #cc785c);
}
.detail-body {
padding: 28px;
}
.detail-meta {
display: flex;
justify-content: space-between;
align-items: center;
gap: 10px;
margin-bottom: 20px;
}
.detail-author {
font-size: 15px;
font-weight: 600;
color: var(--detail-accent, #cc785c);
letter-spacing: 0.02em;
}
.detail-platform {
font-size: 12px;
font-weight: 600;
padding: 3px 12px;
background: var(--detail-accent, #cc785c);
color: #fff;
border-radius: 14px;
letter-spacing: 0.02em;
}
.detail-date {
font-size: 12px;
color: var(--color-muted-soft, #8e8b82);
}
.detail-content {
font-size: 16px;
line-height: 1.75;
color: var(--color-body, #3d3d3a);
margin: 0 0 24px;
white-space: pre-wrap;
word-break: break-word;
}
.detail-footer {
display: flex;
justify-content: flex-end;
}
/* ── Responsive ── */
@media (max-width: 860px) {
.home-split {
flex-direction: column;
}
.form-panel {
width: 100%;
border-right: none;
border-bottom: 1px solid var(--color-hairline, #e6dfd8);
}
.form-sticky {
position: static;
padding: 28px 20px;
}
.cards-panel {
padding: 28px 20px;
}
.hero-sub {
font-size: 16px;
.cards-grid {
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
gap: 16px;
}
}
</style>

0
db.sqlite

BIN
packages/drizzle-pkg/db.sqlite

Binary file not shown.

18
packages/drizzle-pkg/lib/schema/content.ts

@ -194,6 +194,24 @@ export const articleCards = sqliteTable(
],
);
// ============ Idea(点子/想法)============
export const ideas = sqliteTable(
"ideas",
{
id: integer("id").primaryKey({ autoIncrement: true }),
author: text("author", { length: 30 }).notNull(),
content: text("content").notNull(),
platform: text("platform", { length: 30 }),
color: text("color", { length: 30 }),
createdAt: integer("created_at", { mode: "timestamp_ms" })
.defaultNow()
.notNull(),
},
(table) => [
index("idx_idea_created").on(table.createdAt),
],
);
// ============ ChatMessage(聊天室消息)============
export const chatMessages = sqliteTable(
"chat_messages",

1
packages/drizzle-pkg/migrations/0007_add_card_content.sql

@ -1 +0,0 @@
ALTER TABLE `cards` ADD `content` text;

4
packages/drizzle-pkg/migrations/0008_chat_messages.sql → packages/drizzle-pkg/migrations/0008_magical_black_knight.sql

@ -2,7 +2,9 @@ CREATE TABLE `chat_messages` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`nickname` text(20) NOT NULL,
`content` text NOT NULL,
`created_at` integer NOT NULL
`created_at` integer DEFAULT (cast((julianday('now') - 2440587.5)*86400000 as integer)) NOT NULL,
`client_id` text,
`user_id` integer
);
--> statement-breakpoint
CREATE INDEX `idx_chat_msg_created` ON `chat_messages` (`created_at`);

9
packages/drizzle-pkg/migrations/0009_ideas.sql

@ -0,0 +1,9 @@
CREATE TABLE `ideas` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`author` text(30) NOT NULL,
`content` text NOT NULL,
`color` text(30),
`created_at` integer DEFAULT (cast((julianday('now') - 2440587.5)*86400000 as integer)) NOT NULL
);
--> statement-breakpoint
CREATE INDEX `idx_idea_created` ON `ideas` (`created_at`);

1
packages/drizzle-pkg/migrations/0010_broken_machine_man.sql

@ -0,0 +1 @@
ALTER TABLE ideas ADD platform text(30);

1303
packages/drizzle-pkg/migrations/meta/0008_snapshot.json

File diff suppressed because it is too large

1364
packages/drizzle-pkg/migrations/meta/0010_snapshot.json

File diff suppressed because it is too large

20
packages/drizzle-pkg/migrations/meta/_journal.json

@ -60,9 +60,23 @@
},
{
"idx": 8,
"version": "7",
"when": 1780800000000,
"tag": "0008_chat_messages",
"version": "6",
"when": 1782497421774,
"tag": "0008_magical_black_knight",
"breakpoints": true
},
{
"idx": 9,
"version": "6",
"when": 1782500000000,
"tag": "0009_ideas",
"breakpoints": true
},
{
"idx": 10,
"version": "6",
"when": 1782501620751,
"tag": "0010_broken_machine_man",
"breakpoints": true
}
]

10
server/api/ideas/index.get.ts

@ -0,0 +1,10 @@
import { listIdeas } from "../../service/ideas";
export default defineWrappedResponseHandler(async (event) => {
const query = getQuery(event);
const page = Math.max(1, parseInt(String(query.page || "1")));
const pageSize = Math.min(50, Math.max(1, parseInt(String(query.pageSize || "30"))));
const result = await listIdeas({ page, pageSize });
return R.success(result);
});

18
server/api/ideas/index.post.ts

@ -0,0 +1,18 @@
import { z } from "zod";
import { validate } from "#server/utils/validation";
import { createIdea } from "../../service/ideas";
const createSchema = z.object({
author: z.string().max(30, "昵称不能超过 30 字").nullable().optional(),
content: z.string().min(1, "想法不能为空").max(500, "想法不能超过 500 字"),
platform: z.string().max(30).nullable().optional(),
color: z.string().max(30).nullable().optional(),
});
export default defineWrappedResponseHandler(async (event) => {
const body = await readBody(event);
const data = validate(createSchema, body);
const idea = await createIdea(data);
return R.success(idea);
});

106
server/service/ideas/index.ts

@ -0,0 +1,106 @@
import { dbGlobal } from "drizzle-pkg/lib/db";
import { ideas } from "drizzle-pkg/lib/schema/content";
import { desc, sql } from "drizzle-orm";
// ============ Types ============
export interface IdeaItem {
id: number;
author: string | null;
content: string;
platform: string | null;
color: string | null;
createdAt: Date;
}
export interface CreateIdeaInput {
author: string | null;
content: string;
platform?: string | null;
color?: string | null;
}
// ============ Color palette for random assignment ============
const IDEA_COLORS = [
"#cc785c", // primary coral
"#5db8a6", // accent teal
"#e8a55a", // accent amber
"#8b7ec8", // soft purple
"#6fa8dc", // soft blue
"#e07b7b", // soft red
"#7eb77f", // soft green
"#c9a0dc", // lavender
];
export function randomIdeaColor(): string {
return IDEA_COLORS[Math.floor(Math.random() * IDEA_COLORS.length)];
}
// ============ CRUD ============
export async function listIdeas(opts: {
page?: number;
pageSize?: number;
}): Promise<{ items: IdeaItem[]; total: number; page: number; pageSize: number; hasMore: boolean }> {
const page = opts.page ?? 1;
const pageSize = opts.pageSize ?? 30;
const [rows, countResult] = await Promise.all([
dbGlobal
.select()
.from(ideas)
.orderBy(desc(ideas.createdAt))
.limit(pageSize)
.offset((page - 1) * pageSize),
dbGlobal
.select({ count: sql<number>`count(*)` })
.from(ideas),
]);
// $count returns differently — use sql count as fallback
let total = 0;
if (countResult && countResult.length > 0) {
total = (countResult[0] as any).count ?? 0;
}
const items: IdeaItem[] = rows.map((row) => ({
id: row.id,
author: row.author,
content: row.content,
platform: row.platform,
color: row.color,
createdAt: row.createdAt,
}));
return {
items,
total,
page,
pageSize,
hasMore: page * pageSize < total,
};
}
export async function createIdea(input: CreateIdeaInput): Promise<IdeaItem> {
const color = input.color || randomIdeaColor();
const [inserted] = await dbGlobal
.insert(ideas)
.values({
author: input.author || '',
content: input.content,
platform: input.platform || null,
color,
})
.returning({ id: ideas.id });
if (!inserted) throw new Error("Failed to create idea");
return {
id: inserted.id,
author: input.author,
content: input.content,
platform: input.platform || null,
color,
createdAt: new Date(),
};
}
Loading…
Cancel
Save