Guest User

Untitled

a guest
Oct 7th, 2023
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. using Microsoft.AspNetCore.Mvc;
  2. using Models;
  3. using System.Diagnostics;
  4. using RestSharp;
  5. using Models.ViewModels;
  6. using Microsoft.Data.Sqlite;
  7.  
  8. namespace Controllers
  9. {
  10. public class HomeController : Controller
  11. {
  12. private readonly ILogger<HomeController> _logger;
  13.  
  14. private string connectionString = "Data Source=todos.db";
  15.  
  16. public HomeController(ILogger<HomeController> logger)
  17. {
  18. _logger = logger;
  19. }
  20.  
  21. public IActionResult Index()
  22. {
  23. var todoList = GetAllTodos();
  24. return View(todoList);
  25. }
  26.  
  27. public TodoViewModel GetAllTodos()
  28. {
  29. List<TodoModel> todoList = new();
  30.  
  31. using (SqliteConnection connection = new SqliteConnection(connectionString))
  32. {
  33. using (var tableCmd = connection.CreateCommand())
  34. {
  35. connection.Open();
  36. tableCmd.CommandText = "SELECT * FROM todos";
  37.  
  38. using (var reader = tableCmd.ExecuteReader())
  39. {
  40. if (reader.HasRows)
  41. {
  42. while (reader.Read())
  43. {
  44. todoList.Add(
  45. new TodoModel
  46. {
  47. Id = reader.GetInt32(0),
  48. Name = reader.GetString(1)
  49. });
  50. }
  51. }
  52. else
  53. {
  54. return new TodoViewModel
  55. {
  56. TodoList = todoList
  57. };
  58. }
  59. }
  60. }
  61. }
  62.  
  63. return new TodoViewModel
  64. {
  65. TodoList = todoList
  66. };
  67. }
  68.  
  69. public IActionResult Insert(TodoModel todo)
  70. {
  71. var todoList = GetAllTodos();
  72.  
  73. if (!ModelState.IsValid)
  74. {
  75. todoList.Todo = todo;
  76.  
  77. return View("Index", todoList);
  78. }
  79.  
  80. using (SqliteConnection connection = new SqliteConnection(connectionString))
  81. {
  82. using (var tableCmd = connection.CreateCommand())
  83. {
  84. connection.Open();
  85. tableCmd.CommandText = $"INSERT INTO todos (name) VALUES ('{todo.Name}')";
  86. try
  87. {
  88. tableCmd.ExecuteNonQuery();
  89. return Redirect("https://localhost:7234");
  90. }
  91. catch (Exception ex)
  92. {
  93. Console.WriteLine(ex.Message);
  94. }
  95. }
  96. }
  97. return Redirect("https://localhost:7234");
  98. }
  99.  
  100. public IActionResult Delete(int id)
  101. {
  102. TodoViewModel todoViewModel = new();
  103.  
  104. using (SqliteConnection connection = new SqliteConnection(connectionString))
  105. {
  106. using (var tableCmd = connection.CreateCommand())
  107. {
  108. connection.Open();
  109. tableCmd.CommandText = $"DELETE FROM todos WHERE Id = '{id}'";
  110. try
  111. {
  112. tableCmd.ExecuteNonQuery();
  113.  
  114. todoViewModel = GetAllTodos();
  115.  
  116. todoViewModel.Todo = null;
  117. }
  118. catch (Exception ex)
  119. {
  120. Console.WriteLine(ex.Message);
  121. }
  122. }
  123. }
  124.  
  125. return Redirect("https://localhost:7234");
  126. }
  127.  
  128. public IActionResult Update(TodoModel todo)
  129. {
  130. var todoList = GetAllTodos();
  131.  
  132. if (!ModelState.IsValid)
  133. {
  134. todoList.Todo = todo;
  135.  
  136. return View("Index", todoList);
  137. }
  138.  
  139. using (SqliteConnection connection = new SqliteConnection(connectionString))
  140. {
  141. using (var tableCmd = connection.CreateCommand())
  142. {
  143. connection.Open();
  144. tableCmd.CommandText = $"UPDATE todos SET Name = '{todo.Name}' WHERE Id = {todo.Id}";
  145. try
  146. {
  147. tableCmd.ExecuteNonQuery();
  148. }
  149. catch (Exception ex)
  150. {
  151. Console.WriteLine(ex.Message);
  152. }
  153. }
  154. }
  155.  
  156. return Redirect("https://localhost:7234");
  157. }
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment