Refactor editAnnuncio functionality to use AnnunciUpdate schema
- Updated the editAnnuncio procedure to accept a new input structure containing annuncioId and data. - Removed the old EdiAnnuncioSchema and replaced it with a direct use of AnnunciUpdate. - Simplified the editAnnuncioHandler to directly spread the data object into the database update call. - Renamed getAnnuncioById to getAnnuncioByIdForEdit for clarity.
This commit is contained in:
parent
26f1664696
commit
9d80dad9bd
3 changed files with 1396 additions and 102 deletions
File diff suppressed because it is too large
Load diff
|
|
@ -1,4 +1,5 @@
|
||||||
import { z } from "zod/v4";
|
import { z } from "zod/v4";
|
||||||
|
import { type AnnunciUpdate } from "~/schemas/public/Annunci";
|
||||||
import {
|
import {
|
||||||
adminProcedure,
|
adminProcedure,
|
||||||
createTRPCRouter,
|
createTRPCRouter,
|
||||||
|
|
@ -6,7 +7,6 @@ import {
|
||||||
publicProcedure,
|
publicProcedure,
|
||||||
} from "~/server/api/trpc";
|
} from "~/server/api/trpc";
|
||||||
import {
|
import {
|
||||||
EdiAnnuncioSchema,
|
|
||||||
editAnnuncioHandler,
|
editAnnuncioHandler,
|
||||||
getProprietarioDataHandler,
|
getProprietarioDataHandler,
|
||||||
getCodici_AnnunciHandler,
|
getCodici_AnnunciHandler,
|
||||||
|
|
@ -45,7 +45,7 @@ export const annunciRouter = createTRPCRouter({
|
||||||
.query(async ({ input }) => {
|
.query(async ({ input }) => {
|
||||||
return await getAnnunciMetaByCod({ ...input });
|
return await getAnnunciMetaByCod({ ...input });
|
||||||
}),
|
}),
|
||||||
getAnnuncioById: publicProcedure
|
getAnnuncioByIdForEdit: publicProcedure
|
||||||
.input(
|
.input(
|
||||||
z.object({
|
z.object({
|
||||||
id: zAnnuncioId,
|
id: zAnnuncioId,
|
||||||
|
|
@ -88,7 +88,9 @@ export const annunciRouter = createTRPCRouter({
|
||||||
}),
|
}),
|
||||||
|
|
||||||
editAnnuncio: adminProcedure
|
editAnnuncio: adminProcedure
|
||||||
.input(EdiAnnuncioSchema)
|
.input(
|
||||||
|
z.object({ annuncioId: zAnnuncioId, data: z.custom<AnnunciUpdate>() }),
|
||||||
|
)
|
||||||
.mutation(async ({ input }) => {
|
.mutation(async ({ input }) => {
|
||||||
return await editAnnuncioHandler({
|
return await editAnnuncioHandler({
|
||||||
...input,
|
...input,
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
import type { Annunci, AnnunciId } from "~/schemas/public/Annunci";
|
import type {
|
||||||
|
Annunci,
|
||||||
|
AnnunciId,
|
||||||
|
AnnunciUpdate,
|
||||||
|
} from "~/schemas/public/Annunci";
|
||||||
import { TRPCError } from "@trpc/server";
|
import { TRPCError } from "@trpc/server";
|
||||||
import { createSrcset } from "~/server/services/imageServer";
|
import { createSrcset } from "~/server/services/imageServer";
|
||||||
import { AnnuncioObjectWithImages } from "~/server/services/annunci.service";
|
import { AnnuncioObjectWithImages } from "~/server/services/annunci.service";
|
||||||
import { zAnnuncioId } from "~/server/utils/zod_types";
|
|
||||||
import { z } from "zod/v4";
|
|
||||||
import { db } from "~/server/db";
|
import { db } from "~/server/db";
|
||||||
import type { ServizioServizioId } from "~/schemas/public/Servizio";
|
import type { ServizioServizioId } from "~/schemas/public/Servizio";
|
||||||
import { env } from "~/env.mjs";
|
import { env } from "~/env.mjs";
|
||||||
|
|
@ -132,7 +134,7 @@ export const getAnnunciById = async ({
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return AnnuncioObjectWithImages<typeof annuncio>(annuncio);
|
return annuncio;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
code: "INTERNAL_SERVER_ERROR",
|
code: "INTERNAL_SERVER_ERROR",
|
||||||
|
|
@ -350,34 +352,18 @@ export const getOptions_AnnunciHandler = async () => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const EdiAnnuncioSchema = z.object({
|
|
||||||
annuncioId: zAnnuncioId,
|
|
||||||
titolo_it: z.string().nonempty("Inserisci un titolo valido"),
|
|
||||||
desc_it: z.string().nonempty("Inserisci una descrizione valida"),
|
|
||||||
titolo_en: z.string().nonempty("Inserisci un titolo valido"),
|
|
||||||
desc_en: z.string().nonempty("Inserisci una descrizione valida"),
|
|
||||||
status: z.string().nonempty("Seleziona uno stato"),
|
|
||||||
});
|
|
||||||
|
|
||||||
type EditAnnuncioFormValues = z.infer<typeof EdiAnnuncioSchema>;
|
|
||||||
|
|
||||||
export const editAnnuncioHandler = async ({
|
export const editAnnuncioHandler = async ({
|
||||||
annuncioId,
|
annuncioId,
|
||||||
titolo_it,
|
data,
|
||||||
desc_it,
|
}: {
|
||||||
titolo_en,
|
annuncioId: AnnunciId;
|
||||||
desc_en,
|
data: AnnunciUpdate;
|
||||||
status,
|
}) => {
|
||||||
}: EditAnnuncioFormValues) => {
|
|
||||||
try {
|
try {
|
||||||
await db
|
await db
|
||||||
.updateTable("annunci")
|
.updateTable("annunci")
|
||||||
.set({
|
.set({
|
||||||
titolo_it: titolo_it,
|
...data,
|
||||||
desc_it: desc_it,
|
|
||||||
titolo_en: titolo_en,
|
|
||||||
desc_en: desc_en,
|
|
||||||
stato: status,
|
|
||||||
})
|
})
|
||||||
.where("id", "=", annuncioId)
|
.where("id", "=", annuncioId)
|
||||||
.execute();
|
.execute();
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue