shoppingmallv2 init...

This commit is contained in:
최준흠 2023-08-02 13:09:38 +09:00
parent ae2d59ff20
commit 2a3d768b44

View File

@ -5,16 +5,13 @@ namespace App\Controllers\Front;
use App\Entities\OrderEntity;
use App\Models\OrderModel;
use App\Models\ProductModel;
use CodeIgniter\Cookie\Cookie;
use CodeIgniter\Cookie\CookieStore;
use CodeIgniter\Cookie\Exceptions\CookieException;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use DateTime;
use Psr\Log\LoggerInterface;
class OrderController extends FrontController
{
private $_productModel = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
$this->_model = new OrderModel($this->getFields());
@ -54,21 +51,6 @@ class OrderController extends FrontController
$this->_session->set(SESSION_NAMES['CART'], [...$order_uids, $entity->getPrimaryKey()]);
}
//장바구니에 담기
protected function insert_process()
{
//fieldData Rule 검사및 fieldData 적용
parent::insert_process();
//상품재고 줄이기
$productModel = new ProductModel();
$product = $productModel->getEntity([$this->_model->getPrimaryKey() => $this->_viewDatas['fieldDatas']['product_uid']]);
//구매 금액 비교
$price = ($product->getPrice() - $product->getSale()) * $this->_viewDatas['fieldDatas']['quantity'];
if ($price != $this->_viewDatas['fieldDatas']['price']) {
throw new \Exception("실 상품금액{$price} 와 구매금액{$this->_viewDatas['fieldDatas']['price']}이 서로 다릅니다.");
}
$productModel->decreaseStock($product, $this->_viewDatas['fieldDatas']['quantity']);
}
public function insert()
{
$msg = "";
@ -76,9 +58,19 @@ class OrderController extends FrontController
$this->_viewDatas['fields'] = $this->getFields(__FUNCTION__);
$this->_viewDatas['fieldRules'] = $this->_model->getFieldRules($this->_viewDatas['fields'], __FUNCTION__);
$this->insert_process();
$this->_productModel ?: new ProductModel();
//상품정보 가져오기
$product = $this->_productModel->getEntity([$this->_model->getPrimaryKey() => $this->_viewDatas['fieldDatas']['product_uid']]);
//구매 금액 비교
$price = ($product->getPrice() - $product->getSale()) * $this->_viewDatas['fieldDatas']['quantity'];
if ($price != $this->_viewDatas['fieldDatas']['price']) {
throw new \Exception("실 상품금액{$price} 와 구매금액{$this->_viewDatas['fieldDatas']['price']}이 서로 다릅니다.");
}
//Transaction 시작
$this->_model->transStart();
//Order(장바구니)에 넣기,상품재고 줄이기,Order(장바구니)번호 세션넣기
$entity = $this->_model->create($this->_viewDatas['fieldDatas']);
$this->_productModel->decreaseStock($product, $this->_viewDatas['fieldDatas']['quantity']);
$this->setCart($entity);
//Transaction Commit
$this->_model->transComplete();