-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3150 from ProjectSidewalk/3079-custom-audit-routes
RouteBuilder: a way to create a custom route to follow!
- Loading branch information
Showing
49 changed files
with
1,136 additions
and
416 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package controllers | ||
|
||
import javax.inject.Inject | ||
import com.mohiva.play.silhouette.api.{Environment, Silhouette} | ||
import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator | ||
import play.api.libs.json._ | ||
import controllers.headers.ProvidesHeader | ||
import formats.json.RouteBuilderFormats.NewRoute | ||
import models.daos.slick.DBTableDefinitions.{DBUser, UserTable} | ||
import models.route.{Route, RouteStreet, RouteStreetTable, RouteTable} | ||
import models.user.{User, WebpageActivity, WebpageActivityTable} | ||
import scala.concurrent.Future | ||
import play.api.mvc.BodyParsers | ||
import java.sql.Timestamp | ||
import java.time.Instant | ||
|
||
/** | ||
* Holds the HTTP requests associated with managing neighborhoods. | ||
* | ||
* @param env The Silhouette environment. | ||
*/ | ||
class RouteBuilderController @Inject() (implicit val env: Environment[User, SessionAuthenticator]) | ||
extends Silhouette[User, SessionAuthenticator] with ProvidesHeader { | ||
|
||
val anonymousUser: DBUser = UserTable.find("anonymous").get | ||
|
||
def saveRoute = UserAwareAction.async(BodyParsers.parse.json) { implicit request => | ||
val submission = request.body.validate[NewRoute] | ||
submission.fold( | ||
errors => { | ||
Future.successful(BadRequest(Json.obj("status" -> "Error", "message" -> JsError.toFlatJson(errors)))) | ||
}, | ||
submission => { | ||
val timestamp: Timestamp = new Timestamp(Instant.now.toEpochMilli) | ||
val ipAddress: String = request.remoteAddress | ||
val userIdStr: String = request.identity.map(_.userId.toString).getOrElse(anonymousUser.userId) | ||
WebpageActivityTable.save(WebpageActivity(0, userIdStr, ipAddress, "SaveRoute", timestamp)) | ||
|
||
// Save new route in the database. | ||
val newRouteId: Int = RouteTable.save(Route(0, userIdStr, submission.regionId, "temp", public = false, deleted = false)) | ||
val newRouteStreets: Seq[RouteStreet] = submission.streetIds.zipWithIndex.map { case (streetId, index) => | ||
RouteStreet(0, newRouteId, streetId, firstStreet = index == 0) | ||
} | ||
RouteStreetTable.saveMultiple(newRouteStreets) | ||
|
||
Future.successful(Ok(Json.obj("route_id" -> newRouteId))) | ||
} | ||
) | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,13 @@ | ||
package formats.json | ||
|
||
import play.api.libs.json.{JsPath, Reads} | ||
import play.api.libs.functional.syntax._ | ||
|
||
object RouteBuilderFormats { | ||
case class NewRoute(regionId: Int, streetIds: Seq[Int]) | ||
|
||
implicit val newRouteReads: Reads[NewRoute] = ( | ||
(JsPath \ "region_id").read[Int] and | ||
(JsPath \ "street_ids").read[Seq[Int]] | ||
)(NewRoute.apply _) | ||
} |
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
Oops, something went wrong.