-
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.
implement user routes and user validation
- Loading branch information
Showing
12 changed files
with
253 additions
and
29 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
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 |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
"singleQuote": true, | ||
"trailingComma": "all", | ||
"printWidth": 80 | ||
} | ||
} |
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 |
---|---|---|
|
@@ -22,4 +22,4 @@ services: | |
networks: | ||
- appnetwork | ||
networks: | ||
appnetwork: | ||
appnetwork: |
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,18 @@ | ||
import { ValidationOptions, registerDecorator } from 'class-validator'; | ||
|
||
export function IsUsername(validationOptions?: ValidationOptions) { | ||
return function (object: object, propertyName: string) { | ||
registerDecorator({ | ||
name: 'isUsername', | ||
target: object.constructor, | ||
propertyName: propertyName, | ||
options: validationOptions, | ||
validator: { | ||
validate(value: string) { | ||
// alphanumeric + dashes, cannot start with a dash | ||
return !!value.match(/^[\w][\w-]+$/); | ||
}, | ||
}, | ||
}); | ||
}; | ||
} |
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,2 @@ | ||
export const MIN_USERNAME_LENGTH = 3; | ||
export const MIN_PASSWORD_LENGTH = 8; |
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 +1,22 @@ | ||
export class CreateUserDto {} | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsEmail, IsOptional, IsString, MinLength } from 'class-validator'; | ||
import { IsUsername } from '../../auth/validators/is-username'; | ||
import { MIN_PASSWORD_LENGTH, MIN_USERNAME_LENGTH } from '../../constants'; | ||
|
||
export class CreateUserDto { | ||
@ApiProperty() | ||
@IsEmail() | ||
email: string; | ||
|
||
@ApiProperty() | ||
@IsString() | ||
@MinLength(MIN_USERNAME_LENGTH) | ||
@IsUsername() | ||
@IsOptional() | ||
username?: string; | ||
|
||
@ApiProperty() | ||
@IsString() | ||
@MinLength(MIN_PASSWORD_LENGTH) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,32 @@ | ||
export class User {} | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { User } from '@prisma/client'; | ||
import { Exclude } from 'class-transformer'; | ||
import { IsOptional } from 'class-validator'; | ||
|
||
export class UserEntity implements User { | ||
@ApiProperty() | ||
id: number; | ||
|
||
@ApiProperty() | ||
createdAt: Date; | ||
|
||
@ApiProperty() | ||
updatedAt: Date; | ||
|
||
@ApiProperty() | ||
email: string; | ||
|
||
@ApiProperty() | ||
@IsOptional() | ||
username: string; | ||
|
||
@Exclude() | ||
hash: string; | ||
|
||
@ApiProperty() | ||
drafts: []; | ||
|
||
constructor(partial: Partial<UserEntity>) { | ||
Object.assign(this, partial); | ||
} | ||
} |
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,26 +1,41 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { CreateUserDto } from './dto/create-user.dto'; | ||
import { User } from '@prisma/client'; | ||
import * as argon from 'argon2'; | ||
import { PrismaService } from '../prisma/prisma.service'; | ||
import { UpdateUserDto } from './dto/update-user.dto'; | ||
|
||
@Injectable() | ||
export class UserService { | ||
create(createUserDto: CreateUserDto) { | ||
return 'This action adds a new user'; | ||
} | ||
constructor(private prisma: PrismaService) {} | ||
|
||
findAll() { | ||
return `This action returns all user`; | ||
} | ||
async update(user: User, dto: UpdateUserDto) { | ||
const data = { | ||
...user, | ||
...dto, | ||
}; | ||
|
||
findOne(id: number) { | ||
return `This action returns a #${id} user`; | ||
} | ||
delete data.password; | ||
|
||
let hash: string; | ||
if (dto.password) { | ||
hash = await argon.hash(dto.password); | ||
data.hash = hash; | ||
console.log({ hash }); | ||
} | ||
|
||
update(id: number, updateUserDto: UpdateUserDto) { | ||
return `This action updates a #${id} user`; | ||
return await this.prisma.user.update({ | ||
where: { | ||
id: user.id, | ||
}, | ||
data, | ||
}); | ||
} | ||
|
||
remove(id: number) { | ||
return `This action removes a #${id} user`; | ||
async remove(id: number) { | ||
return 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