public IActionResult InsertPrimeRecord([FromBody] PrimeRecord record) { if (record != null) { _primeCacheDb.InsertPrimeRecord(record); } return(Ok()); }
public void InsertPrimeRecord(PrimeRecord record) { var command = new MySqlCommand("INSERT IGNORE INTO primefactoring VALUES (@number, @isPrime, @primeFactors);", _connection); command.Parameters.Add("@number", MySqlDbType.UInt64); command.Parameters.Add("@isPrime", MySqlDbType.Bit); command.Parameters.Add("@primeFactors", MySqlDbType.JSON); command.Parameters["@number"].Value = record.Number; command.Parameters["@isPrime"].Value = record.IsPrime; command.Parameters["@primeFactors"].Value = JsonSerializer.Serialize(record.PrimeFactors); command.ExecuteNonQuery(); }
/// <summary> /// Tries to find a prime record for the given number /// </summary> /// <returns>If a record was found or not</returns> public bool TryGetPrimeRecord(ulong number, out PrimeRecord record) { var command = new MySqlCommand("SELECT * FROM primefactoring WHERE number = @number LIMIT 1;", _connection); command.Parameters.Add("@number", MySqlDbType.UInt64); command.Parameters["@number"].Value = number; using MySqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { record = GetRecordFromReader(reader); return(true); } else { record = null; return(false); } }
public async Task SubmitPrimeRecord(PrimeRecord record) { HttpContent content; using (MemoryStream jsonStream = new()) using (StreamReader reader = new(jsonStream, Encoding.UTF8)) { await JsonSerializer.SerializeAsync(jsonStream, record); jsonStream.Position = 0; string jsonString = await reader.ReadToEndAsync().ConfigureAwait(false); content = new StringContent(jsonString, Encoding.UTF8, "application/json"); } HttpResponseMessage response = await _client.PostAsync("Primes", content).ConfigureAwait(false); if (!response.IsSuccessStatusCode) { throw new Exception($"Submitting prime record failed, got status code [{response.StatusCode}]."); } }
private static async Task <bool> SubmitPrimeNumber(PrimeRecord record) { HttpClient client = new HttpClient() { BaseAddress = new Uri("http://192.168.1.18:30006") }; HttpContent content; using (MemoryStream jsonStream = new ()) using (StreamReader reader = new (jsonStream, Encoding.UTF8)) { await JsonSerializer.SerializeAsync(jsonStream, record); jsonStream.Position = 0; string jsonString = await reader.ReadToEndAsync(); content = new StringContent(jsonString, Encoding.UTF8, "application/json"); } HttpResponseMessage response = await client.PostAsync("Primes", content); return(response.IsSuccessStatusCode); }