Commit c8e4c941 authored by Georgios Tsakalos's avatar Georgios Tsakalos
Browse files

connect staff service with view

parent 14ddb086
...@@ -24,18 +24,24 @@ class StaffCreate ...@@ -24,18 +24,24 @@ class StaffCreate
public function __invoke(Request $req, Response $res, array $args = []) public function __invoke(Request $req, Response $res, array $args = [])
{ {
$params = $req->getParams(); $params = $req->getParams();
if (array_key_exists('id', $params)){ $id = $params['id'];
$id = $params['id']; unset($params['id']);
unset($params['id']); if ($id > 0){
$id = $this->staffservice->updateTeacher($params, $id); $id = $this->staffservice->updateTeacher($params, $id);
$teacher = $this->staffservice->getTeacherById($id); $teacher = $this->staffservice->getTeacherById($id);
} }
else{ else{
$id = $this->staffservice->createTeacher($params); $id = $this->staffservice->createTeacher($params);
$teacher = $this->staffservice->getTeacherById($id); if ($id > 0){
$teacher = $this->staffservice->getTeacherById($id);
}
} }
$res = $res->withJson($teacher->export()); if (isset($teacher)){
return $res; return $res->withJson($teacher->export())->withStatus(201);
}
else{
return $res->withStatus(400);
}
} }
} }
...@@ -21,9 +21,11 @@ class StaffService implements StaffServiceInterface ...@@ -21,9 +21,11 @@ class StaffService implements StaffServiceInterface
public function createTeacher(array $data) public function createTeacher(array $data)
{ {
unset($data['id']);
$teacher = R::dispense('teacher'); $teacher = R::dispense('teacher');
$required = ['school_id', 'name', 'surname', 'phoneNumber', 'labSupervisor', 'schoolPrincipal']; $required = ['school_id', 'name','email', 'surname', 'telephone',
$optional = ['speciality', 'email']; 'position', 'branch'];
$data['school_id'] = 1;
foreach ($required as $value) { foreach ($required as $value) {
if (array_key_exists($value, $data)) { if (array_key_exists($value, $data)) {
$teacher[$value] = $data[$value]; $teacher[$value] = $data[$value];
...@@ -31,13 +33,6 @@ class StaffService implements StaffServiceInterface ...@@ -31,13 +33,6 @@ class StaffService implements StaffServiceInterface
return -1; return -1;
} }
} }
foreach ($optional as $value) {
if (array_key_exists($value, $data)) {
$teacher[$value] = $data[$value];
} else {
$$value = '';
}
}
$id = R::store($teacher); $id = R::store($teacher);
return $id; return $id;
} }
......
...@@ -109,15 +109,17 @@ ...@@ -109,15 +109,17 @@
hash[pair.name] = pair.value; hash[pair.name] = pair.value;
return hash; return hash;
}, {}); }, {});
var that = this;
evt.preventDefault(); evt.preventDefault();
if (!data.id) { $.post("", data).
data.id = (100 * Math.random()).toFixed(0); done(function(response){
} that.form.data('done')(response);
this.form.data('done')(data); console.log(response);
this.form.data('done', undefined); that.form.data('done', undefined);
this.hide(); that.hide();
});
} }
}); });
new StaffView({ model: new Staff() }); new StaffView({ model: new Staff() });
}()); }());
\ No newline at end of file
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