no message
This commit is contained in:
48
database/abstracts/BaseAccess.php
Normal file
48
database/abstracts/BaseAccess.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../../config/db_config.php';
|
||||
|
||||
/**
|
||||
* BaseAccessクラス(抽象クラス)
|
||||
*
|
||||
* データベースアクセスの基底クラスです。
|
||||
* 共通のデータベース接続機能を提供します。
|
||||
*/
|
||||
abstract class BaseAccess
|
||||
{
|
||||
protected ?PDO $pdo = null;
|
||||
|
||||
/**
|
||||
* コンストラクタ
|
||||
* データベース接続を確立します。
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
try {
|
||||
// TODO: PDOを使用してデータベースに接続してください
|
||||
// ヒント: new PDO(DSN, ユーザー名, パスワード, オプション)
|
||||
// 接続情報は config/db_config.php で定義されています
|
||||
// ユーザー: rezouser
|
||||
// パスワード: Rezo_0000
|
||||
} catch (PDOException $e) {
|
||||
throw new Exception("データベース接続エラー: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* データベース接続を取得
|
||||
*
|
||||
* @return PDO PDOインスタンス
|
||||
*/
|
||||
public function getConnection(): PDO
|
||||
{
|
||||
return $this->pdo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 接続を閉じる
|
||||
*/
|
||||
public function closeConnection(): void
|
||||
{
|
||||
$this->pdo = null;
|
||||
}
|
||||
}
|
||||
151
database/products/ProductAccess.php
Normal file
151
database/products/ProductAccess.php
Normal file
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../abstracts/BaseAccess.php';
|
||||
require_once __DIR__ . '/../../interfaces/DataAccessInterface.php';
|
||||
|
||||
/**
|
||||
* ProductAccessクラス
|
||||
*
|
||||
* DataAccessInterfaceを実装し、商品データの永続化を担当します。
|
||||
* BaseAccessを継承してデータベース接続機能を利用します。
|
||||
* インタフェースを通じた多態性(ポリモーフィズム)を実現しています。
|
||||
*/
|
||||
class ProductAccess extends BaseAccess implements DataAccessInterface
|
||||
{
|
||||
/**
|
||||
* 商品を作成(登録)する
|
||||
*
|
||||
* @param array $data 商品データ ['category_id', 'name', 'description', 'price', 'stock']
|
||||
* @return bool 成功した場合はtrue
|
||||
* @throws Exception データベースエラーが発生した場合
|
||||
*/
|
||||
public function create(array $data): bool
|
||||
{
|
||||
try {
|
||||
// TODO: プリペアドステートメントを使用してINSERT文を実行してください
|
||||
// ヒント: $this->pdo->prepare() と execute() を使用します
|
||||
// 必要なカラム: category_id, name, description, price, stock
|
||||
|
||||
$sql = ""; // ここにSQL文を記述
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
|
||||
// ここでexecute()を実行
|
||||
|
||||
return true;
|
||||
} catch (PDOException $e) {
|
||||
throw new Exception("商品の登録に失敗しました: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 商品を読み取る(取得)
|
||||
*
|
||||
* @param int|null $id 商品ID(nullの場合は全件取得)
|
||||
* @return array 商品データの配列
|
||||
* @throws Exception データベースエラーが発生した場合
|
||||
*/
|
||||
public function read(?int $id = null): array
|
||||
{
|
||||
try {
|
||||
if ($id === null) {
|
||||
// 全商品を取得するSELECT文を実行
|
||||
// fetchAll()を使用して全件取得します
|
||||
$sql = "SELECT * FROM products ORDER BY id";
|
||||
$stmt = $this->pdo->query($sql);
|
||||
return $stmt->fetchAll();
|
||||
} else {
|
||||
// 指定されたIDの商品を取得するSELECT文を実行
|
||||
// WHERE句を使用し、プリペアドステートメントで実装します
|
||||
$sql = "SELECT * FROM products WHERE id = :id";
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$stmt->execute(['id' => $id]);
|
||||
$result = $stmt->fetch();
|
||||
return $result ? $result : [];
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
throw new Exception("商品の取得に失敗しました: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 商品を更新する
|
||||
*
|
||||
* @param int $id 商品ID
|
||||
* @param array $data 更新データ ['category_id', 'name', 'description', 'price', 'stock']
|
||||
* @return bool 成功した場合はtrue
|
||||
* @throws Exception データベースエラーが発生した場合
|
||||
*/
|
||||
public function update(int $id, array $data): bool
|
||||
{
|
||||
try {
|
||||
// TODO: プリペアドステートメントを使用してUPDATE文を実行してください
|
||||
|
||||
$sql = ""; // ここにSQL文を記述
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
|
||||
// ここでexecute()を実行
|
||||
|
||||
return true;
|
||||
} catch (PDOException $e) {
|
||||
throw new Exception("商品の更新に失敗しました: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 商品を削除する
|
||||
*
|
||||
* @param int $id 商品ID
|
||||
* @return bool 成功した場合はtrue
|
||||
* @throws Exception データベースエラーが発生した場合
|
||||
*/
|
||||
public function delete(int $id): bool
|
||||
{
|
||||
try {
|
||||
// TODO: プリペアドステートメントを使用してDELETE文を実行してください
|
||||
|
||||
$sql = ""; // ここにSQL文を記述
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
|
||||
// ここでexecute()を実行
|
||||
|
||||
return true;
|
||||
} catch (PDOException $e) {
|
||||
throw new Exception("商品の削除に失敗しました: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* カテゴリIDから商品を検索する
|
||||
*
|
||||
* @param int $categoryId カテゴリID
|
||||
* @return array 商品データの配列
|
||||
* @throws Exception データベースエラーが発生した場合
|
||||
*/
|
||||
public function findByCategoryId(int $categoryId): array
|
||||
{
|
||||
try {
|
||||
$sql = "SELECT * FROM products WHERE category_id = :category_id ORDER BY id";
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$stmt->execute(['category_id' => $categoryId]);
|
||||
return $stmt->fetchAll();
|
||||
} catch (PDOException $e) {
|
||||
throw new Exception("商品の検索に失敗しました: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* カテゴリ一覧を取得する
|
||||
*
|
||||
* @return array カテゴリデータの配列
|
||||
* @throws Exception データベースエラーが発生した場合
|
||||
*/
|
||||
public function getCategories(): array
|
||||
{
|
||||
try {
|
||||
$sql = "SELECT * FROM categories ORDER BY id";
|
||||
$stmt = $this->pdo->query($sql);
|
||||
return $stmt->fetchAll();
|
||||
} catch (PDOException $e) {
|
||||
throw new Exception("カテゴリの取得に失敗しました: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user