用户模块和文章模块的接口服务
import { Controller, Get, Param, Post, Body, Put, Delete, Query } from '@nestjs/common'
import { UserService } from './user.service'
import { PostService } from './post.service'
import { User as UserModel, Post as PostModel } from '@prisma/client'
@Controller()
export class AppController {
constructor(
private readonly userService: UserService,
private readonly postService: PostService
) {}
@Get('post/:id')
async getPostById(@Param('id') id: string): Promise<PostModel> {
return this.postService.post({ id: Number(id) })
}
@Get('feed')
async getPublishedPosts(
@Query('current') current: string,
@Query('pageSize') pageSize: string
): Promise<PostModel[]> {
const data = this.postService.posts({
where: { published: true },
current: Number(current) || 1,
pageSize: Number(pageSize) || 10
})
return data
}
@Get('filtered-posts/:searchString')
async getFilteredPosts(@Param('searchString') searchString: string): Promise<PostModel[]> {
return this.postService.posts({
where: {
OR: [
{
title: { contains: searchString }
},
{
content: { contains: searchString }
}
]
}
})
}
@Post('post')
async createDraft(@Body() postData: { title: string; content?: string; authorEmail: string }): Promise<PostModel> {
const { title, content, authorEmail } = postData
return this.postService.createPost({
title,
content,
author: {
connect: { email: authorEmail }
}
})
}
@Get('userList')
async getUserList(
@Query('current') current: string,
@Query('pageSize') pageSize: string,
@Query('name') name: string,
@Query('nickName') nickName: string,
@Query('gender') gender: string
): Promise<UserModel[]> {
const data = this.userService.users({
current: Number(current) || 1,
pageSize: Number(pageSize) || 10,
where: {
name: {
contains: name
},
nickName: {
contains: nickName
},
gender
}
})
return data
}
@Post('user')
async signupUser(
@Body()
userData: {
name?: string
nickName?: string
gender?: string
email: string
}
): Promise<UserModel> {
return this.userService.createUser(userData)
}
@Put('publish/:id')
async publishPost(@Param('id') id: string): Promise<PostModel> {
return this.postService.updatePost({
where: { id: Number(id) },
data: { published: true }
})
}
@Delete('post/:id')
async deletePost(@Param('id') id: string): Promise<PostModel> {
return this.postService.deletePost({ id: Number(id) })
}
}
import { Injectable } from '@nestjs/common'
import { PrismaService } from './prisma.service'
import { Post, Prisma } from '@prisma/client'
import { PageResult } from '../common/utils'
@Injectable()
export class PostService {
constructor(private prisma: PrismaService) {}
async post(postWhereUniqueInput: Prisma.PostWhereUniqueInput): Promise<Post | null> {
return this.prisma.post.findUnique({
where: postWhereUniqueInput
})
}
async posts(params: {
current?: number
pageSize?: number
cursor?: Prisma.PostWhereUniqueInput
where?: Prisma.PostWhereInput
orderBy?: Prisma.PostOrderByWithRelationInput
}): Promise<any> {
const { current, pageSize, cursor, where, orderBy } = params
const skip = (current - 1) * pageSize
const postList = await this.prisma.post.findMany({
skip,
take: pageSize,
cursor,
where,
orderBy
})
const total = await this.prisma.user.count()
return PageResult(postList, total, skip, pageSize)
}
async createPost(data: Prisma.PostCreateInput): Promise<Post> {
return this.prisma.post.create({
data
})
}
async updatePost(params: { where: Prisma.PostWhereUniqueInput; data: Prisma.PostUpdateInput }): Promise<Post> {
const { data, where } = params
return this.prisma.post.update({
data,
where
})
}
async deletePost(where: Prisma.PostWhereUniqueInput): Promise<Post> {
return this.prisma.post.delete({
where
})
}
}
import { Injectable } from '@nestjs/common'
import { PrismaService } from './prisma.service'
import { User, Prisma } from '@prisma/client'
import { PageResult } from '../common/utils'
@Injectable()
export class UserService {
constructor(private prisma: PrismaService) {}
async user(userWhereUniqueInput: Prisma.UserWhereUniqueInput): Promise<User | null> {
return this.prisma.user.findUnique({
where: userWhereUniqueInput
})
}
async users(params: {
current?: number
pageSize?: number
cursor?: Prisma.UserWhereUniqueInput
where?: Prisma.UserWhereInput
orderBy?: Prisma.UserOrderByWithRelationInput
}): Promise<User[]> {
const { current, pageSize, cursor, where, orderBy } = params
const skip = (current - 1) * pageSize
const userList = await this.prisma.user.findMany({
skip,
take: pageSize,
cursor,
where,
orderBy
})
const total = await this.prisma.user.count()
return PageResult(userList, total, skip, pageSize)
}
async createUser(data: Prisma.UserCreateInput): Promise<User> {
return this.prisma.user.create({
data
})
}
async updateUser(params: { where: Prisma.UserWhereUniqueInput; data: Prisma.UserUpdateInput }): Promise<User> {
const { where, data } = params
return this.prisma.user.update({
data,
where
})
}
async deleteUser(where: Prisma.UserWhereUniqueInput): Promise<User> {
return this.prisma.user.delete({
where
})
}
}