+
diff --git a/website/src/pages/api/posts/likes.ts b/website/src/pages/api/posts/likes.ts
new file mode 100644
index 0000000..e9ebd08
--- /dev/null
+++ b/website/src/pages/api/posts/likes.ts
@@ -0,0 +1,88 @@
+import {NextApiRequest, NextApiResponse} from 'next';
+import {getClient, getTables, makeQuery} from "@/lib/databse";
+import {getServerSession} from "next-auth";
+import {authOptions} from "@/pages/api/auth/[...nextauth]";
+import {checkApiPermissions} from "@/lib/api_tools";
+import {getStrings, RongoaUser} from "@/lib/users";
+import { Logger } from 'next-axiom';
+export default async function handler(
+ request: NextApiRequest,
+ response: NextApiResponse,
+) {
+
+ // Get the client
+ const client = await getClient()
+
+ // Get the logger
+ const logger = new Logger()
+
+ // Get the tables
+ const tables = getTables();
+
+ // Check if the user is permitted to access the API
+ const session = await getServerSession(request, response, authOptions)
+ let permission = await checkApiPermissions(request, response, session, client, makeQuery, "api:user:follow:access")
+ if (!permission) return response.status(401).json({error: "Not Authorized"})
+
+ let query = ''
+
+ const {id, publicUserID} = request.query;
+
+ try {
+
+ // Get the session
+ const session = await getServerSession(request, response, authOptions)
+
+ // If there is no session then return an error
+ if (!session || !session.user) {
+ return response.status(401).json({error: 'User not logged in'});
+ }
+
+ // Get the user details
+ const user = session.user as RongoaUser;
+ const userId = user.database.id;
+
+ // Get the operation
+ const {operation, id} = request.query;
+ if (!operation) {
+ return response.status(400).json({error: 'No operation specified'});
+ }
+
+ switch (operation) {
+
+ case "likes":
+ query = `SELECT COUNT(*) FROM likes WHERE ${tables.like_post_id} = ${id}`;
+ break;
+
+ case "like":
+ query = `INSERT INTO likes (${tables.like_user_id}, ${tables.like_post_id}) VALUES (${userId}, ${id})`;
+ break;
+
+ case "unlike":
+ query = `DELETE FROM likes WHERE ${tables.like_user_id} = ${userId} AND ${tables.like_post_id} = ${id}`;
+ break;
+
+ case "check":
+ query = `SELECT COUNT(*) FROM likes WHERE ${tables.like_user_id} = ${userId} AND ${tables.like_post_id} = ${id}`;
+ break;
+
+ case "list":
+ query = `SELECT * FROM likes WHERE ${tables.like_user_id} = ${userId}`;
+ break;
+
+ default:
+ return response.status(400).json({error: 'Invalid operation'});
+
+ }
+
+ const follow = await makeQuery(query, client)
+ return response.status(200).json({data: follow});
+
+ // Execute the query
+
+
+ } catch (e : any) {
+ logger.error(e)
+ return response.status(500).json({error: 'Internal Server Error'})
+ }
+}
diff --git a/website/src/pages/media/components/cards.tsx b/website/src/pages/media/components/cards.tsx
index 5461007..bea0716 100644
--- a/website/src/pages/media/components/cards.tsx
+++ b/website/src/pages/media/components/cards.tsx
@@ -32,6 +32,7 @@ export function PostCard(props: PostCardProps) {
const [likes, setLikes] = useState(0)
const [plantName, setPlantName] = useState("Loading...")
const [width, setWidth] = useState(0)
+ const [liked, setLiked] = useState(false)
const router = useRouter();
const dataFetch = useRef(false);
@@ -116,7 +117,14 @@ export function PostCard(props: PostCardProps) {
}
// Get the likes
- //todo
+ const likes = await makeRequestWithToken("get", `/api/posts/likes?operation=likes&id=${props.id}`);
+ setLikes(likes.data.data[0]["COUNT(*)"]);
+
+ // Check if the user has liked the post
+ const liked = await makeRequestWithToken("get", `/api/posts/likes?operation=check&id=${props.id}`);
+ if(liked.data.data[0]["COUNT(*)"] > 0) {
+ setLiked(true);
+ }
}
@@ -126,6 +134,26 @@ export function PostCard(props: PostCardProps) {
router.push("/media/profile?id="+props.post_user_id)
}
+ const likePost = async () => {
+ await makeRequestWithToken("post", `/api/posts/likes?operation=like&id=${props.id}`);
+ }
+
+ const unlikePost = async () => {
+ await makeRequestWithToken("post", `/api/posts/likes?operation=unlike&id=${props.id}`);
+ }
+
+ const toggleLike = async () => {
+ if(liked) {
+ setLiked(false);
+ await unlikePost();
+ setLikes(likes - 1);
+ } else {
+ setLiked(true);
+ await likePost();
+ setLikes(likes + 1);
+ }
+ }
+
return(
<>
@@ -151,8 +179,8 @@ export function PostCard(props: PostCardProps) {
-