feat: Add core services for user, category, item, and restaurant management, including caching and ownership verification.

This commit is contained in:
2025-12-11 11:16:09 +03:00
parent 66cab810c1
commit 6ad4871bdc
6 changed files with 122 additions and 24 deletions
+2 -1
View File
@@ -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],
})
+20 -2
View File
@@ -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) {