public IActionResult Add([FromBody] CruiseScanGroup cruiseScanGroup) { if (cruiseScanGroup == null) { return(BadRequest("CruiseScanGroup object can not be null!")); } if (ExistsName(cruiseScanGroup.GroupName, cruiseScanGroup.CameraId, cruiseScanGroup.CruiseScanGroupId)) { return(BadRequest("CruiseScanGroup name has been used!")); } using (var db = new AllInOneContext.AllInOneContext()) { try { db.CruiseScanGroup.Add(cruiseScanGroup); db.SaveChanges(); return(CreatedAtAction("", cruiseScanGroup)); } catch (DbUpdateException dbEx) { _logger.LogError("添加巡航异常:Message:{0}\r\n,StackTrace:{1}", dbEx.Message, dbEx.StackTrace); return(BadRequest(new ApplicationException { ErrorCode = "DBUpdate", ErrorMessage = "数据保存异常:" + dbEx.Message })); } catch (Exception ex) { _logger.LogError("添加巡航异常:Message:{0}\r\n,StackTrace:{1}", ex.Message, ex.StackTrace); return(BadRequest(new ApplicationException { ErrorCode = "Unknown", ErrorMessage = ex.Message })); } } }
public IActionResult Remove(Guid id) { using (var db = new AllInOneContext.AllInOneContext()) { try { CruiseScanGroup deleteObj = db.CruiseScanGroup.FirstOrDefault(t => t.CruiseScanGroupId.Equals(id)); if (deleteObj == null) { return(NotFound()); } db.CruiseScanGroup.Remove(deleteObj); db.SaveChanges(); return(NoContent()); } catch (DbUpdateException dbEx) { _logger.LogError("删除巡航异常:Message:{0}\r\n,StackTrace:{1}", dbEx.Message, dbEx.StackTrace); return(BadRequest(new ApplicationException { ErrorCode = "DBUpdate", ErrorMessage = "数据保存异常:" + dbEx.Message })); } catch (Exception ex) { _logger.LogError("删除巡航异常:Message:{0}\r\n,StackTrace:{1}", ex.Message, ex.StackTrace); return(BadRequest(new ApplicationException { ErrorCode = "Unknown", ErrorMessage = ex.Message })); } } }
public IActionResult Update([FromBody] CruiseScanGroup cruiseScanGroup) { if (cruiseScanGroup == null) { return(BadRequest("CruiseScanGroup object can not be null!")); } if (ExistsName(cruiseScanGroup.GroupName, cruiseScanGroup.CameraId, cruiseScanGroup.CruiseScanGroupId)) { return(BadRequest("CruiseScanGroup name has been used!")); } using (var db = new AllInOneContext.AllInOneContext()) { try { using (var tran = db.Database.BeginTransaction()) { //先删除,再更新 var dbCruiseScanGroup = db.CruiseScanGroup.Include(t => t.PresetSites).FirstOrDefault(t => t.CruiseScanGroupId.Equals(cruiseScanGroup.CruiseScanGroupId)); if (dbCruiseScanGroup.PresetSites != null) { db.Set <CruiseScanGroupPresetSite>().RemoveRange(dbCruiseScanGroup.PresetSites); } db.SaveChanges(); foreach (CruiseScanGroupPresetSite ps in cruiseScanGroup.PresetSites) { dbCruiseScanGroup.PresetSites.Add(ps); } //dbCruiseScanGroup.PresetSites = cruiseScanGroup.PresetSites; db.CruiseScanGroup.Update(cruiseScanGroup); db.SaveChanges(); tran.Commit(); return(NoContent()); } } catch (DbUpdateException dbEx) { _logger.LogError("更新巡航异常:Message:{0}\r\n,StackTrace:{1}", dbEx.Message, dbEx.StackTrace); return(BadRequest(new ApplicationException { ErrorCode = "DBUpdate", ErrorMessage = "数据保存异常:" + dbEx.Message })); } catch (Exception ex) { _logger.LogError("更新巡航异常:Message:{0}\r\n,StackTrace:{1}", ex.Message, ex.StackTrace); return(BadRequest(new ApplicationException { ErrorCode = "Unknown", ErrorMessage = ex.Message })); } } }