diff --git a/NucuCar.Domain/Utilities/FirebaseRestTranslator.cs b/NucuCar.Domain/Utilities/FirebaseRestTranslator.cs index 943a4d4..d371a99 100644 --- a/NucuCar.Domain/Utilities/FirebaseRestTranslator.cs +++ b/NucuCar.Domain/Utilities/FirebaseRestTranslator.cs @@ -8,6 +8,42 @@ namespace NucuCar.Domain.Utilities /// public static class FirebaseRestTranslator { + public class ReferenceValue + { + private readonly string _value; + + public ReferenceValue(string value) + { + _value = value; + } + + public override string ToString() + { + return _value; + } + } + + public class GeoPointValue + { + private readonly double _latitude; + private readonly double _longitude; + + public GeoPointValue(double latitude, double longitude) + { + _latitude = latitude; + _longitude = longitude; + } + + public Dictionary ToValue() + { + return new Dictionary + { + ["latitude"] = _latitude, + ["longitude"] = _longitude + }; + } + } + public static Dictionary Translate(string name, Dictionary dict) { return BuildRoot(name, dict); @@ -52,10 +88,26 @@ namespace NucuCar.Domain.Utilities { return BuildBool(v); } + case null: + { + return BuildNull(null); + } + case byte[] v: + { + return BuildBytes(v); + } case DateTime v: { return BuildTimestamp(v); } + case ReferenceValue v: + { + return BuildReference(v); + } + case GeoPointValue v: + { + return BuildGeoPoint(v); + } case List> v: { return BuildArray(v); @@ -74,9 +126,11 @@ namespace NucuCar.Domain.Utilities { return BuildInteger((int) value); } + break; } } + throw new ArgumentException($"Can't build leaf! Unknown type for: {value}"); } @@ -113,6 +167,27 @@ namespace NucuCar.Domain.Utilities return BuildSimpleValue("booleanValue", value); } + private static Dictionary BuildNull(object value) + { + return BuildSimpleValue("nullValue", value); + } + + private static Dictionary BuildBytes(byte[] value) + { + return BuildSimpleValue("bytesValue", Convert.ToBase64String(value)); + } + + private static Dictionary BuildReference(ReferenceValue value) + { + return BuildSimpleValue("referenceValue", value.ToString()); + } + + private static Dictionary BuildGeoPoint(GeoPointValue value) + { + return BuildSimpleValue("geoPointValue", value.ToValue()); + } + + private static Dictionary BuildArray(List> array) { var values = new List>(); diff --git a/NucuCar.UnitTests/NucuCar.Domain.Tests/Utilities/FirebaseRestTranslatorTests.cs b/NucuCar.UnitTests/NucuCar.Domain.Tests/Utilities/FirebaseRestTranslatorTests.cs index 8b544d2..94565f9 100644 --- a/NucuCar.UnitTests/NucuCar.Domain.Tests/Utilities/FirebaseRestTranslatorTests.cs +++ b/NucuCar.UnitTests/NucuCar.Domain.Tests/Utilities/FirebaseRestTranslatorTests.cs @@ -202,5 +202,56 @@ namespace NucuCar.UnitTests.NucuCar.Domain.Tests.Utilities Assert.Equal(expectedJson, actualJson); } + [Fact] + public void Test_FirebaseTranslator_NullValue() + { + var data = new Dictionary() + { + ["myKey"] = null + }; + var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"nullValue\":null}}}"; + var result = FirebaseRestTranslator.Translate("Test_FirebaseTranslator", data); + var actualJson = JsonConvert.SerializeObject(result); + Assert.Equal(expectedJson, actualJson); + } + + [Fact] + public void Test_FirebaseTranslator_BytesValue() + { + var data = new Dictionary() + { + ["myKey"] = new byte[] {97, 98, 99, 100, 101, 102, 103, 104, 105 } + }; + var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"bytesValue\":\"YWJjZGVmZ2hp\"}}}"; + var result = FirebaseRestTranslator.Translate("Test_FirebaseTranslator", data); + var actualJson = JsonConvert.SerializeObject(result); + Assert.Equal(expectedJson, actualJson); + } + + [Fact] + public void Test_FirebaseTranslator_ReferenceValue() + { + var data = new Dictionary() + { + ["myKey"] = new FirebaseRestTranslator.ReferenceValue("test") + }; + var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"referenceValue\":\"test\"}}}"; + var result = FirebaseRestTranslator.Translate("Test_FirebaseTranslator", data); + var actualJson = JsonConvert.SerializeObject(result); + Assert.Equal(expectedJson, actualJson); + } + + [Fact] + public void Test_FirebaseTranslator_GeoPointvalue() + { + var data = new Dictionary() + { + ["myKey"] = new FirebaseRestTranslator.GeoPointValue(10, 22) + }; + var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"geoPointValue\":{\"latitude\":10.0,\"longitude\":22.0}}}}"; + var result = FirebaseRestTranslator.Translate("Test_FirebaseTranslator", data); + var actualJson = JsonConvert.SerializeObject(result); + Assert.Equal(expectedJson, actualJson); + } } } \ No newline at end of file