sharee.bike-App/TINKLib/Model/Connector/Updater/RentalDescriptionFactory.cs

62 lines
2.2 KiB
C#
Raw Normal View History

2023-06-06 12:00:24 +02:00
using System.Collections.Generic;
2022-08-30 15:42:25 +02:00
using System.Linq;
2023-06-06 12:00:24 +02:00
using TINK.Model.Bikes.BikeInfoNS;
2022-08-30 15:42:25 +02:00
namespace TINK.Model.Connector.Updater
{
2022-09-06 16:08:19 +02:00
public static class RentalDescriptionFactory
{
/// <summary>
2023-06-06 12:00:24 +02:00
/// Creates rental description object from JSON- tariff description object.
2022-09-06 16:08:19 +02:00
/// </summary>
/// <param name="rentalDesciption">Source JSON object.</param>
/// <returns>Tariff description object.</returns>
2022-08-30 15:42:25 +02:00
2023-06-06 12:00:24 +02:00
public static RentalDescription Create(this Repository.Response.RentalDescription rentalDesciption)
2022-09-06 16:08:19 +02:00
{
2023-06-06 12:00:24 +02:00
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,
};
2022-08-30 15:42:25 +02:00
2023-06-06 12:00:24 +02:00
RentalDescription.InfoElement CreateInfoElement(string[] elementValue) =>
new RentalDescription.InfoElement
2022-09-06 16:08:19 +02:00
{
Key = elementValue != null && elementValue.Length > 0 ? elementValue[0] : string.Empty,
Value = elementValue != null && elementValue.Length > 1 ? elementValue[1] : string.Empty,
};
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
// Read tariff elements.
var tarifEntries = rentalDesciption?.tarif_elements != null
? rentalDesciption.tarif_elements.Select(x => new
{
2023-06-06 12:00:24 +02:00
x.Key,
2022-09-06 16:08:19 +02:00
Value = CreateTarifEntry(x.Value)
}).ToLookup(x => x.Key, x => x.Value).ToDictionary(x => x.Key, x => x.First())
2023-06-06 12:00:24 +02:00
: new Dictionary<string, RentalDescription.TariffElement>();
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
// Read info elements.
var InfoEntries = rentalDesciption?.rental_info != null
? rentalDesciption.rental_info.Select(x => new
{
2023-06-06 12:00:24 +02:00
x.Key,
2022-09-06 16:08:19 +02:00
Value = CreateInfoElement(x.Value)
}).ToLookup(x => x.Key, x => x.Value).ToDictionary(x => x.Key, x => x.First())
2023-06-06 12:00:24 +02:00
: new Dictionary<string, RentalDescription.InfoElement>();
2022-08-30 15:42:25 +02:00
2023-06-06 12:00:24 +02:00
var bike = new RentalDescription
2022-09-06 16:08:19 +02:00
{
Name = rentalDesciption?.name ?? string.Empty,
Id = int.TryParse(rentalDesciption?.id ?? string.Empty, out int number) ? number : (int?)null,
2023-06-06 12:00:24 +02:00
MaxReservationTimeSpan = rentalDesciption.GetMaxReservationTimeSpan(),
2022-09-06 16:08:19 +02:00
TariffEntries = tarifEntries,
InfoEntries = InfoEntries
};
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
return bike;
}
}
2022-08-30 15:42:25 +02:00
}