|
| 1 | +using Microsoft.AspNetCore.Http; |
| 2 | +using Microsoft.AspNetCore.Mvc; |
| 3 | +using System.Data; |
| 4 | +using System.Data.SqlClient; |
| 5 | + |
| 6 | +namespace backend.Controllers |
| 7 | +{ |
| 8 | + [Route("api/[controller]")] |
| 9 | + [ApiController] |
| 10 | + public class TodoController : ControllerBase |
| 11 | + { |
| 12 | + private IConfiguration _configuration; |
| 13 | + |
| 14 | + |
| 15 | + public TodoController(IConfiguration configuration) |
| 16 | + { |
| 17 | + _configuration = configuration; |
| 18 | + } |
| 19 | + |
| 20 | + [HttpGet] |
| 21 | + [Route("GetNotes")] |
| 22 | + public JsonResult GetNotes() |
| 23 | + { |
| 24 | + string query = "select * from dbo.Notes"; |
| 25 | + DataTable table = new DataTable(); |
| 26 | + string sqlDatasource = _configuration.GetConnectionString("todoAppDB"); |
| 27 | + SqlDataReader myReader; |
| 28 | + using(SqlConnection myCon=new SqlConnection(sqlDatasource)) |
| 29 | + { |
| 30 | + myCon.Open(); |
| 31 | + using(SqlCommand myCommand = new SqlCommand(query, myCon)) { |
| 32 | + myReader=myCommand.ExecuteReader(); |
| 33 | + table.Load(myReader); |
| 34 | + myReader.Close(); |
| 35 | + myCon.Close(); |
| 36 | + } |
| 37 | + } |
| 38 | + return new JsonResult(table); |
| 39 | + } |
| 40 | + |
| 41 | + [HttpPost] |
| 42 | + [Route("AddNotes")] |
| 43 | + public JsonResult AddNotes([FromForm] string newNotes) |
| 44 | + { |
| 45 | + string query = "insert into dbo.Notes values(@newNotes)"; |
| 46 | + DataTable table = new DataTable(); |
| 47 | + string sqlDatasource = _configuration.GetConnectionString("todoAppDB"); |
| 48 | + SqlDataReader myReader; |
| 49 | + using(SqlConnection myCon=new SqlConnection(sqlDatasource)) |
| 50 | + { |
| 51 | + myCon.Open(); |
| 52 | + using(SqlCommand myCommand = new SqlCommand(query, myCon)) { |
| 53 | + myCommand.Parameters.AddWithValue("@newNotes", newNotes); |
| 54 | + myReader=myCommand.ExecuteReader(); |
| 55 | + table.Load(myReader) ; |
| 56 | + myReader.Close(); |
| 57 | + myCon.Close(); |
| 58 | + } |
| 59 | + } |
| 60 | + return new JsonResult("Added Successfully"); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + [HttpGet] |
| 65 | + [Route("GetNotesById")] |
| 66 | + public JsonResult GetNotesById([FromForm] string id) |
| 67 | + { |
| 68 | + string query = "select * from dbo.Notes where id=@id"; |
| 69 | + DataTable table = new DataTable(); |
| 70 | + string sqlDatasource = _configuration.GetConnectionString("todoAppDB"); |
| 71 | + SqlDataReader myReader; |
| 72 | + using (SqlConnection myCon = new SqlConnection(sqlDatasource)) |
| 73 | + { |
| 74 | + myCon.Open(); |
| 75 | + using (SqlCommand myCommand = new SqlCommand(query, myCon)) |
| 76 | + { |
| 77 | + myReader = myCommand.ExecuteReader(); |
| 78 | + table.Load(myReader); |
| 79 | + myReader.Close(); |
| 80 | + myCon.Close(); |
| 81 | + } |
| 82 | + } |
| 83 | + return new JsonResult(table); |
| 84 | + } |
| 85 | + |
| 86 | + [HttpPost] |
| 87 | + [Route("UpdateNotes")] |
| 88 | + public JsonResult UpdateNotes(string id, string description) |
| 89 | + { |
| 90 | + string query = "update dbo.Notes set description=@description where id=@id"; |
| 91 | + DataTable table = new DataTable(); |
| 92 | + string sqlDatasource = _configuration.GetConnectionString("todoAppDB"); |
| 93 | + SqlDataReader myReader; |
| 94 | + using (SqlConnection myCon = new SqlConnection(sqlDatasource)) |
| 95 | + { |
| 96 | + myCon.Open(); |
| 97 | + using (SqlCommand myCommand = new SqlCommand(query, myCon)) |
| 98 | + { |
| 99 | + myCommand.Parameters.AddWithValue("@id", id); |
| 100 | + myCommand.Parameters.AddWithValue("@description", description); |
| 101 | + myReader = myCommand.ExecuteReader(); |
| 102 | + table.Load(myReader); |
| 103 | + myReader.Close(); |
| 104 | + myCon.Close(); |
| 105 | + } |
| 106 | + } |
| 107 | + return new JsonResult("updated Successfully"); |
| 108 | + } |
| 109 | + |
| 110 | + [HttpDelete] |
| 111 | + [Route("DeleteNote")] |
| 112 | + public JsonResult DeleteNote([FromForm] string id) |
| 113 | + { |
| 114 | + string query = "delete from dbo.Notes where id=@id"; |
| 115 | + DataTable table = new DataTable(); |
| 116 | + string sqlDatasource = _configuration.GetConnectionString("todoAppDB"); |
| 117 | + SqlDataReader myReader; |
| 118 | + using (SqlConnection myCon = new SqlConnection(sqlDatasource)) |
| 119 | + { |
| 120 | + myCon.Open(); |
| 121 | + using (SqlCommand myCommand = new SqlCommand(query, myCon)) |
| 122 | + { |
| 123 | + myCommand.Parameters.AddWithValue("@id", id); |
| 124 | + myReader = myCommand.ExecuteReader(); |
| 125 | + table.Load(myReader); |
| 126 | + myReader.Close(); |
| 127 | + myCon.Close(); |
| 128 | + } |
| 129 | + } |
| 130 | + return new JsonResult("Notes deleted Successfully"); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments