16 changed files with 284 additions and 19 deletions
-
18prisma/migrations/20230414070358_add_user_model/migration.sql
-
12prisma/schema.prisma
-
48prisma/seed.ts
-
3src/app.module.ts
-
24src/articles/articles.controller.ts
-
6src/articles/articles.service.ts
-
15src/articles/entities/article.entity.ts
-
5src/main.ts
-
20src/users/dto/create-user.dto.ts
-
4src/users/dto/update-user.dto.ts
-
27src/users/entities/user.entity.ts
-
20src/users/users.controller.spec.ts
-
43src/users/users.controller.ts
-
11src/users/users.module.ts
-
18src/users/users.service.spec.ts
-
29src/users/users.service.ts
@ -0,0 +1,18 @@ |
|||||
|
-- AlterTable |
||||
|
ALTER TABLE `article` ADD COLUMN `authorId` INTEGER NULL; |
||||
|
|
||||
|
-- CreateTable |
||||
|
CREATE TABLE `User` ( |
||||
|
`id` INTEGER NOT NULL AUTO_INCREMENT, |
||||
|
`name` VARCHAR(191) NULL, |
||||
|
`email` VARCHAR(191) NOT NULL, |
||||
|
`password` VARCHAR(191) NOT NULL, |
||||
|
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), |
||||
|
`updatedAt` DATETIME(3) NOT NULL, |
||||
|
|
||||
|
UNIQUE INDEX `User_email_key`(`email`), |
||||
|
PRIMARY KEY (`id`) |
||||
|
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; |
||||
|
|
||||
|
-- AddForeignKey |
||||
|
ALTER TABLE `Article` ADD CONSTRAINT `Article_authorId_fkey` FOREIGN KEY (`authorId`) REFERENCES `User`(`id`) ON DELETE SET NULL ON UPDATE CASCADE; |
||||
@ -0,0 +1,20 @@ |
|||||
|
import { ApiProperty } from "@nestjs/swagger"; |
||||
|
import { IsNotEmpty, IsString, MinLength } from "class-validator"; |
||||
|
|
||||
|
export class CreateUserDto { |
||||
|
@IsString() |
||||
|
@IsNotEmpty() |
||||
|
@ApiProperty() |
||||
|
name: string |
||||
|
|
||||
|
@IsString() |
||||
|
@IsNotEmpty() |
||||
|
@ApiProperty() |
||||
|
email: string |
||||
|
|
||||
|
@IsString() |
||||
|
@IsNotEmpty() |
||||
|
@MinLength(6) |
||||
|
@ApiProperty() |
||||
|
password: string |
||||
|
} |
||||
@ -0,0 +1,4 @@ |
|||||
|
import { PartialType } from '@nestjs/swagger'; |
||||
|
import { CreateUserDto } from './create-user.dto'; |
||||
|
|
||||
|
export class UpdateUserDto extends PartialType(CreateUserDto) {} |
||||
@ -0,0 +1,27 @@ |
|||||
|
import { ApiProperty } from '@nestjs/swagger' |
||||
|
import { User } from '@prisma/client' |
||||
|
import { Exclude } from 'class-transformer' |
||||
|
|
||||
|
export class UserEntity implements User { |
||||
|
constructor(partial: Partial<UserEntity>) { |
||||
|
Object.assign(this, partial) |
||||
|
} |
||||
|
|
||||
|
@ApiProperty() |
||||
|
id: number |
||||
|
|
||||
|
@ApiProperty() |
||||
|
createdAt: Date |
||||
|
|
||||
|
@ApiProperty() |
||||
|
updatedAt: Date |
||||
|
|
||||
|
@ApiProperty() |
||||
|
name: string |
||||
|
|
||||
|
@ApiProperty() |
||||
|
email: string |
||||
|
|
||||
|
@Exclude() |
||||
|
password: string |
||||
|
} |
||||
@ -0,0 +1,20 @@ |
|||||
|
import { Test, TestingModule } from '@nestjs/testing'; |
||||
|
import { UsersController } from './users.controller'; |
||||
|
import { UsersService } from './users.service'; |
||||
|
|
||||
|
describe('UsersController', () => { |
||||
|
let controller: UsersController; |
||||
|
|
||||
|
beforeEach(async () => { |
||||
|
const module: TestingModule = await Test.createTestingModule({ |
||||
|
controllers: [UsersController], |
||||
|
providers: [UsersService], |
||||
|
}).compile(); |
||||
|
|
||||
|
controller = module.get<UsersController>(UsersController); |
||||
|
}); |
||||
|
|
||||
|
it('should be defined', () => { |
||||
|
expect(controller).toBeDefined(); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,43 @@ |
|||||
|
import { Controller, Get, Post, Body, Patch, Param, Delete, ParseIntPipe } from '@nestjs/common'; |
||||
|
import { UsersService } from './users.service'; |
||||
|
import { CreateUserDto } from './dto/create-user.dto'; |
||||
|
import { UpdateUserDto } from './dto/update-user.dto'; |
||||
|
import { ApiCreatedResponse, ApiOkResponse, ApiTags } from '@nestjs/swagger'; |
||||
|
import { UserEntity } from './entities/user.entity'; |
||||
|
|
||||
|
@Controller('users') |
||||
|
@ApiTags('users') |
||||
|
export class UsersController { |
||||
|
constructor(private readonly usersService: UsersService) { } |
||||
|
|
||||
|
@Post() |
||||
|
@ApiCreatedResponse({ type: UserEntity }) |
||||
|
async create(@Body() createUserDto: CreateUserDto) { |
||||
|
return new UserEntity(await this.usersService.create(createUserDto)); |
||||
|
} |
||||
|
|
||||
|
@Get() |
||||
|
@ApiOkResponse({ type: UserEntity, isArray: true }) |
||||
|
async findAll() { |
||||
|
const users = await this.usersService.findAll() |
||||
|
return users.map((user) => new UserEntity(user)) |
||||
|
} |
||||
|
|
||||
|
@Get(':id') |
||||
|
@ApiOkResponse({ type: UserEntity }) |
||||
|
async findOne(@Param('id', ParseIntPipe) id: number) { |
||||
|
return new UserEntity(await this.usersService.findOne(id)); |
||||
|
} |
||||
|
|
||||
|
@Patch(':id') |
||||
|
@ApiOkResponse({ type: UserEntity }) |
||||
|
async update(@Param('id', ParseIntPipe) id: number, @Body() updateUserDto: UpdateUserDto) { |
||||
|
return new UserEntity(await this.usersService.update(id, updateUserDto)); |
||||
|
} |
||||
|
|
||||
|
@Delete(':id') |
||||
|
@ApiOkResponse({ type: UserEntity }) |
||||
|
async remove(@Param('id', ParseIntPipe) id: number) { |
||||
|
return new UserEntity(await this.usersService.remove(id)); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
import { Module } from '@nestjs/common'; |
||||
|
import { UsersService } from './users.service'; |
||||
|
import { UsersController } from './users.controller'; |
||||
|
import { PrismaModule } from 'src/prisma/prisma.module'; |
||||
|
|
||||
|
@Module({ |
||||
|
controllers: [UsersController], |
||||
|
providers: [UsersService], |
||||
|
imports: [PrismaModule] |
||||
|
}) |
||||
|
export class UsersModule { } |
||||
@ -0,0 +1,18 @@ |
|||||
|
import { Test, TestingModule } from '@nestjs/testing'; |
||||
|
import { UsersService } from './users.service'; |
||||
|
|
||||
|
describe('UsersService', () => { |
||||
|
let service: UsersService; |
||||
|
|
||||
|
beforeEach(async () => { |
||||
|
const module: TestingModule = await Test.createTestingModule({ |
||||
|
providers: [UsersService], |
||||
|
}).compile(); |
||||
|
|
||||
|
service = module.get<UsersService>(UsersService); |
||||
|
}); |
||||
|
|
||||
|
it('should be defined', () => { |
||||
|
expect(service).toBeDefined(); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,29 @@ |
|||||
|
import { Injectable } from '@nestjs/common'; |
||||
|
import { CreateUserDto } from './dto/create-user.dto'; |
||||
|
import { UpdateUserDto } from './dto/update-user.dto'; |
||||
|
import { PrismaService } from 'src/prisma/prisma.service'; |
||||
|
|
||||
|
@Injectable() |
||||
|
export class UsersService { |
||||
|
constructor(private prisma: PrismaService) { } |
||||
|
|
||||
|
create(createUserDto: CreateUserDto) { |
||||
|
return this.prisma.user.create({ data: createUserDto }) |
||||
|
} |
||||
|
|
||||
|
findAll() { |
||||
|
return this.prisma.user.findMany() |
||||
|
} |
||||
|
|
||||
|
findOne(id: number) { |
||||
|
return this.prisma.user.findUnique({ where: { id } }) |
||||
|
} |
||||
|
|
||||
|
update(id: number, updateUserDto: UpdateUserDto) { |
||||
|
return this.prisma.user.update({ where: { id }, data: updateUserDto }) |
||||
|
} |
||||
|
|
||||
|
remove(id: number) { |
||||
|
return this.prisma.user.delete({ where: { id } }) |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue