sharee.bike-App/TINKLib/Model/Connector/Updater/RentalDescriptionFactory.cs
2023-06-06 12:05:48 +02:00

61 lines
2.2 KiB
C#

using System.Collections.Generic;
using System.Linq;
using TINK.Model.Bikes.BikeInfoNS;
namespace TINK.Model.Connector.Updater
{
public static class RentalDescriptionFactory
{
/// <summary>
/// Creates rental description object from JSON- tariff description object.
/// </summary>
/// <param name="rentalDesciption">Source JSON object.</param>
/// <returns>Tariff description object.</returns>
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<string, RentalDescription.TariffElement>();
// 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<string, RentalDescription.InfoElement>();
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;
}
}
}