Commit 8b69e6c9 authored by Georgios Tsakalos's avatar Georgios Tsakalos

update functionality of labservice

add/create methods
add action that create/update are called
update bootstrap file
clean code
parent 6390ca9e
......@@ -40,6 +40,12 @@ return function (Slim\App $app) {
);
};
$container[GrEduLabs\Schools\Action\LabCreate::class] = function ($c) {
return new GrEduLabs\Schools\Action\LabCreate(
$c->get('labservice')
);
};
$container[GrEduLabs\Schools\Action\Assets::class] = function ($c) {
return new GrEduLabs\Schools\Action\Assets($c->get('view'));
};
......@@ -54,6 +60,13 @@ return function (Slim\App $app) {
);
};
$container['labservice'] = function($c){
return new GrEduLabs\Schools\Service\LabService(
$c->get('schoolservice'),
$c->get('staffservice')
);
};
$events = $container['events'];
......@@ -64,8 +77,9 @@ return function (Slim\App $app) {
$app->group('/school', function () {
$this->get('', GrEduLabs\Schools\Action\Index::class)->setName('school');
$this->get('/staff', GrEduLabs\Schools\Action\Staff::class)->setName('school.staff');
$this->post('/staff', GrEduLabs\Schools\Action\StaffCreate::class)->setName('school.staffcreate');
$this->get('/labs', GrEduLabs\Schools\Action\Labs::class)->setName('school.labs');
$this->post('/labs', GrEduLabs\Schools\Action\LabCreate::class)->setName('school.labcreate');
$this->get('/assets', GrEduLabs\Schools\Action\Assets::class)->setName('school.assets');
$this->post('/staff', GrEduLabs\Schools\Action\StaffCreate::class)->setName('school.staffcreate');
});
};
<?php
/**
* gredu_labs.
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
*
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\Schools\Action;
use Slim\Http\Request;
use Slim\Http\Response;
class LabCreate
{
public function __construct($labservice)
{
$this->labservice = $labservice;
}
public function __invoke(Request $req, Response $res, array $args = [])
{
$params = $req->getParams();
if (array_key_exists('id', $params)){
$id = $params['id'];
unset($params['id']);
$id = $this->labservice->updateLab($params, $id);
$lab = $this->labservice->getLabById($id);
}
else{
$id = $this->labservice->createLab($params);
$lab = $this->labservice->getLabById($id);
}
$res = $res->withJson($lab->export());
return $res;
}
}
......@@ -7,7 +7,7 @@
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\School\Service;
namespace GrEduLabs\Schools\Service;
use RedBeanPHP\R;
......@@ -23,36 +23,38 @@ class LabService implements LabServiceInterface
$this->schoolService = $schoolService;
$this->staffService = $staffService;
}
public function createLab(array $data)
{
$lab = R::dispense('lab');
$required = ['name', 'school', 'area'];
$required = ['name', 'school_id', 'area'];
foreach ($required as $value){
if (array_key_exists($value, $data)){
if ($value == 'school')
{
$school_id = $data[$value];
}
else
{
$lab[$value] = $data[$value];
}
}
else
{
return -1;
}
}
if (array_key_exists('teacher', $data)) {
$teacher_id = $data['teacher'];
if (array_key_exists('teacher_id', $data)) {
$lab['teacher_id'] = $data['teacher_id'];
}
$school = $this->schoolService->getSchool($school_id);
$teacher = $this->staffService->getTeacherById($teacher_id);
$lab->school = $school;
$lab->teacher = $teacher;
$id = R::store($lab);
return $id;
}
public function updateLab(array $data, $id){
$lab= R::load('lab', $id);
foreach ($data as $key => $value){
$lab[$key] = $value;
}
$id = R::store($lab);
return $id;
}
public function getLabById($id)
{
$lab = R::load('lab', $id);
......
......@@ -7,11 +7,12 @@
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\DBService;
namespace GrEduLabs\Schools\Service;
interface LabServiceInterface
{
public function createLab(array $data);
public function updateLab(array $data, $id);
public function getLabById($id);
public function getLabsBySchoolId($id);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment