Skip to content

Commit

Permalink
fix: same song check
Browse files Browse the repository at this point in the history
  • Loading branch information
ZTL-UwU committed Dec 5, 2023
1 parent cc7f26e commit 9dd1f4e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ onMounted(async () => {
<div class="grid grid-cols-2 gap-2">
<UiCard class="shadow p-0 pt-1 pb-2">
<UiCardHeader class="pt-1 pb-0 text-3xl font-bold">
{{ songListInfo?.allSongs }}
{{ songListInfo }}
</UiCardHeader>
<UiCardContent class="pt-0 pb-0">
<span class="text-md">已收集歌曲</span>
Expand Down
21 changes: 16 additions & 5 deletions server/trpc/controllers/song.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LibsqlError } from '@libsql/client';
import { and, eq, gt, inArray } from 'drizzle-orm';
import { and, eq, gt, inArray, lt, or } from 'drizzle-orm';
import { type TNewSong, db } from '../../db/db';
import { songs } from '~/server/db/schema';

Expand All @@ -16,14 +16,25 @@ export class SongController {
const sameSong = await db.select().from(songs).where(
and(
eq(songs.name, newSong.name),
gt(songs.createdAt, new Date(Date.now() - 4 * 24 * 60 * 60 * 1000)),
eq(songs.status, 'used'),
),
or(
// Last week
and(
gt(songs.createdAt, new Date(Date.now() - 10 * 24 * 60 * 60 * 1000)),
eq(songs.status, 'used'),
),
// This week
gt(songs.createdAt, new Date(Date.now() - 3 * 24 * 60 * 60 * 1000)),
),
)
);

if (lastSong.length)
return { success: false, message: '每周每人只能投一首歌哦' };
else if (sameSong.length)
else if (sameSong.length && sameSong[0].status === 'used')
return { success: false, message: '上周放过的不能再投稿哦' };
else if (sameSong.length)
return { success: false, message: '本周已投稿的歌不能再投稿哦' };

try {
const id = (await db.insert(songs).values(newSong).returning({ id: songs.id }))[0].id;
return { success: true, res: id, message: '创建成功!' };
Expand Down
3 changes: 1 addition & 2 deletions server/trpc/routers/song.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ export const songRouter = router({
throw new TRPCError({ code: 'BAD_REQUEST', message: res.message });
} else {
const allSongs = res.res.length;
const reviewedSongs = res.res.filter(song => song.status !== 'unset').length;
return { allSongs, reviewedSongs };
return allSongs;
}
}),
});

0 comments on commit 9dd1f4e

Please sign in to comment.