Skip to content

Instantly share code, notes, and snippets.

@pynej

pynej/School.cs Secret

Last active December 29, 2016 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pynej/576b138748daef2925b6526b7d56bca8 to your computer and use it in GitHub Desktop.
Save pynej/576b138748daef2925b6526b7d56bca8 to your computer and use it in GitHub Desktop.
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Core;
using Umbraco.Core.Services;
using Umbraco.Web;
namespace Project.Models
{
public class School
{
private JToken _self;
public School(JToken self)
{
_self = self;
}
public int Id
{
get
{
return (int)_self["school"];
}
}
private string _Name;
public string Name
{
get
{
if (_Name == null)
_Name = UmbracoContext.Current.ContentCache.GetById(Id).Name;
return _Name;
}
}
public int Level
{
get
{
return (int)_self["level"];
}
}
public override string ToString()
{
return Name + " " + Level;
}
}
}
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using Mage_Wars_Spellbook_Builder.Helpers;
namespace Project.Models
{
public class Schools : IEnumerable<School>
{
private JArray _self;
public Schools(JArray self)
{
_self = self;
}
public new static Schools Parse(string json)
{
return new Schools(JArray.Parse(json));
}
public IEnumerator<School> GetEnumerator()
{
return (_self == null ? new School[] { } : _self.Children().Select(s => s.As<School>())).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return (_self == null ? new School[] { } : _self.Children().Select(s => s.As<School>())).GetEnumerator();
}
public bool HasSchool(string school)
{
return this.Where(s => s.Name == school).Count() > 0;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
namespace Project.Models.PropertyConverters
{
[PropertyValueType(typeof(Schools))]
[PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)]
public class SchoolsPropertyConverter : PropertyValueConverterBase
{
private readonly string PropertyEditorAlias = "NestedDataType";
private readonly int[] DataTypeId = { 1096 };
public override bool IsConverter(PublishedPropertyType propertyType)
{
return propertyType.PropertyEditorAlias.Equals(PropertyEditorAlias) && DataTypeId.Contains(propertyType.DataTypeId);
}
public override object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
{
if (source == null)
{
return null;
}
try
{
return Schools.Parse((string)source);
}
catch
{
return null;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment