Skip to content

Commit

Permalink
feat: [#37]
Browse files Browse the repository at this point in the history
  • Loading branch information
newbieJanghan committed Nov 2, 2022
1 parent 826aaa0 commit e668ae9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 59 deletions.
1 change: 1 addition & 0 deletions server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { PoliticianModule } from './politician/politician.module';
useNewUrlParser: true,
useUnifiedTopology: true,
connectionFactory: (connection) => {
console.log(process.env.MONGODB_URI)
if (connection.readyState === 1) {
Logger.log('DB connected');
console.log('################ MongoDB connected #################');
Expand Down
10 changes: 2 additions & 8 deletions server/src/issue/issue.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,11 @@ export class IssueController {
try {
const { targetPolitician, regiStatus, ranked, pageOptions } = issueQuery;

// 메인페이지 모든 정치인, 인생 전체 그래프
// if (!targetPolitician) {
// const issues = await this.issueService.getAllIssues();
// return response.json(issues);
// }

// 등록된 이슈
if (regiStatus && !ranked) {
const issues = await this.issueService.getIssuesRegistered(targetPolitician, pageOptions);

const itemCount = await this.issueService.getAllIssuesCount('active');
const itemCount = await this.issueService.getAllActiveIssuesCount();
const pageMeta = new PageMetaDto({ pageOptions, itemCount });
return response.json({ data: issues, meta: pageMeta });
}
Expand All @@ -56,7 +50,7 @@ export class IssueController {

// 미등록 이슈 나머지
else if (!regiStatus && !ranked) {
const itemCount = await this.issueService.getAllIssuesCount('inactive');
const itemCount = await this.issueService.getAllInactiveIssuesCount();

if (itemCount <= 3) {
return response.json({ data: [] });
Expand Down
60 changes: 9 additions & 51 deletions server/src/issue/issue.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,60 +19,18 @@ export class IssueService {
const { targetPolitician, issueDate, content, title, link } = body;
const result = await new this.issueModel({ targetPolitician, issueDate, content, title, link, regiUser }).save();

// // test case 만들기 위한 임시
// const result = await new this.issueModel({ ...body, regiUser }).save();

// const setRegiStatusInactiveJob = scheduleJob(regiDueDate, () => {
// this.setRegiStatus(save._id, 'expired');
// });

return result;
}

// async getAllIssues() {
// const allIssues = await this.issueModel.aggregate([
// {
// $project: {
// _id: 1,
// targetPolitician: 1,
// issueDate: 1,
// totalPolls: { $add: ['$poll.total.pro', '$poll.total.neu', '$poll.total.con'] },
// score: { $subtract: ['$poll.total.pro', '$poll.total.con'] },
// },
// },
// { $sort: { totalPolls: -1 } },
// { $limit: 40 },
// { $sort: { issueDate: 1 } },
// {
// $group: { _id: '$targetPolitician', issues: { $push: '$$ROOT' } },
// },
// {
// $lookup: {
// from: 'politicians',
// localField: '_id',
// foreignField: '_id',
// as: 'politicianInfo',
// pipeline: [
// {
// $project: {
// issues: 0,
// _id: 0,
// createdAt: 0,
// updatedAt: 0,
// __v: 0,
// },
// },
// ],
// },
// },
// ]);

// return allIssues;
// }
// regiStatus: 'active'
async getAllActiveIssuesCount(): Promise<number> {
return await this.issueModel.find({ regiStatus: 'active' }).count();
}

// regiStatus type needed
async getAllIssuesCount(regiStatus): Promise<number> {
return await this.issueModel.find({ regiStatus }).count();
// regiStatus: 'inactive'
async getAllInactiveIssuesCount(): Promise<number> {
const due = DateTime.now().minus({ weeks: 1 }).toBSON();
return await this.issueModel.find({ regiStatus: 'inactive', createdAt: { $gt: due } }).count();
}

// return type needed
Expand All @@ -88,7 +46,7 @@ export class IssueService {
// return type needed
async getIssueNotRegisteredRanked(id: string): Promise<Issue[]> {
const due = DateTime.now().minus({ weeks: 1 }).toBSON();

const issues = await this.issueModel.aggregate([
{
$match: { $expr: { $eq: ['$targetPolitician', { $toObjectId: id }] } },
Expand Down
1 change: 1 addition & 0 deletions server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
console.log(process.env.MONGODB_URI)
const app = await NestFactory.create(AppModule);
app.enableCors();
app.useGlobalPipes(
Expand Down
10 changes: 10 additions & 0 deletions server/src/politician/politician.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export class PoliticianService {
foreignField: 'targetPolitician',
as: 'count',
pipeline: [
{
$match: {
regiStatus: 'active',
},
},
{
$count: 'count',
},
Expand All @@ -43,6 +48,11 @@ export class PoliticianService {
foreignField: 'targetPolitician',
as: 'issues',
pipeline: [
{
$match: {
regiStatus: 'active',
},
},
{
$project: {
issueDate: 1,
Expand Down

0 comments on commit e668ae9

Please sign in to comment.