public JsonResult Create(CHGSiteModel model) { if (!HasContextRole(new string[] { "CRO" })) { return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "Unauthorized." })); } var ltachId = _dataContext.ServiceTypes.Where(p => p.Name == "LTACH" && p.Deleted == false).Single().ServiceTypeId; if (ModelState.IsValid) { using (var transaction = _dataContext.Database.BeginTransaction()) { using (var changeSet = new ChangeSetTracking(_dataContext)) { if (model.ServiceTypeId == ltachId && (model.PreScreenUpdateTypeId == null || model.PreScreenUpdateTypeId <= 0)) { return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "One or more fields failed validation." })); } if (model.ServiceTypeId != ltachId) { model.PreScreenUpdateTypeId = null; } if (_dataContext.CHGSites.Where(p => p.FullName == model.FullName && p.Deleted == false).Count() <= 0) { CHGSite entity = new CHGSite() { FullName = model.FullName, RegionTypeId = model.RegionTypeId, ShortName = model.ShortName, Address = model.Address, BedCount = model.BedCount, OperationalICUBedCount = model.OperationalICUBedCount, PreScreenUpdateTypeId = model.PreScreenUpdateTypeId, ServiceTypeId = model.ServiceTypeId }; _dataContext.CHGSites.Add(entity); _dataContext.SaveChanges(); transaction.Commit(); return(Json(new { Status = Constant.RESPONSE_OK, Description = "Site created successfully." })); } else { return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "The site with the same full name already exists. Please enter a different name." })); } } } } return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "One or more fields failed validation." })); }
public JsonResult Delete(long?id) { if (!HasContextRole(new string[] { "CRO" })) { return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "Unauthorized." })); } using (var transaction = _dataContext.Database.BeginTransaction()) { using (var changeSetTracking = new ChangeSetTracking(_dataContext)) { var entity = _dataContext.CHGSites.Where(p => p.CHGSiteId == id).SingleOrDefault(); if (entity != null) { entity.Deleted = true; _dataContext.SaveChanges(); transaction.Commit(); return(Json(new { Status = Constant.RESPONSE_OK })); } } } return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "Entity not found!" })); }
public JsonResult Edit(long?id, CHGSiteModel model) { if (!HasContextRole(new string[] { "CRO" })) { return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "Unauthorized." })); } if (id == null || id == 0) { return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "Invalid ID Specification." })); } var ltachId = _dataContext.ServiceTypes.Where(p => p.Name == "LTACH" && p.Deleted == false).Single().ServiceTypeId; if (ModelState.IsValid) { using (var transaction = _dataContext.Database.BeginTransaction()) { using (var changeSetTracking = new ChangeSetTracking(_dataContext)) { if (model.ServiceTypeId == ltachId && (model.PreScreenUpdateTypeId == null || model.PreScreenUpdateTypeId <= 0)) { return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "One or more fields failed validation." })); } if (model.ServiceTypeId != ltachId) { model.PreScreenUpdateTypeId = null; } if (_dataContext.CHGSites.Where(p => p.FullName == model.FullName && p.CHGSiteId != id && p.Deleted == false).Count() <= 0) { var entity = _dataContext.CHGSites.Where(p => p.CHGSiteId == id && p.Deleted == false).SingleOrDefault(); if (entity == null) { return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "CHG site not found. Invalid CHG site." })); } entity.FullName = model.FullName; entity.ShortName = model.ShortName; entity.RegionTypeId = model.RegionTypeId; entity.ServiceTypeId = model.ServiceTypeId; entity.Address = model.Address; entity.OperationalICUBedCount = model.OperationalICUBedCount; entity.BedCount = model.BedCount; entity.PreScreenUpdateTypeId = model.PreScreenUpdateTypeId; _dataContext.SaveChanges(); transaction.Commit(); return(Json(new { Status = Constant.RESPONSE_OK, Description = "Site saved successfully." })); } else { return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "The site with the same full name already exists. Please enter a different name." })); } } } } return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "One or more fields failed validation." })); }