Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 917583e

Browse files
feat: featured properties route
1 parent 6ba5216 commit 917583e

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import connectDB from "@/config/database";
2+
import { NextRequest } from "next/server";
3+
import Property, { PropertyInterface } from "@/models/Property";
4+
5+
// GET /api/properties/featured
6+
export const GET = async (req: NextRequest, res: Response) => {
7+
try {
8+
await connectDB();
9+
10+
const properties: PropertyInterface[] = await Property.find({
11+
is_featured: true,
12+
});
13+
14+
return new Response(JSON.stringify(properties), {
15+
status: 200,
16+
});
17+
} catch (error) {
18+
return new Response("An error occurred", { status: 500 });
19+
}
20+
};

‎07-property-pulse/utils/requests.ts‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
1+
import { PaginatedProperties, PropertyInterface } from "@/models/Property";
2+
13
const apiDomain = process.env.NEXT_PUBLIC_API_DOMAIN || null;
24

5+
type FetchPropertiesParams = {
6+
showFeatured?: boolean;
7+
};
8+
39
// Fetch properties from the API
4-
export async function fetchProperties() {
10+
export async function fetchProperties({
11+
showFeatured = false,
12+
}: FetchPropertiesParams = {}) {
513
try {
6-
if (!apiDomain) return [];
7-
const response = await fetch(`${apiDomain}/properties`, {
8-
cache: "no-store",
9-
});
14+
if (!apiDomain) return showFeatured ? [] : { properties: [], total: 0 };
15+
const response = await fetch(
16+
`${apiDomain}/properties${showFeatured ? "/featured" : ""}`,
17+
{
18+
cache: "no-store",
19+
}
20+
);
1021
if (!response.ok) throw new Error("Failed to fetch data");
1122
return await response.json();
1223
} catch (error) {
1324
console.log(error);
14-
return [];
25+
return showFeatured ? [] : {properties: [],total: 0};
1526
}
1627
}
1728

1829
// Fetch a single property from the API
19-
export async function fetchProperty(id: string) {
30+
export async function fetchProperty(
31+
id: string
32+
): Promise<PropertyInterface | null> {
2033
try {
2134
if (!apiDomain) return null;
2235
const response = await fetch(`${apiDomain}/properties/${id}`);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /