Skip to content

PHP engineer challenge #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions PHP/library-api-php/controllers/LoanController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
class LoanController {
// GET /loans
public function index() {
// TODO: Implement logic to list active loans with borrower and book details.
header('Content-Type: application/json');
echo json_encode(['message' => 'List active loans functionality to be implemented.']);
header('Status: 200');
echo json_encode(bookList());
}

// POST /loans/return
public function returnBook() {
// TODO: Implement logic to process the return of a book and calculate fines if overdue.
header('Content-Type: application/json');
echo json_encode(['message' => 'Return book functionality to be implemented.']);
header('Status: 200');
echo json_encode(returnBook($_POST['bookId']));
}
}
16 changes: 11 additions & 5 deletions PHP/library-api-php/controllers/ReservationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@
class ReservationController {
// POST /reservations
public function reserve() {
// TODO: Implement logic to reserve a book currently on loan.
header('Content-Type: application/json');
echo json_encode(['message' => 'Reserve book functionality to be implemented.']);
$reservation = reservation($_POST['bookId'], $_POST['borrowerId']);
if ($reservation === NULL) {
header('Content-Type: application/json');
header('Status: 404');
} else {
header('Content-Type: application/json');
header('Status: 200');
echo json_encode($reservation);
}
}

// GET /reservations
public function status() {
// TODO: Implement logic to return reservation status for a given borrower and book.
header('Content-Type: application/json');
echo json_encode(['message' => 'Reservation status functionality to be implemented.']);
header('Status: 200');
echo json_encode(reservationStatus($_GET['bookId']));
}
}
74 changes: 74 additions & 0 deletions PHP/library-api-php/data/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,77 @@
// Fines and reservations (empty arrays to start)
$fines = [];
$reservations = [];

//

function maxId($a) {
return array_reduce(fn($c, $o) => $c > $o->id ? $c : $o->id,$a);
}

function book($bookId) {
return array_first(array_filter(fn($o) => $bookId==$o->bookId,$books));
}

function bookList() {
return array_map(function($o) {
$book = book($o->bookId);
return ['title' => $book->title, 'isOnLoan'=>$o->isOnLoan,'loanEndDate'=>$o->loanEndDate,'borrowerId'=>$o->borrowerId];
},$bookStocks);
}

function fineAmount($bookStock) {
// TODO calculate the fine amount
}

function fineDetails($bookStock) {
// TODO fine details
}

function returnBook($bookId) {
$bookStock = array_first(array_filter(fn($o) => $bookId ==$o->bookId, $bookStocks));
$bookStocks = array_filter(fn($o) => $o->$bookId != $bookId,$bookStocks);

if (strtotime($bookStock->loadEndDate) < strtotime(date('now'))) {
return new Fine($id,$bookStock->borrowerId,fineAmount($bookStock), fineDetails($bookStock));
} else {
return [];
}
}

function reservation($bookId, $borrowerId) {
$bookReservations = array_filter(
fn($o) => $o->bookId == $bookId
,$reservations
);

if (count($bookReservations)) {
$id = maxId($reservations) + 1;
$reservation = new Reservation($id, $bookId, $borrowerId, date('now'));
$reservations[]=$reservation;
return [];
}

return NULL;
}

function reservationStatus($bookId) {
$available = true;
$reservedAt = NULL;

$bookReservations = array_filter(
fn($o) => $o->$bookId == $bookId
,$reservations
);

if (count($bookReservations)) {
$available = false;
$reservedAt = array_reduce(
fn($v, $o) => (strtotime($o->reservedAt) > strtotime($v)) ? $o->reservedAt : $v
,$bookReservations);
}

return [
'available' => $available,
'reservedAt' => $reservedAt
];
}