public async Task <IActionResult> Delete(int id, [FromQuery] Int32 hid = 0) { 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")); } SqlConnection conn = null; String queryString = ""; String strErrMsg = ""; HttpStatusCode errorCode = HttpStatusCode.OK; SqlCommand cmd = null; SqlTransaction tran = 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; } // Step 1. Delete events for recur tran = conn.BeginTransaction(); queryString = HIHDBUtility.Event_GetNormalEventForRecurDeletionString(); cmd = new SqlCommand(queryString, conn); cmd.Transaction = tran; HIHDBUtility.Event_BindNormalEventForRecurDeletionParameters(cmd, hid, id); await cmd.ExecuteNonQueryAsync(); cmd.Dispose(); cmd = null; // Step 2. Delete recur item queryString = HIHDBUtility.Event_GetRecurEventDeletionString(); cmd = new SqlCommand(queryString, conn); cmd.Transaction = tran; HIHDBUtility.Event_BindRecurEventDeletionParameters(cmd, hid, id); await cmd.ExecuteNonQueryAsync(); cmd.Dispose(); cmd = null; tran.Commit(); } } catch (Exception exp) { #if DEBUG System.Diagnostics.Debug.WriteLine(exp.Message); #endif strErrMsg = exp.Message; if (errorCode == HttpStatusCode.OK) { errorCode = HttpStatusCode.InternalServerError; } if (tran != null) { tran.Rollback(); } } finally { if (tran != null) { tran.Dispose(); tran = 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)); } } return(Ok()); }