public IEnumerable<AppModel> GetApps(CIURequest req, int osID) { // CUIRequest Validation if (string.IsNullOrEmpty(req.sortFieldName) == false) { Type t = typeof(AppModel); PropertyInfo pi = t.GetProperty(req.sortFieldName); if (pi == null) { throw new Exception("Invalid Request Property Name."); } } if (string.IsNullOrEmpty(req.sortDirection) == false && (req.sortDirection.ToUpper() != "ASC" && req.sortDirection.ToUpper() != "DESC")) { throw new Exception("Invalid Sort Direction."); } // OK, vaidation passed, let's get on with it... int totalAppCount = 0; OS os = null; IEnumerable<AppModel> appmodels = null; using (CanIUpdateEFDB db = new CanIUpdateEFDB()) { os = db.OSs.Where(o => o.OSId == osID).FirstOrDefault<OS>(); if (os == null) throw new Exception("Missing or invalid OS id."); DateTime osReleaseDate = os.ReleaseDate; DateTime osExpirationDate = (os.ExpirationDate.HasValue == true) ? os.ExpirationDate.Value : DateTime.Now; appmodels = (from a in db.Apps join o in db.OSs on a.MinOSVersionID equals o.OSId select new AppModel() { AppId = a.AppId, Desc = a.Desc, LatestVersion = a.LatestVersion, MinOSVersion = o.Version, MinOSVersionID = a.MinOSVersionID, Name = a.Name, ReleaseDate = a.ReleaseDate }).ToList<AppModel>(); if (appmodels != null) totalAppCount = appmodels.Count(); # region Search // Limit to App Name Search if (string.IsNullOrEmpty(req.searchTerm) == false) appmodels = appmodels.Where(a => a.Name.ToUpper().Contains(req.searchTerm.ToUpper())); # endregion # region Skip & Take if (req.skip == 0 && req.take == 0 || req.skip > 0 && req.take == 0) req.take = totalAppCount; appmodels = appmodels.Skip(req.skip).Take(req.take); # endregion # region Sort if (string.IsNullOrEmpty(req.sortFieldName) == true && string.IsNullOrEmpty(req.sortDirection) == true) { // Default- no sort parameters provided appmodels = appmodels.OrderBy(a => a.Name); } if (string.IsNullOrEmpty(req.sortFieldName) == true && string.IsNullOrEmpty(req.sortDirection) == false) { if (req.sortDirection.ToUpper() == "ASC") { // Sort By App Name ASC appmodels = appmodels.OrderBy(a => a.Name); } else { // Sort by App Name DESC appmodels = appmodels.OrderByDescending(a => a.Name); } } if (string.IsNullOrEmpty(req.sortFieldName) == false && string.IsNullOrEmpty(req.sortDirection) == true) { // Sort by Field ASC appmodels = appmodels.OrderBy(a => a.GetType().GetProperty(req.sortFieldName).GetValue(a, null)); // ThenBy is secondary sort when we can sort by fields other than App Name //.ThenBy(a => a.GetType().GetProperty(a.Name).GetValue(a, null)); } if (string.IsNullOrEmpty(req.sortFieldName) == false && string.IsNullOrEmpty(req.sortDirection) == false) { // Sort by Field and Direction if (req.sortDirection.ToUpper() == "ASC") { appmodels = appmodels.OrderBy(a => a.GetType().GetProperty(req.sortFieldName).GetValue(a, null)) .ThenBy(a => a.GetType().GetProperty(a.Name).GetValue(a, null)); } else { appmodels = appmodels.OrderByDescending(a => a.GetType().GetProperty(req.sortFieldName).GetValue(a, null)) .ThenByDescending(a => a.GetType().GetProperty("Name").GetValue(a, null)); } } # endregion foreach(AppModel appm in appmodels) { List<Bug> bugs = db.Bugs.Where(b => b.AppId == appm.AppId).ToList<Bug>(); appm.AppStatus = GetAppStatus(bugs, osReleaseDate, osExpirationDate); } } return appmodels; }
private static IEnumerable<AppModel> GetAppsByReq(CIURequest req, int osID) { return SiteData.Apps.GetApps(req, osID); }