在庫追加のカレンダー修正

feature-backend-zhang
akito.nishiwaki 4 months ago committed by zhang.pengcheng
parent 9ae705a849
commit 34d2487df8
  1. 4
      backend/src/main/java/com/example/todoapp/service/StocksService.java
  2. 22
      backend/src/main/java/com/example/todoapp/service/ToBuysService.java
  3. 12
      frontend/src/pages/StockPage.tsx

@ -1,6 +1,5 @@
package com.example.todoapp.service;
import com.example.todoapp.dto.StockDTO;
import com.example.todoapp.dto.AddStocksDTO;
import com.example.todoapp.dto.UpdateStockRequest;
import com.example.todoapp.model.Stocks;
@ -57,9 +56,6 @@ public class StocksService {
} else {
// 材料情報を取得
Optional<Stuffs> existstuffs = stuffsRepository.findById(stock.getStuffId());
if (existstuffs == null) {
throw new RuntimeException("材料がありません");
}
stuffs = existstuffs.get();
}

@ -91,14 +91,22 @@ public class ToBuysService {
stuff = optStuff.get();
}
ToBuys toBuys = new ToBuys();
toBuys.setUser(user);
toBuys.setStuff(stuff);
toBuys.setAmount(toBuyDTO.getAmount());
toBuys.setStore(toBuyDTO.getShop());
Optional<ToBuys> existingToBuy = toBuysRepository.findByUserAndStuff(user, stuff);
// データベースに保存
return toBuysRepository.save(toBuys);
if (existingToBuy.isPresent()) {
// 存在する場合は数量を更新
ToBuys existing = existingToBuy.get();
existing.setAmount(existing.getAmount() + toBuyDTO.getAmount());
return toBuysRepository.save(existing);
} else {
// 新しい材料を作成
ToBuys toBuys = new ToBuys();
toBuys.setUser(user);
toBuys.setStuff(stuff);
toBuys.setAmount(toBuyDTO.getAmount());
toBuys.setStore(toBuyDTO.getShop());
return toBuysRepository.save(toBuys);
}
}

@ -30,6 +30,14 @@ import { STOCK_ERRORS } from '../constants/errorMessages';
import DatePicker, { registerLocale } from 'react-datepicker';
import { ja } from 'date-fns/locale/ja'; // date-fnsの日本語ロケールをインポート
// 日付をyyyy-MM-dd形式で返す関数
const formatDateLocal = (date: Date) => {
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
return `${year}-${month}-${day}`;
};
// 新規在庫の初期状態
const EMPTY_STOCK: Omit<Stock, 'stockId' | 'stuffId'> & { stuffId: number | null } & { newAddition: boolean } = {
stuffId: null,
@ -494,7 +502,7 @@ const StockPage: React.FC = () => {
popperClassName="custom-datepicker-popper"
selected={newStock.buyDate ? new Date(newStock.buyDate) : null}
onChange={(date) =>
setNewStock({ ...newStock, buyDate: date ? date.toISOString().split('T')[0] : '' })
setNewStock({ ...newStock, buyDate: date ? formatDateLocal(date) : '' })
}
dateFormat="yyyy/MM/dd"
customInput={
@ -521,7 +529,7 @@ const StockPage: React.FC = () => {
popperClassName="custom-datepicker-popper"
selected={newStock.expDate ? new Date(newStock.expDate) : null}
onChange={(date) =>
setNewStock({ ...newStock, expDate: date ? date.toISOString().split('T')[0] : '' })
setNewStock({ ...newStock, expDate: date ? formatDateLocal(date) : '' })
}
dateFormat="yyyy/MM/dd"
customInput={

Loading…
Cancel
Save