clearcode/eh-library

rest api 课程的模式和用例

dev-master 2015-12-03 11:38 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:04:19 UTC


README

Build Status Coverage Status Scrutinizer Code Quality Dependency Status

EH-library 应用用于 rest api 课程

此仓库是一个简单的模型,可用于构建 REST API。

需求

    "require": {
        "php": ">=5.5",
        "ramsey/uuid": "~3.0",
        "everzet/persisted-objects": "~1.0"
    },

安装

composer require --dev clearcode/eh-library

功能

  • 将书籍添加到图书馆,
  • 查看图书馆中的书籍,
  • 预订一本书,
  • 赠送一本书,
  • 归还一本书,
  • 查看预订。

描述

图书馆的 API。

//...

interface Library
{
    /**
     * @param UuidInterface $bookId
     * @param string        $title
     * @param string        $authors
     * @param string        $isbn
     */
    public function addBook(UuidInterface $bookId, $title, $authors, $isbn);

    /**
     * @param int  $page
     * @param null $booksPerPage
     *
     * @return BookView[]
     */
    public function listOfBooks($page = 1, $booksPerPage = null);

    /**
     * @param UuidInterface $reservationId
     * @param UuidInterface $bookId
     * @param string        $email
     */
    public function createReservation(UuidInterface $reservationId, UuidInterface $bookId, $email);

    /**
     * @param UuidInterface $reservationId
     * @param \DateTime     $givenAwayAt
     *
     * @throws BookInReservationAlreadyGivenAway
     */
    public function giveAwayBookInReservation(UuidInterface $reservationId, \DateTime $givenAwayAt);

    /**
     * @param UuidInterface $reservationId
     * 
     * @throws CannotGiveBackReservationWhichWasNotGivenAway
     */
    public function giveBackBookFromReservation(UuidInterface $reservationId);

    /**
     * @param UuidInterface $bookId
     *
     * @return ReservationView[]
     */
    public function listReservationsForBook(UuidInterface $bookId);
}

此接口由 Clearcode\EHLibrary\Application 类实现。

示例

添加书籍到图书馆的示例。

//..
namespace Clearcode;

use Clearcode\EHLibrary\Application;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class Controller
{
    public function addBookToLibraryAction(Request $request)
    {
        $bookId = Uuid::fromString($request->request->get('bookId'));
        
        //Library implementation
        $app = new Application();
        $app->addBook($bookId, $request->request->get('title'), $request->request->get('authors'), $request->request->get('isbn'));
        
        return new Response(json_encode(['id' => (string) $bookId]), 201);
    }
}