-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
123 additions
and
115 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { User } from '../user/models/user.entity'; | ||
import { UserEntity } from '../user/models/user.entity'; | ||
|
||
export interface UserRequest extends Request { | ||
user: User; | ||
user: UserEntity; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { NestExpressApplication } from '@nestjs/platform-express'; | ||
import { AppModule } from '../../app.module'; | ||
import { Test } from '@nestjs/testing'; | ||
import { DataSource } from 'typeorm'; | ||
import { appSetting } from '../../main'; | ||
import * as supertest from 'supertest'; | ||
import { UserEntity } from '../../user/models/user.entity'; | ||
import { UserFixture } from '../fixture/user.fixture'; | ||
import { HttpStatus } from '@nestjs/common'; | ||
|
||
describe('signup test', () => { | ||
let testServer: NestExpressApplication; | ||
let dataSource: DataSource; | ||
|
||
let user: UserEntity; | ||
|
||
beforeAll(async () => { | ||
const module = await Test.createTestingModule({ | ||
imports: [AppModule], | ||
}).compile(); | ||
|
||
testServer = module.createNestApplication<NestExpressApplication>(); | ||
dataSource = testServer.get(DataSource); | ||
await dataSource.synchronize(true); | ||
appSetting(testServer); | ||
|
||
await testServer.init(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await dataSource.synchronize(true); | ||
|
||
user = await UserFixture.create({ | ||
name: 'hi', | ||
username: 'hello', | ||
password: 'world', | ||
}); | ||
}); | ||
|
||
it('Check if the username already exists', async () => { | ||
await supertest(testServer.getHttpServer()) | ||
.post('/user/signup') | ||
.send({ | ||
name: 'John Doe', | ||
username: 'hello', | ||
password: 'password123', | ||
}) | ||
.expect(HttpStatus.CONFLICT); | ||
}); | ||
|
||
afterAll(async () => { | ||
await dataSource.synchronize(true); | ||
|
||
await testServer.close(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { IsString, IsNotEmpty } from 'class-validator'; | ||
|
||
export class CreateUserDto { | ||
@IsString() | ||
name: string; | ||
|
||
@IsString() | ||
@IsNotEmpty() | ||
username: string; | ||
|
||
@IsString() | ||
@IsNotEmpty() | ||
password: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { CustomRepository } from '../../typeorm-ex/typeorm-ex.decorator'; | ||
import { Repository } from 'typeorm'; | ||
import { UserEntity } from '../models/user.entity'; | ||
|
||
@CustomRepository(UserEntity) | ||
export class UserRepository extends Repository<UserEntity> { | ||
async findByUsername(username: string): Promise<UserEntity | undefined> { | ||
const user = await this.findOne({ where: { username } }); | ||
return user ? user : undefined; // Return undefined when no user is found | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
import { Controller, Post, Body, ConflictException } from '@nestjs/common'; | ||
import { UserService } from './user.service'; | ||
import { User } from './models/user.entity'; | ||
import {CreateUserDto} from "../auth/controller/signup-dtos/createuser.dto"; | ||
import { UserEntity } from './models/user.entity'; | ||
import { CreateUserDto } from './in-dtos/createuser.dto'; | ||
import { UserRepository } from './repostiories/user.repository'; | ||
|
||
@Controller('user') | ||
export class UserController { | ||
constructor(private readonly userService: UserService) {} | ||
constructor( | ||
private readonly userService: UserService, | ||
private readonly userRepository: UserRepository, | ||
) {} | ||
|
||
|
||
@Post('signup') | ||
async signUp(@Body() createUserDto: CreateUserDto): Promise<User> { | ||
const existingUser = await this.userService.findByUsername(createUserDto.username); | ||
if (existingUser) { | ||
throw new ConflictException('Username already exists'); | ||
} | ||
return this.userService.create(createUserDto); | ||
@Post('signup') | ||
async signUp(@Body() createUserDto: CreateUserDto): Promise<UserEntity> { | ||
const existingUser = await this.userRepository.findByUsername( | ||
createUserDto.username, | ||
); | ||
if (existingUser) { | ||
throw new ConflictException('Username already exists'); | ||
} | ||
|
||
return this.userService.create(createUserDto); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { UserController } from './user.controller'; | ||
import { UserService } from './user.service'; | ||
import { User } from './models/user.entity'; | ||
import { TypeOrmExModule } from '../typeorm-ex/typeorm-ex.module'; | ||
import { UserRepository } from './repostiories/user.repository'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([User])], | ||
controllers: [UserController], | ||
providers: [UserService], | ||
imports: [TypeOrmExModule.forCustomRepository([UserRepository])], | ||
controllers: [UserController], | ||
providers: [UserService], | ||
}) | ||
export class UserModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,15 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { User } from './models/user.entity'; | ||
import { CreateUserDto } from '../auth/controller/signup-dtos/createuser.dto'; | ||
import { UserEntity } from './models/user.entity'; | ||
import { CreateUserDto } from './in-dtos/createuser.dto'; | ||
import { UserRepository } from './repostiories/user.repository'; | ||
|
||
@Injectable() | ||
export class UserService { | ||
constructor( | ||
@InjectRepository(User) | ||
private usersRepository: Repository<User>, | ||
) {} | ||
constructor(private userRepository: UserRepository) {} | ||
|
||
async create(createUserDto: CreateUserDto): Promise<User> { | ||
const { name, username, password } = createUserDto; | ||
const user = this.usersRepository.create({ name, username, password }); | ||
return this.usersRepository.save(user); | ||
} | ||
|
||
async findByUsername(username: string): Promise<User | undefined> { | ||
const user = await this.usersRepository.findOne({ where: { username } }); | ||
return user ? user : undefined; // Return undefined when no user is found | ||
} | ||
async create(createUserDto: CreateUserDto): Promise<UserEntity> { | ||
const { name, username, password } = createUserDto; | ||
const user = this.userRepository.create({ name, username, password }); | ||
return this.userRepository.save(user); | ||
} | ||
} |