实现添加博客
This commit is contained in:
@@ -13,6 +13,7 @@ import { AdminUserRoleController } from './controller/admin-user-role.controller
|
||||
import { AdminWebResourceController } from './controller/web/admin-web-resource.controller';
|
||||
import { AdminWebBlogController } from './controller/web/admin-web-blog.controller';
|
||||
import { ResourceModule } from 'src/resource/resource.module';
|
||||
import { BlogModule } from 'src/blog/blog.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -22,6 +23,7 @@ import { ResourceModule } from 'src/resource/resource.module';
|
||||
UserModule,
|
||||
RoleModule,
|
||||
ResourceModule,
|
||||
BlogModule,
|
||||
],
|
||||
controllers: [
|
||||
AdminController,
|
||||
|
||||
@@ -1,6 +1,45 @@
|
||||
import { Controller } from "@nestjs/common";
|
||||
import { Body, Controller, Delete, Get, Param, ParseUUIDPipe, Post, Put } from "@nestjs/common";
|
||||
import { CreateBlogDto } from "src/admin/dto/admin-web/create-blog.dto";
|
||||
import { BlogService } from "src/blog/blog.service";
|
||||
|
||||
@Controller('/admin/web/blog')
|
||||
export class AdminWebBlogController {
|
||||
|
||||
constructor(
|
||||
private readonly adminWebBlogService: BlogService,
|
||||
) { }
|
||||
|
||||
@Get()
|
||||
async list() {
|
||||
return this.adminWebBlogService.list();
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(
|
||||
@Body() dto: CreateBlogDto,
|
||||
) {
|
||||
return this.adminWebBlogService.create(dto);
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
async update(
|
||||
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
|
||||
@Body() dto: CreateBlogDto,
|
||||
) {
|
||||
return this.adminWebBlogService.update(id, dto);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async get(
|
||||
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
|
||||
) {
|
||||
return this.adminWebBlogService.findById(id);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async remove(
|
||||
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
|
||||
) {
|
||||
return this.adminWebBlogService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -26,14 +26,16 @@ export class AdminWebResourceController {
|
||||
|
||||
@Put(':id')
|
||||
async update(
|
||||
@Param('id') id: string,
|
||||
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
|
||||
@Body() data: CreateResourceDto
|
||||
) {
|
||||
return this.resourceService.update(id, data);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async delete(@Param('id') id: string) {
|
||||
async delete(
|
||||
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
|
||||
) {
|
||||
return this.resourceService.delete(id);
|
||||
}
|
||||
}
|
||||
12
tone-page-server/src/admin/dto/admin-web/create-blog.dto.ts
Normal file
12
tone-page-server/src/admin/dto/admin-web/create-blog.dto.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { IsString } from "class-validator";
|
||||
|
||||
export class CreateBlogDto {
|
||||
@IsString()
|
||||
title: string;
|
||||
|
||||
@IsString()
|
||||
description: string;
|
||||
|
||||
@IsString()
|
||||
contentUrl: string;
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import { Blog } from './entity/Blog.entity';
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Blog])],
|
||||
controllers: [BlogController],
|
||||
providers: [BlogService]
|
||||
providers: [BlogService],
|
||||
exports: [BlogService],
|
||||
})
|
||||
export class BlogModule { }
|
||||
|
||||
@@ -15,9 +15,28 @@ export class BlogService {
|
||||
return this.blogRepository.find({
|
||||
where: { deletedAt: null },
|
||||
order: {
|
||||
publishAt: 'DESC',
|
||||
createdAt: 'DESC',
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async create(blog: Partial<Blog>) {
|
||||
const newBlog = this.blogRepository.create(blog);
|
||||
return this.blogRepository.save(newBlog);
|
||||
}
|
||||
|
||||
async update(id: string, blog: Partial<Blog>) {
|
||||
await this.blogRepository.update(id, blog);
|
||||
return this.blogRepository.findOneBy({ id });
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
const blog = await this.blogRepository.findOneBy({ id });
|
||||
if (!blog) return null;
|
||||
return this.blogRepository.softRemove(blog);
|
||||
}
|
||||
|
||||
async findById(id: string) {
|
||||
return this.blogRepository.findOneBy({ id });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,9 +14,6 @@ export class Blog {
|
||||
@Column()
|
||||
contentUrl: string;
|
||||
|
||||
@Column({ precision: 3 })
|
||||
publishAt: Date;
|
||||
|
||||
@CreateDateColumn({ precision: 3 })
|
||||
createdAt: Date;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user