using System; using DtoAbstractLayer; using LibraryDTO; using Microsoft.AspNetCore.Mvc; using StubbedDTO; namespace OpenLibraryWrapper.Controllers { [ApiController] [Route("[controller]")] public class BookController : ControllerBase { private readonly ILogger _logger; private IDtoManager DtoManager; public BookController(ILogger logger, IDtoManager dtoManager) { _logger = logger; DtoManager = dtoManager; } /// /// Gets books of the collection matching a particular title /// /// part of a title to look for in book titles (case is ignored) /// index of the page /// number of elements per page /// sort criterium of the resulting books: /// /// /// /// a collection of count (or less) books /// /// Sample requests: /// /// book/getbooksbytitle?title=ne&index=0&count=5 /// book/getbooksbytitle?title=ne&index=0&count=5&sort=old /// /// /// Returns count books at page index /// no books within this range [HttpGet("getBooksByTitle")] [ProducesResponseType(typeof(Tuple>), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task GetBooksByTitle([FromQuery] string title, [FromQuery] int index, [FromQuery] int count, [FromQuery] string sort = "") { _logger.LogDebug("Get books by title"); var booksDto = (await DtoManager.GetBooksByTitle(title, index, count, sort)); _logger.LogInformation($"{booksDto.Item1} books found"); if(booksDto.Item1 == 0) { return NotFound(); } return Ok(booksDto); } /// /// Gets books of the collection whose of one the authors is matching a particular name /// /// part of a author name to look for in book authors (case is ignored) /// index of the page /// number of elements per page /// sort criterium of the resulting books: ///
    ///
  • ```title```: sort books by titles in alphabetical order,
  • ///
  • ```title_reverse```: sort books by titles in reverse alphabetical order,
  • ///
  • ```new```: sort books by publishing dates, beginning with the most recents,
  • ///
  • ```old```: sort books by publishing dates, beginning with the oldest
  • ///
/// /// /// a collection of count (or less) books /// /// Sample requests: /// /// book/getbooksbyauthor?name=al&index=0&count=5 /// book/getbooksbyauthor?name=al&index=0&count=5&sort=old /// /// Note: /// name is also looked for in alternate names of the authors /// /// Returns count books at page index /// no books within this range [HttpGet("getBooksByAuthor")] [ProducesResponseType(typeof(Tuple>), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task GetBooksByAuthor([FromQuery] string name, [FromQuery] int index, [FromQuery] int count, [FromQuery] string sort = "") { _logger.LogDebug("Get books by author"); var booksDto = (await DtoManager.GetBooksByAuthor(name, index, count, sort)); _logger.LogInformation($"{booksDto.Item1} books found"); if(booksDto.Item1 == 0) { return NotFound(); } return Ok(booksDto); } /// /// Gets books of the collection of a particular author /// /// id of the author /// index of the page /// number of elements per page /// sort criterium of the resulting books: ///
    ///
  • ```title```: sort books by titles in alphabetical order,
  • ///
  • ```title_reverse```: sort books by titles in reverse alphabetical order,
  • ///
  • ```new```: sort books by publishing dates, beginning with the most recents,
  • ///
  • ```old```: sort books by publishing dates, beginning with the oldest
  • ///
/// /// /// a collection of count (or less) books /// /// Sample requests: /// /// book/getbooksbyauthorid?id=OL1846639A&index=0&count=5 /// book/getbooksbyauthorid?id=OL1846639A&index=0&count=5&sort=old /// /// /// Returns count books at page index /// no books within this range [HttpGet("getBooksByAuthorId")] [ProducesResponseType(typeof(Tuple>), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task GetBooksByAuthorId([FromQuery] string id, [FromQuery] int index, [FromQuery] int count, [FromQuery] string sort = "") { _logger.LogDebug("Get books by author id"); var booksDto = (await DtoManager.GetBooksByAuthorId(id, index, count, sort)); _logger.LogInformation($"{booksDto.Item1} books found"); if(booksDto.Item1 == 0) { return NotFound(); } return Ok(booksDto); } /// /// Gets authors of the collection matching a particular name /// /// name to look for in author names /// index of the page /// number of elements per page /// sort criterium of the resulting authors: ///
    ///
  • ```name```: sort authors by names in alphabetical order,
  • ///
  • ```name_reverse```: sort authors by names in reverse alphabetical order,
  • ///
/// /// /// a collection of count (or less) authors /// /// Sample requests: /// /// book/getauthorsbyname?name=al&index=0&count=5 /// book/getauthorsbyname?name=al&index=0&count=5&sort=name /// /// /// Returns count authors at page index /// no authors within this range [HttpGet("getAuthorsByName")] [ProducesResponseType(typeof(Tuple>), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task GetAuthorsByName([FromQuery] string name, [FromQuery] int index, [FromQuery] int count, [FromQuery] string sort = "") { _logger.LogDebug("Get authors by name"); var authorsDto = (await DtoManager.GetAuthorsByName(name, index, count, sort)); _logger.LogInformation($"{authorsDto.Item1} authors found"); if(authorsDto.Item1 == 0) { return NotFound(); } return Ok(authorsDto); } /// /// Gets book by isbn /// /// isbn of the book to get /// the book with the seeked isbn (or null) /// /// Sample requests: /// /// book/getBookByIsbn/9782330033118 /// /// /// Returns the book with this isbn /// no book found [HttpGet("getBookByIsbn/{isbn?}")] [ProducesResponseType(typeof(BookDTO), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task GetBookByIsbn(string isbn) { _logger.LogDebug("Get book by isbn"); var bookDto = (await DtoManager.GetBookByISBN(isbn)); if(bookDto == null) { _logger.LogInformation($"{isbn} not found"); return NotFound(); } _logger.LogInformation($"{bookDto.Title} found"); return Ok(bookDto); } /// /// Gets book by id /// /// id of the book to get /// the book with the seeked id (or null) /// /// Sample requests: /// /// book/getBookById/OL25910297M /// /// /// Returns the book with this id /// no book found [HttpGet("getBookById/{id?}")] [ProducesResponseType(typeof(BookDTO), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task GetBookById(string id) { _logger.LogDebug("Get book by ID"); var bookDto = (await DtoManager.GetBookById(id)); if(bookDto == null) { _logger.LogInformation($"{id} not found"); return NotFound(); } _logger.LogInformation($"{bookDto.Title} found"); return Ok(bookDto); } /// /// Gets author by id /// /// id of the author to get /// the author with the seeked id (or null) /// /// Sample requests: /// /// book/getAuthorById/OL1846639A /// /// /// Returns the author with this id /// no author found [HttpGet("getAuthorById/{id?}")] [ProducesResponseType(typeof(AuthorDTO), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task GetAuthorById(string id) { _logger.LogDebug("Get Author by ID"); var authorDTO = (await DtoManager.GetAuthorById(id)); if(authorDTO == null) { _logger.LogInformation($"{id} not found"); return NotFound(); } _logger.LogInformation($"{authorDTO.Name} found"); return Ok(authorDTO); } } }