Skip to content

Commit

Permalink
update put instructor dto
Browse files Browse the repository at this point in the history
  • Loading branch information
coleji committed Nov 26, 2023
1 parent 9de42c6 commit 3465b73
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 18 deletions.
1 change: 1 addition & 0 deletions app/com/coleji/neptune/API/RestControllerWithDTO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.coleji.neptune.API
import com.coleji.neptune.Core.{PermissionsAuthority, UnlockedRequestCache}
import com.coleji.neptune.Storable.{DTOClass, StorableClass, StorableObject}

@deprecated
abstract class RestControllerWithDTO[S <: StorableClass, D <: DTOClass[S]](obj: StorableObject[S]) {
protected def runValidationsForUpdate(rc: UnlockedRequestCache, d: D): ValidationResult
protected def runValidationsForInsert(rc: UnlockedRequestCache, d: D): ValidationResult
Expand Down
77 changes: 77 additions & 0 deletions app/com/coleji/neptune/API/RestControllerWithDTO2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.coleji.neptune.API

import com.coleji.neptune.Core.{PermissionsAuthority, UnlockedRequestCache}
import com.coleji.neptune.Storable.{DTOClass, StorableClass, StorableObject}

import scala.reflect.runtime.universe

abstract class RestControllerWithDTO2[S <: StorableClass, D](obj: StorableObject[S])(implicit manifest: scala.reflect.Manifest[S]) {
protected def runValidationsForUpdate(rc: UnlockedRequestCache, dto: D): ValidationResult
protected def runValidationsForInsert(rc: UnlockedRequestCache, dto: D): ValidationResult
protected def mutateStorableForUpdate(storable: S, dto: D): S
protected def mutateStorableForInsert(storable: S, dto: D): S
protected def getId(dto: D): Option[Int]

protected def mutateDtoBeforeOperating(dto: D): D = dto

lazy private val runtimeMirror = universe.runtimeMirror(getClass.getClassLoader)

protected def put(rc: UnlockedRequestCache, rawDto: D)(implicit PA: PermissionsAuthority): Either[ValidationError, S] = {
rc.withTransaction(() => {
val dto = mutateDtoBeforeOperating(rawDto)
val result = recurseThroughObject(rc, dto, obj)
result match {
case Left(e) => {
Left(e)
}
case Right(s) => recurse(rc, s) match {
case e: ValidationError => {
Left(e)
}
case ValidationOk => Right(s)
}
}
})
}

private def recurseThroughObject(rc: UnlockedRequestCache, dto: D, obj: StorableObject[S]): Either[ValidationError, S] = {
getId(dto) match {
case Some(dtoId: Int) => {
runValidationsForUpdate(rc, dto) match {
case ve: ValidationError => Left(ve)
case ValidationOk => {
println("about to fetch " + obj.entityName + "#" + dtoId)
val storable = rc.getObjectById(obj, dtoId, Set(obj.primaryKey)).get
mutateStorableForUpdate(storable, dto)
rc.commitObjectToDatabase(storable)
Right(storable)
}
}
}
case None => {
runValidationsForInsert(rc, dto) match {
case ve: ValidationError => Left(ve)
case ValidationOk => {
val storable = unpackage(rc, dto)
rc.commitObjectToDatabase(storable)
Right(storable)
}
}
}
}
}

private def unpackage(rc: UnlockedRequestCache, dto: D): S = {
val s: S = manifest.runtimeClass.newInstance.asInstanceOf[S]
mutateStorableForInsert(s, dto)
// getCaseValues.foreach(tup => {
// val (name, value) = tup
// s.valuesList.find(_.persistenceFieldName == name).map(fv => {
// fv.asInstanceOf[FieldValue[Any]].update(value)
// })
// })
// s
}

private def recurse(rc: UnlockedRequestCache, s: S): ValidationResult = ValidationOk
}
2 changes: 1 addition & 1 deletion app/com/coleji/neptune/Storable/DTOClass.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ abstract class DTOClass[S <: StorableClass](implicit manifest: scala.reflect.Man

object DTOClass {

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.sailcbi.APIServer.Api.Endpoints.Staff.Rest.ClassInstructor

import com.coleji.neptune.API.{RestControllerWithDTO, ValidationError, ValidationOk, ValidationResult}
import com.coleji.neptune.API.{RestControllerWithDTO, RestControllerWithDTO2, ValidationError, ValidationOk, ValidationResult}
import com.coleji.neptune.Core.{ParsedRequest, PermissionsAuthority, UnlockedRequestCache}
import org.sailcbi.APIServer.Entities.EntityDefinitions.ClassInstructor
import org.sailcbi.APIServer.Entities.access.CbiPermissions
Expand All @@ -12,22 +12,33 @@ import play.api.mvc.InjectedController
import javax.inject.Inject
import scala.concurrent.{ExecutionContext, Future}

class PutClassInstructor @Inject()(implicit exec: ExecutionContext) extends RestControllerWithDTO[ClassInstructor, PutClassInstructorDTO](ClassInstructor) with InjectedController {
class PutClassInstructor @Inject()(implicit exec: ExecutionContext) extends RestControllerWithDTO2[ClassInstructor, PutClassInstructorDTO](ClassInstructor) with InjectedController {
def post()(implicit PA: PermissionsAuthority) = Action.async { request =>
val parsedRequest = ParsedRequest(request)
PA.withParsedPostBodyJSON(parsedRequest.postJSON, PutClassInstructorDTO.apply)(parsed => {
PA.withRequestCache(StaffRequestCache, CbiPermissions.PERM_UPDATE_JP_INSTRUCTORS)(None, parsedRequest, rc => {
put(rc, parsed) match {
case Left(ve: ValidationError) => Future(Ok(ve.toResultError.asJsObject))
case Right(i: ClassInstructor) => Future(Ok(new JsObject(Map(
"INSTRUCTOR_ID" -> JsNumber(i.values.instructorId.get)
"instructorId" -> JsNumber(i.values.instructorId.get)
))))
}
})
})
}

override def getId(d: PutClassInstructorDTO): Option[Int] = d.instructorId

override def mutateStorableForUpdate(s: ClassInstructor, d: PutClassInstructorDTO): ClassInstructor = {
s.update(_.nameFirst, d.nameFirst)
s.update(_.nameLast, d.nameLast)
s
}

override def mutateStorableForInsert(s: ClassInstructor, d: PutClassInstructorDTO): ClassInstructor = mutateStorableForUpdate(s, d)

override def runValidationsForUpdate(rc: UnlockedRequestCache, d: PutClassInstructorDTO): ValidationResult = ValidationOk

override def runValidationsForInsert(rc: UnlockedRequestCache, d: PutClassInstructorDTO): ValidationResult = ValidationOk
}

18 changes: 4 additions & 14 deletions app/org/sailcbi/APIServer/Entities/dto/PutClassInstructorDTO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,10 @@ import org.sailcbi.APIServer.Entities.EntityDefinitions.ClassInstructor
import play.api.libs.json.{JsValue, Json}

case class PutClassInstructorDTO(
INSTRUCTOR_ID: Option[Int],
NAME_FIRST: String,
NAME_LAST: String,
) extends DTOClass[ClassInstructor] {
override def getId: Option[Int] = INSTRUCTOR_ID

override def mutateStorableForUpdate(s: ClassInstructor): ClassInstructor = {
s.update(_.nameFirst, NAME_FIRST)
s.update(_.nameLast, NAME_LAST)
s
}

override def mutateStorableForInsert(s: ClassInstructor): ClassInstructor = mutateStorableForUpdate(s)
}
instructorId: Option[Int],
nameFirst: String,
nameLast: String,
)

object PutClassInstructorDTO {
implicit val format = Json.format[PutClassInstructorDTO]
Expand Down

0 comments on commit 3465b73

Please sign in to comment.