public string Add(SBU sbu) { MyResponse response = new MyResponse(); try { SBU _sbu = db.SBU.FirstOrDefault(X => X.Name == sbu.Name); if (_sbu != null) { throw new Exception(StatusCode.ObjectHadExist.ToString()); } db.SBU.Add(sbu); db.SaveChanges(); } catch (Exception ex) { if (ex.Message == StatusCode.ObjectHadExist.ToString()) { response.Code = StatusCode.ObjectHadExist; } else { response.Code = StatusCode.Error; } } return(response.ToString()); }
public HttpResponseMessage GetOne(string id) { MyResponse response = new MyResponse(); try { SBU sbu = db.SBU.Find(id); if (sbu == null) { throw new Exception(StatusCode.ObjectNotFound.ToString()); } string json = JsonConvert.SerializeObject(sbu); response.Data = json; } catch (Exception ex) { if (ex.Message == StatusCode.ObjectNotFound.ToString()) { response.Code = StatusCode.ObjectNotFound; } else { response.Code = StatusCode.Error; } } return(new HttpResponseMessage { Content = new StringContent(response.ToString(), System.Text.Encoding.UTF8, "application/json") }); }
public string Delete(string id) { MyResponse response = new MyResponse(); try { SBU sbu = db.SBU.Find(id); if (sbu == null) { throw new Exception(StatusCode.ObjectNotFound.ToString()); } db.SBU.Remove(sbu); db.SaveChanges(); } catch (Exception ex) { if (ex.Message == StatusCode.ObjectNotFound.ToString()) { response.Code = StatusCode.ObjectNotFound; } else { response.Code = StatusCode.Error; } } return(response.ToString()); }
public string Update(SBU sbu) { MyResponse response = new MyResponse(); try { //判断要改的新名字是否存在 SBU _sbu = db.SBU.Find(sbu.Name); if (_sbu == null) { throw new Exception(StatusCode.ObjectNotFound.ToString()); } db.Entry(sbu).State = EntityState.Modified; db.SaveChanges(); } catch (Exception ex) { if (ex.Message == StatusCode.ObjectNotFound.ToString()) { response.Code = StatusCode.ObjectNotFound; } else { response.Code = StatusCode.Error; } } return(response.ToString()); }
/// <summary> /// Add Update Data /// </summary> /// <param name="model"></param> /// <returns>Jsonresponse</returns> public JsonResponse AddUpdate(SBU model) { try { // If these condition true the data was not exsits in the database if (!IsExsits(model.Name, model.ID)) { // If model.ID == 0 the data goes to the Add part. if (model.ID == 0) { model.CreatedOn = DateTime.Now; model.CreatedBy = GetUserID(); _context.Set <SBU>().Add(model); int i = _context.SaveChanges(); if (i != 0) { resp.Status = Constants.ResponseStatus.Success; resp.Message = Constants.Service.Data_insert_success; } else { resp.Message = Constants.Service.Data_insert_failed; } } // Else data goes to the Update part. else { resp.Message = Constants.Service.Data_Update_failed; var models = GetData(model.ID); if (models != null) { models.Name = model.Name; models.Code = model.Code; models.IsActive = model.IsActive; models.ModifiedOn = DateTime.Now; models.ModifiedBy = GetUserID(); _context.Set <SBU>().Update(models); int i = _context.SaveChanges(); if (i != 0) { resp.Status = Constants.ResponseStatus.Success; resp.Message = Constants.Service.Data_Update_success; } } } } // The data was in the database so, It return the else part else { resp.Message = Constants.ControllerMessage.Data_Exsists; } } catch (Exception) { resp.Message = Constants.Service.Common_message; } return(resp); }
private void PopulateDropDowns() { DataTable tbl; SBU sbu = new SBU(); tbl = sbu.GetAllActiveRecords().Tables[0]; tbl.Rows.InsertAt(tbl.NewRow(), 0); dvSBU = tbl.DefaultView; this.ddlBU.Enabled = false; this.ddlProject.Enabled = false; //BusinessUnit bu = new BusinessUnit(); //tbl = bu.GetAllActiveRecords().Tables[0]; //tbl.Rows.InsertAt(tbl.NewRow(), 0); //dvBU = tbl.DefaultView; //Project prj = new Project(); //tbl = prj.GetAllActiveRecords().Tables[0]; //tbl.Rows.InsertAt(tbl.NewRow(), 0); //dvProject = tbl.DefaultView; Frequency freq = new Frequency(); tbl = freq.GetAllActiveRecords().Tables[0]; tbl.Rows.InsertAt(tbl.NewRow(), 0); dvFrequency = tbl.DefaultView; Impact imp = new Impact(); tbl = imp.GetAllActiveRecords().Tables[0]; tbl.Rows.InsertAt(tbl.NewRow(), 0); dvImpact = tbl.DefaultView; Backend.Maintenance.Type type = new Backend.Maintenance.Type(); tbl = type.GetAllActiveRecords().Tables[0]; tbl.Rows.InsertAt(tbl.NewRow(), 0); dvType = tbl.DefaultView; FinancialImpact fi = new FinancialImpact(); tbl = fi.GetAllActiveRecords().Tables[0]; tbl.Rows.InsertAt(tbl.NewRow(), 0); dvFinancialImpact = tbl.DefaultView; }