Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into refactor-#151-container-scheduling
  • Loading branch information
mjh000526 committed Nov 28, 2024
2 parents ade3f47 + 36b63a9 commit f75f395
Show file tree
Hide file tree
Showing 104 changed files with 1,971 additions and 688 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ yarn-error.log*
# Misc
.DS_Store
*.pem


#docker
mysql-data/*
2 changes: 1 addition & 1 deletion apps/backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class AuthService {
) {}

private JWT_SECRET_KEY = this.configService.get<string>('JWT_SECRET_KEY');
createJwt(userId: string) {
createJwt(userId: string): string {
const payload = { userId };
return this.jwtService.sign(payload, {
secret: this.JWT_SECRET_KEY
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/docker/docker.consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class DockerConsumer {
});

stream.on('end', async () => {
// await container.remove({ force: true });
await container.remove({ force: true });
let result = this.filterAnsiCode(output);
if (inputs.length !== 0) {
result = result.split('\n').slice(1).join('\n');
Expand Down
9 changes: 8 additions & 1 deletion apps/backend/src/lotus/dto/lotus.detail.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsArray, IsBoolean, IsDate, IsString, ValidateNested } from 'class-validator';
import { IsArray, IsBoolean, IsDate, IsString, IsUrl, ValidateNested } from 'class-validator';
import { SimpleFileResponseDto } from './simple.file.response.dto';
import { SimpleUserResponseDto } from './simple.user.response.dto';
import { GistApiFileListDto } from '@/gist/dto/gistApiFileList.dto';
Expand Down Expand Up @@ -43,6 +43,12 @@ export class LotusDetailDto {
})
version: string;

@IsUrl()
@ApiProperty({
example: 'https://gist.github.com/gistId~~/commitId~~'
})
gistUrl: string;

@IsBoolean()
@ApiProperty({
example: true
Expand Down Expand Up @@ -73,6 +79,7 @@ export class LotusDetailDto {
id: lotus.lotusId,
title: lotus.title,
language: lotus.language,
gistUrl: `https://gist.github.com/${lotus.gistRepositoryId}/${lotus.commitId}`,
version: lotus.version,
date: lotus.createdAt,
isPublic: lotus.isPublic,
Expand Down
10 changes: 9 additions & 1 deletion apps/backend/src/lotus/dto/lotus.response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsBoolean, IsDate, IsString, ValidateNested } from 'class-validator';
import { IsBoolean, IsDate, IsString, IsUrl, ValidateNested } from 'class-validator';
import { SimpleUserResponseDto } from './simple.user.response.dto';
import { Lotus } from '@/lotus/lotus.entity';

Expand Down Expand Up @@ -29,6 +29,12 @@ export class LotusResponseDto {
})
language: string;

@IsUrl()
@ApiProperty({
example: 'https://gist.github.com/gistId~~/commitId~~'
})
gistUrl: string;

@IsDate()
@ApiProperty({
example: '로투스 생성 일시'
Expand All @@ -54,6 +60,7 @@ export class LotusResponseDto {
author: user,
title: lotus.title,
language: lotus.language,
gistUrl: `https://gist.github.com/${lotus.gistRepositoryId}/${lotus.commitId}`,
isPublic: lotus.isPublic,
date: lotus.createdAt,
tags
Expand All @@ -67,6 +74,7 @@ export class LotusResponseDto {
id: lotus.lotusId,
author: simpleUser,
title: lotus.title,
gistUrl: `https://gist.github.com/${lotus.gistRepositoryId}/${lotus.commitId}`,
language: lotus.language,
isPublic: lotus.isPublic,
date: lotus.createdAt,
Expand Down
9 changes: 8 additions & 1 deletion apps/backend/src/lotus/dto/simple.user.response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ export class SimpleUserResponseDto {
})
profile: string;

@IsUrl()
@ApiProperty({
example: 'https://gist.github.com/nickname'
})
gistUrl: string;

static ofUserDto(userData: User) {
return {
id: userData.userId,
nickname: userData.nickname,
profile: userData.profilePath
profile: userData.profilePath,
gistUrl: userData.gistUrl
};
}
}
73 changes: 40 additions & 33 deletions apps/backend/src/lotus/lotus.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { title } from 'process';
import { In } from 'typeorm';
import { In, Like } from 'typeorm';
import { LotusCreateRequestDto } from './dto/lotus.createRequest.dto';
import { LotusDetailDto } from './dto/lotus.detail.dto';
import { LotusDto } from './dto/lotus.dto';
Expand Down Expand Up @@ -132,65 +132,72 @@ export class LotusService {
}

async getPublicLotus(page: number, size: number, search: string): Promise<LotusPublicDto> {
const lotusData = await this.getLotusByTags(search);

const totalNum = lotusData.length;
//const [lotusData, totalNum] = await this.getLotusByTags(page, size, search);
const [lotusData, totalNum] = await this.getLotusByTitle(page, size, search);
const maxPage = Math.ceil(totalNum / size);
if (page > maxPage && maxPage !== 0) {
throw new HttpException('page must be lower than max page', HttpStatus.NOT_FOUND);
}
if (page <= 0) {
throw new HttpException('page must be higher than 0', HttpStatus.NOT_FOUND);
}
const firstIdx = size * (page - 1);
const returnLotusData = lotusData.splice(firstIdx, size);
return LotusPublicDto.ofLotusList(lotusData, page, maxPage);
}

return LotusPublicDto.ofLotusList(returnLotusData, page, maxPage);
async getLotusByTitle(page: number, size: number, search: string) {
const whereData = {
isPublic: true
};
if (search) {
whereData['title'] = Like(`%${search}%`);
}
return await this.lotusRepository.findAndCount({
where: whereData,
skip: (page - 1) * size,
take: size,
relations: ['tags', 'user'],
order: { createdAt: 'DESC' }
});
}

async getLotusByTags(search: string) {
if (!search) {
return await this.lotusRepository.find({
where: {
isPublic: true
},
relations: ['tags', 'user']
});
}
const tags = await this.tagService.searchTag(search);
return await this.lotusRepository.find({
where: {
isPublic: true,
tags: {
tagId: In(tags)
}
},
relations: ['tags', 'user']
async getLotusByTags(page: number, size: number, search: string) {
const whereData = {
isPublic: true
};
if (search) {
const tags = await this.tagService.searchTag(search);
whereData['tags'] = { tagId: In(tags) };
}
return await this.lotusRepository.findAndCount({
where: whereData,
skip: (page - 1) * size,
take: size,
relations: ['tags', 'user'],
order: { createdAt: 'DESC' }
});
}

async getUserLotus(userId: string, page: number, size: number) {
const user = this.userService.findOneByUserId(userId);
if (!user) {
throw new HttpException('user data is not found', HttpStatus.NOT_FOUND);
throw new HttpException('user data is not found', HttpStatus.UNAUTHORIZED);
}

const lotusData = await this.lotusRepository.find({
const [lotusData, totalNum] = await this.lotusRepository.findAndCount({
where: { user: { userId } },
relations: ['tags', 'user']
skip: (page - 1) * size,
take: size,
relations: ['tags', 'user'],
order: { createdAt: 'DESC' }
});
const totalNum = lotusData.length;
const maxPage = Math.ceil(totalNum / size);
if (page > maxPage && maxPage !== 0) {
throw new HttpException('page must be lower than max page', HttpStatus.NOT_FOUND);
}
if (page <= 0) {
throw new HttpException('page must be higher than 0', HttpStatus.NOT_FOUND);
}
const firstIdx = size * (page - 1);
const returnLotusData = lotusData.splice(firstIdx, size);

return LotusPublicDto.ofLotusList(returnLotusData, page, maxPage);
return LotusPublicDto.ofLotusList(lotusData, page, maxPage);
}

async checkAlreadyExist(gistUuid: string, commitId: string) {
Expand Down
4 changes: 4 additions & 0 deletions apps/backend/src/user/dto/user.create.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export class UserCreateDto {
@IsNumber()
gitId: number;

@IsString()
gistUrl: string;

@ValidateNested({ each: true })
@Type(() => Lotus)
lotuses: Lotus[];
Expand All @@ -32,6 +35,7 @@ export class UserCreateDto {
this.profilePath = response.avatar_url;
this.gitToken = accessToken;
this.gitId = response.id;
this.gistUrl = `https://gist.github.com/${response.login}`;
this.lotuses = [];
this.comments = [];
}
Expand Down
47 changes: 19 additions & 28 deletions apps/backend/src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ import {
DefaultValuePipe,
Get,
HttpCode,
HttpException,
HttpStatus,
Param,
ParseIntPipe,
Patch,
Post,
Query,
Redirect,
Req,
Res
Req
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { ApiBody, ApiOperation, ApiQuery, ApiResponse } from '@nestjs/swagger';
import { Request, Response } from 'express';
import { Request } from 'express';
import { FileDto } from './dto/file.dto';
import { FileResponseDto } from './dto/file.response.dto';
import { TokenDTO } from './dto/token.dto';
Expand Down Expand Up @@ -79,27 +75,22 @@ export class UserController {
}

@Get('login/callback')
async githubCallback(@Query('code') code: string, @Res() res: Response): Promise<void> {
const clientUrl = this.configService.get<string>('CLIENT_REDIRECT_URL');
try {
const tokenResponse = await fetch('https://github.com/login/oauth/access_token', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
client_id: this.OAUTH_CLIENT_ID,
client_secret: this.OAUTH_CLIENT_SECRETS,
code
})
});
const tokenData = await tokenResponse.json();
const token = await this.userService.loginUser(tokenData);
res.redirect(`${clientUrl}/login/success?token=${token}`);
} catch (error) {
res.redirect(`${clientUrl}/login/error`);
}
async githubCallback(@Query('code') code: string) {
const tokenResponse = await fetch('https://github.com/login/oauth/access_token', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
client_id: this.OAUTH_CLIENT_ID,
client_secret: this.OAUTH_CLIENT_SECRETS,
code
})
});
const tokenData = await tokenResponse.json();
const token = await this.userService.loginUser(tokenData);
return { token };
}

@Get('/lotus')
Expand Down Expand Up @@ -131,7 +122,7 @@ export class UserController {
@ApiOperation({ summary: '사용자 정보 수정하기' })
@ApiBody({ type: UserPatchDTO })
@ApiResponse({ status: 200, description: '실행 성공', type: UserPatchDTO })
PatchUserInfo(@Req() request: Request, @Body() userData: UserPatchDTO): Promise<UserPatchDTO> {
patchUserInfo(@Req() request: Request, @Body() userData: UserPatchDTO): Promise<UserPatchDTO> {
const userId = this.authService.getIdFromRequest(request);
return this.userService.patchUserDataByUserId(userId, userData);
}
Expand Down
3 changes: 3 additions & 0 deletions apps/backend/src/user/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export class User {
@Column({ name: 'git_token' })
gitToken: string;

@Column({ name: 'gist_url' })
gistUrl: string;

@Column({ name: 'git_id', unique: true })
gitId: number;

Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export class UserService {
Authorization: `Bearer ${accessToken}`
}
});

const inputUser = await userResponse.json();
let user = await this.findOne(inputUser.id);
if (!user) {
Expand All @@ -74,7 +73,8 @@ export class UserService {
} else {
await this.userRepository.update({ gitId: inputUser.id }, { gitToken: accessToken });
}
return this.authService.createJwt(user.userId);
const token = this.authService.createJwt(user.userId);
return token;
}

async makeTestUser(user: User) {
Expand Down
4 changes: 2 additions & 2 deletions apps/frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<link rel="icon" type="image/svg+xml" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Froxy</title>
</head>
<body>
<body class="bg-zinc-100">
<div id="root"></div>
<script type="module" src="/src/app/main.tsx"></script>
</body>
Expand Down
Binary file added apps/frontend/public/android-chrome-192x192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added apps/frontend/public/android-chrome-512x512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added apps/frontend/public/apple-touch-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added apps/frontend/public/favicon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added apps/frontend/public/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added apps/frontend/public/favicon.ico
Binary file not shown.
1 change: 1 addition & 0 deletions apps/frontend/public/json/emptyHistoryAnimation.json

Large diffs are not rendered by default.

Loading

0 comments on commit f75f395

Please sign in to comment.