infoalloggi-monorepo/apps/infoalloggi/src/server/controllers/etichette.controller.ts

157 lines
3 KiB
TypeScript
Raw Normal View History

2025-08-28 18:27:07 +02:00
import { TRPCError } from "@trpc/server";
2025-08-04 17:45:44 +02:00
import type { ChatsChatid } from "~/schemas/public/Chats";
import type { EtichetteIdEtichetta } from "~/schemas/public/Etichette";
import type { Querier } from "~/server/db";
export const getAllEtichette = async ({ db }: { db: Querier }) => {
2025-08-28 18:27:07 +02:00
return await db.selectFrom("etichette").selectAll().execute();
2025-08-04 17:45:44 +02:00
};
export const addEtichetta = async ({
2025-08-28 18:27:07 +02:00
db,
title,
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
color_hex,
2025-08-04 17:45:44 +02:00
}: {
2025-08-28 18:27:07 +02:00
db: Querier;
title: string;
color_hex: string;
2025-08-04 17:45:44 +02:00
}) => {
2025-08-28 18:27:07 +02:00
try {
return await db
.insertInto("etichette")
.returning("id_etichetta")
2025-08-29 16:18:32 +02:00
.values({ color_hex, title })
2025-08-28 18:27:07 +02:00
.execute();
} catch {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Errore aggiunta etichetta",
});
}
2025-08-04 17:45:44 +02:00
};
export const updateEtichetta = async ({
2025-08-28 18:27:07 +02:00
db,
etichettaid,
title,
color_hex,
2025-08-04 17:45:44 +02:00
}: {
2025-08-28 18:27:07 +02:00
db: Querier;
etichettaid: EtichetteIdEtichetta;
title: string;
color_hex: string;
2025-08-04 17:45:44 +02:00
}) => {
2025-08-28 18:27:07 +02:00
try {
return await db
.updateTable("etichette")
.returning("id_etichetta")
2025-08-29 16:18:32 +02:00
.set({ color_hex, title })
2025-08-28 18:27:07 +02:00
.where("id_etichetta", "=", etichettaid)
.execute();
} catch {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Errore aggiornamento etichetta",
});
}
2025-08-04 17:45:44 +02:00
};
export const removeEtichetta = async ({
2025-08-28 18:27:07 +02:00
db,
etichettaid,
2025-08-04 17:45:44 +02:00
}: {
2025-08-28 18:27:07 +02:00
db: Querier;
etichettaid: EtichetteIdEtichetta;
2025-08-04 17:45:44 +02:00
}) => {
2025-08-28 18:27:07 +02:00
try {
await db
.deleteFrom("etichette")
.where("id_etichetta", "=", etichettaid)
.execute();
return "ok";
} catch {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Errore rimozione etichetta",
});
}
2025-08-04 17:45:44 +02:00
};
export const getChatEtichette = async ({
2025-08-28 18:27:07 +02:00
db,
chatid,
2025-08-04 17:45:44 +02:00
}: {
2025-08-28 18:27:07 +02:00
db: Querier;
chatid: ChatsChatid;
2025-08-04 17:45:44 +02:00
}) => {
2025-08-28 18:27:07 +02:00
try {
return await db
.selectFrom("etichette")
.innerJoin(
"chats_etichette",
"etichette.id_etichetta",
"chats_etichette.etichettaid",
)
.where("chats_etichette.chatid", "=", chatid)
.select([
"etichette.id_etichetta",
"etichette.title",
"etichette.color_hex",
])
.execute();
} catch {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Errore recupero etichette",
});
}
2025-08-04 17:45:44 +02:00
};
export const addChatEtichette = async ({
2025-08-28 18:27:07 +02:00
db,
chatid,
etichettaid,
2025-08-04 17:45:44 +02:00
}: {
2025-08-28 18:27:07 +02:00
db: Querier;
chatid: ChatsChatid;
etichettaid: EtichetteIdEtichetta;
2025-08-04 17:45:44 +02:00
}) => {
2025-08-28 18:27:07 +02:00
try {
return await db
.insertInto("chats_etichette")
.returning("chatid")
.values({ chatid, etichettaid })
.onConflict((oc) => oc.columns(["chatid", "etichettaid"]).doNothing())
.execute();
} catch {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Errore aggiunta etichetta",
});
}
2025-08-04 17:45:44 +02:00
};
export const removeChatEtichette = async ({
2025-08-28 18:27:07 +02:00
db,
chatid,
etichettaid,
2025-08-04 17:45:44 +02:00
}: {
2025-08-28 18:27:07 +02:00
db: Querier;
chatid: ChatsChatid;
etichettaid: EtichetteIdEtichetta;
2025-08-04 17:45:44 +02:00
}) => {
2025-08-28 18:27:07 +02:00
try {
await db
.deleteFrom("chats_etichette")
.where("chatid", "=", chatid)
.where("etichettaid", "=", etichettaid)
.execute();
return "ok";
} catch {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Errore rimozione etichetta",
});
}
2025-08-04 17:45:44 +02:00
};