Skip to content

Commit

Permalink
Student Model for Education Sample App (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
biniona-mongodb committed Sep 20, 2022
1 parent 5464c5a commit 0fa2d4f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
4 changes: 1 addition & 3 deletions php/school_app/README.md
@@ -1,9 +1,7 @@
# Buildfest 2022 PHP Project



## How to run the app locally

1. Build the container: `docker build -t school_app .`
2. Run the app: `docker run -v $(pwd)/src:/school_app/src -p8080:80 -e MONGODB_URI='mongodb+srv://your_atlas_cluster/' -e DATABASE='school_app' school_app`
2. Run the app: `docker run -v $(pwd)/src:/school_app/src -p 8080:80 -e MONGODB_URI='mongodb+srv://your_atlas_cluster/' -e DATABASE='school_app' school_app`
3. Open [http://localhost:8080](http://localhost:8080).
12 changes: 12 additions & 0 deletions php/school_app/src/Controller/Students.php
@@ -1,10 +1,22 @@
<?php namespace SchoolApp\Controller;

use \SchoolApp\Model\Student as Student;
use \MongoDB\BSON\ObjectId as ObjectId;

class Students {
static function index() {
$students = \SchoolApp\Repository\Students::getAll();
\SchoolApp\View\Students::index($students);
}
static function create(Student $student) {
\SchoolApp\Repository\Students::insertOne($student);
return "successful insert!";
}
static function show(string $student) {
$objectId = new ObjectId($student);
$found_student = \SchoolApp\Repository\Students::findOne($objectId);
\SchoolApp\View\Students::index([$found_student]);
}
}

?>
22 changes: 22 additions & 0 deletions php/school_app/src/Model/Student.php
@@ -0,0 +1,22 @@
<?php namespace SchoolApp\Model;

class Student {

public function __construct(string $name, array $courses ){
$this->name = $name;
$this->courses = $courses;
}

public static function makeWithPost(array $post) {
return new self($post['name'], $post['courses']);
}

function get(): array {
return [
'name' => $this->name,
'courses' => $this->courses,
];
}
}

?>
13 changes: 13 additions & 0 deletions php/school_app/src/Repository/Students.php
@@ -1,9 +1,22 @@
<?php namespace SchoolApp\Repository;

use \SchoolApp\Model\Student as Student;
use \MongoDB\BSON\ObjectId as ObjectId;


class Students {
static function getAll() {
return \SchoolApp\Repository\Database::getInstance()->students->find();
}

static function insertOne(Student $student) {
return \SchoolApp\Repository\Database::getInstance()->students->insertOne($student->get());
}

static function findOne(ObjectId $id) {
return \SchoolApp\Repository\Database::getInstance()->students->findOne(['_id' => $id]);
}

}

?>
12 changes: 8 additions & 4 deletions php/school_app/src/index.php
Expand Up @@ -2,8 +2,14 @@

require __DIR__ . '/../vendor/autoload.php';

use \SchoolApp\Model\Student as Student;

$router = new \Bramus\Router\Router();

function DecodeJSONPOST(){
return json_decode(file_get_contents('php://input'), true);
}

$router->get('/', function() {
\SchoolApp\Controller\Home::index();
});
Expand All @@ -13,13 +19,11 @@
});

$router->get('/students/{studentId}', function($studentId) {
// NOT IMPLEMENTED YET
// \SchoolApp\Controller\Students::show($studentId);
\SchoolApp\Controller\Students::show($studentId);
});

$router->post('/students', function() {
// NOT IMPLEMENTED YET
// \SchoolApp\Controller\Students::create($_POST['name']);
\SchoolApp\Controller\Students::create(Student::makeWithPost(DecodeJSONPOST()));
});

$router->set404(function() {
Expand Down

0 comments on commit 0fa2d4f

Please sign in to comment.