Skip to content
This repository has been archived by the owner on Jul 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #67 from rmagur1203/develop
Browse files Browse the repository at this point in the history
Implement: Like
  • Loading branch information
rmagur1203 authored Jun 10, 2022
2 parents 57c4e8f + ea06cee commit 17ae52c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 19 deletions.
17 changes: 17 additions & 0 deletions src/controllers/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ export class NamedBoardController {
}

@Get('/:id/like', [accessTokenGuard])
async isLiked(
@Request() req: _Request,
@Response() res: _Response,
@Params('id') id: number
) {
if (!req.user)
return ErrorHandler(new TypeError('req.user is undefined'), res);
try {
const result = await this.service.isLiked(req.user, id);
return res.status(result.status).json(result.data).end();
} catch (err) {
logger.error(err);
return res.status(HttpStatusCode.INTERNAL_SERVER_ERROR).end();
}
}

@Post('/:id/like', [accessTokenGuard])
async recommend(
@Request() req: _Request,
@Response() res: _Response,
Expand Down
17 changes: 10 additions & 7 deletions src/entities/Board.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable } from '@decorators/di';
import {
AfterLoad,
Column,
CreateDateColumn,
Entity,
Expand Down Expand Up @@ -39,9 +40,11 @@ export class BoardEntity {
@UpdateDateColumn()
updated: Date;

@ManyToMany(() => UserEntity)
@JoinColumn()
likedUsers?: UserEntity[];
@ManyToMany((type) => UserEntity, {
cascade: true,
})
@JoinTable()
likedUsers: UserEntity[];

@ManyToMany(() => AttachmentEntity)
@JoinTable()
Expand All @@ -51,16 +54,16 @@ export class BoardEntity {
@Entity('named_board')
@Injectable()
export class NamedBoardEntity extends BoardEntity {
@ManyToOne((type) => UserEntity, { eager: true, nullable: true })
@ManyToOne((type) => UserEntity, { eager: true })
@JoinColumn()
author?: UserEntity;
author: UserEntity;
}

@Entity('anonymous_board')
@Injectable()
export class AnonymousBoardEntity extends BoardEntity {
// TODO: author column change to encrypted user id
@ManyToOne((type) => UserEntity, { nullable: true })
@ManyToOne((type) => UserEntity)
@JoinColumn()
author?: UserEntity;
author: UserEntity;
}
1 change: 0 additions & 1 deletion src/entities/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
JoinTable,
} from 'typeorm';
import { UserDepartment } from '../types/user';
import { BoardEntity } from './Board';
import { ClubInfoEntity } from './ClubInfo';

export const USER_SELECT: (keyof UserEntity)[] = [
Expand Down
39 changes: 28 additions & 11 deletions src/services/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,23 @@ export class NamedBoardService {
};
}

async isLiked(userData: IUser, id: number): Promise<Board.WorkResult> {
const user = await this.userRepository.findOne(userData);
if (!user) return { status: HttpStatusCode.UNAUTHORIZED };
const board = await this.boardRepository.findOne(id, {
relations: ['likedUsers'],
});
if (!board) return { status: HttpStatusCode.NOT_FOUND };
const isLiked = board.likedUsers.some((x) => x.id === user.id);
return {
status: HttpStatusCode.OK,
data: isLiked,
};
}

async recommend(userData: IUser, id: number): Promise<Board.WorkResult> {
const user = await this.userRepository.findOne(userData);
if (!user) throw new Error('Unauthorization');
if (!user) return { status: HttpStatusCode.UNAUTHORIZED };
const board = await this.boardRepository.findOne(id, {
relations: ['likedUsers'],
});
Expand All @@ -56,18 +70,21 @@ export class NamedBoardService {
data: { success: false, message: 'Article not found' },
};

let likedUsers = board.likedUsers || [];
let likes = board.likes;
board.likedUsers = board.likedUsers || [];
const liked = board.likedUsers.some(
(x: UserEntity) => x.id === user.id
);
if (liked)
board.likedUsers = board.likedUsers.filter(
(x: UserEntity) => x.id !== user.id
);
else board.likedUsers.push(user);
board.likes = board.likedUsers.length;

if (board.likedUsers?.some((x: UserEntity) => x.id === user.id))
likedUsers = likedUsers.filter((x: UserEntity) => x.id !== user.id);
else likedUsers.push(user);
await this.boardRepository.update(id, {
likes,
likedUsers,
});
await this.boardRepository.save(board);
return {
status: HttpStatusCode.NO_CONTENT,
status: HttpStatusCode.OK,
data: { ...board, liked: !liked },
};
}

Expand Down

0 comments on commit 17ae52c

Please sign in to comment.