/// <summary> /// Insert new ticket booking record into database /// </summary> /// <returns></returns> public async Task <bool> BookTrain(TrainBookingDetails bookingDetails) { try { using (SqlConnection sqlConnection = new SqlConnection(_appSettings.Value.DbConnection)) { await sqlConnection.OpenAsync(); string command = "Insert Into dbo.Bookings (trainId, source, destination, dateOfJourney, bookingTime) " + "VALUES (@trainId, @source, @destination, @date, @bookingTime) "; SqlCommand cmd = new SqlCommand(command, sqlConnection); cmd.Parameters.Add("@trainId", SqlDbType.Int, 100).Value = bookingDetails.TrainId; cmd.Parameters.Add("@source", SqlDbType.Int, 100).Value = bookingDetails.SourceId; cmd.Parameters.Add("@destination", SqlDbType.Int, 100).Value = bookingDetails.DestinationId; cmd.Parameters.Add("@date", SqlDbType.Date, 100).Value = DateTime.Parse(bookingDetails.DateOfJourney.ToString()); cmd.Parameters.Add("@bookingTime", SqlDbType.DateTime, 100).Value = DateTime.Now.ToString(); await cmd.ExecuteNonQueryAsync(); sqlConnection.Close(); return(true); } } catch (Exception) { throw; } }
/// <summary> /// Method used for sending an email after successful insertion of ticket booking /// </summary> /// <param name="bookingDetails"></param> /// <returns></returns> public bool SendEmail(TrainBookingDetails bookingDetails) { _logger.LogInformation("Accessed SendEmail Method in BusinessManager"); MailMessage mail = new MailMessage(); SmtpClient smtp = new SmtpClient(); try { mail.From = new MailAddress(_appSettings.Value.Email); mail.To.Add(new MailAddress("*****@*****.**")); mail.Subject = "Ticket Booking Confirmation"; mail.IsBodyHtml = true; //to make message body as html mail.Body = GetHtmlBody(bookingDetails); smtp.Port = 587; smtp.Host = "smtp.gmail.com"; smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; smtp.Credentials = new NetworkCredential(_appSettings.Value.Email, _appSettings.Value.Password); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Send(mail); return(true); } catch (Exception) { throw; } }
/// <summary> /// Method will be used for adding booking details /// </summary> /// <param name="bookingDetails"></param> /// <returns></returns> public async Task <bool> BookTrain(TrainBookingDetails bookingDetails) { _logger.LogInformation("Accessed BookTrain Method in BusinessManager"); try { return(await _trainData.BookTrain(bookingDetails)); } catch (Exception) { throw; } }
/// <summary> /// Method used for parsing Email HTML Body /// </summary> /// <param name="bookingDetails"></param> /// <returns></returns> public static string GetHtmlBody(TrainBookingDetails bookingDetails) { try { string messageBody = "<font>Please find the ticket confirmation: </font><br><br>"; if (bookingDetails == null) { return(messageBody); } string htmlTableStart = "<table style=\"border-collapse:collapse; text-align:center;\" >"; string htmlTableEnd = "</table>"; string htmlHeaderRowStart = "<tr style=\"background-color:#6FA1D2; color:#ffffff;\">"; string htmlHeaderRowEnd = "</tr>"; string htmlTrStart = "<tr style=\"color:#555555;\">"; string htmlTrEnd = "</tr>"; string htmlTdStart = "<td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">"; string htmlTdEnd = "</td>"; messageBody += htmlTableStart; messageBody += htmlHeaderRowStart; messageBody += htmlTdStart + "Train Number" + htmlTdEnd; messageBody += htmlTdStart + "Train Name" + htmlTdEnd; messageBody += htmlTdStart + "Source" + htmlTdEnd; messageBody += htmlTdStart + "Destination" + htmlTdEnd; messageBody += htmlTdStart + "Date Of Journey" + htmlTdEnd; messageBody += htmlTdStart + "Booking Status" + htmlTdEnd; messageBody += htmlHeaderRowEnd; messageBody = messageBody + htmlTrStart; messageBody = messageBody + htmlTdStart + bookingDetails.TrainNumber + htmlTdEnd; messageBody = messageBody + htmlTdStart + bookingDetails.TrainName + htmlTdEnd; messageBody = messageBody + htmlTdStart + bookingDetails.Source + htmlTdEnd; messageBody = messageBody + htmlTdStart + bookingDetails.Destination + htmlTdEnd; messageBody = messageBody + htmlTdStart + DateTime.Parse(bookingDetails.DateOfJourney.ToString()) + htmlTdEnd; messageBody = messageBody + htmlTdStart + "Success" + htmlTdEnd; messageBody = messageBody + htmlTrEnd; messageBody = messageBody + htmlTableEnd; return(messageBody); } catch (Exception) { throw; } }
public async Task <IActionResult> BookTicket([FromBody] TrainBookingDetails bookingDetails) { _logger.LogInformation("Accessed BookTicket Method"); try { bool bookingStatus = await _businessManager.BookTrain(bookingDetails); if (bookingStatus) { _businessManager.SendEmail(bookingDetails); } return(Ok(bookingStatus)); } catch (Exception ex) { _logger.LogInformation("Error while accessing stations data from database", ex.Message); return(StatusCode(Microsoft.AspNetCore.Http.StatusCodes.Status500InternalServerError, "There is an error while processing your request")); } }