Skip to content

Commit 08583c1

Browse files
feat(tg-groups): impl update and delete groups
1 parent 124b476 commit 08583c1

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "backend",
3-
"version": "0.8.1",
3+
"version": "0.8.2",
44
"description": "PoliNetwork backend server",
55
"private": true,
66
"keywords": [],

backend/src/routers/tg/groups.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { DB, SCHEMA } from "@/db";
22
import { createTRPCRouter, publicProcedure } from "@/trpc";
3-
import { and, eq, ilike } from "drizzle-orm";
3+
import { and, eq, ilike, sql } from "drizzle-orm";
44
import { z } from "zod";
55

6+
const GROUPS = SCHEMA.TG.groups;
67
export default createTRPCRouter({
78
getAll: publicProcedure.query(async () => {
8-
const results = await DB.select().from(SCHEMA.TG.groups);
9+
const results = await DB.select().from(GROUPS);
910
return results;
1011
}),
1112

@@ -17,7 +18,7 @@ export default createTRPCRouter({
1718
)
1819
.query(async ({ input }) => {
1920
return await DB.select()
20-
.from(SCHEMA.TG.groups)
21+
.from(GROUPS)
2122
.where((t) =>
2223
and(...input.query.split(" ").map((q) => ilike(t.title, `%${q}%`))),
2324
);
@@ -31,7 +32,7 @@ export default createTRPCRouter({
3132
)
3233
.query(async ({ input }) => {
3334
return await DB.select()
34-
.from(SCHEMA.TG.groups)
35+
.from(GROUPS)
3536
.limit(1)
3637
.where((t) => eq(t.telegramId, input.telegramId));
3738
}),
@@ -46,11 +47,32 @@ export default createTRPCRouter({
4647
}),
4748
),
4849
)
50+
.output(z.array(z.number()))
4951
.mutation(async ({ input }) => {
50-
const rows = await DB.insert(SCHEMA.TG.groups)
52+
const rows = await DB.insert(GROUPS)
5153
.values(input)
52-
.onConflictDoNothing()
54+
.onConflictDoUpdate({
55+
target: GROUPS.telegramId,
56+
set: {
57+
title: sql.raw(`excluded.${GROUPS.title.name}`),
58+
link: sql.raw(`excluded.${GROUPS.link.name}`),
59+
},
60+
})
5361
.returning();
5462
return rows.map((r) => r.telegramId);
5563
}),
64+
65+
delete: publicProcedure
66+
.input(
67+
z.object({
68+
telegramId: z.number(),
69+
}),
70+
)
71+
.output(z.boolean())
72+
.mutation(async ({ input }) => {
73+
const rows = await DB.delete(GROUPS)
74+
.where(eq(GROUPS.telegramId, input.telegramId))
75+
.returning();
76+
return rows.length === 1;
77+
}),
5678
});

package/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@polinetwork/backend",
3-
"version": "0.8.1",
3+
"version": "0.8.2",
44
"description": "Utils to interact with the backend.",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)