@@ -17,7 +17,7 @@ import play.api.libs.json.Json._
1717import play .api .libs .json ._
1818import play .api .libs .ws .{Response , WS }
1919import play .api .libs .functional .syntax ._
20- import play .api .mvc .MultipartFormData
20+ import play .api .mvc .{ Action , MultipartFormData , Result , SimpleResult }
2121import services ._
2222
2323import scala .collection .mutable .ListBuffer
@@ -678,23 +678,53 @@ class Extractions @Inject()(
678678 // Fetch parameters from request body
679679 val (name, category, assignedExtractors) = parseExtractorsLabel(request)
680680
681- // Validate and create new label
682- validateExtractorsLabel(name, category, assignedExtractors)
683- val label = extractors.createExtractorsLabel(name, category, assignedExtractors)
681+ // Validate that name is not empty
682+ if (name.isEmpty) {
683+ BadRequest (" Label Name cannot be empty" )
684+ } else {
685+ // Validate that name is unique
686+ extractors.getExtractorsLabel(name) match {
687+ case Some (lbl) => Conflict (" Label name is already in use: " + lbl.name)
688+ case None => {
689+ // Create the new label
690+ val label = extractors.createExtractorsLabel(name, category, assignedExtractors)
691+ Ok (Json .toJson(label))
692+ }
693+ }
694+
695+ }
684696
685- Ok (Json .toJson(label))
686697 }
687698
688699 def updateExtractorsLabel (id : UUID ) = ServerAdminAction (parse.json) { implicit request =>
689700 // Fetch parameters from request body
690701 val (name, category, assignedExtractors) = parseExtractorsLabel(request)
691702
692- // Validate and perform update
693- validateExtractorsLabel(name, category, assignedExtractors)
694- val label = ExtractorsLabel (id, name, category, assignedExtractors)
695- val updatedLabel = extractors.updateExtractorsLabel(label)
696-
697- Ok (Json .toJson(updatedLabel))
703+ // Validate that name is not empty
704+ if (name.isEmpty) {
705+ BadRequest (" Label Name cannot be empty" )
706+ } else {
707+ // Validate that name is still unique
708+ extractors.getExtractorsLabel(name) match {
709+ case Some (lbl) => {
710+ // Exclude current id (in case name hasn't changed)
711+ if (lbl.id != id) {
712+ Conflict (" Label name is already in use: " + lbl.name)
713+ } else {
714+ // Update the label
715+ val label = ExtractorsLabel (id, name, category, assignedExtractors)
716+ val updatedLabel = extractors.updateExtractorsLabel(label)
717+ Ok (Json .toJson(updatedLabel))
718+ }
719+ }
720+ case None => {
721+ // Update the label
722+ val label = ExtractorsLabel (id, name, category, assignedExtractors)
723+ val updatedLabel = extractors.updateExtractorsLabel(label)
724+ Ok (Json .toJson(updatedLabel))
725+ }
726+ }
727+ }
698728 }
699729
700730 def deleteExtractorsLabel (id : UUID ) = ServerAdminAction { implicit request =>
@@ -715,10 +745,4 @@ class Extractions @Inject()(
715745
716746 (name, category, assignedExtractors)
717747 }
718-
719- def validateExtractorsLabel (name : String , category : Option [String ], assignedExtractors : List [String ]): Unit = {
720- if (name.isEmpty) {
721- BadRequest (" Label Name cannot be empty" )
722- }
723- }
724748}
0 commit comments