-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from henriqueweiand/user-crud
added all crud for users
- Loading branch information
Showing
27 changed files
with
897 additions
and
61 deletions.
There are no files selected for viewing
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
54 changes: 50 additions & 4 deletions
54
libs/common/src/database/prisma/repository/prisma-user.repository.ts
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,25 +1,71 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Prisma } from '@prisma/client'; | ||
import { Prisma, User } from '@prisma/client'; | ||
import { UserRepository } from '../../repository/user.repositoy'; | ||
import { PrismaService } from '../prisma.service'; | ||
|
||
@Injectable() | ||
export class PrismaUserRepository implements UserRepository { | ||
constructor(private prisma: PrismaService) { } | ||
|
||
async findOne(where: Prisma.UserWhereUniqueInput) { | ||
async findById(id: string): Promise<User | null> { | ||
const user = await this.prisma.user.findUnique({ | ||
where, | ||
}); | ||
where: { | ||
id, | ||
}, | ||
}) | ||
|
||
if (!user) { | ||
return null | ||
} | ||
|
||
return user; | ||
} | ||
|
||
async findByUsername(username: string): Promise<User | null> { | ||
const user = await this.prisma.user.findUnique({ | ||
where: { | ||
username, | ||
}, | ||
}) | ||
|
||
if (!user) { | ||
return null | ||
} | ||
|
||
return user; | ||
} | ||
|
||
async findMany(): Promise<User[]> { | ||
const users = await this.prisma.user.findMany() | ||
|
||
return users; | ||
} | ||
|
||
async create(data: Prisma.UserCreateInput) { | ||
const user = await this.prisma.user.create({ | ||
data, | ||
}); | ||
|
||
return user; | ||
} | ||
|
||
async update(id: string, data: Prisma.UserUpdateInput): Promise<User> { | ||
const user = this.prisma.user.update({ | ||
where: { | ||
id | ||
}, | ||
data, | ||
}); | ||
|
||
return user | ||
} | ||
|
||
async delete(id: string): Promise<void> { | ||
await this.prisma.user.delete({ | ||
where: { | ||
id, | ||
}, | ||
}) | ||
} | ||
|
||
} |
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,6 +1,10 @@ | ||
import { Prisma, User } from "@prisma/client"; | ||
|
||
export abstract class UserRepository { | ||
abstract findOne(where: Prisma.UserWhereUniqueInput): Promise<User | null> | ||
abstract create(user: any): Promise<User> | ||
abstract findById(id: string): Promise<User | null> | ||
abstract findByUsername(username: string): Promise<User | null> | ||
abstract findMany(): Promise<User[]> | ||
abstract create(data: Prisma.UserCreateInput): Promise<User> | ||
abstract update(id: string, data: Prisma.UserUpdateInput): Promise<User> | ||
abstract delete(id: string): Promise<void> | ||
} |
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
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,34 @@ | ||
import { | ||
Body, | ||
ConflictException, | ||
Controller, | ||
HttpCode, | ||
HttpException, | ||
HttpStatus, | ||
Post | ||
} from '@nestjs/common'; | ||
import { CreateUserUseCase } from '../use-case/create-user'; | ||
import { CreateUserDto } from '../dto/create-user.dto'; | ||
import { ApiOkResponse, ApiTags } from '@nestjs/swagger'; | ||
import { CreateUserDto } from '../dto/create-user.dto'; | ||
import { UserDto } from '../dto/user.dto'; | ||
import { CreateUserUseCase } from '../use-case/create-user'; | ||
|
||
@Controller('user') | ||
@Controller('/user') | ||
@ApiTags('user') | ||
export class CreateUserController { | ||
constructor(private createUserUseCase: CreateUserUseCase) { } | ||
|
||
@HttpCode(HttpStatus.OK) | ||
@ApiOkResponse({ type: UserDto }) | ||
@Post('create') | ||
create(@Body() createUserDto: CreateUserDto) { | ||
return this.createUserUseCase.create(); | ||
@HttpCode(HttpStatus.CREATED) | ||
@Post('') | ||
handle(@Body() createUserDto: CreateUserDto) { | ||
try { | ||
const user = this.createUserUseCase.execute(createUserDto); | ||
return user; | ||
} catch (e) { | ||
if (e instanceof ConflictException) { | ||
throw new HttpException('This username is already in use', HttpStatus.CONFLICT); | ||
} | ||
throw new HttpException('Was not possible to register', HttpStatus.BAD_REQUEST); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/modules/users/controllers/delete-user.controller.e2e-spec.ts
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,59 @@ | ||
import { INestApplication, ValidationPipe } from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
import * as request from 'supertest'; | ||
import { UsersModule } from '../users.module'; | ||
import { DatabaseModule, PrismaService } from '@app/common'; | ||
import { randomUUID } from 'node:crypto' | ||
|
||
describe('User (E2E) Delete', () => { | ||
let app: INestApplication; | ||
let prisma: PrismaService; | ||
let httpServer: any; | ||
let UUID: string; | ||
|
||
beforeAll(async () => { | ||
const moduleRef = await Test.createTestingModule({ | ||
imports: [UsersModule, DatabaseModule], | ||
providers: [], | ||
}).compile(); | ||
|
||
prisma = moduleRef.get(PrismaService); | ||
app = moduleRef.createNestApplication(); | ||
app.useGlobalPipes(new ValidationPipe()); | ||
httpServer = app.getHttpServer(); | ||
|
||
await app.init(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.close(); | ||
}); | ||
|
||
beforeEach(() => { | ||
UUID = randomUUID(); | ||
}) | ||
|
||
test('[DELETE] /user/:id', async () => { | ||
const user = await prisma.user.create({ | ||
data: { | ||
username: `user-${UUID}`, | ||
password: '123456', | ||
}, | ||
}); | ||
|
||
const response = await request(httpServer) | ||
.delete(`/user/${user.id}`) | ||
.send(); | ||
|
||
expect(response.statusCode).toBe(200); | ||
|
||
const userOnDatabase = await prisma.user.findUnique({ | ||
where: { | ||
id: user.id, | ||
}, | ||
}); | ||
|
||
expect(userOnDatabase).toBeNull(); | ||
}); | ||
|
||
}); |
Oops, something went wrong.