44import static org .springframework .hateoas .server .mvc .WebMvcLinkBuilder .methodOn ;
55
66import com .contentgrid .appserver .application .model .Application ;
7+ import com .contentgrid .appserver .application .model .Entity ;
78import com .contentgrid .appserver .application .model .relations .ManyToManyRelation ;
89import com .contentgrid .appserver .application .model .relations .ManyToOneRelation ;
910import com .contentgrid .appserver .application .model .relations .OneToManyRelation ;
1920import java .net .URI ;
2021import java .util .List ;
2122import java .util .Map ;
23+ import java .util .Optional ;
2224import java .util .UUID ;
2325import lombok .RequiredArgsConstructor ;
2426import org .springframework .beans .factory .annotation .Autowired ;
2527import org .springframework .http .HttpStatus ;
2628import org .springframework .http .ResponseEntity ;
2729import org .springframework .web .bind .annotation .DeleteMapping ;
28- import org .springframework .web .bind .annotation .GetMapping ;
2930import org .springframework .web .bind .annotation .PathVariable ;
3031import org .springframework .web .bind .annotation .PostMapping ;
3132import org .springframework .web .bind .annotation .PutMapping ;
3536
3637@ RestController
3738@ RequiredArgsConstructor (onConstructor_ = @ Autowired )
38- public class RelationRestController {
39+ public class RelationRestController implements PropertyRestController {
3940
4041 private final DatamodelApi datamodelApi ;
4142
@@ -44,42 +45,46 @@ private Relation getRelationOrThrow(Application application, PathSegmentName ent
4445 .orElseThrow (() -> new ResponseStatusException (HttpStatus .NOT_FOUND , "Entity not found" ));
4546 }
4647
47- @ GetMapping ( "/{entityName}/{sourceId}/{relationName}" )
48- public ResponseEntity <Void > followRelation (
48+ @ Override
49+ public Optional < ResponseEntity <Object >> getProperty (
4950 Application application ,
50- @ PathVariable PathSegmentName entityName ,
51- @ PathVariable EntityId sourceId ,
52- @ PathVariable PathSegmentName relationName
51+ Entity entity ,
52+ EntityId instanceId ,
53+ PathSegmentName propertyName
5354 ) {
54- var relation = getRelationOrThrow (application , entityName , relationName );
55+ var maybeRelation = application .getRelationForPath (entity .getPathSegment (), propertyName );
56+ if (maybeRelation .isEmpty ()) {
57+ return Optional .empty ();
58+ }
59+ var relation = maybeRelation .get ();
5560 var targetPathSegment = relation .getTargetEndPoint ().getEntity ().getPathSegment ();
5661 try {
5762 URI redirectUrl ;
5863 switch (relation ) {
5964 case OneToOneRelation oneToOneRelation -> {
60- var targetId = datamodelApi .findRelationTarget (application , oneToOneRelation , sourceId )
61- .orElseThrow (() -> new ResponseStatusException (HttpStatus .NOT_FOUND , "Target of %s not found" .formatted (relationName )));
65+ var targetId = datamodelApi .findRelationTarget (application , oneToOneRelation , instanceId )
66+ .orElseThrow (() -> new ResponseStatusException (HttpStatus .NOT_FOUND , "Target of %s not found" .formatted (propertyName )));
6267 redirectUrl = linkTo (methodOn (EntityRestController .class ).getEntity (application , targetPathSegment , targetId )).toUri ();
6368 }
6469 case ManyToOneRelation manyToOneRelation -> {
65- var targetId = datamodelApi .findRelationTarget (application , manyToOneRelation , sourceId )
66- .orElseThrow (() -> new ResponseStatusException (HttpStatus .NOT_FOUND , "Target of %s not found" .formatted (relationName )));
70+ var targetId = datamodelApi .findRelationTarget (application , manyToOneRelation , instanceId )
71+ .orElseThrow (() -> new ResponseStatusException (HttpStatus .NOT_FOUND , "Target of %s not found" .formatted (propertyName )));
6772 redirectUrl = linkTo (methodOn (EntityRestController .class ).getEntity (application , targetPathSegment , targetId )).toUri ();
6873 }
6974 case OneToManyRelation oneToManyRelation -> {
70- datamodelApi .findById (application , oneToManyRelation .getSourceEndPoint ().getEntity (), sourceId )
71- .orElseThrow (() -> new ResponseStatusException (HttpStatus .NOT_FOUND , "Entity with id %s not found" .formatted (sourceId )));
75+ datamodelApi .findById (application , oneToManyRelation .getSourceEndPoint ().getEntity (), instanceId )
76+ .orElseThrow (() -> new ResponseStatusException (HttpStatus .NOT_FOUND , "Entity with id %s not found" .formatted (instanceId )));
7277 redirectUrl = linkTo (methodOn (EntityRestController .class ).listEntity (application , targetPathSegment , 0 ,
73- Map .of (relation .getTargetEndPoint ().getName ().getValue (), sourceId .toString ()))).toUri (); // TODO: use RelationSearchFilter
78+ Map .of (relation .getTargetEndPoint ().getName ().getValue (), instanceId .toString ()))).toUri (); // TODO: use RelationSearchFilter
7479 }
7580 case ManyToManyRelation manyToManyRelation -> {
76- datamodelApi .findById (application , manyToManyRelation .getSourceEndPoint ().getEntity (), sourceId )
77- .orElseThrow (() -> new ResponseStatusException (HttpStatus .NOT_FOUND , "Entity with id %s not found" .formatted (sourceId )));
81+ datamodelApi .findById (application , manyToManyRelation .getSourceEndPoint ().getEntity (), instanceId )
82+ .orElseThrow (() -> new ResponseStatusException (HttpStatus .NOT_FOUND , "Entity with id %s not found" .formatted (instanceId )));
7883 redirectUrl = linkTo (methodOn (EntityRestController .class ).listEntity (application , targetPathSegment , 0 ,
79- Map .of (relation .getTargetEndPoint ().getName ().getValue (), sourceId .toString ()))).toUri (); // TODO: use RelationSearchFilter
84+ Map .of (relation .getTargetEndPoint ().getName ().getValue (), instanceId .toString ()))).toUri (); // TODO: use RelationSearchFilter
8085 }
8186 }
82- return ResponseEntity .status (HttpStatus .FOUND ).location (redirectUrl ).build ();
87+ return Optional . of ( ResponseEntity .status (HttpStatus .FOUND ).location (redirectUrl ).build () );
8388 } catch (EntityNotFoundException e ) {
8489 throw new ResponseStatusException (HttpStatus .NOT_FOUND , e .getMessage (), e );
8590 }
0 commit comments