feat: Add core services for user, category, item, and restaurant management, including caching and ownership verification.
This commit is contained in:
@@ -2,9 +2,10 @@ import { Module } from '@nestjs/common';
|
||||
import { UserController } from './user.controller';
|
||||
import { UserService } from './user.service';
|
||||
import { PrismaModule } from 'src/prisma/prisma.module';
|
||||
import { CacheModule } from 'src/cache/cache.module';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
imports: [PrismaModule, CacheModule],
|
||||
controllers: [UserController],
|
||||
providers: [UserService],
|
||||
})
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { PrismaService } from 'src/prisma/prisma.service';
|
||||
import { CacheService } from 'src/cache/cache.service';
|
||||
import { keys } from 'src/cache/cache.keys';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly cache: CacheService,
|
||||
) {}
|
||||
|
||||
async getAllUsers() {
|
||||
return this.prisma.user.findMany({
|
||||
@@ -12,11 +17,17 @@ export class UserService {
|
||||
}
|
||||
|
||||
async getUserById(id: string) {
|
||||
const cacheKey = keys.user(id);
|
||||
const cached = await this.cache.get(cacheKey);
|
||||
if (cached) return cached;
|
||||
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { id },
|
||||
include: { accounts: true, sessions: true, restaurants: true },
|
||||
});
|
||||
if (!user) throw new NotFoundException('User not found');
|
||||
|
||||
await this.cache.set(cacheKey, user, 600);
|
||||
return user;
|
||||
}
|
||||
|
||||
@@ -25,7 +36,14 @@ export class UserService {
|
||||
await this.prisma.session.deleteMany({ where: { userId: id } });
|
||||
await this.prisma.account.deleteMany({ where: { userId: id } });
|
||||
await this.prisma.restaurant.deleteMany({ where: { ownerId: id } });
|
||||
return this.prisma.user.delete({ where: { id } });
|
||||
|
||||
const deleted = await this.prisma.user.delete({ where: { id } });
|
||||
|
||||
// Invalidate caches
|
||||
await this.cache.del(keys.user(id));
|
||||
await this.cache.del(keys.userRestaurants(id));
|
||||
|
||||
return deleted;
|
||||
}
|
||||
|
||||
async getSessionsByUserId(id: string) {
|
||||
|
||||
Reference in New Issue
Block a user