Skip to content

Commit

Permalink
Merge pull request #39 from CPNV-ES/release/1.0.0
Browse files Browse the repository at this point in the history
Release/1.0.0
  • Loading branch information
EthannSchneider authored Nov 21, 2024
2 parents f0cec13 + 0982612 commit 0d00572
Show file tree
Hide file tree
Showing 31 changed files with 723 additions and 230 deletions.
9 changes: 5 additions & 4 deletions docker-compose-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ services:
- internal

php-fpm:
image: php:8.3.11-fpm
build:
context: .
dockerfile: php.prod.Dockerfile
volumes:
- ./src:/var/www/src
- ./public:/var/www/html
- ./.env:/var/www/.env
- ./vendor:/var/www/vendor
networks:
- internal
environment:
XDEBUG_MODE: debug
XDEBUG_CONFIG: client_host=host.docker.internal client_port=9003
postgresql:
image: postgres:16.4
environment:
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
php-fpm:
build:
context: .
dockerfile: Dockerfile
dockerfile: php.dev.Dockerfile
volumes:
- ./src:/var/www/src
- ./public:/var/www/html
Expand Down
12 changes: 6 additions & 6 deletions docs/database_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,34 @@ Here is the database structure we want to use, you can use https://dbdiagram.io/
// Docs: https://dbml.dbdiagram.io/docs
Table exercises {
id integer [primary key]
id serial [primary key]
title text
status integer //0:Building 1:Answering 2:Closed
}
Table fields {
id integer [primary key]
id serial [primary key]
exercise_id integer
label text
kind integer // 0:Single line text 1:List of single lines 2:Multi-line text
}
Table fulfillments {
id integer [primary key]
id serial [primary key]
exercise_id integer
creation_date timestamp
}
Table fulfillments_data {
id integer [primary key]
fulfilment_id integer
id serial [primary key]
fulfillment_id integer
field_id integer
body text //raw data with \n to separate lines
}
Ref: fields.exercise_id > exercises.id
Ref: fulfillments.exercise_id > exercises.id
Ref: fulfillments.id < fulfillments_data.fulfilment_id
Ref: fulfillments.id < fulfillments_data.fulfillment_id
Ref: fields.id < fulfillments_data.field_id
```
Binary file modified docs/looper.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
2 changes: 2 additions & 0 deletions php.prod.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM php:8.3.11-fpm
RUN apt-get update && apt-get install -y libpq-dev && docker-php-ext-install pdo pdo_pgsql
45 changes: 2 additions & 43 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,15 @@

require BASE_DIR . '/vendor/autoload.php';
require CONTROLLER_DIR . '/error.php';
require MODEL_DIR . '/exceptions/looper_exception.php';
require MODEL_DIR . '/router.php';

$_ENV = parse_ini_file(BASE_DIR . '/.env');

$request_uri = $_SERVER['REQUEST_URI'] ?? '/';
$request_uri = explode('?', $request_uri)[0];

$router = new Router(CONTROLLER_DIR);
$router = new Router();
if (!$router->run($_SERVER['REQUEST_METHOD'], $request_uri)) {
lost();
}
// try {

// } catch (\Throwable $e) {
// serverError($e->getTraceAsString());
// }

// function getRedirection($redirect_uri)
// {
// if (preg_match("/^\/exercises\/([0-9]+)$/", $redirect_uri, $str)) {
// deleteExercise($str[1]);
// return;
// }
// switch ($redirect_uri) {
// case '/':
// home();
// break;
// case '/exercises':
// manageExercises();
// break;
// case '/exercises/answering':
// takeAnExercises();
// break;
// case '/exercises/new':
// createAnExercises();
// break;
// case (preg_match('/^\/exercises\/([0-9]+)$/A', $redirect_uri, $output_array) ? true : false):
// changeStateOfExercise($output_array[1]);
// break;
// default:
// lost();
// }
// }

// function postRedirection($redirect_uri)
// {
// switch ($redirect_uri) {
// case '/exercises':
// createExercise();
// break;
// default:
// lost();
// >>>>>>> develop
22 changes: 16 additions & 6 deletions src/controllers/error.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
<?php

function lost()
function lost($return_code = 404, $error_message = 'Page not found')
{
include VIEW_DIR . '/errors/lost.php';
include VIEW_DIR . '/errors/error.php';
}

function badRequest()
function error($return_code, $error_message)
{
include VIEW_DIR . '/errors/bad_request.php';
include VIEW_DIR . '/errors/error.php';
}

function serverError(string $console_log = '')
function badRequest($return_code = 400, $error_message = 'Bad Request')
{
include VIEW_DIR . '/errors/server_error.php';
include VIEW_DIR . '/errors/error.php';
}

function unauthorized($return_code = 401, $error_message = 'Unauthorized')
{
include VIEW_DIR . '/errors/error.php';
}

function serverError($return_code = 500, $error_message = 'Server Error')
{
include VIEW_DIR . '/errors/error.php';
}
26 changes: 2 additions & 24 deletions src/controllers/exercise_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@

include_once MODEL_DIR . '/exercise.php';

$entry = [
'ExerciseController()' => [
'GET' => [
'/exercises/:id:int' => 'changeStateOfExercise(:id:int)',
'/exercises/:id:int/delete' => 'deleteExercise(:id:int)'
],
'POST' => [
'/exercises' => 'createExercise()'
]
]
];

class ExerciseController
{
public function createExercise()
Expand All @@ -29,12 +17,7 @@ public function createExercise()

public function deleteExercise(int $id)
{
try {
$exercise = new Exercise($id);
} catch (Exception $e) {
lost();
return;
}
$exercise = new Exercise($id);

if ($exercise->getStatus() == Status::Building || $exercise->getStatus() == Status::Closed) {
$exercise->delete();
Expand All @@ -50,12 +33,7 @@ public function changeStateOfExercise(int $id)
}

$exercise = null;
try {
$exercise = new Exercise($id);
} catch (Exception) {
lost();
return;
}
$exercise = new Exercise($id);

if ($exercise->getFieldsCount() < 1) {
badRequest();
Expand Down
44 changes: 8 additions & 36 deletions src/controllers/field_controller.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
<?php

$entry = [
'FieldController()' => [
'GET' => [
'/exercises/:id:int/fields/:idFields:int' => 'deleteField(:id:int, :idFields:int)'
],
'POST' => [
'/exercises/:id:int/fields' => 'createField(:id:int)',
'/exercises/:id:int/fields/:idFields:int' => 'editField(:id:int, :idFields:int)'
]
]
];
require_once MODEL_DIR . '/exercise.php';

class FieldController
{
Expand All @@ -23,12 +13,7 @@ public function createField(int $exercise_id)

$exercise = null;

try {
$exercise = new Exercise($exercise_id);
} catch (Exception $e) {
lost();
return;
}
$exercise = new Exercise($exercise_id);

$kind = $this->kindStringToKindEnum($_POST['field']['value_kind']);

Expand All @@ -41,33 +26,20 @@ public function deleteField(int $exercise_id, int $field_id)
{
$exercise = null;

try {
$exercise = new Exercise($exercise_id);
$field = new Field($field_id);
$exercise = new Exercise($exercise_id);
$field = new Field($field_id);

if ($exercise->isFieldInExercise($field)) {
$field->delete();
}
} catch (Exception) {
lost();
return;
if ($exercise->isFieldInExercise($field)) {
$field->delete();
}

header('Location: /exercises/' . $exercise_id . '/fields');
}

public function editField(int $exercise_id, int $field_id)
{
$exercise = null;
$field = null;

try {
$exercise = new Exercise($exercise_id);
$field = new Field($field_id);
} catch (Exception) {
lost();
return;
}
$exercise = new Exercise($exercise_id);
$field = new Field($field_id);

if (!$exercise->isFieldInExercise($field)) {
lost();
Expand Down
61 changes: 61 additions & 0 deletions src/controllers/fulfillment_controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

include_once MODEL_DIR . '/exercise.php';

class FulfillmentController
{
public function createFulfillment(int $exercise_id)
{
if (!isset($_POST['fulfillment']['answers_attributes'])) {
badRequest();
return;
}

$exercise = new Exercise($exercise_id);

$fields = $exercise->getFields();

foreach ($_POST['fulfillment']['answers_attributes'] as $answers_attribute) {
if (!isset($answers_attribute['field_id'], $answers_attribute['value'])) {
badRequest();
return;
}
if (!$exercise->isFieldInExercise(new Field($answers_attribute['field_id']))) {
badRequest();
return;
}
}
$fulfillment = $exercise->createFulfillment();

foreach ($fields as $field) {
$fulfillment->createFields($field, $_POST['fulfillment']['answers_attributes'][$field->getId()]['value']);
}

header('Location: /exercises/' . $exercise->getId() . '/fulfillments/' . $fulfillment->getId() . '/edit');
}

public function editFulfillment(int $exercise_id, int $fulfillment_id)
{
if (!isset($_POST['fulfillment']['answers_attributes'])) {
badRequest();
return;
}

$exercise = new Exercise($exercise_id);

$fulfillment = new Fulfillment($fulfillment_id);

foreach ($_POST['fulfillment']['answers_attributes'] as $answers_attribute) {
if (!isset($answers_attribute['field_id'], $answers_attribute['value'])) {
badRequest();
return;
}

$fulfillment_data = new FulfillmentField($answers_attribute['field_id'], $fulfillment_id);

$fulfillment_data->setBody($answers_attribute['value']);
}

header('Location: /exercises/' . $exercise->getId() . '/fulfillments/' . $fulfillment->getId() . '/edit');
}
}
Loading

0 comments on commit 0d00572

Please sign in to comment.