Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[スキーマ案2] assignee を item が持つ #138

Merged
merged 7 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions app/models/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const itemSchema = z.object({
required_error: "種類が未選択です",
invalid_type_error: "不正な種類です",
}),
assignee: z.string().nullable(),
});

export type Item = z.infer<typeof itemSchema>;
Expand All @@ -33,10 +34,11 @@ export class ItemEntity implements Item {
public readonly name: string,
public readonly price: number,
public readonly type: ItemType,
public assignee: string | null,
) {}

static createNew({ name, price, type }: Item): ItemEntity {
return new ItemEntity(undefined, name, price, type);
static createNew({ name, price, type }: Omit<Item, "assignee">): ItemEntity {
return new ItemEntity(undefined, name, price, type, null);
}

static fromItem(item: WithId<Item>): WithId<ItemEntity> {
Expand All @@ -45,6 +47,7 @@ export class ItemEntity implements Item {
item.name,
item.price,
item.type,
item.assignee,
) as WithId<ItemEntity>;
}
}
3 changes: 3 additions & 0 deletions app/models/order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ describe("[unit] order entity", () => {
name: "item1",
price: 100,
type: "hot",
assignee: null,
},
{
id: "2",
name: "item2",
price: 341,
type: "ice",
assignee: null,
},
];
order.items.push(...items);
Expand All @@ -34,6 +36,7 @@ describe("[unit] order entity", () => {
name: "item3",
price: 100,
type: "ore",
assignee: null,
});
expect(order.total).toBe(541);
});
Expand Down
24 changes: 12 additions & 12 deletions app/models/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export const orderSchema = z.object({
createdAt: z.date(),
servedAt: z.date().nullable(),
items: z.array(itemSchema.required()),
assignee: z.string().nullable(),
total: z.number(),
orderReady: z.boolean(),
description: z.string(),
});

export type Order = z.infer<typeof orderSchema>;
Expand All @@ -23,9 +23,9 @@ export class OrderEntity implements Order {
private readonly _createdAt: Date,
private _servedAt: Date | null,
private _items: WithId<ItemEntity>[],
private _assignee: string | null,
private _total: number,
private _orderReady: boolean,
private _description: string,
) {}

static createNew({ orderId }: { orderId: number }): OrderEntity {
Expand All @@ -35,9 +35,9 @@ export class OrderEntity implements Order {
new Date(),
null,
[],
null,
0,
false,
"",
);
}

Expand All @@ -48,9 +48,9 @@ export class OrderEntity implements Order {
order.createdAt,
order.servedAt,
order.items,
order.assignee,
order.total,
order.orderReady,
order.description,
) as WithId<OrderEntity>;
}

Expand Down Expand Up @@ -81,13 +81,6 @@ export class OrderEntity implements Order {
this._items = items;
}

get assignee() {
return this._assignee;
}
set assignee(assignee: string | null) {
this._assignee = assignee;
}

get total() {
// items の更新に合わせて total を自動で計算する
// その代わり total は直接更新できない
Expand All @@ -100,6 +93,13 @@ export class OrderEntity implements Order {
return this._orderReady;
}

get description() {
return this._description;
}
set description(description: string) {
this._description = description;
}

// --------------------------------------------------
// methods
// --------------------------------------------------
Expand All @@ -121,9 +121,9 @@ export class OrderEntity implements Order {
createdAt: this.createdAt,
servedAt: this.servedAt,
items: this.items,
assignee: this.assignee,
total: this.total,
orderReady: this.orderReady,
description: this.description,
};
}
}
2 changes: 2 additions & 0 deletions app/repositories/item.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ describe("[db] itemRepository", async () => {
});

test("itemRepository.save (update)", async () => {
savedItemHoge.assignee = "toririm";
const savedItem = await itemRepository.save(savedItemHoge);
expect(savedItem.id).toEqual(savedItemHoge.id);
expect(savedItem.assignee).toEqual("toririm");
});

test("itemRepository.findById", async () => {
Expand Down
20 changes: 13 additions & 7 deletions app/repositories/order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const isEmulatorRunning = async (): Promise<boolean> => {
describe("[db] orderRepository", async () => {
// To use this environment, firebase emulator must be running.

let savedOrderHoge: WithId<OrderEntity>;
let savedOrderChange: WithId<OrderEntity>;
let orderRepository: OrderRepository;

beforeAll(async () => {
Expand All @@ -47,15 +47,21 @@ describe("[db] orderRepository", async () => {

test("orderRepository.save (create)", async () => {
const order = OrderEntity.createNew({ orderId: 2024 });
savedOrderHoge = await orderRepository.save(order);
expect(savedOrderHoge.id).toBeDefined();
savedOrderChange = await orderRepository.save(order);
expect(savedOrderChange.id).toBeDefined();
});

test("orderRepository.save (update)", async () => {
savedOrderHoge.assignee = "hoge";
const savedOrder = await orderRepository.save(savedOrderHoge);
expect(savedOrder.id).toEqual(savedOrderHoge.id);
expect(savedOrder.assignee).toEqual("hoge");
savedOrderChange.items.push({
id: "1",
name: "item1",
price: 100,
type: "hot",
assignee: null,
});
const savedOrder = await orderRepository.save(savedOrderChange);
expect(savedOrder.id).toEqual(savedOrderChange.id);
expect(savedOrder.items).toEqual(savedOrderChange.items);
});

test("orderRepository.findById", async () => {
Expand Down
16 changes: 12 additions & 4 deletions app/routes/_header.casher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ const mockOrder: Order = {
// price: 300,
// },
],
assignee: "1st",
total: 0,
orderReady: false,
description: "",
};

export default function Casher() {
Expand Down Expand Up @@ -207,7 +207,13 @@ export const clientAction: ClientActionFunction = async ({ request }) => {

const newItem = submission.value;
// あとでマシなエラーハンドリングにする
const savedItem = await itemRepository.save(ItemEntity.createNew(newItem));
const savedItem = await itemRepository.save(
ItemEntity.createNew({
name: newItem.name,
price: newItem.price,
type: newItem.type,
}),
);

console.log("Document written with ID: ", savedItem.id);
return new Response(null, { status: 204 });
Expand Down Expand Up @@ -236,10 +242,11 @@ export class ItemEntity implements Item {
public readonly name: string,
public readonly price: number,
public readonly type: ItemType,
public assignee: string | null,
) {}

static createNew({ name, price, type }: Item): ItemEntity {
return new ItemEntity(undefined, name, price, type);
static createNew({ name, price, type }: Omit<Item, "assignee">): ItemEntity {
return new ItemEntity(undefined, name, price, type, null);
}

static fromItem(item: WithId<Item>): WithId<ItemEntity> {
Expand All @@ -248,6 +255,7 @@ export class ItemEntity implements Item {
item.name,
item.price,
item.type,
item.assignee,
) as WithId<ItemEntity>;
}
}