Controller class
namespace App\Http\Controllers\Website\SportsType;
use App\Classes\Contract\SportsType\ISportsType;
use \App\Http\Requests\SportsType\SportsTypeRequest as SportsTypeRequest;
class SportsTypeController extends \App\Http\Controllers\BaseController
{
private $AllSportsTypes = "SportsTypes";
private $sportstype;
public function __construct(ISportsType $_sportstype) {
$this->sportstype = $_sportstype;
parent::__construct();
}
public function index() {
$SportsTypes = $this->sportstype->All();
return view('SportsType.List')->with('SportsTypes', $SportsTypes);
}
public function create() {
return view('SportsType.Create');
}
public function edit($SportsTypeID) {
$SportsType = $this->sportstype->Get($SportsTypeID);
return view('SportsType.Edit', array('SportsType' => $SportsType));
}
public function save(SportsTypeRequest $request) {
$data = [
'SportsType' => $request['SportsType'],
'SportsTypeID' => $request['SportsTypeID'],
];
$result = $this->sportstype->Save($data);
return redirect()->route($this->AllSportsTypes);
}
}
Business logic class
namespace App\Classes\BusinessLogic\SportsType;
use App\Classes\DatabaseLayer\SportsType\SportsTypeDb;
use App\Classes\Contract\SportsType\ISportsType;
class SportsTypeBL implements ISportsType {
public function All() {
$SportsTypes = (new SportsTypeDb())->All();
return $SportsTypes;
}
public function Get($SportsTypeID) {
$SportsType = (new SportsTypeDb())->Get($SportsTypeID);
if($SportsType == null) {
\App::abort(404);
return;
}
return $SportsType;
}
public function Save($data) {
return (new SportsTypeDb())->Save($data);
}
}
Database class
namespace App\Classes\DatabaseLayer\SportsType;
class SportsTypeDb {
public function All() {
$SportsTypes = \App\Models\SportsType\SportsTypeModel::all();
return $SportsTypes;
}
public function Get($SportsTypeID) {
$SportsType = \App\Models\SportsType\SportsTypeModel
::where('SportsTypeID', $SportsTypeID)
->first();
return $SportsType;
}
public function Save($data) {
if($data["SportsTypeID"] == 0) {
$SportsType = new \App\Models\SportsType\SportsTypeModel();
}
else {
$SportsType = $this->Get($data["SportsTypeID"]);
}
$SportsType->SportsType = $data["SportsType"];
$SportsType->save();
return true;
}
}
- Which class should have code for caching?
- Am I writing bad or very bad code? Can you please suggest good ways to improve it?