This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val getBookCoverRoute: Route = Endpoints.getBookCover.toRoute { bookId => | |
bookCovers.get(bookId) match { | |
case None => Future.successful(Left((StatusCodes.NotFound, | |
ErrorInfo("Book not found")))) | |
case Some(bookCoverPath) => Future.successful(Right(FileIO.fromPath(bookCoverPath))) | |
} | |
} | |
val addBookRoute: Route = Endpoints.addBook.toRoute { | |
case (authToken, newBook) => |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tapir.server.akkahttp._ | |
import tapir.model.StatusCodes | |
import Database._ | |
import akka.http.scaladsl.server.Route | |
import scala.concurrent.Future | |
val getBooksRoute: Route = Endpoints.getBooks.toRoute { booksQuery => | |
if (booksQuery.limit.getOrElse(0) < 0) { | |
Future.successful(Left((StatusCodes.BadRequest, ErrorInfo("Limit must be positive")))) | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Database { | |
var books: List[Book] = List( | |
Book(UUID.randomUUID(), "The Sorrows of Young Werther", new Year(1774), | |
Author("Johann Wolfgang von Goethe", Country("Germany"))), | |
Book(UUID.randomUUID(), "Iliad", new Year(-8000), | |
Author("Homer", Country("Greece"))), | |
Book(UUID.randomUUID(), "Nad Niemnem", new Year(1888), | |
Author("Eliza Orzeszkowa", Country("Poland"))), | |
Book(UUID.randomUUID(), "The Colour of Magic", new Year(1983), | |
Author("Terry Pratchett", Country("United Kingdom"))), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val addBook: Endpoint[(AuthToken, NewBook), (StatusCode, ErrorInfo), Unit, Nothing] = | |
baseEndpoint | |
.post | |
.in(auth.bearer) | |
.in("books") | |
.in(multipartBody[NewBook]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type AuthToken = String |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.nio.file.Path | |
case class NewBook(title: String, cover: Option[Path], year: Year, | |
authorName: String, authorCountry: String) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val getBookCover: Endpoint[UUID, (StatusCode, ErrorInfo), Source[ByteString, Any], | |
Source[ByteString, Any]] = | |
baseEndpoint | |
.get | |
.in("books" / path[UUID]("bookId") / "cover") | |
.out(streamBody[Source[ByteString, Any]](schemaFor[Array[Byte]], | |
MediaType.OctetStream())) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val getBooks: Endpoint[BooksQuery, (StatusCode, ErrorInfo), List[Book], Nothing] = | |
baseEndpoint | |
.get | |
.in("books") | |
.in(booksQueryInput) | |
.out(jsonBody[List[Book]]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val booksQueryInput: EndpointInput[BooksQuery] = query[Option[Year]]("year") | |
.and(query[Option[Int]]("limit")) | |
.mapTo(BooksQuery) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class BooksQuery(year: Option[Year], limit: Option[Int]) |