Skip to content

Commit

Permalink
Merge pull request #61 from donghquinn/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
donghquinn authored Mar 4, 2024
2 parents b6aaba2 + 96b4bc8 commit 522f2b4
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 131 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ npm-debug.log
example.env

migrations
.eslintrc.json

newsData
14 changes: 7 additions & 7 deletions src/controllers/geek/geek.ctl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Body, Controller, Post, Query } from '@nestjs/common';
import { hadaNewsStarValidator, hadaNewsUnStarValidator, hadaNewsValidator } from '@validators/hada.validator';
import { geekNewsStarValidator, geekNewsUnStarValidator, geekNewsValidator } from '@validators/geek.validator';
import { SetErrorResponse, SetResponse } from 'dto/response.dto';
import { GeekProvider } from 'providers/news/geek/geek.pvd';
import { DailyHadaNewsRequest } from 'types/geek.type';
Expand All @@ -9,14 +9,14 @@ import { StarRequest } from 'types/request.type';
export class GeekController {
constructor(private readonly geek: GeekProvider) {}

@Post('/news')
@Post('news')
async geekGetLatestNewsController(
@Body() request: DailyHadaNewsRequest,
@Query('page') page: number,
@Query('size') size: number,
) {
try {
const { today } = await hadaNewsValidator(request);
const { today } = await geekNewsValidator(request);

const { result, total } = await this.geek.getNews(today, page, size);

Expand All @@ -26,10 +26,10 @@ export class GeekController {
}
}

@Post('/star')
@Post('star')
async geekGiveStarController(@Body() request: StarRequest) {
try {
const { uuid: postUuid, email } = await hadaNewsStarValidator(request);
const { uuid: postUuid, email } = await geekNewsStarValidator(request);

await this.geek.giveStar(postUuid, email);

Expand All @@ -39,10 +39,10 @@ export class GeekController {
}
}

@Post('/unstar')
@Post('unstar')
async geekUnStarController(@Body() request: StarRequest) {
try {
const { uuid: postUuid, email } = await hadaNewsUnStarValidator(request);
const { uuid: postUuid, email } = await geekNewsUnStarValidator(request);

await this.geek.unStar(postUuid, email);

Expand Down
6 changes: 3 additions & 3 deletions src/controllers/hacker/hacker.ctl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { StarRequest } from 'types/request.type';
export class HackerController {
constructor(private readonly hacker: HackersNewsProvider) {}

@Post('/news')
@Post('news')
async hackerGetLatestNewsController(
@Body() request: DailyHackerNewsRequest,
@Query('page') page: number,
Expand All @@ -26,7 +26,7 @@ export class HackerController {
}
}

@Post('/star')
@Post('star')
async hackerGiveStarController(@Body() request: StarRequest) {
try {
const { uuid: PostUuid, email } = await hackerNewsStarValidator(request);
Expand All @@ -39,7 +39,7 @@ export class HackerController {
}
}

@Post('/unstar')
@Post('unstar')
async hackerUnStarController(@Body() request: StarRequest) {
try {
const { uuid: postUuid, email } = await hackerNewsUnStarValidator(request);
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/machine/machine.ctl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { StarRequest } from 'types/request.type';
export class MachineLearningController {
constructor(private readonly mlNews: MachineLearningProvider) {}

@Post('/latest')
@Post('news')
async mlGetLatestNewsController(
@Body() request: DailyMlNewsRequest,
@Query('page') page: number,
Expand All @@ -26,7 +26,7 @@ export class MachineLearningController {
}
}

@Post('/star')
@Post('star')
async mlGiveStarController(@Body() request: StarRequest) {
try {
const { uuid: postUuid, email } = await mlNewsStarValidator(request);
Expand All @@ -39,7 +39,7 @@ export class MachineLearningController {
}
}

@Post('/unstar')
@Post('unstar')
async mlUnStarController(@Body() request: StarRequest) {
try {
const { uuid: postUuid, email } = await mlNewsUnStarValidator(request);
Expand Down
2 changes: 1 addition & 1 deletion src/dto/response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AuthError } from '@errors/auth.error';
import { ClientError } from '@errors/client.error';
import { HackerError } from '@errors/hacker.error';
import { HadaError } from '@errors/hada.error';
import { HadaError } from '@errors/geek.error';
import { MachineLearningError } from '@errors/machine.error';
import { PrismaError } from '@errors/prisma.error';
import { ValidatorError } from '@errors/validator.error';
Expand Down
4 changes: 2 additions & 2 deletions src/errors/hada.error.ts → src/errors/geek.error.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export class HadaError extends Error {
export class GeekError extends Error {
type: string;

constructor(type: string, message: string, cause?: Error) {
super(message);

this.type = type;

this.name = '[Hada Error]';
this.name = '[Geek Error]';

this.cause = cause;
}
Expand Down
96 changes: 26 additions & 70 deletions src/providers/news/geek/geek-prisma.lib.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { HadaError } from '@errors/hada.error';
import { PrismaError } from '@errors/prisma.error';
import { GeekError } from '@errors/geek.error';
import { Injectable } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
import { NewsLogger } from '@utils/logger.util';

@Injectable()
export class GeekPrismaLibrary extends PrismaClient {
async bringHadaNews(startDate: Date, endDate: Date, page: number, size: number) {
async bringGeekNews(startDate: Date, endDate: Date, page: number, size: number) {
try {
const result = await this.geek.findMany({
select: { uuid: true, post: true, link: true, descLink: true, founded: true, liked: true },
Expand All @@ -23,12 +22,12 @@ export class GeekPrismaLibrary extends PrismaClient {

return result;
} catch (error) {
NewsLogger.error('[HADA] Bring Geek News Error: %o', {
NewsLogger.error('[GEEK] Bring Geek News Error: %o', {
error,
});

throw new PrismaError(
'[HADA] Bring Geek News',
throw new GeekError(
'[GEEK] Bring Geek News',
'Bring Geek News Error. Please Try Again.',
error instanceof Error ? error : new Error(JSON.stringify(error)),
);
Expand All @@ -48,19 +47,19 @@ export class GeekPrismaLibrary extends PrismaClient {

return Math.ceil(totalCounts / size);
} catch (error) {
NewsLogger.error('[HADA] Bring Total Geek News Count Error: %o', {
NewsLogger.error('[GEEK] Bring Total Geek News Count Error: %o', {
error,
});

throw new PrismaError(
'[HADA] Bring Total Geek News Count',
throw new GeekError(
'[GEEK] Bring Total Geek News Count',
'Bring Total Geek News Count Error. Please Try Again.',
error instanceof Error ? error : new Error(JSON.stringify(error)),
);
}
}

async checkHadaNewsIsLiked(postUuid: string, clientUuid: string) {
async checkGeekNewsIsLiked(postUuid: string, clientUuid: string) {
try {
const isStarred = await this.geek_Liked.findFirst({
select: {
Expand All @@ -77,29 +76,29 @@ export class GeekPrismaLibrary extends PrismaClient {
},
});

if (isStarred === null) throw new HadaError('[Hada] Get Star Info', 'No Star Info Found.');
if (isStarred === null) throw new GeekError('[GEEK] Get Star Info', 'No Star Info Found.');

NewsLogger.debug('[Hada] Found Is Starred Info: %o', {
NewsLogger.debug('[GEEK] Found Is Starred Info: %o', {
isLiked: isStarred.geek_news.liked,
});

return { uuid: isStarred.uuid, liked: isStarred.geek_news.liked };
} catch (error) {
NewsLogger.error('[Hada] Check Hada News Liked Info Error: %o', {
NewsLogger.error('[GEEK] Check Hada News Liked Info Error: %o', {
error,
});

throw new HadaError(
'[Hada] Check Hada News Liked Info',
throw new GeekError(
'[GEEK] Check Hada News Liked Info',
'Check Hada News Liked Info Error.',
error instanceof Error ? error : new Error(JSON.stringify(error)),
);
}
}

async updateHadaNewsLikedtoUnliked(likedUuid: string, postUuid: string, clientUuid: string) {
async updateGeekNewsLikedtoUnliked(likedUuid: string, postUuid: string, clientUuid: string) {
try {
NewsLogger.debug('[HADA] Give Hada News unStar Request: %o', {
NewsLogger.debug('[GEEK] Give Hada News unStar Request: %o', {
likedUuid,
postUuid,
clientUuid,
Expand All @@ -120,25 +119,25 @@ export class GeekPrismaLibrary extends PrismaClient {
},
});

NewsLogger.info('[HADA] Unstarred Updated');
NewsLogger.info('[GEEK] Unstarred Updated');

return 0;
} catch (error) {
NewsLogger.error('[Hada] Update Liked to UnLiked Error: %o', {
NewsLogger.error('[GEEK] Update Liked to UnLiked Error: %o', {
error,
});

throw new HadaError(
'[Hada] Update Liked to UnLiked',
throw new GeekError(
'[GEEK] Update Liked to UnLiked',
'Update Liked to UnLiked Error.',
error instanceof Error ? error : new Error(JSON.stringify(error)),
);
}
}

async updateHadaNewsLiked(likedUuid: string, postUuid: string, clientUuid: string) {
async updateGeekNewsLiked(likedUuid: string, postUuid: string, clientUuid: string) {
try {
NewsLogger.debug('[HADA] Give Hacker News Star Request: %o', {
NewsLogger.debug('[GEEK] Give Hacker News Star Request: %o', {
likedUuid,
postUuid,
clientUuid,
Expand All @@ -159,62 +158,19 @@ export class GeekPrismaLibrary extends PrismaClient {
},
});

NewsLogger.info('[HADA] Starred Updated');
NewsLogger.info('[GEEK] Starred Updated');

return 0;
} catch (error) {
NewsLogger.error('[Hada] Update News Liked Error: %o', {
NewsLogger.error('[GEEK] Update News Liked Error: %o', {
error,
});

throw new HadaError(
'[Hada] Update News Liked',
throw new GeekError(
'[GEEK] Update News Liked',
'Update News Liked Error.',
error instanceof Error ? error : new Error(JSON.stringify(error)),
);
}
}

async getStarredHadaNewsPagination(page: number, size: number) {
try {
const totalPosts = await this.geek.count({ where: { liked: 1 } });

const starredNews = await this.geek.findMany({
select: {
uuid: true,
post: true,
link: true,
founded: true,
},
orderBy: {
founded: 'desc',
},
where: {
liked: 1,
},
take: size,
skip: (page - 1) * size,
});

NewsLogger.debug('[Hada] Founded Starred News: %o', {
totalPosts,
newsSize: starredNews.length,
});

return {
totalPosts,
starredNews,
};
} catch (error) {
NewsLogger.info('[Hada] Get Starred News Error: %o', {
error,
});

throw new HadaError(
'[Hada] Get Starred News',
'Get Starred News Error.',
error instanceof Error ? error : new Error(JSON.stringify(error)),
);
}
}
}
Loading

0 comments on commit 522f2b4

Please sign in to comment.