なんかもうめっちゃ変えた

This commit is contained in:
syuilo
2022-09-18 03:27:08 +09:00
committed by GitHub
parent d9ab03f086
commit b75184ec8e
946 changed files with 41219 additions and 28839 deletions

View File

@@ -1,7 +1,9 @@
import define from '../../../define.js';
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { GalleryLikesRepository, GalleryPostsRepository } from '@/models/index.js';
import { IdService } from '@/core/IdService.js';
import { DI } from '@/di-symbols.js';
import { ApiError } from '../../../error.js';
import { GalleryPosts, GalleryLikes } from '@/models/index.js';
import { genId } from '@/misc/gen-id.js';
export const meta = {
tags: ['gallery'],
@@ -40,33 +42,46 @@ export const paramDef = {
} as const;
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => {
const post = await GalleryPosts.findOneBy({ id: ps.postId });
if (post == null) {
throw new ApiError(meta.errors.noSuchPost);
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> {
constructor(
@Inject(DI.galleryPostsRepository)
private galleryPostsRepository: GalleryPostsRepository,
@Inject(DI.galleryLikesRepository)
private galleryLikesRepository: GalleryLikesRepository,
private idService: IdService,
) {
super(meta, paramDef, async (ps, me) => {
const post = await this.galleryPostsRepository.findOneBy({ id: ps.postId });
if (post == null) {
throw new ApiError(meta.errors.noSuchPost);
}
if (post.userId === me.id) {
throw new ApiError(meta.errors.yourPost);
}
// if already liked
const exist = await this.galleryLikesRepository.findOneBy({
postId: post.id,
userId: me.id,
});
if (exist != null) {
throw new ApiError(meta.errors.alreadyLiked);
}
// Create like
await this.galleryLikesRepository.insert({
id: this.idService.genId(),
createdAt: new Date(),
postId: post.id,
userId: me.id,
});
this.galleryPostsRepository.increment({ id: post.id }, 'likedCount', 1);
});
}
if (post.userId === user.id) {
throw new ApiError(meta.errors.yourPost);
}
// if already liked
const exist = await GalleryLikes.findOneBy({
postId: post.id,
userId: user.id,
});
if (exist != null) {
throw new ApiError(meta.errors.alreadyLiked);
}
// Create like
await GalleryLikes.insert({
id: genId(),
createdAt: new Date(),
postId: post.id,
userId: user.id,
});
GalleryPosts.increment({ id: post.id }, 'likedCount', 1);
});
}