Skip to content

Commit

Permalink
style: Create logger #173
Browse files Browse the repository at this point in the history
  • Loading branch information
seheon99 committed Dec 11, 2021
1 parent cb21690 commit acba18e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
16 changes: 5 additions & 11 deletions backend/src/users/users.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ import { env } from 'src/utils/envs';

@Resolver((of) => User)
export class UsersResolver {
private readonly logger = new Logger(UsersResolver.name);

constructor(
private readonly usersService: UsersService,
private readonly statusService: StatusService,
Expand All @@ -49,14 +47,10 @@ export class UsersResolver {
const defaultAvatarReadStream = createReadStream(
join(process.cwd(), 'src', env.defaultAvatar),
);
if (
this.usersService.createDefaultAvatar(
defaultAvatarReadStream,
env.defaultAvatar,
)
)
this.logger.log(`Initialize default avatar to src/${env.defaultAvatar}`);
else this.logger.log(`Skip default avatar initialization`);
this.usersService.createDefaultAvatar(
defaultAvatarReadStream,
env.defaultAvatar,
);
}

/*
Expand Down Expand Up @@ -238,7 +232,7 @@ export class UsersResolver {
** ANCHOR: ResolveField
*/

@ResolveField('avatar', (returns) => String, { nullable: true })
@ResolveField('avatar', (returns) => String)
async getAvatar(@Parent() user: User): Promise<string> {
const { id } = user;
return await this.usersService.getAvatar(id);
Expand Down
16 changes: 12 additions & 4 deletions backend/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
forwardRef,
Inject,
Injectable,
Logger,
NotFoundException,
} from '@nestjs/common';
import { DatabaseService } from 'src/database/database.service';
Expand All @@ -20,6 +21,8 @@ import { StorageService } from 'src/storage/storage.service';

@Injectable()
export class UsersService {
private readonly logger = new Logger(UsersService.name);

constructor(
private databaseService: DatabaseService,
@Inject(forwardRef(() => GamesService))
Expand Down Expand Up @@ -219,7 +222,6 @@ export class UsersService {
}

async findDefaultAvatar(): Promise<string> {
console.log('Default avatar');
return (
await this.databaseService.executeQuery(
`SELECT url FROM ${env.database.schema}.storage_url WHERE filename = 'default_avatar';`,
Expand All @@ -228,11 +230,17 @@ export class UsersService {
}

async createDefaultAvatar(fileStream, filename) {
const defaultAvatar = await this.findDefaultAvatar();
if (defaultAvatar) {
this.logger.verbose(`Skip creating default avatar`);
return false;
}
const url = await this.storageService.post(fileStream, filename);
const result = await this.databaseService.executeQuery(
`INSERT ${env.database.schema}.storage_url VALUES('default_avatar', '${url}') ON CONFLICT DO NOTHING RETURNING url;`,
await this.databaseService.executeQuery(
`INSERT INTO ${env.database.schema}.storage_url VALUES('default_avatar', '${url}');`,
);
return result.length === 1;
this.logger.verbose(`Create default avatar: ${filename}`);
return true;
}

async deleteDefaultAvatar() {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/utils/envs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ export const env = {
host: process.env.STORAGE_HOST,
port: process.env.STORAGE_PORT,
},
defaultAvatar: 'default-avatar.jpg',
defaultAvatar: 'default-avatar.png',
};

0 comments on commit acba18e

Please sign in to comment.