/// <summary> /// Removes a given DrugEntry from database, and updates all clients /// </summary> /// <param name="entry">DrugEntry to remove</param> /// <param name="amount">Amount of doses left</param> /// <returns></returns> public async Task RemoveDrugEntry(DrugEntry entry, decimal amount) { Database.RemoveDrugEntry(entry); Database.UpdateNumberLeft(entry.DrugGuid, amount); DrugInfo relevantInfo = Database.GetDrugInfo(entry.DrugGuid)[0]; relevantInfo.ReCalculateStats(); await Clients.All.SendAsync("RemoveDrugEntry", entry, relevantInfo.Stats); }
/// <summary> /// Updates a given DrugEntry /// </summary> /// <param name="entry">Entry to update</param> /// <param name="change">Change in number remaining</param> /// <returns></returns> public async Task UpdateDrugEntry(DrugEntry entry, decimal change) { Database.UpdateDrugEntry(entry); DrugInfo relevantInfo = Database.GetDrugInfo(entry.DrugGuid)[0]; relevantInfo.ReCalculateStats(); Database.UpdateNumberLeft(entry.DrugGuid, relevantInfo.NumberLeft + change); await Clients.All.SendAsync("UpdateDrugEntry", entry, relevantInfo.Stats, relevantInfo.NumberLeft + change); }
/// <summary> /// Handles sending a message to a given webhook url /// </summary> /// <param name="entry">Entry to send</param> /// <param name="drugName">Name of drug</param> /// <param name="webHookUrl">Webhook to send to</param> /// <returns></returns> public static async Task SendMessage(DrugEntry entry, string drugName, string webHookUrl) { var messageContent = new JObject { ["content"] = $"Timer started for {drugName} at {entry.Time}" }; var content = new StringContent(messageContent.ToString(), Encoding.UTF8, "application/json"); using HttpResponseMessage response = await HttpClient.PostAsync(webHookUrl, content); response.EnsureSuccessStatusCode(); }
/// <summary> /// Adds a given DrugEntry to database, and updates all clients /// </summary> /// <param name="entry">DrugEntry to add</param> /// <param name="amount">Amount of doses left</param> /// <returns></returns> public async Task AddDrugEntry(DrugEntry entry, decimal amount) { Database.AddDrugEntry(entry); Database.UpdateNumberLeft(entry.DrugGuid, amount); //sends a discord message, if enabled DrugInfo relevantInfo = Database.GetDrugInfo(entry.DrugGuid)[0]; relevantInfo.ReCalculateStats(); await Clients.All.SendAsync("AddDrugEntry", entry, relevantInfo.Stats); if (relevantInfo.DrugSettings.DiscordWebHookEnabled) { await Discord.SendMessage(entry, relevantInfo.Name, relevantInfo.DrugSettings.DiscordWebHook); } }
/// <summary> /// Removes a given DrugEntry from the database /// </summary> /// <param name="drugEntry">DrugEntry to remove</param> public static void RemoveDrugEntry(DrugEntry drugEntry) { //creates and opens the connection using var connection = new SQLiteConnection(_connectionInfo); connection.Open(); //create a command, set the text and set all parameters to given DrugEntry var command = connection.CreateCommand(); command.CommandText = @"DELETE FROM tblDrugEntries WHERE EntryGuid LIKE $entryGuid"; command.Parameters.AddWithValue("$drugGuid", drugEntry.DrugGuid); command.Parameters.AddWithValue("$entryGuid", drugEntry.EntryGuid); //write to database command.ExecuteNonQuery(); }
/// <summary> /// Gets a list of all times associated with a given DrugInfo /// </summary> /// <returns>A list of DateTimes</returns> public static List <DrugEntry> GetDrugEntries(DrugInfo drugInfo, int count = -1) { //creates and opens the connection using var connection = new SQLiteConnection(_connectionInfo); connection.Open(); //create a command, set the text and set all parameters to given DrugInfo var command = connection.CreateCommand(); command.CommandText = @"SELECT * FROM tblDrugEntries WHERE DrugGuid = $drugGuid ORDER BY Time DESC"; if (count > 0) { command.CommandText += "\nLIMIT $count"; command.Parameters.AddWithValue("$count", count); } command.Parameters.AddWithValue("$drugGuid", drugInfo.Guid); var reader = command.ExecuteReader(); //read a list of DateTimes from the table List <DrugEntry> entries = new List <DrugEntry>(); while (reader.Read()) { DrugEntry entry = new DrugEntry() { DrugGuid = drugInfo.Guid, EntryGuid = (string)reader["EntryGuid"], Time = DateTime.Parse((string)reader["Time"]), Count = Convert.ToDecimal(reader["Count"]), Notes = reader["Notes"].HandleNull <string>() }; entries.Add(entry); } return(entries.ToList()); }
/// <summary> /// Add a given DrugEntry to the database /// </summary> /// <param name="entry">DrugEntry to add</param> public static void AddDrugEntry(DrugEntry entry) { //creates and opens the connection using var connection = new SQLiteConnection(_connectionInfo); connection.Open(); //create a command, set the text and set all parameters to given DrugEntry var command = connection.CreateCommand(); command.CommandText = @"INSERT INTO tblDrugEntries VALUES($drugGuid, $entryGuid, $time, $count, $notes)"; command.Parameters.AddWithValue("$drugGuid", entry.DrugGuid); command.Parameters.AddWithValue("$entryGuid", entry.EntryGuid); command.Parameters.AddWithValue("$time", entry.Time.ToString("yyyy-MM-dd HH:mm:ss.fff")); command.Parameters.AddWithValue("$count", entry.Count); command.Parameters.AddWithValue("$notes", entry.Notes); //write to database command.ExecuteNonQuery(); }
/// <summary> /// Updates a DrugEntry, using GUID as comparator /// </summary> /// <param name="drugEntry">DrugEntry to update</param> public static void UpdateDrugEntry(DrugEntry drugEntry) { //creates and opens the connection using var connection = new SQLiteConnection(_connectionInfo); connection.Open(); //create a command, set the text and set all parameters to given DrugInfo var command = connection.CreateCommand(); command.CommandText = @"UPDATE tblDrugEntries SET Time = $time, Count = $count, Notes = $notes WHERE EntryGuid LIKE $entryGuid"; command.Parameters.AddWithValue("$time", drugEntry.Time); command.Parameters.AddWithValue("$count", drugEntry.Count); command.Parameters.AddWithValue("$notes", drugEntry.Notes); command.Parameters.AddWithValue("$entryGuid", drugEntry.EntryGuid); //write to database command.ExecuteNonQuery(); }