Commit c8e4c941 authored by Georgios Tsakalos's avatar Georgios Tsakalos

connect staff service with view

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