diff --git a/AMESCoreStudio.Web/Controllers/BASController.cs b/AMESCoreStudio.Web/Controllers/BASController.cs index 3f9fbd1e..9f9676e5 100644 --- a/AMESCoreStudio.Web/Controllers/BASController.cs +++ b/AMESCoreStudio.Web/Controllers/BASController.cs @@ -1084,7 +1084,7 @@ namespace AMESCoreStudio.Web.Controllers result = await _basApi.PutFlowRule(model.FlowRuleID, JsonConvert.SerializeObject(model)); } - if (!result.Success) + if (result.Success) { var _msg = model.FlowRuleID == 0 ? "添加成功!" : "修改成功!"; return RedirectToAction("Refresh", "Home", new { msg = _msg }); @@ -1226,7 +1226,7 @@ namespace AMESCoreStudio.Web.Controllers result = await _basApi.PutRuleStation(model.RuleStationID, JsonConvert.SerializeObject(model)); } - if (!result.Success) + if (result.Success) { var _msg = model.RuleStationID == 0 ? "添加成功!" : "修改成功!"; return RedirectToAction("Refresh", "Home", new { msg = _msg }); @@ -1378,7 +1378,7 @@ namespace AMESCoreStudio.Web.Controllers result = await _basApi.PutRules(model.RuleStationID, JsonConvert.SerializeObject(model)); } - if (!result.Success) + if (result.Success) { var _msg = model.CreateDate == System.DateTime.MinValue ? "添加成功!" : "修改成功!"; return RedirectToAction("Refresh", "Home", new { msg = _msg }); diff --git a/AMESCoreStudio.WebApi/Controllers/BAS/ClassInfoesController.cs b/AMESCoreStudio.WebApi/Controllers/BAS/ClassInfoesController.cs index 72ee42fc..d464452d 100644 --- a/AMESCoreStudio.WebApi/Controllers/BAS/ClassInfoesController.cs +++ b/AMESCoreStudio.WebApi/Controllers/BAS/ClassInfoesController.cs @@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using AMESCoreStudio.WebApi; using AMESCoreStudio.WebApi.Models.BAS; +using AMESCoreStudio.CommonTools.Result; namespace AMESCoreStudio.WebApi.Controllers.BAS { @@ -64,11 +65,6 @@ namespace AMESCoreStudio.WebApi.Controllers.BAS IQueryable q = _context.ClassInfoes; q = q.Where(p => p.ClassID.Equals(id)); var classInfo = await q.ToListAsync(); - //foreach (var data in classInfo) - //{ - // //data.Unit = _context.FactoryUnits.Find(data.UnitNo); - //} - if (classInfo == null) { @@ -122,34 +118,33 @@ namespace AMESCoreStudio.WebApi.Controllers.BAS // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://go.microsoft.com/fwlink/?linkid=2123754. [HttpPut("{id}")] - public async Task> PutClassInfo(int id, [FromBody] ClassInfo classInfo) + public async Task> PutClassInfo(int id, [FromBody] ClassInfo classInfo) { - if (id != classInfo.ClassID) - { - return BadRequest(); - } + ResultModel result = new ResultModel(); classInfo.UpdateDate = System.DateTime.Now; classInfo.StatusNo = "A"; + if (id != classInfo.ClassID) + { + result.Success = false; + result.Msg = "序號錯誤"; + return result; + } _context.Entry(classInfo).State = EntityState.Modified; try { await _context.SaveChangesAsync(); + result.Success = true; + result.Msg = "OK"; } - catch (DbUpdateConcurrencyException) + catch (Exception ex) { - if (!ClassInfoExists(id)) - { - return NotFound(); - } - else - { - throw; - } + result.Success = false; + result.Msg = ex.InnerException.Message; } + return result; - return classInfo; } /// @@ -161,18 +156,30 @@ namespace AMESCoreStudio.WebApi.Controllers.BAS // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://go.microsoft.com/fwlink/?linkid=2123754. [HttpPost] - public async Task> PostClassInfo(ClassInfo classInfo) + public async Task> PostClassInfo(ClassInfo classInfo) { + ResultModel result = new ResultModel(); Helper helper = new Helper(_context); classInfo.ClassID = helper.GetIDKey("CLASS_ID").Result; classInfo.StatusNo = "A"; + if (string.IsNullOrEmpty(classInfo.ClassDesc)) classInfo.ClassDesc = " "; _context.ClassInfoes.Add(classInfo); - await _context.SaveChangesAsync(); - return CreatedAtAction("GetClassInfo", new { id = classInfo.ClassID }, classInfo); + try + { + await _context.SaveChangesAsync(); + result.Success = true; + result.Msg = "OK"; + } + catch (Exception ex) + { + result.Success = false; + result.Msg = ex.InnerException.Message; + } + return result; } @@ -183,48 +190,41 @@ namespace AMESCoreStudio.WebApi.Controllers.BAS /// // DELETE: api/ClassInfoes/5 [HttpDelete("{id}")] - public async Task> DeleteClassInfo(int id) + public async Task> DeleteClassInfo(int id) { - //var classInfo = await _context.ClassInfo.FindAsync(id); + ResultModel result = new ResultModel(); var classInfo = await _context.ClassInfoes.Where(m => m.ClassID == id).FirstOrDefaultAsync(); if (classInfo == null) { - return NotFound(); + result.Success = false; + result.Msg = "序號錯誤"; + return result; } - ////// var classInfoNew = new ClassInfo(); classInfoNew = classInfo; _context.Entry(classInfoNew).State = EntityState.Modified; - classInfoNew.StatusNo = "S"; + + if (classInfo.StatusNo == "A") + classInfoNew.StatusNo = "S"; + else + classInfoNew.StatusNo = "A"; try { await _context.SaveChangesAsync(); + result.Success = true; + result.Msg = "OK"; } - catch (DbUpdateConcurrencyException) + catch (Exception ex) { - if (!ClassInfoExists(id)) - { - return NotFound(); - } - else - { - throw; - } + result.Success = false; + result.Msg = ex.InnerException.Message; } - return classInfoNew; - - - - ///// - - //_context.ClassInfoes.Remove(classInfo); - //await _context.SaveChangesAsync(); + return result; - //return classInfo; } private bool ClassInfoExists(int id) diff --git a/AMESCoreStudio.WebApi/Controllers/BAS/FlowRulesController.cs b/AMESCoreStudio.WebApi/Controllers/BAS/FlowRulesController.cs index 472a8485..9fa6860c 100644 --- a/AMESCoreStudio.WebApi/Controllers/BAS/FlowRulesController.cs +++ b/AMESCoreStudio.WebApi/Controllers/BAS/FlowRulesController.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using AMESCoreStudio.WebApi.Models.BAS; +using AMESCoreStudio.CommonTools.Result; namespace AMESCoreStudio.WebApi.Controllers.BAS { @@ -112,33 +113,33 @@ namespace AMESCoreStudio.WebApi.Controllers.BAS // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://go.microsoft.com/fwlink/?linkid=2123754. [HttpPut("{id}")] - public async Task> PutFlowRule(int id, [FromBody] FlowRule flowRule) + public async Task> PutFlowRule(int id, [FromBody] FlowRule flowRule) { + ResultModel result = new ResultModel(); + _context.Entry(flowRule).State = EntityState.Modified; + flowRule.UpdateDate = DateTime.Now; + if (id != flowRule.FlowRuleID) { - return BadRequest(); + result.Success = false; + result.Msg = "序號錯誤"; + return result; } - _context.Entry(flowRule).State = EntityState.Modified; - flowRule.UpdateDate = DateTime.Now; try { await _context.SaveChangesAsync(); + result.Success = true; + result.Msg = "OK"; } - catch (DbUpdateConcurrencyException) + catch (Exception ex) { - if (!FlowRuleExists(id)) - { - return NotFound(); - } - else - { - throw; - } + result.Success = false; + result.Msg = ex.InnerException.Message; } + return result; - return flowRule; } /// @@ -150,16 +151,28 @@ namespace AMESCoreStudio.WebApi.Controllers.BAS // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://go.microsoft.com/fwlink/?linkid=2123754. [HttpPost] - public async Task> PostFlowRule([FromBody] FlowRule flowRule) + public async Task> PostFlowRule([FromBody] FlowRule flowRule) { + ResultModel result = new ResultModel(); Helper helper = new Helper(_context); flowRule.FlowRuleID = helper.GetIDKey("FLOW_RULE_ID").Result; flowRule.CreateDate = DateTime.Now; _context.FlowRules.Add(flowRule); - await _context.SaveChangesAsync(); + try + { + await _context.SaveChangesAsync(); + result.Success = true; + result.Msg = "OK"; + } + catch (Exception ex) + { + result.Success = false; + result.Msg = ex.InnerException.Message; + } + return result; + - return CreatedAtAction("GetFlowRule", new { id = flowRule.FlowRuleID }, flowRule); } /// @@ -169,18 +182,32 @@ namespace AMESCoreStudio.WebApi.Controllers.BAS /// // DELETE: api/FlowRules/5 [HttpDelete("{id}")] - public async Task> DeleteFlowRule(int id) + public async Task> DeleteFlowRule(int id) { + ResultModel result = new ResultModel(); var flowRule = await _context.FlowRules.FindAsync(id); if (flowRule == null) { - return NotFound(); + result.Success = false; + result.Msg = "序號錯誤"; + return result; } _context.FlowRules.Remove(flowRule); - await _context.SaveChangesAsync(); - return flowRule; + try + { + await _context.SaveChangesAsync(); + result.Success = true; + result.Msg = "OK"; + } + catch (Exception ex) + { + result.Success = false; + result.Msg = ex.InnerException.Message; + } + + return result; } private bool FlowRuleExists(int id) diff --git a/AMESCoreStudio.WebApi/Controllers/BAS/RuleStationsController.cs b/AMESCoreStudio.WebApi/Controllers/BAS/RuleStationsController.cs index 1dc66660..4b109450 100644 --- a/AMESCoreStudio.WebApi/Controllers/BAS/RuleStationsController.cs +++ b/AMESCoreStudio.WebApi/Controllers/BAS/RuleStationsController.cs @@ -9,6 +9,7 @@ using AMESCoreStudio.WebApi; using AMESCoreStudio.WebApi.Models.BAS; using AMESCoreStudio.WebApi.DTO.AMES; using AMESCoreStudio.WebApi.Controllers.AMES; +using AMESCoreStudio.CommonTools.Result; namespace AMESCoreStudio.WebApi.Controllers.BAS { @@ -203,11 +204,15 @@ namespace AMESCoreStudio.WebApi.Controllers.BAS // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://go.microsoft.com/fwlink/?linkid=2123754. [HttpPut("{id}")] - public async Task> PutRuleStation(int id, [FromBody] RuleStation ruleStation) + public async Task> PutRuleStation(int id, [FromBody] RuleStation ruleStation) { + + ResultModel result = new ResultModel(); if (id != ruleStation.RuleStationID) { - return BadRequest(); + result.Success = false; + result.Msg = "序號錯誤"; + return result; } _context.Entry(ruleStation).State = EntityState.Modified; @@ -216,20 +221,17 @@ namespace AMESCoreStudio.WebApi.Controllers.BAS try { await _context.SaveChangesAsync(); + result.Success = true; + result.Msg = "OK"; } - catch (DbUpdateConcurrencyException) + catch (Exception ex) { - if (!RuleStationExists(id)) - { - return NotFound(); - } - else - { - throw; - } + result.Success = false; + result.Msg = ex.InnerException.Message; } - return ruleStation; + return result; + } /// @@ -241,16 +243,28 @@ namespace AMESCoreStudio.WebApi.Controllers.BAS // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://go.microsoft.com/fwlink/?linkid=2123754. [HttpPost] - public async Task> PostRuleStation([FromBody] RuleStation ruleStation) + public async Task> PostRuleStation([FromBody] RuleStation ruleStation) { + ResultModel result = new ResultModel(); Helper helper = new Helper(_context); ruleStation.RuleStationID = helper.GetIDKey("RULE_STATION_ID").Result; ruleStation.CreateDate = DateTime.Now; _context.RuleStations.Add(ruleStation); - await _context.SaveChangesAsync(); + try + { + await _context.SaveChangesAsync(); + result.Success = true; + result.Msg = "OK"; + } + catch (Exception ex) + { + result.Success = false; + result.Msg = ex.InnerException.Message; + } + return result; + - return CreatedAtAction("GetRuleStation", new { id = ruleStation.RuleStationID }, ruleStation); } /// @@ -260,18 +274,35 @@ namespace AMESCoreStudio.WebApi.Controllers.BAS /// // DELETE: api/RuleStations/5 [HttpDelete("{id}")] - public async Task> DeleteRuleStation(int id) + public async Task> DeleteRuleStation(int id) { + ResultModel result = new ResultModel(); var ruleStation = await _context.RuleStations.FindAsync(id); if (ruleStation == null) { - return NotFound(); + result.Success = false; + result.Msg = "序號錯誤"; + return result; } _context.RuleStations.Remove(ruleStation); - await _context.SaveChangesAsync(); - return ruleStation; + try + { + await _context.SaveChangesAsync(); + result.Success = true; + result.Msg = "OK"; + } + catch (Exception ex) + { + result.Success = false; + result.Msg = ex.InnerException.Message; + } + + return result; + + + } private bool RuleStationExists(int id) diff --git a/AMESCoreStudio.WebApi/Controllers/BAS/RulesController.cs b/AMESCoreStudio.WebApi/Controllers/BAS/RulesController.cs index 3bc8506e..70338d8d 100644 --- a/AMESCoreStudio.WebApi/Controllers/BAS/RulesController.cs +++ b/AMESCoreStudio.WebApi/Controllers/BAS/RulesController.cs @@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using AMESCoreStudio.WebApi; using AMESCoreStudio.WebApi.Models.BAS; +using AMESCoreStudio.CommonTools.Result; namespace AMESCoreStudio.WebApi.Controllers.BAS { @@ -172,11 +173,14 @@ namespace AMESCoreStudio.WebApi.Controllers.BAS // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://go.microsoft.com/fwlink/?linkid=2123754. [HttpPut("{id}")] - public async Task> PutRules(int id, [FromBody]Rules rules) + public async Task> PutRules(int id, [FromBody]Rules rules) { + ResultModel result = new ResultModel(); if (id != rules.RuleStationID) { - return BadRequest(); + result.Success = false; + result.Msg = "序號錯誤"; + return result; } _context.Entry(rules).State = EntityState.Modified; @@ -185,20 +189,16 @@ namespace AMESCoreStudio.WebApi.Controllers.BAS try { await _context.SaveChangesAsync(); + result.Success = true; + result.Msg = "OK"; } - catch (DbUpdateConcurrencyException) + catch (Exception ex) { - if (!RulesExists(id)) - { - return NotFound(); - } - else - { - throw; - } + result.Success = false; + result.Msg = ex.InnerException.Message; } - return rules; + return result; } /// @@ -210,14 +210,28 @@ namespace AMESCoreStudio.WebApi.Controllers.BAS // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://go.microsoft.com/fwlink/?linkid=2123754. [HttpPost] - public async Task> PostRules([FromBody] Rules rules) + public async Task> PostRules([FromBody] Rules rules) { + ResultModel result = new ResultModel(); rules.CreateDate = DateTime.Now; _context.Ruleses.Add(rules); - await _context.SaveChangesAsync(); - - return CreatedAtAction("GetRules", new { id = rules.RuleStationID }, rules); + + try + { + await _context.SaveChangesAsync(); + result.Success = true; + result.Msg = "OK"; + } + catch (Exception ex) + { + result.Success = false; + result.Msg = ex.InnerException.Message; + } + + + + return result; } /// @@ -227,18 +241,31 @@ namespace AMESCoreStudio.WebApi.Controllers.BAS /// // DELETE: api/Rules/5 [HttpDelete("{id}")] - public async Task> DeleteRules(int id) + public async Task> DeleteRules(int id) { + ResultModel result = new ResultModel(); var rules = await _context.Ruleses.FindAsync(id); if (rules == null) { - return NotFound(); + result.Success = false; + result.Msg = "序號錯誤"; + return result; } _context.Ruleses.Remove(rules); - await _context.SaveChangesAsync(); + try + { + await _context.SaveChangesAsync(); + result.Success = true; + result.Msg = "OK"; + } + catch (Exception ex) + { + result.Success = false; + result.Msg = ex.InnerException.Message; + } - return rules; + return result; } private bool RulesExists(int id) diff --git a/AMESCoreStudio.WebApi/Controllers/BAS/StationsesController.cs b/AMESCoreStudio.WebApi/Controllers/BAS/StationsesController.cs index 296462a2..dd7509b7 100644 --- a/AMESCoreStudio.WebApi/Controllers/BAS/StationsesController.cs +++ b/AMESCoreStudio.WebApi/Controllers/BAS/StationsesController.cs @@ -183,13 +183,16 @@ namespace AMESCoreStudio.WebApi.Controllers.BAS /// // DELETE: api/Stationses/5 [HttpDelete("{id}")] - public async Task> DeleteStations(int id) + public async Task> DeleteStations(int id) { //var stations = await _context.Stations.FindAsync(id); + ResultModel result = new ResultModel(); var stations = await _context.Stationses.Where(m => m.StationID == id).FirstOrDefaultAsync(); if (stations == null) { - return NotFound(); + result.Success = false; + result.Msg = "序號錯誤"; + return result; } ////// @@ -204,20 +207,16 @@ namespace AMESCoreStudio.WebApi.Controllers.BAS try { await _context.SaveChangesAsync(); + result.Success = true; + result.Msg = "OK"; } - catch (DbUpdateConcurrencyException) + catch (Exception ex) { - if (!StationsExists(id)) - { - return NotFound(); - } - else - { - throw; - } + result.Success = false; + result.Msg = ex.InnerException.Message; } - return stationsNew; + return result;