22
33namespace App \V1Module \Presenters ;
44
5+ use App \Exceptions \BadRequestException ;
56use App \Helpers \MetaFormats \Attributes \Post ;
67use App \Helpers \MetaFormats \Attributes \Query ;
78use App \Helpers \MetaFormats \Attributes \Path ;
1617use App \Model \Entity \GroupExternalAttribute ;
1718use App \Model \View \GroupViewFactory ;
1819use App \Security \ACL \IGroupPermissions ;
20+ use Doctrine \DBAL \Exception \UniqueConstraintViolationException ;
1921
2022/**
2123 * Additional attributes used by 3rd parties to keep relations between groups and entities in external systems.
@@ -106,7 +108,7 @@ public function checkAdd()
106108 #[Post("service " , new VString (1 , 32 ), "Identifier of the external service creating the attribute " , required: true )]
107109 #[Post("key " , new VString (1 , 32 ), "Key of the attribute (must be valid identifier) " , required: true )]
108110 #[Post("value " , new VString (0 , 255 ), "Value of the attribute (arbitrary string) " , required: true )]
109- #[Path("groupId " , new VString (), required: true )]
111+ #[Path("groupId " , new VUuid (), required: true )]
110112 public function actionAdd (string $ groupId )
111113 {
112114 $ group = $ this ->groups ->findOrThrow ($ groupId );
@@ -115,8 +117,13 @@ public function actionAdd(string $groupId)
115117 $ service = $ req ->getPost ("service " );
116118 $ key = $ req ->getPost ("key " );
117119 $ value = $ req ->getPost ("value " );
118- $ attribute = new GroupExternalAttribute ($ group , $ service , $ key , $ value );
119- $ this ->groupExternalAttributes ->persist ($ attribute );
120+
121+ try {
122+ $ attribute = new GroupExternalAttribute ($ group , $ service , $ key , $ value );
123+ $ this ->groupExternalAttributes ->persist ($ attribute );
124+ } catch (UniqueConstraintViolationException ) {
125+ throw new BadRequestException ("Attribute already exists. " );
126+ }
120127
121128 $ this ->sendSuccessResponse ("OK " );
122129 }
@@ -135,7 +142,7 @@ public function checkRemove()
135142 #[Query("service " , new VString (1 , 32 ), "Identifier of the external service creating the attribute " , required: true )]
136143 #[Query("key " , new VString (1 , 32 ), "Key of the attribute (must be valid identifier) " , required: true )]
137144 #[Query("value " , new VString (0 , 255 ), "Value of the attribute (arbitrary string) " , required: true )]
138- #[Path("groupId " , new VString (), required: true )]
145+ #[Path("groupId " , new VUuid (), required: true )]
139146 public function actionRemove (string $ groupId , string $ service , string $ key , string $ value )
140147 {
141148 $ attributes = $ this ->groupExternalAttributes ->findBy (
0 commit comments