Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
feat(user): add user repo and profile get endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Jan 28, 2019
1 parent a9cd6e9 commit a60d4f6
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 6 deletions.
14 changes: 10 additions & 4 deletions back/src/user/domain/Profile.vo.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import { Option } from 'tsoption'

export class Profile {
private name?: string
public get name(): Option<string> {
return Option.of(this._name)
}

private _name?: string

public constructor(name?: string) {
this.name = name
this._name = name
}

public changeName(newName: string) {
if (newName.length < 1) {
// TODO: throw error
}

this.name = newName
this._name = newName
}

public removeName() {
this.name = undefined
this._name = undefined
}
}
26 changes: 26 additions & 0 deletions back/src/user/domain/UserRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'

import { User } from './User.entity'

@Injectable()
class UserRepo {
public constructor(
@InjectRepository(User)
private readonly userRepo: Repository<User>,
) {}

public async getOne(login: string): Promise<User> {
const user = await this.userRepo.findOne(login)

if (!user) {
// TODO: throw error
}

return user
}
}

export const UserRepository = UserRepo
export type UserRepository = UserRepo
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ApiUseTags,
} from '@nestjs/swagger'

import { UserRepository } from '@back/user/domain/UserRepository'
import { PostNoCreate } from '@back/utils/presentation/http/PostNoCreate'

import { ProfileRequest } from '../request/ProfileRequest'
Expand All @@ -15,14 +16,18 @@ import { ProfileResponse } from '../response/ProfileResponse'
@ApiUseTags('user')
@ApiBearerAuth()
export class ProfileController {
public constructor(private readonly userRepo: UserRepository) {}

@Get()
@ApiOperation({ title: 'Show user profile' })
@ApiOkResponse({
description: 'Fetching profile success',
type: ProfileResponse,
})
public async showProfile(): Promise<ProfileResponse> {
return {}
const user = await this.userRepo.getOne('email@email.com')

return ProfileResponse.fromProfile(user.profile)
}

@PostNoCreate()
Expand Down
7 changes: 7 additions & 0 deletions back/src/user/presentation/http/response/ProfileResponse.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { ApiModelPropertyOptional } from '@nestjs/swagger'

import { Profile } from '@back/user/domain/Profile.vo'
import { ProfileModel } from '@shared/models/user/ProfileModel'

export class ProfileResponse implements ProfileModel {
public static fromProfile(profile: Profile): ProfileResponse {
return {
name: profile.name.getOrElse(undefined),
}
}

@ApiModelPropertyOptional({ example: 'Nick' })
public readonly name?: string
}
8 changes: 7 additions & 1 deletion back/src/user/user.module.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'

import { DbModule } from '@back/db/db.module'

import { AuthController } from './presentation/http/controller/AuthController'
import { ProfileController } from './presentation/http/controller/ProfileController'

import { User } from './domain/User.entity'
import { UserRepository } from './domain/UserRepository'

import { Registrator } from './application/Registrator'

import { BcryptPasswordEncoder } from './infrastructure/PasswordEncoder/BcryptPasswordEncoder'
import { PasswordEncoder } from './infrastructure/PasswordEncoder/PasswordEncoder'

@Module({
imports: [DbModule],
imports: [DbModule, TypeOrmModule.forFeature([User])],
controllers: [AuthController, ProfileController],
providers: [
{
provide: PasswordEncoder,
useClass: BcryptPasswordEncoder,
},
Registrator,
UserRepository,
],
})
export class UserModule implements NestModule {
Expand Down

0 comments on commit a60d4f6

Please sign in to comment.