add posts, categories entity, module
This commit is contained in:
@@ -7,6 +7,7 @@ import * as Joi from 'joi';
|
|||||||
import {UsersModule} from "./users/user.module";
|
import {UsersModule} from "./users/user.module";
|
||||||
import {AuthenticationModule} from "./authentication/authentication.module";
|
import {AuthenticationModule} from "./authentication/authentication.module";
|
||||||
import { PostsModule } from './posts/posts.module';
|
import { PostsModule } from './posts/posts.module';
|
||||||
|
import { CategoriesModule } from './categories/categories.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@@ -35,7 +36,8 @@ import { PostsModule } from './posts/posts.module';
|
|||||||
}),
|
}),
|
||||||
UsersModule,
|
UsersModule,
|
||||||
AuthenticationModule,
|
AuthenticationModule,
|
||||||
PostsModule
|
PostsModule,
|
||||||
|
CategoriesModule
|
||||||
],
|
],
|
||||||
controllers: [AppController],
|
controllers: [AppController],
|
||||||
providers: [AppService],
|
providers: [AppService],
|
||||||
|
|||||||
34
src/categories/categories.controller.ts
Normal file
34
src/categories/categories.controller.ts
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
|
||||||
|
import { CategoriesService } from './categories.service';
|
||||||
|
import { CreateCategoryDto } from './dto/create-category.dto';
|
||||||
|
import { UpdateCategoryDto } from './dto/update-category.dto';
|
||||||
|
|
||||||
|
@Controller('categories')
|
||||||
|
export class CategoriesController {
|
||||||
|
constructor(private readonly categoriesService: CategoriesService) {}
|
||||||
|
|
||||||
|
@Post()
|
||||||
|
create(@Body() createCategoryDto: CreateCategoryDto) {
|
||||||
|
return this.categoriesService.create(createCategoryDto);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get()
|
||||||
|
findAll() {
|
||||||
|
return this.categoriesService.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get(':id')
|
||||||
|
findOne(@Param('id') id: string) {
|
||||||
|
return this.categoriesService.findOne(+id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Patch(':id')
|
||||||
|
update(@Param('id') id: string, @Body() updateCategoryDto: UpdateCategoryDto) {
|
||||||
|
return this.categoriesService.update(+id, updateCategoryDto);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Delete(':id')
|
||||||
|
remove(@Param('id') id: string) {
|
||||||
|
return this.categoriesService.remove(+id);
|
||||||
|
}
|
||||||
|
}
|
||||||
9
src/categories/categories.module.ts
Normal file
9
src/categories/categories.module.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { CategoriesService } from './categories.service';
|
||||||
|
import { CategoriesController } from './categories.controller';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
controllers: [CategoriesController],
|
||||||
|
providers: [CategoriesService],
|
||||||
|
})
|
||||||
|
export class CategoriesModule {}
|
||||||
70
src/categories/categories.service.ts
Normal file
70
src/categories/categories.service.ts
Normal 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`;
|
||||||
|
}
|
||||||
|
}
|
||||||
1
src/categories/dto/create-category.dto.ts
Normal file
1
src/categories/dto/create-category.dto.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export class CreateCategoryDto {}
|
||||||
4
src/categories/dto/update-category.dto.ts
Normal file
4
src/categories/dto/update-category.dto.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
import { PartialType } from '@nestjs/swagger';
|
||||||
|
import { CreateCategoryDto } from './create-category.dto';
|
||||||
|
|
||||||
|
export class UpdateCategoryDto extends PartialType(CreateCategoryDto) {}
|
||||||
16
src/categories/entities/category.entity.ts
Normal file
16
src/categories/entities/category.entity.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import {Column, Entity, ManyToMany, PrimaryGeneratedColumn} from 'typeorm';
|
||||||
|
import Post from "../../posts/entities/post.entity";
|
||||||
|
|
||||||
|
@Entity()
|
||||||
|
class Category {
|
||||||
|
@PrimaryGeneratedColumn()
|
||||||
|
public id: number;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
public name: string;
|
||||||
|
|
||||||
|
@ManyToMany(() => Post, (post: Post) => post.categories)
|
||||||
|
public posts: Post[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Category;
|
||||||
7
src/categories/exception/categoryNotFound.exception.ts
Normal file
7
src/categories/exception/categoryNotFound.exception.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import { NotFoundException } from '@nestjs/common';
|
||||||
|
|
||||||
|
export class CategoryNotFoundException extends NotFoundException {
|
||||||
|
constructor(categoryId: number) {
|
||||||
|
super(`Category with id ${categoryId} not found`);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1 +1,29 @@
|
|||||||
export class Post {}
|
import {Column, Entity, ManyToOne, PrimaryGeneratedColumn, ManyToMany, JoinTable} from 'typeorm';
|
||||||
|
import User from "../../users/user.entity";
|
||||||
|
import Category from "../../categories/entities/category.entity";
|
||||||
|
|
||||||
|
@Entity()
|
||||||
|
class Post {
|
||||||
|
@PrimaryGeneratedColumn()
|
||||||
|
public id: number;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
public title: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
public content: string;
|
||||||
|
|
||||||
|
@Column({nullable: true})
|
||||||
|
public category?: string;
|
||||||
|
|
||||||
|
@ManyToOne(() => User, (author: User) => author.posts)
|
||||||
|
public author: User;
|
||||||
|
|
||||||
|
|
||||||
|
@ManyToMany(() => Category, (category: Category) => category.posts)
|
||||||
|
@JoinTable()
|
||||||
|
public categories: Category[];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Post;
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { NotFoundException } from '@nestjs/common';
|
import { NotFoundException } from '@nestjs/common';
|
||||||
|
|
||||||
class PostNotFoundException extends NotFoundException {
|
export class PostNotFoundException extends NotFoundException {
|
||||||
constructor(postId: number) {
|
constructor(postId: number) {
|
||||||
super(`Post with id ${postId} not found`);
|
super(`Post with id ${postId} not found`);
|
||||||
}
|
}
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
import { PostsController } from './posts.controller';
|
|
||||||
import { PostsService } from './posts.service';
|
|
||||||
|
|
||||||
describe('PostsController', () => {
|
|
||||||
let controller: PostsController;
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
controllers: [PostsController],
|
|
||||||
providers: [PostsService],
|
|
||||||
}).compile();
|
|
||||||
|
|
||||||
controller = module.get<PostsController>(PostsController);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(controller).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,15 +1,19 @@
|
|||||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
|
import {Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Req} from '@nestjs/common';
|
||||||
import { PostsService } from './posts.service';
|
import {PostsService} from './posts.service';
|
||||||
import { CreatePostDto } from './dto/create-post.dto';
|
import {CreatePostDto} from './dto/create-post.dto';
|
||||||
import { UpdatePostDto } from './dto/update-post.dto';
|
import {UpdatePostDto} from './dto/update-post.dto';
|
||||||
|
import JwtAuthenticationGuard from "../authentication/jwt-authentication.guard";
|
||||||
|
import RequestWithUser from "../authentication/requestWithUser.interface";
|
||||||
|
|
||||||
@Controller('posts')
|
@Controller('posts')
|
||||||
export class PostsController {
|
export class PostsController {
|
||||||
constructor(private readonly postsService: PostsService) {}
|
constructor(private readonly postsService: PostsService) {
|
||||||
|
}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
create(@Body() createPostDto: CreatePostDto) {
|
@UseGuards(JwtAuthenticationGuard)
|
||||||
return this.postsService.create(createPostDto);
|
async createPost(@Body() post: CreatePostDto, @Req() req: RequestWithUser) {
|
||||||
|
return this.postsService.createPost(post, req.user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
import { PostsService } from './posts.service';
|
|
||||||
|
|
||||||
describe('PostsService', () => {
|
|
||||||
let service: PostsService;
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
providers: [PostsService],
|
|
||||||
}).compile();
|
|
||||||
|
|
||||||
service = module.get<PostsService>(PostsService);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(service).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,11 +1,59 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import {Injectable} from '@nestjs/common';
|
||||||
import { CreatePostDto } from './dto/create-post.dto';
|
import {CreatePostDto} from './dto/create-post.dto';
|
||||||
import { UpdatePostDto } from './dto/update-post.dto';
|
import {UpdatePostDto} from './dto/update-post.dto';
|
||||||
|
import User from "../users/user.entity";
|
||||||
|
import {InjectRepository} from "@nestjs/typeorm";
|
||||||
|
import {Repository} from "typeorm";
|
||||||
|
import Post from './entities/post.entity';
|
||||||
|
import {PostNotFoundException} from "./exception/postNotFound.exception";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PostsService {
|
export class PostsService {
|
||||||
create(createPostDto: CreatePostDto) {
|
|
||||||
return 'This action adds a new post';
|
constructor(
|
||||||
|
@InjectRepository(Post) private repo: Repository<Post>,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
async createPost(post: CreatePostDto, user: User) {
|
||||||
|
const newPost = this.repo.create({
|
||||||
|
...post,
|
||||||
|
author: user
|
||||||
|
});
|
||||||
|
await this.repo.save(newPost);
|
||||||
|
return newPost;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
getAllPosts() {
|
||||||
|
return this.repo.find({relations: ['author']});
|
||||||
|
}
|
||||||
|
|
||||||
|
async getPostById(id: number) {
|
||||||
|
const post = await this.repo.findOne(
|
||||||
|
{
|
||||||
|
where: {id},
|
||||||
|
relations: {author: true}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
if (post) {
|
||||||
|
return post;
|
||||||
|
}
|
||||||
|
throw new PostNotFoundException(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
async updatePost(id: number, post: UpdatePostDto) {
|
||||||
|
await this.repo.update(id, post);
|
||||||
|
const updatedPost = await this.repo.findOne({
|
||||||
|
|
||||||
|
where: {id},
|
||||||
|
relations: {author: true}
|
||||||
|
|
||||||
|
});
|
||||||
|
if (updatedPost) {
|
||||||
|
return updatedPost
|
||||||
|
}
|
||||||
|
throw new PostNotFoundException(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
findAll() {
|
findAll() {
|
||||||
|
|||||||
22
src/users/address.entity.ts
Normal file
22
src/users/address.entity.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import {Column, Entity, OneToOne, PrimaryGeneratedColumn} from 'typeorm';
|
||||||
|
import User from "./user.entity";
|
||||||
|
|
||||||
|
@Entity()
|
||||||
|
class Address {
|
||||||
|
@PrimaryGeneratedColumn()
|
||||||
|
public id: number;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
public street: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
public city: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
public country: string;
|
||||||
|
|
||||||
|
@OneToOne(() => User, (user: User) => user.address)
|
||||||
|
public user: User;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Address;
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
import {Column, Entity, PrimaryGeneratedColumn} from 'typeorm';
|
import {Column, Entity, PrimaryGeneratedColumn, OneToOne, JoinColumn, OneToMany} from 'typeorm';
|
||||||
import {Exclude, Expose} from 'class-transformer';
|
import {Exclude, Expose} from 'class-transformer';
|
||||||
|
import Address from "./address.entity";
|
||||||
|
import Post from "../posts/entities/post.entity";
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
class User {
|
class User {
|
||||||
@@ -17,6 +19,18 @@ class User {
|
|||||||
@Column()
|
@Column()
|
||||||
public password: string;
|
public password: string;
|
||||||
|
|
||||||
|
@OneToOne(() => Address, {
|
||||||
|
eager: true,
|
||||||
|
cascade: true
|
||||||
|
})
|
||||||
|
@JoinColumn()
|
||||||
|
public address: Address;
|
||||||
|
|
||||||
|
|
||||||
|
@OneToMany(() => Post, (post: Post) => post.author)
|
||||||
|
public posts: Post[];
|
||||||
|
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
nullable: true,
|
nullable: true,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user