-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Use precise API error responses in endpoints descriptions, rather than the generic type `ApiError` - Make sure responses and documentation are consistent together - Quick fix to the authentication directive
- Loading branch information
Showing
8 changed files
with
134 additions
and
68 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
4 changes: 2 additions & 2 deletions
4
src/main/scala/io/scalac/lab/api/endpoints4s/SecuritySupport.scala
This file contains 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
52 changes: 52 additions & 0 deletions
52
src/main/scala/io/scalac/lab/api/storage/InMemoryNarrowerApartmentsStorage.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.scalac.lab.api.storage | ||
import java.util.concurrent.atomic.AtomicLong | ||
|
||
import io.scalac.lab.api.model.Apartment | ||
import io.scalac.lab.api.model.ApiError.{BadRequestError, NotFoundError} | ||
|
||
import scala.concurrent.Future | ||
|
||
// Exactly the same implementation as `InMemoryApartmentsStorage`, except it | ||
// extends `NarrowerApartmentsStorage`. | ||
class InMemoryNarrowerApartmentsStorage(initState: () => List[Apartment] = () => List.empty) extends NarrowerApartmentsStorage { | ||
private val storageId = new AtomicLong() | ||
private var storage: List[Apartment] = initState() | ||
|
||
override def list(from: Int, limit: Option[Int]): Future[Either[BadRequestError, List[Apartment]]] = | ||
Future.successful(Right(storage.slice(from, from + limit.getOrElse(storage.length)))) | ||
|
||
override def get(id: Int): Future[Either[NotFoundError, Apartment]] = | ||
storage.find(_.id.contains(id)) match { | ||
case Some(value) => Future.successful(Right(value)) | ||
case None => Future.successful(Left(NotFoundError(s"Apartment with id: $id not found!"))) | ||
} | ||
|
||
override def find(city: String, street: String, number: String): Future[Either[Either[BadRequestError, NotFoundError], Apartment]] = { | ||
storage.find { s => | ||
s.address.city.equalsIgnoreCase(city) && | ||
s.address.street.equalsIgnoreCase(street) && | ||
s.address.number.equalsIgnoreCase(number) | ||
} match { | ||
case Some(value) => Future.successful(Right(value)) | ||
case None => | ||
Future.successful(Left(Right(NotFoundError(s"Apartment for city: $city, street: $street, number: $number not found!")))) | ||
} | ||
} | ||
|
||
override def save(apartment: Apartment): Future[Either[BadRequestError, Apartment]] = { | ||
val apartmentWithId = apartment.copy(id = Some(storageId.getAndIncrement())) | ||
|
||
storage = storage :+ apartmentWithId | ||
Future.successful(Right(apartmentWithId)) | ||
} | ||
|
||
override def delete(id: Int): Future[Either[NotFoundError, Apartment]] = { | ||
storage.find(_.id.contains(id)) match { | ||
case Some(value) => | ||
storage = storage.filterNot(_.id.contains(id)) | ||
Future.successful(Right(value)) | ||
case None => | ||
Future.successful(Left(NotFoundError(s"Apartment with id: $id not found!"))) | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/scala/io/scalac/lab/api/storage/NarrowerApartmentsStorage.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.scalac.lab.api.storage | ||
|
||
import io.scalac.lab.api.model.Apartment | ||
import io.scalac.lab.api.model.ApiError.{BadRequestError, NotFoundError} | ||
|
||
import scala.concurrent.Future | ||
|
||
// Similar to `ApartmentsStorage`, but uses precise return | ||
// types instead of `ApiError` everywhere | ||
trait NarrowerApartmentsStorage { | ||
def list(from: Int, limit: Option[Int]): Future[Either[BadRequestError, List[Apartment]]] | ||
def get(id: Int): Future[Either[NotFoundError, Apartment]] | ||
def find(city: String, street: String, number: String): Future[Either[Either[BadRequestError, NotFoundError], Apartment]] | ||
def save(a: Apartment): Future[Either[BadRequestError, Apartment]] | ||
def delete(id: Int): Future[Either[NotFoundError, Apartment]] | ||
} |