mirror of
https://github.com/bubblecup-12/VogelSocialMedia.git
synced 2025-07-08 03:28:50 +00:00
added endpoint for getting postTags and fixed follow bug
This commit is contained in:
parent
268a00d026
commit
5ead261db2
8 changed files with 92 additions and 14 deletions
|
@ -2,11 +2,12 @@ import express, { Request, Response } from "express";
|
|||
import { StatusCodes } from "http-status-codes";
|
||||
import { PrismaClient, Post } from "../../prisma/app/generated/prisma/client";
|
||||
import { feedQuerySchema } from "../schemas/feedSchemas";
|
||||
import { z } from "zod";
|
||||
const prisma = new PrismaClient();
|
||||
export const feed = async (req: Request, res: Response) => {
|
||||
try {
|
||||
const query = feedQuerySchema.parse(req.query);
|
||||
const take = query.limit || 10;
|
||||
const take = query.limit;
|
||||
|
||||
const posts = await prisma.post.findMany({
|
||||
take: take + 1,
|
||||
|
@ -25,6 +26,13 @@ export const feed = async (req: Request, res: Response) => {
|
|||
|
||||
res.status(200).json({ posts, nextCursor });
|
||||
} catch (err) {
|
||||
if (err instanceof z.ZodError) {
|
||||
res.status(400).json({
|
||||
error: "Invalid query parameters",
|
||||
details: err.errors,
|
||||
});
|
||||
return;
|
||||
}
|
||||
console.error(err);
|
||||
res.status(400).json({ error: "Invalid query parameters" });
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ import express, { Request, Response } from "express";
|
|||
import { JwtPayload } from "jsonwebtoken";
|
||||
import { PrismaClient, Follow } from "../../prisma/app/generated/prisma/client";
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export const followUser = async (req: Request, res: Response) => {
|
||||
const username: string = req.params.username;
|
||||
const followingUser: JwtPayload = req.user!;
|
||||
|
@ -27,7 +28,7 @@ export const followUser = async (req: Request, res: Response) => {
|
|||
});
|
||||
return;
|
||||
}
|
||||
if (user.username == username) {
|
||||
if (user.id == followingUser.sub) {
|
||||
res.status(StatusCodes.BAD_REQUEST).json({
|
||||
error: "Bad Request",
|
||||
details: [{ message: "You can`t follow yourself" }],
|
||||
|
|
|
@ -4,7 +4,6 @@ import { StatusCodes } from "http-status-codes";
|
|||
import { JwtPayload } from "jsonwebtoken";
|
||||
import { PrismaClient, Post } from "../../prisma/app/generated/prisma/client";
|
||||
import { minioClient } from "../server";
|
||||
import { object } from "zod";
|
||||
import { uploadPostSchema } from "../schemas/postSchemas";
|
||||
dotenv.config();
|
||||
const prisma = new PrismaClient();
|
||||
|
@ -190,11 +189,20 @@ export const getPost = async (req: Request, res: Response) => {
|
|||
// get all posts from a user
|
||||
export const getUserPosts = async (req: Request, res: Response) => {
|
||||
try {
|
||||
const user: JwtPayload | undefined = req.user;
|
||||
const username: string = req.params.username;
|
||||
const posts = await prisma.post.findMany({
|
||||
where: {
|
||||
...(user
|
||||
? {
|
||||
OR: [
|
||||
{ status: "PRIVATE", userId: user.id },
|
||||
{ status: "PUBLIC" },
|
||||
],
|
||||
}
|
||||
: { status: "PUBLIC" }),
|
||||
user: {
|
||||
username: username, // hier greifst du auf die relationierte User-Tabelle zu
|
||||
username: username,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
@ -329,3 +337,31 @@ export const removeLike = async (req: Request, res: Response) => {
|
|||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const getTags = async (req: Request, res: Response) => {
|
||||
try {
|
||||
const tags = await prisma.tag.findMany({
|
||||
take: 150,
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
posts: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
posts: {
|
||||
_count: "desc",
|
||||
},
|
||||
},
|
||||
});
|
||||
const data: string[] = tags.map((tag) => tag.name);
|
||||
res.status(StatusCodes.OK).json(data);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({
|
||||
error: "Failed to retrieve tags",
|
||||
details: [{ message: "Internal server error" }],
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
|
@ -19,6 +19,7 @@ type PublicUser = {
|
|||
profilePictureUrl: string | null;
|
||||
followers: number;
|
||||
following: number;
|
||||
posts: number;
|
||||
};
|
||||
const getUser: (userId: string) => Promise<PublicUser | undefined> = async (
|
||||
userId: string
|
||||
|
@ -47,6 +48,11 @@ const getUser: (userId: string) => Promise<PublicUser | undefined> = async (
|
|||
followingUserId: userId,
|
||||
},
|
||||
});
|
||||
const postCount = await prisma.post.count({
|
||||
where: {
|
||||
userId: userId,
|
||||
},
|
||||
});
|
||||
const publicUser: PublicUser = {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
|
@ -54,6 +60,7 @@ const getUser: (userId: string) => Promise<PublicUser | undefined> = async (
|
|||
profilePictureUrl,
|
||||
followers: followerCount,
|
||||
following: followingCount,
|
||||
posts: postCount,
|
||||
};
|
||||
return publicUser;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue