feat(backend): feed, posts & pets APIs frontend-ready with visibility enforcement
This commit is contained in:
1 parent
e1e1b7ab21
commit
06e9240909
7 files changed
+177
-53
No files matched your search
@@ -4,7 +4,7 @@ import { APP_GUARD } from '@nestjs/core';
|
||||
import { PostsModule } from './posts/posts.module';
|
||||
import { FeedModule } from './feed/feed.module';
|
||||
import { PrismaModule } from './prisma/prisma.module';
|
||||
|
||||
import { PetsModule } from './pets/pets.module';
|
||||
import { DummyAuthGuard } from './auth/guards/dummy-auth.guard';
|
||||
import { HealthController } from './health.controller';
|
||||
|
||||
@@ -13,6 +13,7 @@ import { HealthController } from './health.controller';
|
||||
PrismaModule, // Basis zuerst
|
||||
PostsModule, // /posts
|
||||
FeedModule, // /feed (1:1 Spiegel)
|
||||
PetsModule, // /pets
|
||||
],
|
||||
controllers: [
|
||||
HealthController,
|
||||
|
||||
@@ -10,45 +10,51 @@ export class FeedService {
|
||||
async getFeed(user: RequestUser | null) {
|
||||
const userId = user?.id;
|
||||
|
||||
return this.prisma.post.findMany({
|
||||
where: {
|
||||
OR: [
|
||||
{
|
||||
pet: {
|
||||
visibility: 'public',
|
||||
},
|
||||
const posts = await this.prisma.post.findMany({
|
||||
where: {
|
||||
OR: [
|
||||
{
|
||||
pet: {
|
||||
visibility: 'public',
|
||||
},
|
||||
...(userId
|
||||
? [
|
||||
{
|
||||
pet: {
|
||||
ownerId: userId,
|
||||
},
|
||||
},
|
||||
...(userId
|
||||
? [
|
||||
{
|
||||
pet: {
|
||||
ownerId: userId,
|
||||
},
|
||||
{
|
||||
pet: {
|
||||
visibility: 'followers',
|
||||
owner: {
|
||||
followers: {
|
||||
some: {
|
||||
followerId: userId,
|
||||
},
|
||||
},
|
||||
{
|
||||
pet: {
|
||||
visibility: 'followers',
|
||||
owner: {
|
||||
followers: {
|
||||
some: {
|
||||
followerId: userId,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
: []),
|
||||
],
|
||||
},
|
||||
include: {
|
||||
pet: true,
|
||||
author: true,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
});
|
||||
},
|
||||
]
|
||||
: []),
|
||||
],
|
||||
},
|
||||
include: {
|
||||
pet: true,
|
||||
author: true,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
});
|
||||
|
||||
return posts.map((post) => ({
|
||||
...post,
|
||||
isDiscovery: false,
|
||||
}));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +1,40 @@
|
||||
import { Patch, Param, Body, Req } from '@nestjs/common';
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Patch,
|
||||
Param,
|
||||
Body,
|
||||
Req,
|
||||
} from '@nestjs/common';
|
||||
|
||||
import { PetsService } from './pets.service';
|
||||
import { UpdatePetDto } from './dto/update-pet.dto';
|
||||
import { AuthRequest } from '../auth/types/auth-request';
|
||||
|
||||
@Controller('pets')
|
||||
export class PetsController {
|
||||
constructor(private readonly petsService: PetsService) {}
|
||||
|
||||
// ✅ READ-ONLY: Pet-Profil anzeigen
|
||||
@Get(':id')
|
||||
getPet(
|
||||
@Param('id') id: string,
|
||||
@Req() req: AuthRequest,
|
||||
): Promise<{ owner: { id: number; name: string; createdAt: Date; email: string; }; } & { id: number; name: string; type: string; breed: string; birthDate: Date | null; gender: string | null; bio: string | null; avatarUrl: string | null; visibility: string; ownerId: number; }> {
|
||||
return this.petsService.getPetById(Number(id), req.user);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
updatePet(
|
||||
@Param('id') id: string,
|
||||
@Body() dto: UpdatePetDto,
|
||||
@Req() req,
|
||||
@Req() req: AuthRequest,
|
||||
) {
|
||||
return this.petsService.updatePet(
|
||||
Number(id),
|
||||
req.user.id,
|
||||
req.user!.id,
|
||||
dto,
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { PetsService } from './pets.service';
|
||||
import { PetsController } from './pets.controller';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [PetsController],
|
||||
providers: [PetsService],
|
||||
})
|
||||
export class PetsModule {}
|
||||
@@ -2,15 +2,64 @@ import {
|
||||
Injectable,
|
||||
BadRequestException,
|
||||
NotFoundException,
|
||||
ForbiddenException,
|
||||
} from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { CreatePetDto } from './dto/create-pet.dto';
|
||||
import { UpdatePetDto } from './dto/update-pet.dto';
|
||||
import { RequestUser } from '../types/request-user.type';
|
||||
|
||||
@Injectable()
|
||||
export class PetsService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
/**
|
||||
* Read-only: einzelnes Pet für Profilansicht
|
||||
* Visibility-Regeln:
|
||||
* - public: immer sichtbar
|
||||
* - owner: immer sichtbar
|
||||
* - followers: nur für Follower
|
||||
*/
|
||||
async getPetById(id: number, user: RequestUser) {
|
||||
const pet = await this.prisma.pet.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
owner: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!pet) {
|
||||
throw new NotFoundException('Pet not found');
|
||||
}
|
||||
|
||||
// 1️⃣ Public Pets → immer sichtbar
|
||||
if (pet.visibility === 'public') {
|
||||
return pet;
|
||||
}
|
||||
|
||||
// 2️⃣ Owner darf sein Pet immer sehen
|
||||
if (user && pet.ownerId === user.id) {
|
||||
return pet;
|
||||
}
|
||||
|
||||
// 3️⃣ Followers-Visibility
|
||||
if (user && pet.visibility === 'followers') {
|
||||
const isFollower = await this.prisma.follow.findFirst({
|
||||
where: {
|
||||
followerId: user.id,
|
||||
followingId: pet.ownerId,
|
||||
},
|
||||
});
|
||||
|
||||
if (isFollower) {
|
||||
return pet;
|
||||
}
|
||||
}
|
||||
|
||||
// 4️⃣ Alles andere → verboten
|
||||
throw new ForbiddenException('You are not allowed to view this pet');
|
||||
}
|
||||
|
||||
// -------------------------------
|
||||
// CREATE PET
|
||||
// -------------------------------
|
||||
|
||||
@@ -8,24 +8,55 @@ import { RequestUser } from '../types/request-user.type';
|
||||
export class PostsService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async getPosts(_user: RequestUser) {
|
||||
return this.prisma.post.findMany({
|
||||
include: {
|
||||
pet: true,
|
||||
author: true,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
});
|
||||
}
|
||||
async getPosts(user: RequestUser) {
|
||||
const userId = user?.id;
|
||||
|
||||
return this.prisma.post.findMany({
|
||||
where: {
|
||||
OR: [
|
||||
{
|
||||
pet: {
|
||||
visibility: 'public',
|
||||
},
|
||||
},
|
||||
...(userId
|
||||
? [
|
||||
{
|
||||
pet: {
|
||||
ownerId: userId,
|
||||
},
|
||||
},
|
||||
{
|
||||
pet: {
|
||||
visibility: 'followers',
|
||||
owner: {
|
||||
followers: {
|
||||
some: {
|
||||
followerId: userId,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
: []),
|
||||
],
|
||||
},
|
||||
include: {
|
||||
pet: true,
|
||||
author: true,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
async getMyPosts(user: RequestUser) {
|
||||
if (!user) return [];
|
||||
|
||||
return this.prisma.post.findMany({
|
||||
return this.prisma.post.findMany({
|
||||
where: {
|
||||
authorId: Number(user.id), // 👈 explizit casten
|
||||
authorId: user!.id, // 👈 explizit casten
|
||||
},
|
||||
include: {
|
||||
pet: true,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { PrismaService } from '../../prisma/prisma.service';
|
||||
import { PrismaService } from './prisma.service';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
|
||||
Reference in new issue
Block a user