-
Notifications
You must be signed in to change notification settings - Fork 1
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 #3 from aukgit/feature/play-mvc/2/run-failed-fix
Rest Web Api - Partial SOLID implementation
- Loading branch information
Showing
258 changed files
with
6,417 additions
and
1,167 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package controllers.apis | ||
|
||
import controllers.webapi.core.AbstractRestWebApi | ||
import javax.inject.Inject | ||
import play.api.mvc._ | ||
import services.CampaignService | ||
import services.core.AbstractBasicPersistentService | ||
import shared.com.ortb.persistent.schema.Tables._ | ||
|
||
class CampaignsApiController @Inject()( | ||
campaignService : CampaignService, | ||
components : ControllerComponents) | ||
extends AbstractRestWebApi[Campaign, CampaignRow, Int](components) { | ||
|
||
override val service : AbstractBasicPersistentService[Campaign, CampaignRow, Int] = | ||
campaignService | ||
} |
118 changes: 118 additions & 0 deletions
118
app/controllers/controllerRoutes/AbstractGenericRouter.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,118 @@ | ||
package controllers.controllerRoutes | ||
|
||
import java.security.spec.InvalidParameterSpecException | ||
|
||
import controllers.controllerRoutes.traits.RouterActionPerformByIds | ||
import controllers.webapi.core.AbstractRestWebApi | ||
import play.api.mvc.{ Action, AnyContent } | ||
import play.api.routing.Router.Routes | ||
import play.api.routing.SimpleRouter | ||
import play.api.routing.sird._ | ||
import shared.com.ortb.enumeration.ControllerDefaultActionType | ||
import shared.com.ortb.model.results.ResultWithBooleanModel | ||
import shared.com.ortb.model.wrappers.http.ControllerGenericActionWrapper | ||
import shared.io.helpers.NumberHelper | ||
|
||
import scala.reflect.runtime.universe._ | ||
|
||
abstract class AbstractGenericRouter[TTable, TRow, TKey : TypeTag]( | ||
controller : AbstractRestWebApi[TTable, TRow, TKey]) | ||
extends SimpleRouter | ||
with RouterActionPerformByIds { | ||
|
||
val routingActionWrapper : ControllerGenericActionWrapper = ControllerGenericActionWrapper( | ||
ControllerDefaultActionType.Routing) | ||
|
||
override def routes : Routes = { | ||
try { | ||
case GET(p"/") => | ||
controller.getAll() | ||
|
||
case POST(p"/create") => | ||
controller.add() | ||
|
||
case POST(p"/createMany") => | ||
controller.addEntities() | ||
|
||
case POST(p"/createMany/$addTimes") => | ||
performAddTimes(addTimes) | ||
|
||
case GET(p"/$id") => | ||
performGetById(id) | ||
|
||
case PUT(p"/$id") => | ||
performUpdateById(id) | ||
|
||
} catch { | ||
case e : Exception => | ||
controller.handleError(e, routingActionWrapper) | ||
throw e | ||
} | ||
} | ||
|
||
private def performAddTimes(addTimes : String) : Action[AnyContent] = { | ||
val addTimesAsInt = NumberHelper.isInt(addTimes) | ||
if (addTimesAsInt.isSuccess) { | ||
controller.addEntitiesBySinge(addTimesAsInt.result.get) | ||
} | ||
else { | ||
performIntegerConversionFailedAction(addTimesAsInt) | ||
} | ||
} | ||
|
||
protected def performIntegerConversionFailedAction( | ||
stringToInt : ResultWithBooleanModel[Int]) : Action[AnyContent] = { | ||
val httpFailedActionWrapper = controller.createHttpFailedActionWrapper( | ||
s"Couldn't convert [$stringToInt] to integer.", | ||
actionWrapper = routingActionWrapper | ||
) | ||
controller.performBadResponseAsAction( | ||
Some(httpFailedActionWrapper)) | ||
} | ||
|
||
protected def performGetByIdAsInteger(id : String) : Action[AnyContent] = { | ||
val idAsInt = NumberHelper.isInt(id) | ||
|
||
if (idAsInt.isSuccess) { | ||
controller.byId(idAsInt.result.get.asInstanceOf[TKey]) | ||
} | ||
else { | ||
performIntegerConversionFailedAction(idAsInt) | ||
} | ||
} | ||
|
||
protected def performUpdateByIdAsInteger(id : String) : Action[AnyContent] = { | ||
val idAsInt = NumberHelper.isInt(id) | ||
// TODO make it more DRY | ||
if (idAsInt.isSuccess) { | ||
controller.update(idAsInt.result.get.asInstanceOf[TKey]) | ||
} | ||
else { | ||
performIntegerConversionFailedAction(idAsInt) | ||
} | ||
} | ||
|
||
override def performUpdateById( | ||
id : String) : Action[AnyContent] = { | ||
val typeOfKey = typeOf[TKey] | ||
typeOfKey match { | ||
case i if i =:= typeOf[Int] => | ||
performUpdateByIdAsInteger(id) | ||
case i if i =:= typeOf[String] => | ||
controller.update(id.asInstanceOf[TKey]) | ||
case _ => throw new InvalidParameterSpecException("Id is not type of Integer or String") | ||
} | ||
} | ||
|
||
override def performGetById( | ||
id : String) : Action[AnyContent] = { | ||
val typeOfKey = typeOf[TKey] | ||
typeOfKey match { | ||
case i if i =:= typeOf[Int] => | ||
performGetByIdAsInteger(id) | ||
case i if i =:= typeOf[String] => | ||
controller.byId(id.asInstanceOf[TKey]) | ||
case _ => throw new InvalidParameterSpecException("Id is not type of Integer or String") | ||
} | ||
} | ||
} |
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,9 @@ | ||
package controllers.controllerRoutes | ||
|
||
import controllers.apis.CampaignsApiController | ||
import javax.inject.Inject | ||
import shared.com.ortb.persistent.schema.Tables._ | ||
|
||
class CampaignApiRouter @Inject()( | ||
campaignsApiController : CampaignsApiController) extends | ||
AbstractGenericRouter[Campaign, CampaignRow, Int](campaignsApiController) |
9 changes: 9 additions & 0 deletions
9
app/controllers/controllerRoutes/traits/RouterActionPerformByIds.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,9 @@ | ||
package controllers.controllerRoutes.traits | ||
|
||
import play.api.mvc.{ Action, AnyContent } | ||
|
||
trait RouterActionPerformByIds { | ||
def performGetById(id : String) : Action[AnyContent] | ||
|
||
def performUpdateById(id : String) : Action[AnyContent] | ||
} |
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,44 @@ | ||
package controllers.webapi.core | ||
|
||
import controllers.webapi.core.traits._ | ||
import controllers.webapi.core.traits.implementations._ | ||
import controllers.webapi.core.traits.implementations.actions._ | ||
import play.api.mvc._ | ||
import shared.com.ortb.model.wrappers.http._ | ||
import shared.io.loggers.AppLogger | ||
|
||
abstract class AbstractRestWebApi[TTable, TRow, TKey]( | ||
val components : ControllerComponents) | ||
extends AbstractController(components) | ||
with RestWebApiContracts[TTable, TRow, TKey] | ||
with RestWebApiHandleErrorImplementation[TTable, TRow, TKey] | ||
with RestWebApiBodyProcessorImplementation[TTable, TRow, TKey] | ||
with RestWebApiResponsePerformImplementation[TTable, TRow, TKey] | ||
with RestWebApiAddActionImplementation[TTable, TRow, TKey] | ||
with RestWebApiAddOrUpdateActionImplementation[TTable, TRow, TKey] | ||
with RestWebApiDeleteActionImplementation[TTable, TRow, TKey] | ||
with RestWebApiUpdateActionImplementation[TTable, TRow, TKey] | ||
with RestWebApiAddEntitiesActionImplementation[TTable, TRow, TKey] | ||
with RestWebApiGetByIdActionImplementation[TTable, TRow, TKey] | ||
with RestWebApiGetAllActionImplementation[TTable, TRow, TKey] | ||
with RestWebApiMessagesImplementation[TTable, TRow, TKey] | ||
with RestWebApiPropertiesImplementation[TTable, TRow, TKey] { | ||
|
||
def getRequestUri(request : Request[AnyContent]) : String = { | ||
request.uri | ||
} | ||
|
||
def createHttpFailedActionWrapper( | ||
message : String, | ||
actionWrapper : ControllerGenericActionWrapper, | ||
methodName : String = "" | ||
) : HttpFailedActionWrapper[TRow, TKey] = { | ||
AppLogger.error(s"${ message } $methodName") | ||
val httpFailedActionWrapper = HttpFailedActionWrapper[TRow, TKey]( | ||
additionalMessage = Some(message), | ||
methodName = Some(methodName), | ||
controllerGenericActionWrapper = actionWrapper) | ||
|
||
httpFailedActionWrapper | ||
} | ||
} |
Oops, something went wrong.