not used to plugin testing - trying to get working but fixtures not working yet

This commit is contained in:
2025-03-26 00:07:05 -07:00
parent 23147fc574
commit b17634c08a
21 changed files with 628 additions and 58 deletions

View File

@@ -5,6 +5,7 @@ namespace CakeProducts\Controller;
use Cake\Log\Log;
use CakeProducts\Controller\AppController;
use CakeProducts\Service\InternalCatalogManagerService;
/**
* ExternalProductCatalogs Controller
@@ -45,18 +46,24 @@ class ExternalProductCatalogsController extends AppController
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
*/
public function add()
public function add(InternalCatalogManagerService $catalogManagerService)
{
$externalProductCatalog = $this->ExternalProductCatalogs->newEmptyEntity();
if ($this->request->is('post')) {
$externalProductCatalog = $this->ExternalProductCatalogs->patchEntity($externalProductCatalog, $this->request->getData());
if ($this->ExternalProductCatalogs->save($externalProductCatalog)) {
$postData = $this->request->getData();
if ($this->request->getSession()->read('Auth.User.id')) {
$postData['created_by'] = $this->request->getSession()->read('Auth.User.id');
}
$result = $catalogManagerService->createNewExternalCatalog($externalProductCatalog, $postData);
Log::debug(print_r('$result from createNewExternalCatalog', true));
Log::debug(print_r($result, true));
if ($result['result']) {
$this->Flash->success(__('The external product catalog has been saved.'));
return $this->redirect(['action' => 'index']);
}
Log::debug(print_r('$externalProductCatalog->getErrors() next - failed /add', true));
Log::debug(print_r($externalProductCatalog->getErrors(), true));
// Log::debug(print_r('$externalProductCatalog->getErrors() next - failed /add', true));
// Log::debug(print_r($externalProductCatalog->getErrors(), true));
$this->Flash->error(__('The external product catalog could not be saved. Please, try again.'));
}
$productCatalogs = $this->ExternalProductCatalogs->ProductCatalogs->find('list', limit: 200)->all();

View File

@@ -97,8 +97,8 @@ class ProductsTable extends Table
*/
public function buildRules(RulesChecker $rules): RulesChecker
{
$rules->add($rules->isUnique(['product_category_id', 'name']), ['errorField' => '0']);
$rules->add($rules->existsIn(['product_category_id'], 'ProductCategories'), ['errorField' => '1']);
$rules->add($rules->isUnique(['product_category_id', 'name']), ['errorField' => 'product_category_id']);
$rules->add($rules->existsIn(['product_category_id'], 'ProductCategories'), ['errorField' => 'product_category_id']);
return $rules;
}

View File

@@ -10,6 +10,7 @@ use Cake\Log\Log;
use Cake\ORM\Locator\LocatorAwareTrait;
use Cake\ORM\Table;
use Cake\Utility\Text;
use CakeProducts\Model\Entity\ExternalProductCatalog;
use CakeProducts\Model\Entity\ProductCategory;
use CakeProducts\Model\Table\ProductCatalogsTable;
@@ -55,6 +56,40 @@ class InternalCatalogManagerService
return $this->ProductCatalogs->get($id, contain: $contain);
}
/**
* @param ExternalProductCatalog $externalProductCatalog external product catalog entity
* @param array $data data to save
*
* @return array
*/
public function createNewExternalCatalog(ExternalProductCatalog $externalProductCatalog, array $data = []): array
{
$now = Time::now();
$associated = [];
Log::info('posted data - adding new ExternalProductCatalog');
Log::info(print_r($data, true));
$saveOptions = [
'associated' => $associated,
];
$externalProductCatalog = $this->ProductCatalogs->ExternalProductCatalogs->patchEntity($externalProductCatalog, $data, $saveOptions);
if ($externalProductCatalog->getErrors()) {
Log::debug(print_r('$externalProductCatalog->getErrors() next - failed to save from create new external product catalog', true));
Log::debug(print_r($externalProductCatalog->getErrors(), true));
}
$returnData = [
'entity' => $externalProductCatalog,
'result' => $this->ProductCatalogs->ExternalProductCatalogs->save($externalProductCatalog, $saveOptions),
'apiResults' => [],
];
if ($returnData['result'] && $this->externalCatalogManager) {
// $returnData['apiResults'] = $this->externalCatalogManager->newCatalogCreated($returnData['result']);
}
return $returnData;
}
/**
* @param string|null $id
*