using System.Collections.Generic; using System.Linq; using TINK.Model.Bikes.BikeInfoNS; namespace TINK.Model.Connector.Updater { public static class RentalDescriptionFactory { /// /// Creates rental description object from JSON- tariff description object. /// /// Source JSON object. /// Tariff description object. public static RentalDescription Create(this Repository.Response.RentalDescription rentalDesciption) { RentalDescription.TariffElement CreateTarifEntry(string[] elementValue) => new RentalDescription.TariffElement { Description = elementValue != null && elementValue.Length > 0 ? elementValue[0] : string.Empty, Value = elementValue != null && elementValue.Length > 1 ? elementValue[1] : string.Empty, }; RentalDescription.InfoElement CreateInfoElement(string[] elementValue) => new RentalDescription.InfoElement { Key = elementValue != null && elementValue.Length > 0 ? elementValue[0] : string.Empty, Value = elementValue != null && elementValue.Length > 1 ? elementValue[1] : string.Empty, }; // Read tariff elements. var tarifEntries = rentalDesciption?.tarif_elements != null ? rentalDesciption.tarif_elements.Select(x => new { x.Key, Value = CreateTarifEntry(x.Value) }).ToLookup(x => x.Key, x => x.Value).ToDictionary(x => x.Key, x => x.First()) : new Dictionary(); // Read info elements. var InfoEntries = rentalDesciption?.rental_info != null ? rentalDesciption.rental_info.Select(x => new { x.Key, Value = CreateInfoElement(x.Value) }).ToLookup(x => x.Key, x => x.Value).ToDictionary(x => x.Key, x => x.First()) : new Dictionary(); var bike = new RentalDescription { Name = rentalDesciption?.name ?? string.Empty, Id = int.TryParse(rentalDesciption?.id ?? string.Empty, out int number) ? number : (int?)null, MaxReservationTimeSpan = rentalDesciption.GetMaxReservationTimeSpan(), TariffEntries = tarifEntries, InfoEntries = InfoEntries }; return bike; } } }