public async Task <SetCalendarLogRs> SetEvent(SetCalendarLogRq request) { var result = new SetCalendarLogRs(); try { _logger.LogDebug("Getting sensor data from api"); var baseUrl = _configuration.GetSection("PlantApi").GetSection("BaseUrl").Value.ToString(); _logger.LogInformation($"ApiBaseUrl -> {baseUrl}"); var httpRequest = new HttpRequestMessage(HttpMethod.Post, $"{baseUrl}api/Calendar"); httpRequest.Content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"); var client = _clientFactory.CreateClient(); var response = await client.SendAsync(httpRequest); if (response.IsSuccessStatusCode) { result = await response.Content.ReadAsAsync <SetCalendarLogRs> (); } else { result = null; } } catch (System.Exception ex) { _logger.LogError(ex.Message); throw; } return(result); }
public IActionResult Post([FromBody] SetCalendarLogRq request) { var result = new CalendarLogRs(); if (request == null || string.IsNullOrEmpty(request.Title)) { return(StatusCode(400, "Please fill correctly request.")); } else { _logger.LogInformation($" [Request] ... {JsonConvert.SerializeObject(request)}"); } try { using (MySqlConnection connection = new MySqlConnection(_appSettings.GetDataBaseConnectionString())) { connection.Open(); var query = $"INSERT INTO " + $"CALENDAR_EVENT(`title`,`start`,`end`,`timestamp`) " + $"VALUES (@title,@start,@end,@timestamp)"; var command = new MySqlCommand(query, connection); command.Parameters.Add("@title", MySqlDbType.String); command.Parameters["@title"].Value = request.Title; command.Parameters.Add("@start", MySqlDbType.DateTime); command.Parameters["@start"].Value = request.Start; command.Parameters.Add("@end", MySqlDbType.DateTime); command.Parameters["@end"].Value = request.End; command.Parameters.Add("@timestamp", MySqlDbType.DateTime); command.Parameters["@timestamp"].Value = DateTime.Now; Int32 id = (int)command.ExecuteScalar(); result.Id = id; _logger.LogInformation($" [Insered id] ... {id}"); } } catch (System.Exception ex) { _logger.LogError($"{ex.Message}"); return(StatusCode(500)); } return(StatusCode(200, result)); }