public ModelEnvelope <CropZone> Convert(CropZoneDto cropZoneDto) { var cropZone = new CropZone() { Description = cropZoneDto.Name, TimeScopes = new List <TimeScope>() { new TimeScope { TimeStamp1 = new DateTime(cropZoneDto.CropYear, 1, 1), TimeStamp2 = new DateTime(cropZoneDto.CropYear, 12, 31) } } }; var cropZoneUniqueId = _uniqueIdFactory.CreateInt(cropZoneDto.Id); var cropZoneCompoundId = cropZone.Id; cropZone.Id.UniqueIds.Add(cropZoneUniqueId); var selfLink = new ReferenceLink { Id = cropZoneCompoundId, Rel = Relationships.Self, Link = $"/CropZones/{cropZoneUniqueId.Source}/{cropZoneUniqueId.Id}" }; var fieldUniqueId = _uniqueIdFactory.CreateGuid(cropZoneDto.FieldUid); var fieldCompoundId = fieldUniqueId.ToCompoundIdentifier(); var fieldLink = new ReferenceLink { Id = fieldCompoundId, Rel = typeof(Field).Name.ToLower(), Link = $"/Fields/{fieldUniqueId.Source}/{fieldUniqueId.Id}" }; cropZone.FieldId = fieldCompoundId.ReferenceId; // NOTE: Skipped BoundingRegion to not introduce Spatial Dependencies in conversion. var cropZoneEnvelope = new ModelEnvelope <CropZone>(cropZone); cropZoneEnvelope.Links.Add(selfLink); cropZoneEnvelope.Links.Add(fieldLink); return(cropZoneEnvelope); }
public void WHEN_Convert_GIVEN_Valid_Dto_THEN_Get_Valid_Result_With_All_Links() { var uniqueIdFactory = SampleObjectsIdFactory.Instance; var cropZoneDto = new CropZoneDto { Id = 123, FieldUid = Guid.NewGuid(), CropYear = DateTime.Now.Year, Name = $"{DateTime.Now.Year} Corn" }; var converter = new CropZoneDtoConverter(uniqueIdFactory); var cropZone = converter.Convert(cropZoneDto); Assert.Equal(cropZoneDto.Id.ToString(), cropZone.Object.Id.UniqueIds.First().Id); Assert.Equal(cropZoneDto.Name, cropZone.Object.Description); var selfLink = cropZone.Links.Single(l => l.Rel == Relationships.Self); Assert.Equal($"/CropZones/{uniqueIdFactory.UniqueIdSource}/{cropZoneDto.Id}", selfLink.Link); Assert.Equal(selfLink.Id.ReferenceId, cropZone.Object.Id.ReferenceId); var fieldsLink = cropZone.Links.Single(l => l.Rel == typeof(Field).ObjectRel()); Assert.Equal($"/Fields/{uniqueIdFactory.UniqueIdSource}/{cropZoneDto.FieldUid}", fieldsLink.Link); }