Skip to content

Commit

Permalink
Merge pull request #12 from mju-likelion/feature/room-result
Browse files Browse the repository at this point in the history
방 결과 API 만들기
  • Loading branch information
ndaemy authored Aug 9, 2023
2 parents 83dc0d3 + b718a55 commit 76aeee1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"bcrypt": "^5.1.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"lodash": "^4.17.21",
"nanoid": "^3.3.6",
"passport": "^0.6.0",
"passport-jwt": "^4.0.1",
Expand All @@ -45,6 +46,7 @@
"@nestjs/testing": "^9.0.0",
"@types/express": "^4.17.13",
"@types/jest": "29.5.0",
"@types/lodash": "^4.14.196",
"@types/node": "18.15.11",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^5.0.0",
Expand Down
6 changes: 3 additions & 3 deletions src/api/rooms/rooms.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export class RoomsController {
return this.roomsService.findOne(code);
}

@Get(':code/summary')
getRoomSummary(@Param('code') code: string) {
return this.roomsService.getRoomSummary(code);
@Get(':code/result')
getRoomResult(@Param('code') code: string) {
return this.roomsService.getRoomResult(code);
}
}
50 changes: 48 additions & 2 deletions src/api/rooms/rooms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
NotFoundException,
} from '@nestjs/common';
import { Room } from '@prisma/client';
import uniq from 'lodash/uniq';
import { customAlphabet } from 'nanoid';

import { PrismaService } from '@/prisma/prisma.service';
Expand Down Expand Up @@ -48,8 +49,53 @@ export class RoomsService {
return room;
}

getRoomSummary(code: string) {
return `This action returns a ${code} room summary`;
async getRoomResult(code: string) {
const room = await this.prismaService.room.findUnique({
where: { code },
include: {
users: {
select: {
enableTimes: true,
},
},
},
});

if (!room) {
throw new NotFoundException(`Room with code ${code} not found`);
}

let enableTimesList: string[];

if (room.dateOnly) {
enableTimesList = room.users
.map(user => user.enableTimes)
.flat()
.sort();
} else {
enableTimesList = room.users
.map(user => uniq(user.enableTimes.map(time => time.split(' ')[0])))
.flat()
.map(time => time.split(' ')[0])
.sort();
}

const timeMap = new Map();
enableTimesList.forEach(time => {
if (timeMap.has(time)) {
timeMap.set(time, timeMap.get(time) + 1);
} else {
timeMap.set(time, 1);
}
});
const enableTimes = Object.fromEntries(timeMap.entries());

delete room.users;

return {
...room,
enableTimes,
};
}

validateDates(createRoomDto: CreateRoomDto) {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"esModuleInterop": true,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false,
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,11 @@
dependencies:
"@types/node" "*"

"@types/lodash@^4.14.196":
version "4.14.196"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.196.tgz#a7c3d6fc52d8d71328b764e28e080b4169ec7a95"
integrity sha512-22y3o88f4a94mKljsZcanlNWPzO0uBsBdzLAngf2tp533LzZcQzb6+eZPJ+vCTt+bqF2XnvT9gejTLsAcJAJyQ==

"@types/mime@*":
version "3.0.1"
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-3.0.1.tgz#5f8f2bca0a5863cb69bc0b0acd88c96cb1d4ae10"
Expand Down

0 comments on commit 76aeee1

Please sign in to comment.