-
Notifications
You must be signed in to change notification settings - Fork 640
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#312 Add API to describe attributes of an entity
- Loading branch information
Showing
8 changed files
with
72 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package controllers | ||
|
||
import javax.inject.{ Inject, Singleton } | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
import play.api.mvc.{ AbstractController, Action, AnyContent, ControllerComponents } | ||
|
||
import models.Roles | ||
|
||
import org.elastic4play.controllers.{ Authenticated, Renderer } | ||
import org.elastic4play.models.Attribute | ||
import org.elastic4play.models.JsonFormat.attributeDefinitionWrites | ||
import org.elastic4play.services.{ DBLists, ModelSrv } | ||
|
||
@Singleton | ||
class DescribeCtrl @Inject() ( | ||
dblists: DBLists, | ||
modelSrv: ModelSrv, | ||
authenticated: Authenticated, | ||
renderer: Renderer, | ||
components: ControllerComponents, | ||
implicit val ec: ExecutionContext) extends AbstractController(components) { | ||
|
||
def describe(modelName: String): Action[AnyContent] = authenticated(Roles.read) { implicit request ⇒ | ||
modelSrv(modelName) | ||
.map { model ⇒ | ||
val attributeDefinitions = model.attributes.flatMap { | ||
case attribute: Attribute[t] ⇒ attribute.format.definition(dblists, attribute) | ||
} | ||
renderer.toOutput(OK, attributeDefinitions) | ||
} | ||
.getOrElse(NotFound(s"Model $modelName not found")) | ||
} | ||
} |
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,26 @@ | ||
package models | ||
|
||
import play.api.libs.json.JsNumber | ||
|
||
import org.elastic4play.models.{ Attribute, AttributeDefinition, NumberAttributeFormat } | ||
import org.elastic4play.services.DBLists | ||
|
||
object SeverityAttributeFormat extends NumberAttributeFormat { | ||
override def definition(dblists: DBLists, attribute: Attribute[Long]): Seq[AttributeDefinition] = | ||
Seq(AttributeDefinition( | ||
attribute.name, | ||
name, | ||
attribute.description, | ||
Seq(JsNumber(1), JsNumber(2), JsNumber(3)), | ||
Seq("low", "medium", "high"))) | ||
} | ||
|
||
object TlpAttributeFormat extends NumberAttributeFormat { | ||
override def definition(dblists: DBLists, attribute: Attribute[Long]): Seq[AttributeDefinition] = | ||
Seq(AttributeDefinition( | ||
attribute.name, | ||
name, | ||
attribute.description, | ||
Seq(JsNumber(0), JsNumber(1), JsNumber(2), JsNumber(3)), | ||
Seq("white", "green", "amber", "red"))) | ||
} |
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