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 editing user profile
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Jan 28, 2019
1 parent a60d4f6 commit a1f0c73
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
31 changes: 31 additions & 0 deletions back/src/user/application/ProfileEditor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Injectable } from '@nestjs/common'

import { EntitySaver } from '@back/db/EntitySaver'

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

@Injectable()
export class ProfileEditor {
public constructor(
private readonly userRepo: UserRepository,
private readonly entitySaver: EntitySaver,
) {}

public async edit(login: string, fields: ProfileFields): Promise<void> {
const user = await this.userRepo.getOne(login)

this.editName(user, fields)

await this.entitySaver.save(user)
}

private editName(user: User, { name }: ProfileFields): void {
if (name.length > 1) {
user.profile.changeName(name)
} else {
user.profile.removeName()
}
}
}
3 changes: 3 additions & 0 deletions back/src/user/application/dto/ProfileFields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface ProfileFields {
readonly name?: string
}
2 changes: 2 additions & 0 deletions back/src/user/domain/Profile.vo.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Option } from 'tsoption'
import { Column } from 'typeorm'

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

@Column()
private _name?: string

public constructor(name?: string) {
Expand Down
22 changes: 14 additions & 8 deletions back/src/user/presentation/http/controller/ProfileController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ApiUseTags,
} from '@nestjs/swagger'

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

Expand All @@ -16,7 +17,10 @@ import { ProfileResponse } from '../response/ProfileResponse'
@ApiUseTags('user')
@ApiBearerAuth()
export class ProfileController {
public constructor(private readonly userRepo: UserRepository) {}
public constructor(
private readonly userRepo: UserRepository,
private readonly profileEditor: ProfileEditor,
) {}

@Get()
@ApiOperation({ title: 'Show user profile' })
Expand All @@ -25,9 +29,7 @@ export class ProfileController {
type: ProfileResponse,
})
public async showProfile(): Promise<ProfileResponse> {
const user = await this.userRepo.getOne('email@email.com')

return ProfileResponse.fromProfile(user.profile)
return this.getResponseByLogin('email@email.com')
}

@PostNoCreate()
Expand All @@ -39,10 +41,14 @@ export class ProfileController {
public async editProfile(
@Body() request: ProfileRequest,
): Promise<ProfileResponse> {
const { name } = request
await this.profileEditor.edit('email@email.com', request)

return this.getResponseByLogin('email@email.com')
}

private async getResponseByLogin(login: string): Promise<ProfileResponse> {
const user = await this.userRepo.getOne(login)

return {
name,
}
return ProfileResponse.fromProfile(user.profile)
}
}
2 changes: 2 additions & 0 deletions back/src/user/user.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ProfileController } from './presentation/http/controller/ProfileControl
import { User } from './domain/User.entity'
import { UserRepository } from './domain/UserRepository'

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

import { BcryptPasswordEncoder } from './infrastructure/PasswordEncoder/BcryptPasswordEncoder'
Expand All @@ -23,6 +24,7 @@ import { PasswordEncoder } from './infrastructure/PasswordEncoder/PasswordEncode
useClass: BcryptPasswordEncoder,
},
Registrator,
ProfileEditor,
UserRepository,
],
})
Expand Down

0 comments on commit a1f0c73

Please sign in to comment.