From be110e9ab2eddf74bae73c4fc11c48c0dfcafba0 Mon Sep 17 00:00:00 2001
From: "Mathieu.Vonlanthen" <mathieu.vonlanthen@unige.ch>
Date: Wed, 11 Dec 2024 15:07:39 +0100
Subject: [PATCH] feat(MultiRelation2TiersController): allow to create relation
 with join resource

---
 .../service/ResourceClientService.java        |  7 ++++++
 .../MultiRelation2TiersController.java        | 23 +++++++++++++++----
 2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/solidify-client/src/main/java/ch/unige/solidify/service/ResourceClientService.java b/solidify-client/src/main/java/ch/unige/solidify/service/ResourceClientService.java
index a950ea218..b24d049bc 100644
--- a/solidify-client/src/main/java/ch/unige/solidify/service/ResourceClientService.java
+++ b/solidify-client/src/main/java/ch/unige/solidify/service/ResourceClientService.java
@@ -270,6 +270,13 @@ public abstract class ResourceClientService<T extends Resource>
     restTemplate.postForObject(this.getAssociationUrl(itemTypeLabel), ids, arrayType, params);
   }
 
+  protected <U extends Resource> void addRelationItem(String resId, String itemTypeLabel, Class<U> itemClass, String itemId, Object joinResource) {
+    final Map<String, String> params = new HashMap<>();
+    params.put(ID_LABEL, resId);
+    final RestTemplate restTemplate = this.getRestClientTool().getClient(this.getResourceUrl());
+    restTemplate.postForObject(this.getAssociationUrl(itemTypeLabel) + "/" + itemId, joinResource, itemClass, params);
+  }
+
   protected <U extends Resource> void updateRelationItem(String resId, String itemTypeLabel, Class<U> itemClass, JoinResource<?> joinResource) {
     final Class<? extends Resource[]> arrayType = (Class<? extends Resource[]>) Array.newInstance(itemClass, 0).getClass();
     final Map<String, String> params = new HashMap<>();
diff --git a/solidify-controller/src/main/java/ch/unige/solidify/controller/MultiRelation2TiersController.java b/solidify-controller/src/main/java/ch/unige/solidify/controller/MultiRelation2TiersController.java
index ad4598140..a007c4637 100644
--- a/solidify-controller/src/main/java/ch/unige/solidify/controller/MultiRelation2TiersController.java
+++ b/solidify-controller/src/main/java/ch/unige/solidify/controller/MultiRelation2TiersController.java
@@ -9,12 +9,12 @@
  * it under the terms of the GNU General Public License as
  * published by the Free Software Foundation, either version 2 of the
  * License, or (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public
  * License along with this program.  If not, see
  * <http://www.gnu.org/licenses/gpl-2.0.html>.
@@ -33,14 +33,15 @@ import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 
+import ch.unige.solidify.SolidifyConstants;
 import ch.unige.solidify.rest.JoinResource;
 import ch.unige.solidify.rest.JoinResourceContainer;
 import ch.unige.solidify.rest.Resource;
 import ch.unige.solidify.security.RootPermissions;
 
 /**
- * Parent controller to manage relations between 2 entity types linked by a relation table with properties.
- * Unlike Relation2TiersController multiple relations with the same entities and different properties is allowed.
+ * Parent controller to manage relations between 2 entity types linked by a relation table with properties. Unlike Relation2TiersController
+ * multiple relations with the same entities and different properties is allowed.
  *
  * @param <T> Type of the entity considered as the parent when managing the relation
  * @param <V> Type of the entity considered as the children when managing the relation
@@ -66,4 +67,18 @@ public abstract class MultiRelation2TiersController<T extends Resource, V extend
     return new ResponseEntity<>(HttpStatus.OK);
   }
 
+  @Override
+  @PostMapping(SolidifyConstants.URL_ID)
+  public HttpEntity<JoinResourceContainer<J>> create(@PathVariable String parentid, @PathVariable String id, @RequestBody J joinResource) {
+    final T parentItem = this.itemService.findOne(parentid);
+    final V childItem = this.childItemService.findOne(id);
+
+    if (joinResource == null) {
+      joinResource = this.relationItemService.getDefaultJoinResource();
+    }
+    J savedJoinResource = this.relationItemService.saveRelation(parentItem, childItem, joinResource);
+    this.addLinks(parentid, this.relationItemService.getChildResource(joinResource));
+    return new ResponseEntity<>(this.relationItemService.getChildDTO(savedJoinResource), HttpStatus.OK);
+  }
+
 }
-- 
GitLab