public IActionResult Find(string area_no, string area_na, string search_type, string submitbutton, string singarea_state) { if (!LoginUtil.CheckIfLoggedIn(HttpContext)) { return(View(nameof(Index))); } StateOfRequest mState; if (string.IsNullOrEmpty(singarea_state)) { mState = new StateOfRequest("AreaNo"); } else { mState = JsonUtil.GetObjectFromJsonString <StateOfRequest>(singarea_state); } mState.StartTime = DateTime.Now; string temp_state = JsonUtil.SetJsonStringFromObject(mState); if (string.IsNullOrEmpty(area_no)) { area_no = string.Empty; } area_no = area_no.Trim(); if (string.IsNullOrEmpty(area_na)) { area_na = string.Empty; } area_na = area_na.Trim(); string sButton = submitbutton.ToUpper(); if (sButton == "CANCEL") { temp_state = JsonUtil.SetJsonStringFromObject(mState); return(RedirectToAction(nameof(SingareasList), new { singarea_state = temp_state })); } if (string.IsNullOrEmpty(search_type)) { // search_type not defined return(View()); } string searchType = search_type.Trim(); mState.OrderBy = searchType; Singarea singarea = new Singarea(); // new object singarea.AreaNo = area_no; // for order by "AreaNo" singarea.AreaNa = area_na; // for order by "AreaNa" List <Singarea> singareasTemp = _singareasManager.FindOnePageOfSingareasForOneSingarea(mState, singarea, -1); temp_state = JsonUtil.SetJsonStringFromObject(mState); ViewBag.SingareaState = temp_state; return(View(nameof(SingareasList), singareasTemp)); }
// GET: Singarea/Add // the view of adding singareas to Singarea table public IActionResult Add(string singarea_state) { if (!LoginUtil.CheckIfLoggedIn(HttpContext)) { return(View(nameof(Index))); } StateOfRequest mState; if (string.IsNullOrEmpty(singarea_state)) { mState = new StateOfRequest("AreaNo"); } else { mState = JsonUtil.GetObjectFromJsonString <StateOfRequest>(singarea_state); } if ((mState.IsFirstAddRecord) || (mState.OrgId == 0)) { // the first id of this page became the selected original id // or SateOfRequest.OrgId = 0 mState.OrgId = mState.FirstId; } mState.StartTime = DateTime.Now; string temp_state = JsonUtil.SetJsonStringFromObject(mState); ViewBag.SingareaState = temp_state; // pass the Json string to View Singarea singarea = new Singarea(); // create a new Singarea object return(View(singarea)); }
// GET: Singarea/5 public async Task <IActionResult> Details(string singarea_state) { if (!LoginUtil.CheckIfLoggedIn(HttpContext)) { return(View(nameof(Index))); } StateOfRequest mState; if (string.IsNullOrEmpty(singarea_state)) { mState = new StateOfRequest("AreaNo"); } else { mState = JsonUtil.GetObjectFromJsonString <StateOfRequest>(singarea_state); } Singarea singarea = await _singareasManager.FindOneSingareaById(mState.OrgId); if (singarea == null) { // go to previous view (List view) return(Redirect(HttpContext.Request.Headers["Referer"])); } else { mState.OrgId = singarea.Id; mState.OrgNo = singarea.AreaNo; string temp_state = JsonUtil.SetJsonStringFromObject(mState); ViewBag.SingareaState = temp_state; return(View(singarea)); } }
public static JObject ConvertSingerTypeToJsongObject(Singarea singarea, string sex) { JObject jObject = ConvertSingareaToJsongObject(singarea); jObject.Add("sex", sex); return(jObject); }
public async Task <IActionResult> Edit(string submitbutton, string singarea_state, [Bind("Id", "AreaNo, AreaNa, AreaEn")] Singarea singarea) { if (!LoginUtil.CheckIfLoggedIn(HttpContext)) { return(View(nameof(Index))); } StateOfRequest mState; if (string.IsNullOrEmpty(singarea_state)) { mState = new StateOfRequest("AreaNo"); } else { mState = JsonUtil.GetObjectFromJsonString <StateOfRequest>(singarea_state); } mState.StartTime = DateTime.Now; string temp_state = JsonUtil.SetJsonStringFromObject(mState); int orgId = mState.OrgId; // = Singarea.Id string sButton = submitbutton.ToUpper(); if (sButton == "CANCEL") { temp_state = JsonUtil.SetJsonStringFromObject(mState); return(RedirectToAction(nameof(SingareasList), new { singarea_state = temp_state })); } if (ModelState.IsValid) { // start updating table int result = await _singareasManager.UpdateOneSingareaById(orgId, singarea); if (result == ErrorCodeModel.Succeeded) { // succeeded to update Singarea newSingarea = new Singarea(); List <Singarea> singareasTemp = _singareasManager.FindOnePageOfSingareasForOneSingarea(mState, newSingarea, orgId); temp_state = JsonUtil.SetJsonStringFromObject(mState); ViewBag.SingareaState = temp_state; return(View(nameof(SingareasList), singareasTemp)); } else { ViewData["ErrorMessage"] = ErrorCodeModel.GetErrorMessage(result); } } else { // Model.IsValid = false ViewData["ErrorMessage"] = ErrorCodeModel.GetErrorMessage(ErrorCodeModel.ModelBindingFailed); } ViewBag.SingareaState = temp_state; return(View(singarea)); }
public async Task <string> Get(int id) { // get one Singarea Singarea singarea = await _singareasManager.FindOneSingareaById(id); JObject jObject = JsonUtil.ConvertSingareaToJsongObject(singarea); JObject returnJSON = new JObject(); returnJSON.Add("singarea", jObject); return(returnJSON.ToString()); }
public static JObject ConvertSingareaToJsongObject(Singarea singarea) { JObject jObject = new JObject(); if (singarea == null) { return(jObject); } jObject.Add("id", singarea.Id); jObject.Add("areaNo", singarea.AreaNo); jObject.Add("areaNa", singarea.AreaNa); jObject.Add("areaEn", singarea.AreaEn); return(jObject); }
/// <summary> /// Adds the one singarea to table. /// </summary> /// <returns>Return the error code.</returns> /// <param name="singarea">Singarea.</param> public async Task <int> AddOneSingareaToTable(Singarea singarea) { int result = ErrorCodeModel.ErrorBecauseBugs; if (singarea == null) { // the data for updating is empty result = ErrorCodeModel.SingareaIsNull; return(result); } if (string.IsNullOrEmpty(singarea.AreaNo)) { // the singarea no that input by user is empty result = ErrorCodeModel.SingareaNoIsEmpty; return(result); } Singarea oldSingarea = await FindOneSingareaByAreaNo(singarea.AreaNo); if (oldSingarea != null) { // singarea no is duplicate result = ErrorCodeModel.SingareaNoDuplicate; return(result); } using (var dbTransaction = _context.Database.BeginTransaction()) { try { _context.Add(singarea); await _context.SaveChangesAsync(); dbTransaction.Commit(); result = ErrorCodeModel.Succeeded; } catch (DbUpdateException ex) { string errorMsg = ex.ToString(); Console.WriteLine("Failed to add one singarea: \n" + errorMsg); dbTransaction.Rollback(); result = ErrorCodeModel.DatabaseError; } } return(result); }
// private methods /// <summary> /// Verifies the singer. /// </summary> /// <returns>The singer.</returns> /// <param name="singer">Singer.</param> private async Task <int> VerifySinger(Singer singer) { // int result = 1; // valid by verification int result = ErrorCodeModel.SingareaNoNotFound; if (singer.AreaId >= 0) { Singarea area = await _context.Singarea.Where(x => x.Id == singer.AreaId).SingleOrDefaultAsync(); if (area != null) { result = 1; // found singarea } } return(result); }
/// <summary> /// Updates the one singarea by identifier. /// </summary> /// <returns>Return the error code</returns> /// <param name="id">Identifier.</param> /// <param name="singarea">Singarea.</param> public async Task <int> UpdateOneSingareaById(int id, Singarea singarea) { int result = ErrorCodeModel.ErrorBecauseBugs; if (id == 0) { // its a bug, id of singarea cannot be 0 result = ErrorCodeModel.ErrorBecauseBugs; return(result); } if (singarea == null) { // the data for updating is empty result = ErrorCodeModel.SingareaIsNull; return(result); } if (string.IsNullOrEmpty(singarea.AreaNo)) { // the singarea no that input by user is empty result = ErrorCodeModel.SingareaNoIsEmpty; return(result); } Singarea newSingarea = await FindOneSingareaByAreaNo(singarea.AreaNo); if (newSingarea != null) { if (newSingarea.Id != id) { // singarea no is duplicate result = ErrorCodeModel.SingareaNoDuplicate; return(result); } } Singarea orgSingarea = await FindOneSingareaById(id); if (orgSingarea == null) { // the original singarea does not exist any more result = ErrorCodeModel.OriginalSingareaNotExist; return(result); } else { orgSingarea.CopyColumnsFrom(singarea); // check if entry state changed if ((_context.Entry(orgSingarea).State) == EntityState.Modified) { using (var dbTransaction = _context.Database.BeginTransaction()) { try { await _context.SaveChangesAsync(); dbTransaction.Commit(); result = ErrorCodeModel.Succeeded; // succeeded to update } catch (DbUpdateException ex) { string msg = ex.ToString(); Console.WriteLine("Failed to update singarea table: \n" + msg); dbTransaction.Rollback(); result = ErrorCodeModel.DatabaseError; } } } else { result = ErrorCodeModel.SingareaNotChanged; // no changed } } return(result); }
/// <summary> /// Finds the one singarea by identifier. /// </summary> /// <returns>The one singarea by identifier (Singarea.Id).</returns> /// <param name="id">the id of the singarea.</param> public async Task <Singarea> FindOneSingareaById(int id) { Singarea singarea = await _context.Singarea.Where(x => x.Id == id).SingleOrDefaultAsync(); return(singarea); }
/// <summary> /// Finds the one singarea by singarea no. /// </summary> /// <returns>The one singarea by singarea no.</returns> /// <param name="area_no">Singarea no.</param> public async Task <Singarea> FindOneSingareaByAreaNo(string area_no) { Singarea singarea = await _context.Singarea.Where(x => x.AreaNo == area_no).SingleOrDefaultAsync(); return(singarea); }
/// <summary> /// Finds the one page of singareas for one singarea. /// </summary> /// <returns>The one page of singareas for one singarea.</returns> /// <param name="mState">Singarea state.</param> /// <param name="singarea">Singarea.</param> /// <param name="id">Identifier.</param> public List <Singarea> FindOnePageOfSingareasForOneSingarea(StateOfRequest mState, Singarea singarea, int id) { if ((mState == null) || (singarea == null)) { return(new List <Singarea>()); } int pageSize = mState.PageSize; if (pageSize <= 0) { Console.WriteLine("The value of pageSize cannot be less than 0."); return(new List <Singarea>()); } IQueryable <Singarea> totalSingareas = GetAllSingareasIQueryable(mState); if (totalSingareas == null) { return(new List <Singarea>()); } List <Singarea> singareas = null; Singarea singareaWithIndex = null; IQueryable <Singarea> singareasTempList = null; string orderByParam = mState.OrderBy.Trim(); if (id >= 0) { // There was a selected singarea singareasTempList = totalSingareas.Where(x => x.Id == id); } else { // No singarea selected if (orderByParam == "") { int area_id = singarea.Id; singareasTempList = totalSingareas.Where(x => (x.Id == area_id)); } else if (orderByParam.Equals("AreaNo", StringComparison.OrdinalIgnoreCase)) { string area_no = singarea.AreaNo.Trim(); int len = area_no.Length; singareasTempList = totalSingareas.Where(x => x.AreaNo.Trim().Substring(0, len) == area_no); } else if (orderByParam.Equals("AreaNa", StringComparison.OrdinalIgnoreCase)) { string area_na = singarea.AreaNa.Trim(); int len = area_na.Length; singareasTempList = totalSingareas.Where(x => x.AreaNa.Trim().Substring(0, len) == area_na); } else { // not inside range of roder by then return empty lsit return(new List <Singarea>()); } } int totalRecords = totalSingareas.Count(); // the whole singarea table bool isFound = true; singareaWithIndex = singareasTempList.FirstOrDefault(); // the first one found if (singareaWithIndex == null) { isFound = false; // singarea that was assigned is not found if (totalRecords == 0) { // Singarea Table is empty UpdateStateOfRequest(mState, singareaWithIndex, mState.CurrentPageNo, pageSize, 0, 0, true); // return empty list return(new List <Singarea>()); } else { // go to last page singareaWithIndex = totalSingareas.LastOrDefault(); } } singarea.CopyFrom(singareaWithIndex); // find the row number of singareaWithIndex int tempCount = 0; foreach (var singareaVar in totalSingareas) { ++tempCount; // first row number is 1 if (singareaVar.Id == singareaWithIndex.Id) { break; } } int pageNo = tempCount / pageSize; if ((pageNo * pageSize) != tempCount) { pageNo++; } int recordNo = (pageNo - 1) * pageSize; singareas = totalSingareas.Skip(recordNo).Take(pageSize).ToList(); int totalPages = totalRecords / pageSize; if ((totalPages * pageSize) != totalRecords) { totalPages++; } if (isFound) { // found mState.OrgId = singarea.Id; // chnaged OrgId to the singarea id found } else { // not found, then it is last page and last record mState.OrgId = 0; // no singarea is selected } UpdateStateOfRequest(mState, singareas.FirstOrDefault(), pageNo, pageSize, totalRecords, totalPages, true); return(singareas); }