io.katharsis.dispatcher.controller.resource.RelationshipsResourcePatch Maven / Gradle / Ivy
package io.katharsis.dispatcher.controller.resource;
import io.katharsis.dispatcher.controller.HttpMethod;
import io.katharsis.queryParams.QueryParams;
import io.katharsis.request.dto.DataBody;
import io.katharsis.resource.registry.ResourceRegistry;
import io.katharsis.resource.registry.responseRepository.RelationshipRepositoryAdapter;
import io.katharsis.utils.parser.TypeParser;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
public class RelationshipsResourcePatch extends RelationshipsResourceUpsert {
public RelationshipsResourcePatch(ResourceRegistry resourceRegistry, TypeParser typeParser) {
super(resourceRegistry, typeParser);
}
@Override
public HttpMethod method() {
return HttpMethod.PATCH;
}
@Override
public void processToManyRelationship(Object resource, Class extends Serializable> relationshipIdType,
String elementName, Iterable dataBodies, QueryParams queryParams,
RelationshipRepositoryAdapter relationshipRepositoryForClass) {
List parsedIds = new LinkedList<>();
for (DataBody dataBody : dataBodies) {
Serializable parsedId = typeParser.parse(dataBody.getId(), relationshipIdType);
parsedIds.add(parsedId);
}
//noinspection unchecked
relationshipRepositoryForClass.setRelations(resource, parsedIds, elementName, queryParams);
}
@Override
protected void processToOneRelationship(Object resource, Class extends Serializable> relationshipIdType,
String elementName, DataBody dataBody, QueryParams queryParams,
RelationshipRepositoryAdapter relationshipRepositoryForClass) {
Serializable parsedId = null;
if (dataBody != null) {
parsedId = typeParser.parse(dataBody.getId(), relationshipIdType);
}
//noinspection unchecked
relationshipRepositoryForClass.setRelation(resource, parsedId, elementName, queryParams);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy