|
|
@@ -7,7 +7,7 @@ import {
|
|
|
InternalServerErrorException,
|
|
|
UnauthorizedException
|
|
|
} from '@nestjs/common'
|
|
|
-import { Repository, UpdateResult } from 'typeorm'
|
|
|
+import { In, Repository, UpdateResult } from 'typeorm'
|
|
|
import { InjectRepository } from '@nestjs/typeorm'
|
|
|
import { Moments } from './entities/moments.entity'
|
|
|
|
|
|
@@ -15,17 +15,23 @@ import { paginate, Pagination } from 'nestjs-typeorm-paginate'
|
|
|
import { PageRequest } from '../common/dto/page-request'
|
|
|
import { ChatRoleService } from '../chat-role/chat-role.service'
|
|
|
import { LikesService } from 'src/likes/likes.service'
|
|
|
+import { ChatRole } from 'src/chat-role/entities/chat-role.entity'
|
|
|
+import { Likes } from 'src/likes/entities/likes.entity'
|
|
|
|
|
|
@Injectable()
|
|
|
export class MomentsService {
|
|
|
constructor(
|
|
|
@InjectRepository(Moments)
|
|
|
private readonly momentsRepository: Repository<Moments>,
|
|
|
+ @InjectRepository(ChatRole)
|
|
|
+ private readonly chatRoleRepository: Repository<ChatRole>,
|
|
|
+ @InjectRepository(Likes)
|
|
|
+ private readonly likesRepository: Repository<Likes>,
|
|
|
private readonly chatRoleService: ChatRoleService,
|
|
|
private readonly likesService: LikesService
|
|
|
) { }
|
|
|
|
|
|
- async findAll(req: PageRequest<Moments>): Promise<Pagination<Moments>> {
|
|
|
+ async findAll(req: PageRequest<Moments>, userId: number): Promise<Pagination<Moments>> {
|
|
|
const page = await paginate<Moments>(this.momentsRepository, req.page, req.search)
|
|
|
const ids = page.items.reduce((ids: number[], val: Moments) => {
|
|
|
if (!ids.includes(val.userId)) {
|
|
|
@@ -34,9 +40,24 @@ export class MomentsService {
|
|
|
return ids;
|
|
|
}, []);
|
|
|
|
|
|
+ const momnetsIds: number[] = page.items.map((moments) => moments.id)
|
|
|
+ const likes = await this.likesRepository.createQueryBuilder('likes')
|
|
|
+ .select('likes.targetId')
|
|
|
+ .where({
|
|
|
+ userId: +userId,
|
|
|
+ targetId: In(momnetsIds),
|
|
|
+ type: 1,
|
|
|
+ isValid: true
|
|
|
+ })
|
|
|
+ .getMany()
|
|
|
+ const targetIds: number[] = likes.map((like) => like.targetId);
|
|
|
+
|
|
|
const map = await this.chatRoleService.findMapByIds(ids);
|
|
|
page.items.forEach(c => {
|
|
|
c.chatRole = map.get(c.userId)
|
|
|
+ if (targetIds.includes(c.id)) {
|
|
|
+ c.isLiked = true
|
|
|
+ }
|
|
|
})
|
|
|
|
|
|
return page
|
|
|
@@ -52,12 +73,16 @@ export class MomentsService {
|
|
|
public async like(id: number, userId: number): Promise<void> {
|
|
|
const moments = await this.momentsRepository.findOneBy({ id: +id })
|
|
|
if (moments) {
|
|
|
- const likes = await this.likesService.like(id, userId, 2)
|
|
|
+ const likes = await this.likesService.like(id, userId, 1)
|
|
|
+ const chatRole = await this.chatRoleRepository.findOneBy({ id: +moments.userId })
|
|
|
if (likes.isValid) {
|
|
|
moments.momentsLikeCount += 1
|
|
|
+ chatRole.likeNumber += 1
|
|
|
} else {
|
|
|
moments.momentsLikeCount -= 1
|
|
|
+ chatRole.likeNumber -= 1
|
|
|
}
|
|
|
+ await this.chatRoleRepository.save(chatRole)
|
|
|
await this.momentsRepository.save(moments)
|
|
|
}
|
|
|
}
|