Skip to content

Commit

Permalink
CRUD, webapi res working perfectly
Browse files Browse the repository at this point in the history
CRUD, webapi res working perfectly
  • Loading branch information
aukgit committed Apr 17, 2020
1 parent d447b1a commit ef092dd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 28 deletions.
83 changes: 55 additions & 28 deletions app/controllers/apis/CampaignsApiController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import javax.inject.Inject
import play.api.mvc.{Action, _}
import services.CampaignService
import shared.com.ortb.model.results.RepositoryOperationResult
import shared.com.ortb.persistent.schema.Tables
import shared.com.ortb.persistent.schema.Tables._
import shared.io.logger.AppLogger

Expand All @@ -16,7 +15,7 @@ trait WebApi[TTable, TRow, TKey] {

def byId(id : TKey) : Action[AnyContent]

def add(entity : TRow) : Action[AnyContent]
def add() : Action[AnyContent]

def update(id : TKey) : Action[AnyContent]

Expand Down Expand Up @@ -47,40 +46,68 @@ class CampaignsApiController @Inject()(
Ok(json)
}

override def add(
entity : Tables.CampaignRow) : Action[AnyContent] = Action { implicit request =>
val response : RepositoryOperationResult[CampaignRow, Int] = campaignService.add(entity)
val json = response.entity.get.asJson.spaces2
Ok(json)
}

override def update(id : Int) : Action[AnyContent] = Action { implicit request =>
override def add() : Action[AnyContent] = Action { implicit request =>
try {
val entity = decode[CampaignRow](request.body.asText.get).getOrElse(null)
AppLogger.debug("put - update")
AppLogger.logEntityNonFuture(
isExecute = true,
entity = Some(entity),
additionalMessage = request.body.asText.get)

if (entity == null) {
BadRequest("")
}
val body = request.body.asText.getOrElse("")
val entity = decode[CampaignRow](body).getOrElse(null)
val response : RepositoryOperationResult[CampaignRow, Int] = campaignService.add(entity)

val response = campaignService.update(id, entity)
val json = response.entity.get.asJson.spaces2
Ok(json)
if (response.isSuccess) {
val json = response.entity.get.asJson.spaces2
Ok(json)
}
else {
BadRequest(s"Failed to create. (raw body: $body)")
}
} catch {
case e : Exception => AppLogger.error(e)
BadRequest(e.toString)
}
}

BadRequest("")
override def update(id : Int) : Action[AnyContent] = Action {
implicit request =>
try {
val body = request.body.asText.getOrElse("")
val entity = decode[CampaignRow](body).getOrElse(null)
AppLogger.debug("put - update")
AppLogger.logEntityNonFuture(
isExecute = true,
entity = Some(entity),
additionalMessage = body)

if (entity == null) {
BadRequest(s"Entity conversion failed for given (source received:$body).")
}

val response = campaignService.update(id, entity)

if (!response.isSuccess) {
BadRequest(s"Update request failed (source received:$body).")
}
else {
val e2 = response.entity.get
val json = e2.asJson.spaces2

AppLogger.logEntityNonFuture(
isExecute = true,
entity = Some(e2),
additionalMessage = json)

Ok(json)
}

} catch {
case e : Exception => AppLogger.error(e)
BadRequest(e.toString)
}
}

override def delete(id : Int) : Action[AnyContent] = Action { implicit request =>
val response = campaignService.delete(id)
val json = response.entity.get.asJson.spaces2
Ok(json)
override def delete(id : Int) : Action[AnyContent] = Action {
implicit request =>
val response = campaignService.delete(id)
val json = response.entity.get.asJson.spaces2
Ok(json)
}

//
Expand Down
1 change: 1 addition & 0 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ GET /api/campaigns controllers.apis.CampaignsApiController.getAll
GET /api/campaigns/:id controllers.apis.CampaignsApiController.byId(id: Int)
DELETE /api/campaigns/:id controllers.apis.CampaignsApiController.delete(id: Int)
PUT /api/campaigns/:id controllers.apis.CampaignsApiController.update(id: Int)
POST /api/campaigns/create controllers.apis.CampaignsApiController.add

# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)
Expand Down

0 comments on commit ef092dd

Please sign in to comment.