@@ -15,8 +15,7 @@ import scala.concurrent.Future
15
15
import play .api .libs .json .{JsError , JsNull , JsValue , Json }
16
16
import play .api .mvc ._
17
17
18
- import com .salesforce .mce .acdc .db .DatasetQuery
19
- import com .salesforce .mce .acdc .db .DatasetTable
18
+ import com .salesforce .mce .acdc .db .{DatasetQuery , DatasetTable }
20
19
import models .CreateDatasetRequest
21
20
import models .DatasetResponse
22
21
import play .api .libs .json .JsString
@@ -90,47 +89,65 @@ class DatasetController @Inject() (
90
89
case InvalidApiRequest (_) => Future .successful(Unauthorized (JsNull ))
91
90
}
92
91
93
- def filter (like : String , order : Option [String ], page : Option [Int ], perPage : Option [Int ]) =
94
- authAction.async {
95
- case ValidApiRequest (apiRole, _) =>
96
- val validated = for {
97
- vOrder <- DatasetController .validateOrder(order)
98
- vPage <- DatasetController .validateOne(page.getOrElse(1 ), " page" )
99
- limit <- DatasetController .validateOne(perPage.getOrElse(50 ), " per_page" )
100
- } yield (vOrder, limit, (vPage - 1 ) * limit)
101
-
102
- validated match {
103
- case Right ((o, limit, offset)) =>
104
- db.async(DatasetQuery .filter(like, o, limit, offset))
105
- .map(rs => Ok (Json .toJson(rs.map(toResponse))))
106
- case Left (msg) =>
107
- Future .successful(BadRequest (JsString (msg)))
108
- }
109
-
110
- case InvalidApiRequest (_) =>
111
- Future .successful(Unauthorized (JsNull ))
112
- }
92
+ def filter (
93
+ like : String ,
94
+ orderBy : Option [String ],
95
+ order : Option [String ],
96
+ page : Option [Int ],
97
+ perPage : Option [Int ]
98
+ ) = authAction.async {
99
+ case ValidApiRequest (apiRole, _) =>
100
+
101
+ val validated = for {
102
+ vOrderBy <- DatasetController .validateOrderBy(orderBy)
103
+ vOrder <- DatasetController .validateOrder(order)
104
+ vPage <- DatasetController .validateOne(page.getOrElse(1 ), " page" )
105
+ limit <- DatasetController .validateOne(perPage.getOrElse(50 ), " per_page" )
106
+ } yield (vOrderBy, vOrder, limit, (vPage - 1 ) * limit)
107
+
108
+ validated match {
109
+ case Right ((by, ord, limit, offset)) =>
110
+ db.async(DatasetQuery .filter(like, by, ord, limit, offset))
111
+ .map(rs => Ok (Json .toJson(rs.map(toResponse))))
112
+ case Left (msg) =>
113
+ Future .successful(BadRequest (JsString (msg)))
114
+ }
115
+
116
+ case InvalidApiRequest (_) =>
117
+ Future .successful(Unauthorized (JsNull ))
118
+ }
113
119
114
120
}
115
121
116
122
object DatasetController {
117
123
124
+ private def validateOrderBy (
125
+ orderBy : Option [String ]
126
+ ): Either [String , DatasetQuery .OrderBy ] = {
127
+ orderBy
128
+ .fold[Either [String , DatasetQuery .OrderBy ]](
129
+ Right (DatasetQuery .OrderBy .CreatedAt )
130
+ ) {
131
+ case " created_at" => Right (DatasetQuery .OrderBy .CreatedAt )
132
+ case " updated_at" => Right (DatasetQuery .OrderBy .UpdatedAt )
133
+ case other => Left (s " Unknow order_by $other" )
134
+ }
135
+ }
136
+
118
137
private def validateOrder (
119
138
order : Option [String ]
120
- ): Either [String , DatasetQuery .OrderColumn . Value ] = {
139
+ ): Either [String , DatasetQuery .Order ] = {
121
140
order
122
- .fold[Either [String , DatasetQuery .OrderColumn .Value ]](
123
- Right (DatasetQuery .OrderColumn .CreatedAt )
124
- ) { o =>
125
- try {
126
- Right (DatasetQuery .OrderColumn .withName(o))
127
- } catch {
128
- case e : NoSuchElementException =>
129
- Left (s " Unknow order $o" )
130
- }
141
+ .fold[Either [String , DatasetQuery .Order ]](
142
+ Right (DatasetQuery .Order .Desc )
143
+ ) {
144
+ case " desc" => Right (DatasetQuery .Order .Desc )
145
+ case " asc" => Right (DatasetQuery .Order .Asc )
146
+ case other => Left (s " Unknow order $other" )
131
147
}
132
148
}
133
149
150
+
134
151
private def validateOne (num : Int , name : String ): Either [String , Int ] = {
135
152
if (num < 1 ) Left (s " $name must be at least 1 " )
136
153
else Right (num)
0 commit comments