Skip to content

Commit 2556a1f

Browse files
r4aisou1118
andauthored
feat: 商品を論理削除する機能を実装 (#53)
Co-authored-by: Kattyan <[email protected]>
1 parent 6957059 commit 2556a1f

File tree

7 files changed

+50
-25
lines changed

7 files changed

+50
-25
lines changed

app/components/organisms/kitchen/OrderCard.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import PropTypes from "prop-types"
66
type OrderItem = {
77
productName: string
88
quantity: number
9+
deleted: boolean
910
}
1011

1112
type Props = {
@@ -35,7 +36,8 @@ export const OrderCard: FC<Props> = memo((props) => {
3536
{orderItems.map((item, index) => (
3637
<Box key={index}>
3738
<Text>
38-
{item.productName}--数量:{item.quantity}
39+
{item.productName}
40+
{item.deleted && " (削除済み)"} -- 数量:{item.quantity}
3941
</Text>
4042
</Box>
4143
))}
@@ -90,6 +92,7 @@ OrderCard.propTypes = {
9092
productName: PropTypes.string.isRequired,
9193
quantity: PropTypes.number.isRequired,
9294
memo: PropTypes.string,
95+
deleted: PropTypes.bool.isRequired,
9396
}).isRequired,
9497
).isRequired,
9598
status: PropTypes.string.isRequired,

app/crud/crud_products.ts

+15-13
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ export async function createProduct(data: Omit<TypeProduct, "product_id">) {
1616
}
1717

1818
//productの読み込み
19-
export async function readProduct() {
20-
return await prisma.products.findMany()
19+
export async function readProduct({ includeDeleted = false } = {}) {
20+
return await prisma.products.findMany({
21+
where: {
22+
deleted_at: includeDeleted ? undefined : null,
23+
},
24+
})
2125
}
2226

2327
//商品の変更
@@ -27,7 +31,7 @@ export async function updateProduct(
2731
) {
2832
return await prisma.products.update({
2933
where: {
30-
product_id: product_id,
34+
product_id,
3135
},
3236
data: {
3337
...data,
@@ -57,6 +61,9 @@ export async function existProduct(name: string) {
5761
product_name: {
5862
equals: name,
5963
},
64+
deleted_at: {
65+
equals: null,
66+
},
6067
},
6168
})
6269
}
@@ -67,21 +74,16 @@ export async function deleteAllProducts() {
6774
}
6875

6976
//Productの削除
70-
// export async function deleteProduct(product_id: number) {
71-
// console.log("Deleting product with ID:", product_id);
72-
// return await prisma.products.delete({
73-
// where: {
74-
// product_id: product_id,
75-
// },
76-
// });
77-
// }
7877
export async function deleteProduct(product_id: number) {
7978
console.log("Deleting product with ID:", product_id)
8079

8180
try {
82-
const result = await prisma.products.delete({
81+
const result = await prisma.products.update({
8382
where: {
84-
product_id: product_id,
83+
product_id,
84+
},
85+
data: {
86+
deleted_at: new Date(),
8587
},
8688
})
8789
console.log("Product deleted successfully:", result)

app/routes/kitchen.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export default function Kitchen() {
6969
return {
7070
productName: product?.product_name || "",
7171
quantity: detail.quantity,
72+
deleted: product?.deleted_at != null,
7273
}
7374
})
7475

@@ -124,7 +125,7 @@ export default function Kitchen() {
124125
export const loader = async () => {
125126
const response_orders = await readOrder()
126127
const response_details = await readDetail()
127-
const response_products = await readProduct()
128+
const response_products = await readProduct({ includeDeleted: true })
128129
return json({
129130
orders: response_orders,
130131
details: response_details,

app/routes/order_table.tsx

+10-9
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ export default function OrderTable() {
5757
const filteredProducts = products.filter((product) =>
5858
productIds.includes(product.product_id),
5959
)
60-
const productNames = filteredProducts.map(
61-
(product) => product.product_name,
62-
)
6360
const quantities = filteredDetails.map(
6461
(detail) => detail.quantity,
6562
)
@@ -92,11 +89,15 @@ export default function OrderTable() {
9289
<Td>{createTime}</Td>
9390
<Td>{order.table_number}</Td>
9491
<Td>
95-
{productNames.map((name, index) => (
96-
<div key={index}>
97-
{name}--数量:{quantities[index]}
98-
</div>
99-
))}
92+
{filteredProducts.map(
93+
({ product_name, deleted_at }, index) => (
94+
<div key={index}>
95+
{product_name}
96+
{deleted_at != null && " (削除済み)"} -- 数量:
97+
{quantities[index]}
98+
</div>
99+
),
100+
)}
100101
</Td>
101102
<Td>{totalPrice}</Td>
102103
<Td>{order.status}</Td>
@@ -113,7 +114,7 @@ export default function OrderTable() {
113114
export const loader = async () => {
114115
const response_orders = await readOrder()
115116
const response_details = await readDetail()
116-
const response_products = await readProduct()
117+
const response_products = await readProduct({ includeDeleted: true })
117118
return json({
118119
orders: response_orders,
119120
details: response_details,

app/type/typeproduct.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export type TypeProduct = {
44
price: number
55
stock: number
66
image?: string
7+
deleted_at?: string | null
78
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
Warnings:
3+
4+
- A unique constraint covering the columns `[product_name,deleted_at]` on the table `Products` will be added. If there are existing duplicate values, this will fail.
5+
6+
*/
7+
-- DropIndex
8+
DROP INDEX "Products_product_name_key";
9+
10+
-- AlterTable
11+
ALTER TABLE "Products" ADD COLUMN "deleted_at" DATETIME;
12+
13+
-- CreateIndex
14+
CREATE UNIQUE INDEX "Products_product_name_deleted_at_key" ON "Products"("product_name", "deleted_at");

prisma/schema.prisma

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ model Order_details {
3333

3434
model Products {
3535
product_id Int @id @default(autoincrement())
36-
product_name String @unique
36+
product_name String
3737
price Int
3838
stock Int
3939
image String
40+
deleted_at DateTime?
4041
orderDetails Order_details[]
42+
43+
@@unique([product_name, deleted_at])
4144
}

0 commit comments

Comments
 (0)