add auth and user handling
This commit is contained in:
parent
db057ce342
commit
1fce467571
19 changed files with 356 additions and 32 deletions
39
src/controllers/UserController.ts
Normal file
39
src/controllers/UserController.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import { ValidationError, ConflictError } from "../errors/httpErrors";
|
||||
import { CreateUserRequestDto } from "../dtos/CreateUserRequestDto";
|
||||
import { UserDto } from "../dtos/UserDto";
|
||||
import { encrypt } from "../utils/encryptionUtils";
|
||||
import { UserRepository } from "../repositories/UserRepository";
|
||||
import { UserDtoEntityMapper } from "../mappers/UserDtoEntityMapper";
|
||||
import { isNeitherNullNorEmpty } from "../utils/stringUtils";
|
||||
|
||||
export class UserController {
|
||||
constructor(
|
||||
private userRepository: UserRepository,
|
||||
private mapper: UserDtoEntityMapper
|
||||
) {}
|
||||
|
||||
async createUser(dto: CreateUserRequestDto): Promise<UserDto> {
|
||||
// check mandatory fields
|
||||
if (!isNeitherNullNorEmpty(dto.userData.email)) {
|
||||
throw new ValidationError("Email is required");
|
||||
}
|
||||
if (!isNeitherNullNorEmpty(dto.userData.userName)) {
|
||||
throw new ValidationError("Username is required");
|
||||
}
|
||||
if(!isNeitherNullNorEmpty(dto.password){
|
||||
throw new ValidationError("Password is required");
|
||||
}
|
||||
// user name must be uniqu
|
||||
const existingUser = await this.userRepository.findByUserName(dto.userData.email);
|
||||
if (existingUser) {
|
||||
throw new ConflictError("User with this user name already exists");
|
||||
}
|
||||
|
||||
const userEntity = this.mapper.toEntity(dto.userData);
|
||||
userEntity.password = await encrypt.encryptpass(dto.password);
|
||||
|
||||
const savedUser = await this.userRepository.create(userEntity);
|
||||
|
||||
return this.mapper.toDto(savedUser);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue