-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from game-node-app/dev
Dev
- Loading branch information
Showing
15 changed files
with
252 additions
and
9 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { IsNotEmpty, IsString, Max, Min } from "class-validator"; | ||
|
||
export class FollowRegisterDto { | ||
@IsNotEmpty() | ||
@IsString() | ||
@Min(36) | ||
@Max(36) | ||
followedUserId: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export class FollowStatusDto { | ||
isFollowing: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { FollowController } from './follow.controller'; | ||
|
||
describe('FollowController', () => { | ||
let controller: FollowController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [FollowController], | ||
}).compile(); | ||
|
||
controller = module.get<FollowController>(FollowController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Body, Controller, Get, Post, Query, UseGuards } from "@nestjs/common"; | ||
import { AuthGuard } from "../auth/auth.guard"; | ||
import { FollowService } from "./follow.service"; | ||
import { FollowRegisterDto } from "./dto/follow-register.dto"; | ||
import { SessionContainer } from "supertokens-node/recipe/session"; | ||
import { Session } from "../auth/session.decorator"; | ||
import { ApiOkResponse, ApiTags } from "@nestjs/swagger"; | ||
import { FollowStatusDto } from "./dto/follow-status.dto"; | ||
|
||
@Controller("follow") | ||
@ApiTags("follow") | ||
@UseGuards(AuthGuard) | ||
export class FollowController { | ||
constructor(private followService: FollowService) {} | ||
|
||
@Post() | ||
async registerFollow( | ||
@Session() session: SessionContainer, | ||
@Body() dto: FollowRegisterDto, | ||
) { | ||
return await this.followService.registerFollow( | ||
session.getUserId(), | ||
dto.followedUserId, | ||
); | ||
} | ||
|
||
@Get("status") | ||
@ApiOkResponse({ | ||
status: 200, | ||
type: FollowStatusDto, | ||
}) | ||
async getFollowerStatus( | ||
@Query("followerUserId") followerUserId: string, | ||
@Query("followedUserId") followedUserId: string, | ||
) { | ||
return this.followService.getStatus(followerUserId, followedUserId); | ||
} | ||
|
||
@Get("count") | ||
async getFollowersCount(@Query("targetUserId") targetUserId: string) { | ||
return await this.followService.getFollowersCount(targetUserId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,88 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { HttpException, HttpStatus, Injectable, Logger } from "@nestjs/common"; | ||
import { InjectRepository } from "@nestjs/typeorm"; | ||
import { UserFollow } from "./entity/user-follow.entity"; | ||
import { Repository } from "typeorm"; | ||
import { FollowStatusDto } from "./dto/follow-status.dto"; | ||
|
||
@Injectable() | ||
export class FollowService { | ||
private readonly logger = new Logger(FollowService.name); | ||
|
||
constructor( | ||
@InjectRepository(UserFollow) | ||
private userFollowRepository: Repository<UserFollow>, | ||
) {} | ||
|
||
public async registerFollow( | ||
followingUserId: string, | ||
followerUserId: string, | ||
followedUserId: string, | ||
) { | ||
if (followerUserId === followedUserId) { | ||
throw new HttpException( | ||
"User can't follow itself.", | ||
HttpStatus.I_AM_A_TEAPOT, | ||
); | ||
} | ||
try { | ||
await this.userFollowRepository.save({}); | ||
} catch (e) {} | ||
await this.userFollowRepository.save({ | ||
follower: { | ||
userId: followerUserId, | ||
}, | ||
followed: { | ||
userId: followedUserId, | ||
}, | ||
}); | ||
} catch (e) { | ||
this.logger.error(e); | ||
throw new HttpException( | ||
"Error while registering user follow", | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
); | ||
} | ||
} | ||
|
||
public async getStatus( | ||
followerUserId: string, | ||
followedUserId: string, | ||
): Promise<FollowStatusDto> { | ||
const userIdLength = 36; | ||
const params = [followerUserId, followedUserId] as const; | ||
for (const param of params) { | ||
if (typeof param !== "string" || param.length !== userIdLength) { | ||
throw new HttpException( | ||
"Malformed parameters.", | ||
HttpStatus.BAD_REQUEST, | ||
); | ||
} | ||
} | ||
|
||
if (followerUserId === followedUserId) { | ||
return { | ||
isFollowing: false, | ||
}; | ||
} | ||
|
||
const exist = await this.userFollowRepository.exist({ | ||
where: { | ||
follower: { | ||
userId: followerUserId, | ||
}, | ||
followed: { | ||
userId: followedUserId, | ||
}, | ||
}, | ||
}); | ||
|
||
return { | ||
isFollowing: exist, | ||
}; | ||
} | ||
|
||
public async getFollowersCount(userId: string) { | ||
return await this.userFollowRepository.countBy({ | ||
followed: { | ||
userId, | ||
}, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { IsNotEmpty, IsNumber } from "class-validator"; | ||
|
||
export class ReviewScoreRequestDto { | ||
@IsNotEmpty() | ||
@IsNumber() | ||
gameId: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Number of times a given review rating appears for a specific game | ||
*/ | ||
export class ReviewScoreDistribution { | ||
1: number; | ||
2: number; | ||
3: number; | ||
4: number; | ||
5: number; | ||
/** | ||
* Total number of reviews | ||
*/ | ||
total: number; | ||
} | ||
|
||
export class ReviewScoreResponseDto { | ||
median: number; | ||
distribution: ReviewScoreDistribution; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters