private void onDB2VM(SqlDataReader reader, Boolean bIncContent, LearnObjectUIViewModel vm) { var idx = 0; vm.ID = reader.GetInt32(idx++); vm.HID = reader.GetInt32(idx++); vm.CategoryID = reader.GetInt32(idx++); if (!reader.IsDBNull(idx)) { vm.CategoryName = reader.GetString(idx++); } else { vm.CategoryName = String.Empty; ++idx; } if (!reader.IsDBNull(idx)) { vm.Name = reader.GetString(idx++); } else { vm.Name = String.Empty; ++idx; } if (bIncContent) { if (!reader.IsDBNull(idx)) { vm.Content = reader.GetString(idx++); } else { ++idx; } } if (!reader.IsDBNull(idx)) { vm.CreatedBy = reader.GetString(idx++); } else { ++idx; } if (!reader.IsDBNull(idx)) { vm.CreatedAt = reader.GetDateTime(idx++); } else { ++idx; } if (!reader.IsDBNull(idx)) { vm.UpdatedBy = reader.GetString(idx++); } else { ++idx; } if (!reader.IsDBNull(idx)) { vm.UpdatedAt = reader.GetDateTime(idx++); } else { ++idx; } }
public async Task <IActionResult> Get([FromQuery] Int32 hid, Int32 top = 100, Int32 skip = 0, Int32?ctgyid = null, Boolean bIncContent = false) { if (hid <= 0) { return(BadRequest("No Home Inputted")); } String usrName = String.Empty; if (Startup.UnitTestMode) { usrName = UnitTestUtility.UnitTestUser; } else { var usrObj = HIHAPIUtility.GetUserClaim(this); usrName = usrObj.Value; } if (String.IsNullOrEmpty(usrName)) { return(BadRequest("User cannot recognize")); } BaseListViewModel <LearnObjectUIViewModel> listVm = new BaseListViewModel <LearnObjectUIViewModel>(); SqlConnection conn = null; SqlCommand cmd = null; SqlDataReader reader = null; String queryString = ""; String strErrMsg = ""; HttpStatusCode errorCode = HttpStatusCode.OK; try { queryString = this.getQueryString(bIncContent, ctgyid, true, top, skip, null, hid); using (conn = new SqlConnection(Startup.DBConnectionString)) { await conn.OpenAsync(); // Check Home assignment with current user try { HIHAPIUtility.CheckHIDAssignment(conn, hid, usrName); } catch (Exception) { errorCode = HttpStatusCode.BadRequest; throw; } cmd = new SqlCommand(queryString, conn); reader = cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { listVm.TotalCount = reader.GetInt32(0); break; } } reader.NextResult(); if (reader.HasRows) { while (reader.Read()) { LearnObjectUIViewModel vm = new LearnObjectUIViewModel(); onDB2VM(reader, bIncContent, vm); listVm.Add(vm); } } } } catch (Exception exp) { System.Diagnostics.Debug.WriteLine(exp.Message); strErrMsg = exp.Message; if (errorCode == HttpStatusCode.OK) { errorCode = HttpStatusCode.InternalServerError; } } finally { if (reader != null) { reader.Dispose(); reader = null; } if (cmd != null) { cmd.Dispose(); cmd = null; } if (conn != null) { conn.Dispose(); conn = null; } } if (errorCode != HttpStatusCode.OK) { switch (errorCode) { case HttpStatusCode.Unauthorized: return(Unauthorized()); case HttpStatusCode.NotFound: return(NotFound()); case HttpStatusCode.BadRequest: return(BadRequest(strErrMsg)); default: return(StatusCode(500, strErrMsg)); } } var setting = new Newtonsoft.Json.JsonSerializerSettings { DateFormatString = HIHAPIConstants.DateFormatPattern, ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() }; return(new JsonResult(listVm, setting)); }