Sharkey/packages/backend/src/server/api/endpoints/flash/like.ts

53 lines
1.4 KiB
TypeScript
Raw Normal View History

import { Inject, Injectable } from '@nestjs/common';
import type { FlashsRepository, FlashLikesRepository } from '@/models/index.js';
import { IdService } from '@/core/IdService.js';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { DI } from '@/di-symbols.js';
import { ApiError } from '../../error.js';
// eslint-disable-next-line import/no-default-export
@Injectable()
2023-07-03 05:50:13 +00:00
export default class extends Endpoint<'flash/like'> {
name = 'flash/like' as const;
constructor(
@Inject(DI.flashsRepository)
private flashsRepository: FlashsRepository,
@Inject(DI.flashLikesRepository)
private flashLikesRepository: FlashLikesRepository,
private idService: IdService,
) {
2023-07-03 05:50:13 +00:00
super(async (ps, me) => {
const flash = await this.flashsRepository.findOneBy({ id: ps.flashId });
if (flash == null) {
2023-07-03 05:50:13 +00:00
throw new ApiError(this.meta.errors.noSuchFlash);
}
if (flash.userId === me.id) {
2023-07-03 05:50:13 +00:00
throw new ApiError(this.meta.errors.yourFlash);
}
// if already liked
const exist = await this.flashLikesRepository.findOneBy({
flashId: flash.id,
userId: me.id,
});
if (exist != null) {
2023-07-03 05:50:13 +00:00
throw new ApiError(this.meta.errors.alreadyLiked);
}
// Create like
await this.flashLikesRepository.insert({
id: this.idService.genId(),
createdAt: new Date(),
flashId: flash.id,
userId: me.id,
});
this.flashsRepository.increment({ id: flash.id }, 'likedCount', 1);
});
}
}