-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enhanced getList for category and discount
- Loading branch information
Showing
4 changed files
with
52 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,27 @@ | ||
import type { Category, Media } from "~/payload/payload-types"; | ||
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc"; | ||
import { ZGetListParams } from "~/server/types"; | ||
|
||
interface CategoryIncluded extends Category { | ||
icon: Media; | ||
} | ||
|
||
export const categoryRouter = createTRPCRouter({ | ||
getList: publicProcedure.query(async ({ ctx }) => { | ||
const categories = await ctx.payload.find({ | ||
collection: "categories", | ||
sort: "createdAt", | ||
limit: 100, | ||
}); | ||
getList: publicProcedure | ||
.input(ZGetListParams) | ||
.query(async ({ ctx, input }) => { | ||
const { perPage, page, sort } = input; | ||
|
||
return { data: categories.docs as CategoryIncluded[] }; | ||
}), | ||
const categories = await ctx.payload.find({ | ||
collection: "categories", | ||
limit: perPage, | ||
page: page, | ||
sort, | ||
}); | ||
|
||
return { | ||
data: categories.docs as CategoryIncluded[], | ||
metadata: { page, count: categories.docs.length }, | ||
}; | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,28 @@ | ||
import { Category, Discount, Media, Partner } from "~/payload/payload-types"; | ||
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc"; | ||
import { ZGetListParams } from "~/server/types"; | ||
|
||
interface DiscountIncluded extends Discount { | ||
partner: Partner & { icon: Media }; | ||
category: Category & { icon: Media }; | ||
} | ||
|
||
export const discountRouter = createTRPCRouter({ | ||
getList: publicProcedure.query(async ({ ctx }) => { | ||
const discounts = await ctx.payload.find({ | ||
collection: "discounts", | ||
limit: 100, | ||
}); | ||
getList: publicProcedure | ||
.input(ZGetListParams) | ||
.query(async ({ ctx, input }) => { | ||
const { perPage, page, sort } = input; | ||
|
||
return { data: discounts.docs as DiscountIncluded[] }; | ||
}), | ||
const discounts = await ctx.payload.find({ | ||
collection: "discounts", | ||
limit: perPage, | ||
page: page, | ||
sort, | ||
}); | ||
|
||
return { | ||
data: discounts.docs as DiscountIncluded[], | ||
metadata: { page, count: discounts.docs.length }, | ||
}; | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { z } from "zod"; | ||
|
||
export const ZGetListParams = z.object({ | ||
page: z.number(), | ||
perPage: z.number(), | ||
sort: z.string().optional(), | ||
}); | ||
|
||
export type TGetListParams = z.infer<typeof ZGetListParams>; |