Chg: ConfigRepository.getのreturnをEntityに / Chg: setup/initilizationをのconfig操作をRepositoryに / Fix: setup/create-adminの重複チェックを解消
This commit is contained in:
@@ -3,12 +3,10 @@ import type { ConfigEntity } from "@/modules/entities/Config";
|
|||||||
|
|
||||||
export class ConfigRepository extends EntityRepository<ConfigEntity> {
|
export class ConfigRepository extends EntityRepository<ConfigEntity> {
|
||||||
async set(name: string, value: string) {
|
async set(name: string, value: string) {
|
||||||
await this.upsert({
|
return await this.upsert({
|
||||||
name,
|
name,
|
||||||
value,
|
value,
|
||||||
});
|
});
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async get(name: string, defaultValue?: string) {
|
async get(name: string, defaultValue?: string) {
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import logger from "@/lib/logger";
|
|||||||
import type { FastifyInstance } from "fastify";
|
import type { FastifyInstance } from "fastify";
|
||||||
import { UserRepository } from "@/modules/repositories/User";
|
import { UserRepository } from "@/modules/repositories/User";
|
||||||
import { DatabaseError, ErrorBase, InputError } from "@/errors";
|
import { DatabaseError, ErrorBase, InputError } from "@/errors";
|
||||||
import { ConfigEntity } from "@/modules/entities/Config";
|
|
||||||
|
|
||||||
export default function CreateAdmin(fastify: FastifyInstance) {
|
export default function CreateAdmin(fastify: FastifyInstance) {
|
||||||
fastify.post("/", async (req, res) => {
|
fastify.post("/", async (req, res) => {
|
||||||
@@ -15,22 +14,6 @@ export default function CreateAdmin(fastify: FastifyInstance) {
|
|||||||
return res.code(400).send(InputError(result.error.issues));
|
return res.code(400).send(InputError(result.error.issues));
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
const configCount = await fastify.orm.em.count(ConfigEntity);
|
|
||||||
|
|
||||||
if (configCount === 0) {
|
|
||||||
return res.code(409).send(ErrorBase({
|
|
||||||
bad: "client",
|
|
||||||
code: "yet_initialization",
|
|
||||||
message: "初期設定が行われていません。",
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
logger.error("Database Error: Could not check if already initialization:", err);
|
|
||||||
|
|
||||||
return res.code(500).send(DatabaseError());
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const userCount = await fastify.orm.em.getRepository(UserEntity).count();
|
const userCount = await fastify.orm.em.getRepository(UserEntity).count();
|
||||||
|
|
||||||
|
|||||||
@@ -46,12 +46,12 @@ export default function Initialization(fastify: FastifyInstance) {
|
|||||||
const entries = Object.entries(result.data).filter(([key]) => key !== "force");
|
const entries = Object.entries(result.data).filter(([key]) => key !== "force");
|
||||||
|
|
||||||
for (const [key, value] of entries) {
|
for (const [key, value] of entries) {
|
||||||
const entity = fastify.orm.em.create(ConfigEntity, {
|
const entity = fastify.orm.em.getRepository(ConfigEntity).set(
|
||||||
name: key,
|
key,
|
||||||
value: typeof value === "string"
|
typeof value === "string"
|
||||||
? value
|
? value
|
||||||
: String(value),
|
: String(value)
|
||||||
});
|
);
|
||||||
|
|
||||||
fastify.orm.em.persist(entity);
|
fastify.orm.em.persist(entity);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user