public async Task <IActionResult> Patch(int id, [FromQuery] int hid, [FromBody] JsonPatchDocument <EventViewModel> patch) { if (patch == null || id <= 0) { return(BadRequest("No data is inputted")); } if (hid <= 0) { return(BadRequest("No home is inputted")); } // Update the database SqlConnection conn = null; SqlCommand cmd = null; SqlDataReader reader = null; String queryString = ""; String strErrMsg = ""; HttpStatusCode errorCode = HttpStatusCode.OK; 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")); } EventViewModel vm = new EventViewModel(); try { queryString = HIHDBUtility.Event_GetNormalEventQueryString(false, usrName, null, null, null, id); 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; // Re-throw } // Optimization logic for Mark as complete if (patch.Operations.Count == 1 && patch.Operations[0].path == "/completeTimePoint") { // Only update the complete time queryString = HIHDBUtility.Event_GetNormalEventMarkAsCompleteString(); SqlCommand cmdupdate = new SqlCommand(queryString, conn); HIHDBUtility.Event_BindNormalEventMarkAsCompleteParameters(cmdupdate, DateTime.Parse((string)patch.Operations[0].value), usrName, id); await cmdupdate.ExecuteNonQueryAsync(); } else { cmd = new SqlCommand(queryString, conn); reader = cmd.ExecuteReader(); if (!reader.HasRows) { errorCode = HttpStatusCode.BadRequest; strErrMsg = "Object with ID doesnot exist: " + id.ToString(); throw new Exception(strErrMsg); } else { while (reader.Read()) { HIHDBUtility.Event_DB2VM(reader, vm, false); } reader.Dispose(); reader = null; cmd.Dispose(); cmd = null; // Now go ahead for the update //var patched = vm.Copy(); patch.ApplyTo(vm, ModelState); if (!ModelState.IsValid) { return(new BadRequestObjectResult(ModelState)); } queryString = HIHDBUtility.Event_GetNormalEventUpdateString(); cmd = new SqlCommand(queryString, conn); HIHDBUtility.Event_BindNormalEventUpdateParameters(cmd, vm, usrName); Int32 nRst = await cmd.ExecuteNonQueryAsync(); } } } } catch (Exception exp) { #if DEBUG System.Diagnostics.Debug.WriteLine(exp.Message); #endif 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(vm, setting)); }
public async Task <IActionResult> Get(int id, [FromQuery] Int32 hid = 0) { if (hid <= 0) { return(BadRequest("HID is missing")); } if (id <= 0) { return(BadRequest("Invalid ID")); } 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")); } RecurUIEventViewModel vm = new RecurUIEventViewModel(); SqlConnection conn = null; SqlCommand cmd = null; SqlDataReader reader = null; String queryString = ""; String strErrMsg = ""; HttpStatusCode errorCode = HttpStatusCode.OK; try { 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; } queryString = HIHDBUtility.Event_GetRecurEventQueryString(false, usrName, hid, null, null, id); cmd = new SqlCommand(queryString, conn); reader = cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { HIHDBUtility.Event_RecurDB2VM(reader, vm, false); } } reader.Close(); cmd.Dispose(); cmd = null; queryString = HIHDBUtility.Event_GetNormalEventByRecurIDString(); cmd = new SqlCommand(queryString, conn); HIHDBUtility.Event_BindNormalEventForRecurDeletionParameters(cmd, hid, id); reader = await cmd.ExecuteReaderAsync(); if (reader.HasRows) { while (reader.Read()) { var vmevent = new EventViewModel(); HIHDBUtility.Event_DB2VM(reader, vmevent, true); vm.EventList.Add(vmevent); } } } } catch (Exception exp) { #if DEBUG System.Diagnostics.Debug.WriteLine(exp.Message); #endif 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(vm, setting)); }
public async Task <IActionResult> Get([FromQuery] Int32 hid, Int32 top = 100, Int32 skip = 0, Boolean?skipfinished = null, DateTime?dtbgn = null, DateTime?dtend = null) { if (hid <= 0) { return(BadRequest("HID is missing")); } 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 <EventViewModel> listVm = new BaseListViewModel <EventViewModel>(); SqlConnection conn = null; String queryString = ""; String strErrMsg = ""; HttpStatusCode errorCode = HttpStatusCode.OK; SqlCommand cmd = null; SqlDataReader reader = null; try { 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; } queryString = HIHDBUtility.Event_GetNormalEventQueryString(true, usrName, hid, skip, top, null, skipfinished, dtbgn, dtend); cmd = new SqlCommand(queryString, conn); reader = cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { listVm.TotalCount = reader.GetInt32(0); break; } } await reader.NextResultAsync(); if (reader.HasRows) { while (reader.Read()) { EventViewModel vm = new EventViewModel(); HIHDBUtility.Event_DB2VM(reader, vm, true); listVm.Add(vm); } } } } catch (Exception exp) { #if DEBUG System.Diagnostics.Debug.WriteLine(exp.Message); #endif strErrMsg = exp.Message; if (errorCode == HttpStatusCode.OK) { errorCode = HttpStatusCode.InternalServerError; } } finally { if (reader != null) { reader.Close(); 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)); }