io.elsci.signals.mock.attributes.SignalsAttributeApi Maven / Gradle / Ivy
package io.elsci.signals.mock.attributes;
import io.elsci.signals.mock.assets.TestObjectsFactory;
import io.elsci.signals.sdk.common.ListEntry;
import io.elsci.signals.sdk.attributes.Attribute;
import io.elsci.signals.sdk.attributes.AttributeListEntry;
import io.elsci.signals.sdk.common.SgLinkedResponse;
import io.elsci.signals.sdk.common.SgResponse;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import static io.elsci.signals.mock.SignalsCommonUtils.SIGNALS_API_ROOT;
import static io.elsci.signals.sdk.SignalsSdkHttpUtil.SIGNALS_JSON_MIMETYPE;
@RestController
@RequestMapping(value = SIGNALS_API_ROOT + "/attributes", produces = SIGNALS_JSON_MIMETYPE)
public class SignalsAttributeApi {
private final AttributeDao attributeDao;
public SignalsAttributeApi(AttributeDao attributeDao) {
this.attributeDao = attributeDao;
}
@GetMapping
public SgResponse>> list() {
List> array = attributeDao.list().stream().map((a) ->
new ListEntry<>(new AttributeListEntry(a.getId(), a.getName(), a.getType()))
).toList();
return new SgResponse<>(array);
}
@GetMapping("/{eid}")
public SgLinkedResponse getById(@PathVariable String eid, HttpServletRequest req) {
return new SgLinkedResponse<>(attributeDao.get(eid), Map.of("self", req.getRequestURL().toString()));
}
// todo: this response doesn't look like real Signals response yet
@PostMapping(consumes = SIGNALS_JSON_MIMETYPE)
public Attribute createAttribute(@RequestBody Attribute attribute) {
return attributeDao.save(attribute);
}
@PostMapping(value = "/test-set-projects")
public Attribute setProjects(@RequestBody List projectNames) {
Attribute attr = attributeDao.findByName("Projects");
if(attr == null)
attr = TestObjectsFactory.projects("Projects", 0);
Attribute newAttr = new Attribute(attr.getId(), attr.getName(), attr.getType(), attr.getDescription(), projectNames);
attributeDao.save(newAttr);
return newAttr;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy