-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlikes.ts
More file actions
134 lines (119 loc) · 3.2 KB
/
likes.ts
File metadata and controls
134 lines (119 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* POST /api/user/likes
* Toggle user like for a specific content item.
*/
import { auth } from "@clerk/nextjs/server";
import { NextRequest, NextResponse } from "next/server";
import { toggleUserLike } from "../../../src/lib/user/user-metadata-utils";
import type { LikeToggleRequest } from "../../../src/types/api/requests";
import type { LikeToggleResponse } from "../../../src/types/api/responses";
export async function POST(request: NextRequest) {
try {
const { userId } = await auth();
if (!userId) {
return NextResponse.json(
{
success: false,
error: {
code: "UNAUTHORIZED",
message: "Authentication required",
},
},
{ status: 401 }
);
}
const body: LikeToggleRequest = await request.json();
const { contentType, contentId } = body;
if (!contentType || !contentId) {
return NextResponse.json(
{
success: false,
error: {
code: "INVALID_REQUEST",
message: "Content type and ID are required",
},
},
{ status: 400 }
);
}
const result = await toggleUserLike(userId, contentType, contentId);
const response: LikeToggleResponse = {
success: true,
data: {
isLiked: result.isLiked,
likeCount: result.newCount,
},
};
return NextResponse.json(response);
} catch (error) {
console.error("Error toggling like:", error);
return NextResponse.json(
{
success: false,
error: {
code: "TOGGLE_FAILED",
message: "Failed to toggle like",
details: error instanceof Error ? error.message : "Unknown error",
},
},
{ status: 500 }
);
}
}
/**
* GET /api/user/likes
* Get user's liked content.
*/
export async function GET(request: NextRequest) {
try {
const { userId } = await auth();
if (!userId) {
return NextResponse.json(
{
success: false,
error: {
code: "UNAUTHORIZED",
message: "Authentication required",
},
},
{ status: 401 }
);
}
const { searchParams } = new URL(request.url);
const contentType = searchParams.get("contentType") as
| "ashaar"
| "ghazlen"
| "nazmen"
| "rubai"
| null;
const page = parseInt(searchParams.get("page") || "1");
const limit = parseInt(searchParams.get("limit") || "20");
// Import the getUserLikes function (will need to implement)
// const likes = await getUserLikes(userId, contentType, page, limit);
return NextResponse.json({
success: true,
data: {
likes: [], // Placeholder - implement getUserLikes
pagination: {
page,
limit,
total: 0,
hasMore: false,
},
},
});
} catch (error) {
console.error("Error fetching user likes:", error);
return NextResponse.json(
{
success: false,
error: {
code: "FETCH_FAILED",
message: "Failed to fetch user likes",
details: error instanceof Error ? error.message : "Unknown error",
},
},
{ status: 500 }
);
}
}