Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | import { drizzle } from 'drizzle-orm/node-postgres';
import { history } from '../../db/schema';
import {
HistoryDeleteRequestSchemaType,
HistoryDeleteResponseSchemaType,
HistoryGetResponseSchemaType,
HistoryPostRequestSchemaType,
HistoryPostResponseSchemaType,
} from 'validator';
import { HistoryServiceType } from '../model/history';
import { and, eq } from 'drizzle-orm';
export class HistoryService implements HistoryServiceType {
private hyperdrive: Readonly<Hyperdrive>;
constructor(hyperdrive: Hyperdrive) {
this.hyperdrive = hyperdrive;
}
// データベースのhistoryの行の全取得
async selectHistoryDBAll(): Promise<(typeof history.$inferSelect)[]> {
const db = drizzle({ connection: this.hyperdrive });
const result = await db.select().from(history);
return result;
}
// データベースのhistoryの行の取得(fromとtoを指定)
async selectHistoryDBByFromTo({ from, to }: { from: string; to: string }): Promise<(typeof history.$inferSelect)[]> {
const db = drizzle({ connection: this.hyperdrive });
const result = await db
.select()
.from(history)
.where(and(eq(history.from, from), eq(history.to, to)));
return result;
}
// データベースのhistoryの行の挿入(fromとtoとamountを指定)
async insertHistoryDB(historyData: typeof history.$inferInsert): Promise<(typeof history.$inferSelect)[]> {
const db = drizzle({ connection: this.hyperdrive });
const result = await db.insert(history).values(historyData).returning();
return result;
}
// データベースのhistoryの行の削除(idを指定)
async deleteHistoryDBById({ id }: { id: number }): Promise<(typeof history.$inferSelect)[]> {
const db = drizzle({ connection: this.hyperdrive });
const result = await db.delete(history).where(eq(history.id, id)).returning();
return result;
}
// /api/historyのGET
async getHistoryService(): Promise<HistoryGetResponseSchemaType> {
const result = await this.selectHistoryDBAll();
return result;
}
// /api/historyのPOST
async postHistoryService(historyPostRequest: HistoryPostRequestSchemaType): Promise<HistoryPostResponseSchemaType> {
const match_data = await this.selectHistoryDBByFromTo({
from: historyPostRequest.from,
to: historyPostRequest.to,
});
if (match_data.length > 0) {
await this.deleteHistoryDBById({ id: match_data[0].id });
historyPostRequest.amount += match_data[0].amount;
}
const reverse_match_data = await this.selectHistoryDBByFromTo({
from: historyPostRequest.to,
to: historyPostRequest.from,
});
if (reverse_match_data.length > 0) {
await this.deleteHistoryDBById({ id: reverse_match_data[0].id });
historyPostRequest.amount -= reverse_match_data[0].amount;
if (historyPostRequest.amount < 0) {
const temp = historyPostRequest.from;
historyPostRequest.from = historyPostRequest.to;
historyPostRequest.to = temp;
historyPostRequest.amount = -historyPostRequest.amount;
}
}
if (historyPostRequest.amount === 0) {
return null;
}
const result = await this.insertHistoryDB(historyPostRequest);
return result;
}
// /api/historyのDELETE
async deleteHistoryService(
historyDeleteRequest: HistoryDeleteRequestSchemaType
): Promise<HistoryDeleteResponseSchemaType> {
const result = await this.deleteHistoryDBById({ id: historyDeleteRequest.id });
return result.length > 0 ? result[0] : null;
}
}
|