add posts, categories entity, module

This commit is contained in:
2025-05-15 15:28:19 +07:00
parent 301501dbac
commit d97dfd25e2
16 changed files with 306 additions and 85 deletions

View File

@@ -0,0 +1,70 @@
import { Injectable } from '@nestjs/common';
import { CreateCategoryDto } from './dto/create-category.dto';
import { UpdateCategoryDto } from './dto/update-category.dto';
import {InjectRepository} from "@nestjs/typeorm";
import Post from "../posts/entities/post.entity";
import {Repository} from "typeorm";
import Category from "./entities/category.entity";
import {CategoryNotFoundException} from "./exception/categoryNotFound.exception";
@Injectable()
export class CategoriesService {
constructor(
@InjectRepository(Category) private repo: Repository<Category>,
) {
}
getAllCategories() {
return this.repo.find({ relations: ['posts'] });
}
async getCategoryById(id: number) {
const category = await this.repo.findOne({
where: { id },
relations: {
posts: true,
}
});
if (category) {
return category;
}
throw new CategoryNotFoundException(id);
}
async updateCategory(id: number, category: UpdateCategoryDto) {
await this.repo.update(id, category);
const updatedCategory = await this.repo.findOne({
where: { id },
relations: {
posts: true,
}
});
if (updatedCategory) {
return updatedCategory
}
throw new CategoryNotFoundException(id);
}
create(createCategoryDto: CreateCategoryDto) {
return 'This action adds a new category';
}
findAll() {
return `This action returns all categories`;
}
findOne(id: number) {
return `This action returns a #${id} category`;
}
update(id: number, updateCategoryDto: UpdateCategoryDto) {
return `This action updates a #${id} category`;
}
remove(id: number) {
return `This action removes a #${id} category`;
}
}