public AppModel PutApp(AppModel appm) { // Validation int tryInt = -1; if (int.TryParse(appm.AppId.ToString(), out tryInt) == false) throw new Exception("Missing or invalid App ID."); appm.LatestVersion = Utils.CleanString(appm.LatestVersion); if (string.IsNullOrEmpty(appm.LatestVersion) == true) throw new Exception("Missing or invalid Latest Version."); appm.Name = Utils.CleanString(appm.Name); if (string.IsNullOrEmpty(appm.Name) == true) throw new Exception("Missing or invalid app Name."); DateTime tryDate = DateTime.MinValue; if (appm.ReleaseDate == null || DateTime.TryParse(appm.ReleaseDate.ToShortDateString(), out tryDate) == false) throw new Exception("Missing or invalid Release Date."); if (int.TryParse(appm.MinOSVersionID.ToString(), out tryInt) == false) throw new Exception("Missing or invalid MinOSVersionID"); appm.MinOSVersion = Utils.CleanString(appm.MinOSVersion); if (string.IsNullOrEmpty(appm.MinOSVersion) == true) throw new Exception("Missing or invalid MinOSVersion"); appm.Desc = Utils.CleanString(appm.Desc); // Whew, made it past the validation... App tmpApp = null; AppModel retModel = new AppModel(); using (CanIUpdateEFDB db = new CanIUpdateEFDB()) { tmpApp = db.Apps.Where(a => a.AppId == appm.AppId).FirstOrDefault<App>(); if (tmpApp == null) { // INSERT tmpApp = new App(); tmpApp.Desc = retModel.Desc = appm.Desc; tmpApp.LatestVersion = retModel.LatestVersion = appm.LatestVersion; tmpApp.MinOSVersionID = retModel.MinOSVersionID = appm.MinOSVersionID; tmpApp.Name = retModel.Name = appm.Name; tmpApp.ReleaseDate = retModel.ReleaseDate = appm.ReleaseDate; db.Apps.Add(tmpApp); } else { // UPDATE tmpApp.Desc = retModel.Desc = appm.Desc; tmpApp.LatestVersion = retModel.LatestVersion = appm.LatestVersion; tmpApp.MinOSVersionID = retModel.MinOSVersionID = appm.MinOSVersionID; tmpApp.Name = retModel.Name = appm.Name; tmpApp.ReleaseDate = retModel.ReleaseDate = appm.ReleaseDate; } db.SaveChanges(); // Add mising field(s) data to model OS os = db.OSs.Where(o => o.OSId == tmpApp.MinOSVersionID).FirstOrDefault<OS>(); if (os != null) retModel.MinOSVersion = os.Version; retModel.AppId = tmpApp.AppId; } return retModel; }
public AppModel PutApp(AppModel appm) { return SiteData.Apps.PutApp(appm); }