Files
Disco/Disco.Data/Configuration/Modules/OrganisationAddressesConfiguration.cs
T
Gary Sharp fb6067afc3 Update: Configuration Optimisation and Caching
Loads entire configuration at start-up (rather than scope-based
loading). Deserialization occurs once, with the resulting value cached
and replayed if the requested type matches.
2014-05-07 22:45:59 +10:00

77 lines
2.3 KiB
C#

using Disco.Data.Repository;
using Disco.Models.BI.Config;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
namespace Disco.Data.Configuration.Modules
{
public class OrganisationAddressesConfiguration : ConfigurationBase
{
private const string scope = "OrganisationAddresses";
public OrganisationAddressesConfiguration(DiscoDataContext Database) : base(Database) { }
public override string Scope { get { return scope; } }
public OrganisationAddress GetAddress(int Id)
{
return this.Get<OrganisationAddress>(null, Id.ToString());
}
public OrganisationAddress SetAddress(OrganisationAddress Address)
{
if (!Address.Id.HasValue)
{
Address.Id = NextOrganisationAddressId;
}
this.Set(Address, Address.Id.ToString());
return Address;
}
public void RemoveAddress(int Id)
{
// Remove Configuration Item
this.RemoveItem(Id.ToString());
}
public List<OrganisationAddress> Addresses
{
get
{
return this.ItemKeys.Select(key => this.Get<OrganisationAddress>(null, key)).ToList();
}
}
private int NextOrganisationAddressId
{
get
{
int nextId = 0;
while (true)
{
if (this.Get<string>(null, nextId.ToString()) == null)
break;
nextId++;
}
return nextId;
}
}
internal static void MigrateDatabase(DiscoDataContext Database)
{
// Migrate all organisation addresses to JSON
if (Database.ConfigurationItems.Count(i => i.Scope == scope && !i.Value.StartsWith("{")) > 0)
{
var items = Database.ConfigurationItems.Where(i => i.Scope == scope && !i.Value.StartsWith("{")).ToList();
items.ForEach(i =>
{
i.Value = JsonConvert.SerializeObject(OrganisationAddress.FromConfigurationEntry(int.Parse(i.Key), i.Value));
});
Database.SaveChanges();
}
}
}
}