Skip to content

Commit

Permalink
task: add endpoint to request single field definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominick Leppich committed Nov 12, 2024
1 parent b875428 commit 10ea3bf
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.goobi.vocabulary.api;

import io.goobi.vocabulary.api.assemblers.FieldDefinitionAssembler;
import io.goobi.vocabulary.exchange.FieldDefinition;
import io.goobi.vocabulary.service.manager.FieldDefinitionDTOManager;
import org.springframework.hateoas.EntityModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1")
public class FieldDefinitionController {
private final FieldDefinitionDTOManager manager;
private final FieldDefinitionAssembler assembler;

public FieldDefinitionController(FieldDefinitionDTOManager manager, FieldDefinitionAssembler assembler) {
this.manager = manager;
this.assembler = assembler;
}

@GetMapping("/fieldDefinitions/{id}")
public EntityModel<FieldDefinition> one(@PathVariable long id) {
return assembler.toModel(manager.get(id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.goobi.vocabulary.api.assemblers;

import io.goobi.vocabulary.api.FieldTypeController;
import io.goobi.vocabulary.api.VocabularySchemaController;
import io.goobi.vocabulary.exchange.FieldDefinition;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.server.RepresentationModelAssembler;
import org.springframework.stereotype.Component;

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

@Component
public class FieldDefinitionAssembler implements RepresentationModelAssembler<FieldDefinition, EntityModel<FieldDefinition>> {
@Override
public EntityModel<FieldDefinition> toModel(FieldDefinition entity) {
return EntityModel.of(entity,
linkTo(methodOn(FieldTypeController.class).one(entity.getId())).withSelfRel(),
linkTo(methodOn(VocabularySchemaController.class).one(entity.getSchemaId())).withRel("schema")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.goobi.vocabulary.service.manager;

import io.goobi.vocabulary.exception.EntityNotFoundException;
import io.goobi.vocabulary.exchange.FieldDefinition;
import io.goobi.vocabulary.repositories.FieldDefinitionRepository;
import io.goobi.vocabulary.service.exchange.DTOMapper;
import org.springframework.stereotype.Service;

@Service
public class FieldDefinitionDTOManager {
private final FieldDefinitionRepository fieldDefinitionRepository;
private final DTOMapper modelMapper;

public FieldDefinitionDTOManager(FieldDefinitionRepository fieldDefinitionRepository, DTOMapper modelMapper) {
this.fieldDefinitionRepository = fieldDefinitionRepository;
this.modelMapper = modelMapper;
}

public FieldDefinition get(long id) {
return modelMapper.toDTO(
fieldDefinitionRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException(FieldDefinition.class, id))
);
}
}

0 comments on commit 10ea3bf

Please sign in to comment.