Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.AspNetCore.Mvc;
- using Models;
- using System.Diagnostics;
- using RestSharp;
- using Models.ViewModels;
- using Microsoft.Data.Sqlite;
- namespace Controllers
- {
- public class HomeController : Controller
- {
- private readonly ILogger<HomeController> _logger;
- private string connectionString = "Data Source=todos.db";
- public HomeController(ILogger<HomeController> logger)
- {
- _logger = logger;
- }
- public IActionResult Index()
- {
- var todoList = GetAllTodos();
- return View(todoList);
- }
- public TodoViewModel GetAllTodos()
- {
- List<TodoModel> todoList = new();
- using (SqliteConnection connection = new SqliteConnection(connectionString))
- {
- using (var tableCmd = connection.CreateCommand())
- {
- connection.Open();
- tableCmd.CommandText = "SELECT * FROM todos";
- using (var reader = tableCmd.ExecuteReader())
- {
- if (reader.HasRows)
- {
- while (reader.Read())
- {
- todoList.Add(
- new TodoModel
- {
- Id = reader.GetInt32(0),
- Name = reader.GetString(1)
- });
- }
- }
- else
- {
- return new TodoViewModel
- {
- TodoList = todoList
- };
- }
- }
- }
- }
- return new TodoViewModel
- {
- TodoList = todoList
- };
- }
- public IActionResult Insert(TodoModel todo)
- {
- var todoList = GetAllTodos();
- if (!ModelState.IsValid)
- {
- todoList.Todo = todo;
- return View("Index", todoList);
- }
- using (SqliteConnection connection = new SqliteConnection(connectionString))
- {
- using (var tableCmd = connection.CreateCommand())
- {
- connection.Open();
- tableCmd.CommandText = $"INSERT INTO todos (name) VALUES ('{todo.Name}')";
- try
- {
- tableCmd.ExecuteNonQuery();
- return Redirect("https://localhost:7234");
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- }
- return Redirect("https://localhost:7234");
- }
- public IActionResult Delete(int id)
- {
- TodoViewModel todoViewModel = new();
- using (SqliteConnection connection = new SqliteConnection(connectionString))
- {
- using (var tableCmd = connection.CreateCommand())
- {
- connection.Open();
- tableCmd.CommandText = $"DELETE FROM todos WHERE Id = '{id}'";
- try
- {
- tableCmd.ExecuteNonQuery();
- todoViewModel = GetAllTodos();
- todoViewModel.Todo = null;
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- }
- return Redirect("https://localhost:7234");
- }
- public IActionResult Update(TodoModel todo)
- {
- var todoList = GetAllTodos();
- if (!ModelState.IsValid)
- {
- todoList.Todo = todo;
- return View("Index", todoList);
- }
- using (SqliteConnection connection = new SqliteConnection(connectionString))
- {
- using (var tableCmd = connection.CreateCommand())
- {
- connection.Open();
- tableCmd.CommandText = $"UPDATE todos SET Name = '{todo.Name}' WHERE Id = {todo.Id}";
- try
- {
- tableCmd.ExecuteNonQuery();
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- }
- return Redirect("https://localhost:7234");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment