Skip to content

Commit

Permalink
Merge pull request #105 from companieshouse/bugfix/dateTimeFix
Browse files Browse the repository at this point in the history
use generic read/write converters
  • Loading branch information
JAndrewCH authored Mar 11, 2024
2 parents e2ab4f0 + 09ebe8b commit 7a63f22
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import uk.gov.companieshouse.api.InternalApiClient;
import uk.gov.companieshouse.api.api.CompanyMetricsApiService;
import uk.gov.companieshouse.api.converter.EnumWriteConverter;
import uk.gov.companieshouse.pscdataapi.converter.CompanyPscReadConverter;
import uk.gov.companieshouse.pscdataapi.converter.CompanyPscWriteConverter;
import uk.gov.companieshouse.pscdataapi.models.PscData;
import uk.gov.companieshouse.pscdataapi.serialization.LocalDateDeSerializer;
import uk.gov.companieshouse.pscdataapi.serialization.LocalDateSerializer;
import uk.gov.companieshouse.sdk.manager.ApiSdkManager;
Expand All @@ -33,7 +35,8 @@ public class ApplicationConfig {
public MongoCustomConversions mongoCustomConversions() {
ObjectMapper objectMapper = mongoDbObjectMapper();
return new MongoCustomConversions(List.of(new CompanyPscWriteConverter(objectMapper),
new CompanyPscReadConverter(objectMapper)));
new CompanyPscReadConverter(objectMapper, PscData.class),
new EnumWriteConverter()));
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,17 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.bson.Document;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import uk.gov.companieshouse.api.company.Data;
import uk.gov.companieshouse.api.converter.ReadConverter;
import uk.gov.companieshouse.api.delta.Psc;
import uk.gov.companieshouse.api.psc.FullRecordCompanyPSCApi;
import uk.gov.companieshouse.pscdataapi.exceptions.FailedToConvertException;
import uk.gov.companieshouse.pscdataapi.models.PscData;

public class CompanyPscReadConverter implements Converter<Document, FullRecordCompanyPSCApi> {

private final ObjectMapper objectMapper;

public CompanyPscReadConverter(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

/**
* Read convertor.
* @param source source Document.
* @return psc API object.
*/
@Override
public FullRecordCompanyPSCApi convert(Document source) {
try {
return objectMapper.readValue(source.toJson(), FullRecordCompanyPSCApi.class);
} catch (Exception ex) {
throw new FailedToConvertException(ex.getMessage());
}
@ReadingConverter
public class CompanyPscReadConverter extends ReadConverter<PscData> {
public CompanyPscReadConverter(ObjectMapper objectMapper, Class<PscData> objectClass) {
super(objectMapper, objectClass);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,16 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.BasicDBObject;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.WritingConverter;
import uk.gov.companieshouse.api.company.Data;
import uk.gov.companieshouse.api.converter.WriteConverter;
import uk.gov.companieshouse.pscdataapi.exceptions.FailedToConvertException;
import uk.gov.companieshouse.pscdataapi.models.PscData;
import uk.gov.companieshouse.pscdataapi.models.PscDocument;

public class CompanyPscWriteConverter implements Converter<PscDocument, BasicDBObject> {

private final ObjectMapper objectMapper;

@WritingConverter
public class CompanyPscWriteConverter extends WriteConverter<PscData> {
public CompanyPscWriteConverter(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

/**
* Write convertor.
* @param source source Document.
* @return psc BSON object.
*/
@Override
public BasicDBObject convert(PscDocument source) {
try {
return BasicDBObject.parse(objectMapper.writeValueAsString(source));
} catch (Exception ex) {
throw new FailedToConvertException(ex.getMessage());
}
super(objectMapper);
}
}

This file was deleted.

This file was deleted.

0 comments on commit 7a63f22

Please sign in to comment.