Skip to content
This repository was archived by the owner on Jan 22, 2024. It is now read-only.
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
4 changes: 2 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
"extends": "airbnb-base"
};
extends: 'airbnb-base',
};
3 changes: 1 addition & 2 deletions _env/local.env
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://codingchallenge.dev

DB_CONNECTION=mysql
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=codingchallenge
DB_USERNAME=homestead
DB_PASSWORD=secret

Expand Down
47 changes: 47 additions & 0 deletions app/Http/Controllers/API/BookController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Http\Controllers\API;

use App\Models\Book;
use App\Repositories\BookRepository;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Request;
use function response;

class BookController extends BaseController
{
use AuthorizesRequests, ValidatesRequests;

private $repository;

public function __construct(BookRepository $repository)
{
$this->repository = $repository;
}

/**
* @return \Illuminate\Http\JsonResponse
*/
public function getMetadata()
{
$ids = [];
if (Request::has('book_id')) {
$ids[] = Request::get('book_id');
}
$books = $this->repository->findBooks($ids);
$jsonable = [];

foreach ($books as $book) {
$jsonable[$book->book_id] = [
'published_at' => $book->published_at,
'author' => optional($book->authors)->implode(','),
'cover' => optional($book->cover)->image_url,
];
}

return response()->json($jsonable);
}
}
52 changes: 52 additions & 0 deletions app/Http/Controllers/API/ShelfController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Http\Controllers\API;
use App\Repositories\ShelfRepository;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class ShelfController extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
private $repository;

public function __construct(ShelfRepository $repository)
{
$this->repository = $repository;
}

public function getBooks($shelf_slug){
$shelf = $this->repository->findShelfViaSlug($shelf_slug);

if(empty($shelf)){
return response()->json(['message'=>'slug invaild'], 404);
}

/* @var \Illuminate\Support\Collection */
$books = $this->repository->grabBooksFromShelf($shelf);

$data = $books->transform(function ($book){
return [
'name' => $book->name,
'isbn' => $book->isbn,
'id' => $book->book_id,
];
})->all();

return response()->json($data);
}

public function getAllShelives(){
$shelvies = $this->repository->getAllShelvies();
$data = $shelvies->transform(function ($shelf){
return [
'name' => $shelf->name,
'slug' => $shelf->slug,
'id' => $shelf->shelf_id,
];
})->all();
return response()->json($data);
}
}
38 changes: 0 additions & 38 deletions app/Http/Controllers/ApiController.php

This file was deleted.

16 changes: 16 additions & 0 deletions app/Models/Author.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Author extends Model
{
/**
* Books
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function books(){
return $this->belongsToMany(Book::class, 'author_book');
}
}
17 changes: 17 additions & 0 deletions app/Models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Book extends Model
{
protected $table = 'books';
protected $primaryKey = 'book_id';
protected $dates = ['published_at'];

/**
* Shelf relationship
Expand All @@ -22,4 +23,20 @@ public function shelf()
{
return $this->belongsTo(Shelf::class,'shelf_id', 'shelf_id');
}

/**
* Authors
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function authors(){
return $this->belongsToMany(Author::class, 'author_book', 'book_id', 'author_id');
}

/**
* Book cover image
* @return \Illuminate\Database\Eloquent\Relations\MorphOne
*/
public function cover(){
return $this->morphOne(Image::class, 'imageable');
}
}
16 changes: 16 additions & 0 deletions app/Models/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Image extends Model
{
/**
* Get the owning imageable model.
*/
public function imageable()
{
return $this->morphTo();
}
}
1 change: 1 addition & 0 deletions app/Models/Shelf.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Model;


/**
* Class Shelf
* @package App\Models
Expand Down
26 changes: 26 additions & 0 deletions app/Repositories/BookRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Repositories;

use App\Models\Book;

class BookRepository
{
private $book;

public function __construct(Book $book){
$this->book = $book;
}

/**
* fetch Books via given ids, if no id provided, return all books
* @param array $ids
* @return Book[]|\Illuminate\Database\Eloquent\Collection
*/
public function findBooks(Array $ids){
if (empty($ids)){
return $this->book->all();
}
return $this->book->whereIn('book_id', $ids)->get();
}
}
35 changes: 35 additions & 0 deletions app/Repositories/ShelfRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Repositories;

use App\Models\Shelf;
use Illuminate\Support\Collection;


class ShelfRepository
{
private $shelf;
public function __construct(Shelf $shelf){
$this->shelf = $shelf;
}


/**
* find shelf via shelf slug
* @param $slug
* @return Shelf|null
*/
public function findShelfViaSlug($slug): ?Shelf
{
return $this->shelf->where('slug', $slug)->first();
}

public function grabBooksFromShelf(Shelf $shelf): ?Collection
{
return $shelf->books;
}

public function getAllShelvies(){
return $this->shelf->all();
}
}
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"type": "project",
"require": {
"php": ">=7.0.0",
"php": ">=7.1",
"fideloper/proxy": "~3.3",
"laravel/framework": "5.5.*",
"laravel/tinker": "~1.0"
Expand Down Expand Up @@ -51,6 +51,9 @@
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
"optimize-autoloader": true,
"allow-plugins": {
"kylekatarnls/update-helper": true
}
}
}
Loading