-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTestController.scala
132 lines (104 loc) · 3.89 KB
/
TestController.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package wiro
import akka.http.scaladsl.model.{HttpResponse, StatusCodes}
import io.circe.generic.auto._
import scala.concurrent.{ExecutionContext, Future}
import wiro.annotation._
import wiro.server.akkaHttp._
import wiro.server.akkaHttp.FailSupport._
object TestController extends RouterDerivationModule {
case class UserNotFound(userId: Int)
case class Conflict(userId: Int)
case object GenericError
type GenericError = GenericError.type
case class User(id: Int, username: String)
case class Ok(msg: String)
case class Unauthorized(msg: String)
implicit def genericErrorToResponse = new ToHttpResponse[GenericError] {
def response(error: GenericError) = HttpResponse(
status = StatusCodes.InternalServerError,
entity = "Very Bad",
)
}
implicit def unauthorizedToResponse = new ToHttpResponse[Unauthorized] {
def response(error: Unauthorized) = HttpResponse(
status = StatusCodes.Unauthorized,
entity = "Very Bad",
)
}
implicit def conflictToResponse = new ToHttpResponse[Conflict] {
def response(error: Conflict) = HttpResponse(
status = StatusCodes.Conflict,
entity = s"User already exists: ${error.userId}",
)
}
implicit def notFoundToResponse = new ToHttpResponse[UserNotFound] {
def response(error: UserNotFound) = HttpResponse(
status = StatusCodes.NotFound,
entity = s"User not found: ${error.userId}",
)
}
// controllers interface and implementation
@path("user")
trait UserController {
@query
def nobodyCannaCrossIt(token: Auth): Future[Either[Unauthorized, Ok]]
@query
def inLoveWithMyHeaders(
parameters: OperationParameters,
): Future[Either[GenericError, OperationParameters]]
@command
def update(id: Int, user: User): Future[Either[UserNotFound, Ok]]
@command
def updateCommand(id: Int, user: User): Future[Either[UserNotFound, Ok]]
@query
def read(id: Int): Future[Either[UserNotFound, User]]
@query
def readString(id: String): Future[Either[UserNotFound, User]]
@query
def readQuery(id: Int): Future[Either[UserNotFound, User]]
@command(name = Some("insert"))
def insertUser(id: Int, user: User): Future[Either[Conflict, Ok]]
@query(name = Some("number"))
def usersNumber(): Future[Either[GenericError, Int]]
}
private[this] class UserControllerImpl(implicit
ec: ExecutionContext,
) extends UserController {
def nobodyCannaCrossIt(token: Auth): Future[Either[Unauthorized, Ok]] = Future {
if (token == Auth("bus")) Right(Ok("di bus can swim"))
else Left(Unauthorized("yuh cannot cross it"))
}
def update(id: Int, user: User): Future[Either[UserNotFound, Ok]] = Future {
if (id == 1) Right(Ok("update"))
else Left(UserNotFound(id))
}
def updateCommand(id: Int, user: User): Future[Either[UserNotFound, Ok]] = Future {
if (id == 1) Right(Ok("updateCommand"))
else Left(UserNotFound(id))
}
def read(id: Int): Future[Either[UserNotFound, User]] = Future {
if (id == 1) Right(User(id, "read"))
else Left(UserNotFound(id))
}
def readString(id: String): Future[Either[UserNotFound, User]] =
Future(Right(User(1, "read")))
def readQuery(id: Int): Future[Either[UserNotFound, User]] = Future {
if (id == 1) Right(User(id, "readQuery"))
else Left(UserNotFound(id))
}
def insertUser(id: Int, user: User): Future[Either[Conflict, Ok]] = Future {
Right(Ok("inserted!"))
}
def usersNumber(): Future[Either[GenericError, Int]] = Future {
Right(1)
}
def inLoveWithMyHeaders(
parameters: OperationParameters,
): Future[Either[GenericError, OperationParameters]] = Future {
Right(parameters)
}
}
import scala.concurrent.ExecutionContext.Implicits.global
private[this] val userController = new UserControllerImpl
def userRouter = deriveRouter[UserController](userController)
}