Skip to content

Instantly share code, notes, and snippets.

@jmahc
Created April 27, 2016 17:27
Show Gist options
  • Save jmahc/d584bcc26d15b4defad655a973889786 to your computer and use it in GitHub Desktop.
Save jmahc/d584bcc26d15b4defad655a973889786 to your computer and use it in GitHub Desktop.
Gets product information from a json file, taking into account the host (serves multiple domains to get different content based on the path)
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Helpers;
using System.Web.Hosting;
using comSite.Models.Products;
namespace comSite.DataProviders
{
public class ProductsDataProvider
{
public static readonly List<ProductCollectionModel> Collection;
public static readonly List<ProductViewModel> Details;
public static readonly string SiteName;
#region Constructor
static ProductsDataProvider()
{
var name = MvcApplication.GetSiteName();
var path = Helpers.HelperMethods.GetShortSite(name);
/* PRODUCT DETAILS */
var detailsDataPath = HostingEnvironment.MapPath(string.Format("~/Content/{0}/Data/details.json", path));
if (!File.Exists(detailsDataPath))
{
throw new FileNotFoundException("Product Details data file is missing", detailsDataPath);
}
var productDetails = Json.Decode<DetailsJsonModel>(File.ReadAllText(detailsDataPath));
Details = productDetails.Details.Select(p => new ProductViewModel
{
Id = p.Id,
Name = p.Name,
ProductName = p.ProductName,
Category = p.Category,
Image = p.Image,
Features = p.Features,
Disclaimer = p.Disclaimer
}).ToList();
/* PRODUCT COLLECTIONS */
var collectionsDataPath = HostingEnvironment.MapPath("~/Content/CubCadet/Data/collections.json");
if (!File.Exists(collectionsDataPath))
{
throw new FileNotFoundException("Product Details data file is missing", collectionsDataPath);
}
var productCollections = Json.Decode<CollectionsJsonModel>(File.ReadAllText(collectionsDataPath));
Collection = productCollections.Collections.Select((p) => new ProductCollectionModel
{
Id = p.Id,
Title = p.CategoryName,
Location = p.Location,
Products = p.ProductCollection.Select(x => x.ToProduct()).OrderBy(x => x.SortOrder).ToList()
}).ToList();
}
#endregion
#region Methods
public IList<ProductCollectionModel> GetCollection(string page = null)
{
// Returns a collection of products for a specific page
return Collection.Where(x => x.Location == page).ToList();
}
public ProductViewModel GetDetails(string productName = null)
{
// Returns the product for the product details page
return Details.First(o=>o.ProductName == productName);
}
#endregion
#region Inner types
// Product Details
private class DetailsJsonModel
{
public List<ProductDetailJsonModel> Details { get; set; }
}
public class ProductDetailJsonModel
{
public int Id { get; set; }
public string Name { get; set; }
public string ProductName { get; set; }
public string Category { get; set; }
public string Image { get; set; }
public List<string> Features { get; set; }
public string Disclaimer { get; set; }
}
// Product Collections
private class CollectionsJsonModel
{
public List<ProductCollectionJsonModel> Collections { get; set; }
}
public class ProductCollectionJsonModel
{
public int Id { get; set; }
public string Location { get; set; }
public string CategoryName { get; set; }
public List<ProductItemJsonModel> ProductCollection { get; set; }
}
public class ProductItemJsonModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public string ImageName { get; set; }
public string PageName { get; set; }
public int SortOrder { get; set; }
public ProductModel ToProduct()
{
var res = new ProductModel
{
Id = Id,
Name = Name,
Type = Type,
ImageName = ImageName,
PageName = PageName,
SortOrder = SortOrder
};
return res;
}
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment