Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Commit

Permalink
feat(group): add members modify
Browse files Browse the repository at this point in the history
  • Loading branch information
AsyncFox committed Nov 18, 2023
1 parent 50e0ee3 commit 279e045
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
26 changes: 22 additions & 4 deletions src/controllers/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import type { TGroup } from '../serializer/group';
import { groupSerializer } from '../serializer/group';
import { usersToGroups } from '../db/schema/userToGroup';
import { papersToGroups } from '../db/schema/paperToGroup';
import { LibsqlError } from '@libsql/client';

export class GroupController {
async create(newGroup: {
leader: string
members: string[]
papers?: string[]
archived?: boolean
leader: string
members: string[]
papers?: string[]
archived?: boolean
}) {
const { leader, members, papers, archived } = newGroup;
const group = {
Expand Down Expand Up @@ -60,6 +61,23 @@ export class GroupController {
}
}

async modifyMembers(groupId: string, newMembers: string[], newLeader: string) {
if (!newMembers.includes(newLeader)) return { success: false, message: '组长需要包含于组员中' };
if (!(await db.select().from(groups).where(eq(groups.id, groupId))).length) return { success: false, message: '小组id不存在' };
try {
await db.delete(usersToGroups).where(eq(usersToGroups.groupId, groupId));
for (const userId of newMembers) {
await db.insert(usersToGroups).values({ userId, groupId });
}
return { success: true, message: '修改成功' };
}
catch (err) {
if (err instanceof LibsqlError && err.code === 'SQLITE_CONSTRAINT_FOREIGNKEY')
return { success: false, message: '传入了不存在的用户id' };
return { success: false, message: '服务器内部错误' };
}
}

async getContent(id: string) {
try {
const info = (await db.select().from(groups).where(eq(groups.id, id)))[0];
Expand Down
14 changes: 13 additions & 1 deletion src/routers/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,19 @@ export const groupRouter = router({
else
return res.res;
}),

modify: protectedProcedure
.input(z.object({
groupId: z.string().min(1, '不可以传入空ID'),
newMembers: z.array(z.string().min(1, '不可以传入空ID')).nonempty('不可以传入空ID列表'),
newLeader: z.string().min(1, '不可以传入空ID')
}))
.mutation(async ({ ctx, input }) => {
const res = await ctx.groupController.modifyMembers(input.groupId, input.newMembers, input.newLeader);
if (!res.success)
throw new TRPCError({ code: 'BAD_REQUEST', message: res.message });
else
return res;
}),
remove: protectedProcedure
.input(z.object({ id: z.string().min(1, '小组id不存在') }))
.use(requireRoles(['admin', 'teacher']))
Expand Down

0 comments on commit 279e045

Please sign in to comment.