67 lines
1.7 KiB
TypeScript
67 lines
1.7 KiB
TypeScript
import { EntityRepository } from "@mikro-orm/postgresql";
|
|
import { ErrorBase } from "@/errors";
|
|
import z from "zod/v3";
|
|
import { UserRepository } from "@/modules/repositories/User";
|
|
import type { CommunityEntity } from "@/modules/entities/Community";
|
|
|
|
export class CommunityRepository extends EntityRepository<CommunityEntity> {
|
|
public static schema = z.object({
|
|
name: z.string().trim().min(1).max(20),
|
|
description: z.string().trim().min(1).max(4096),
|
|
userid: UserRepository.schema.shape.userid,
|
|
icon: z.string().url(),
|
|
});
|
|
|
|
async listCommunity(limit: number = 20, sinceData?: string) {
|
|
let since = sinceData ?? new Date();
|
|
|
|
if (
|
|
sinceData &&
|
|
!isNaN(new Date(sinceData).getTime())
|
|
) {
|
|
const itCommunity = await this.findOne({ id: sinceData });
|
|
|
|
if (!itCommunity) {
|
|
return ErrorBase({
|
|
bad: "client",
|
|
code: "community_not_found",
|
|
message: "対象のコミュニティが見つかりませんでした。",
|
|
});
|
|
}
|
|
|
|
since = itCommunity.createdAt;
|
|
}
|
|
|
|
const findResult = await this.find({
|
|
createdAt: {
|
|
$lt: since,
|
|
},
|
|
}, {
|
|
orderBy: {
|
|
createdAt: "DESC",
|
|
},
|
|
limit: limit,
|
|
});
|
|
|
|
return findResult ?? [];
|
|
}
|
|
|
|
async createCommunity(data: Omit<z.infer<typeof CommunityRepository.schema>, "icon">) {
|
|
const community = this.create({
|
|
...data,
|
|
createdBy: data.userid,
|
|
});
|
|
|
|
await this.em.persist(community).flush();
|
|
return community.id;
|
|
}
|
|
|
|
async editCommunity(
|
|
target: CommunityEntity,
|
|
data: Partial<Omit<z.infer<typeof CommunityRepository.schema>, "userid">>
|
|
) {
|
|
await this.nativeUpdate(target, data);
|
|
return target.id;
|
|
}
|
|
}
|