色々
This commit is contained in:
@@ -2,6 +2,7 @@ import generateUniqueId from "@/lib/id";
|
||||
import { Entity, EntityRepositoryType, Index, ManyToOne, OptionalProps, PrimaryKey, Property } from "@mikro-orm/core";
|
||||
import { UserEntity } from "@/modules/entities/User";
|
||||
import { ChannelRepository } from "@/modules/repositories/Channel";
|
||||
import { CommunityEntity } from "@/modules/entities/Community";
|
||||
|
||||
@Entity({
|
||||
tableName: "channel",
|
||||
@@ -32,6 +33,9 @@ export class ChannelEntity {
|
||||
})
|
||||
description!: string;
|
||||
|
||||
@ManyToOne(() => CommunityEntity)
|
||||
community!: CommunityEntity;
|
||||
|
||||
@ManyToOne(() => UserEntity)
|
||||
createdBy!: UserEntity;
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import generateUniqueId from "@/lib/id";
|
||||
import { Entity, EntityRepositoryType, Index, ManyToOne, OptionalProps, PrimaryKey, Property } from "@mikro-orm/core";
|
||||
import { UserEntity } from "@/modules/entities/User";
|
||||
import { CommunityRepository } from "@/modules/repositories/Community";
|
||||
|
||||
@Entity({
|
||||
tableName: "community",
|
||||
repository: () => CommunityRepository,
|
||||
})
|
||||
export class CommunityEntity {
|
||||
[OptionalProps]?: "id" | "createdAt";
|
||||
[EntityRepositoryType]?: CommunityRepository;
|
||||
|
||||
@PrimaryKey({
|
||||
type: "string",
|
||||
length: 10,
|
||||
onCreate: () => generateUniqueId(),
|
||||
})
|
||||
id!: string;
|
||||
|
||||
@Property({
|
||||
type: "string",
|
||||
length: 20,
|
||||
unique: true,
|
||||
})
|
||||
@Index()
|
||||
name!: string;
|
||||
|
||||
@Property({
|
||||
type: "string",
|
||||
length: 4096,
|
||||
})
|
||||
description!: string;
|
||||
|
||||
@Property({
|
||||
type: "string",
|
||||
nullable: true,
|
||||
})
|
||||
icon?: string;
|
||||
|
||||
@ManyToOne(() => UserEntity)
|
||||
createdBy!: UserEntity;
|
||||
|
||||
@Property({
|
||||
type: "datetime",
|
||||
onCreate: () => new Date(),
|
||||
})
|
||||
createdAt!: Date;
|
||||
}
|
||||
@@ -9,9 +9,10 @@ export class ChannelRepository extends EntityRepository<ChannelEntity> {
|
||||
name: z.string().trim().min(1).max(20),
|
||||
description: z.string().trim().min(1).max(4096),
|
||||
userid: UserRepository.schema.shape.userid,
|
||||
community: z.string().length(10),
|
||||
});
|
||||
|
||||
async findChannel(limit: number = 20, sinceData?: string) {
|
||||
async listChannel(community: string, limit: number = 20, sinceData?: string) {
|
||||
let since = sinceData ?? new Date();
|
||||
|
||||
if (
|
||||
@@ -32,6 +33,7 @@ export class ChannelRepository extends EntityRepository<ChannelEntity> {
|
||||
}
|
||||
|
||||
const findResult = await this.find({
|
||||
community,
|
||||
createdAt: {
|
||||
$lt: since,
|
||||
},
|
||||
@@ -57,7 +59,7 @@ export class ChannelRepository extends EntityRepository<ChannelEntity> {
|
||||
|
||||
async editChannel(
|
||||
target: ChannelEntity,
|
||||
data: Partial<Omit<z.infer<typeof ChannelRepository.schema>, "userid">>
|
||||
data: Partial<Omit<z.infer<typeof ChannelRepository.schema>, "userid" | "community">>
|
||||
) {
|
||||
await this.nativeUpdate(target, data);
|
||||
return target.id;
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user