@@ -35,8 +35,7 @@ public async Task<IActionResult> StudentData2(Student student)
3535 using IDbConnection dbConnection = new SqlConnection ( _connectionString ) ;
3636 dbConnection . Open ( ) ;
3737
38- // Assuming your table name is 'Students'
39- string query = "INSERT INTO Students (name, email, phone, image) VALUES (@Name, @Email, @Phone, @Image)" ;
38+ string query = "INSERT INTO TBLB_Student (name, email, phone, image) VALUES (@Name, @Email, @Phone, @Image)" ;
4039 int rowsAffected = await dbConnection . ExecuteAsync ( query , student ) ;
4140
4241 if ( rowsAffected > 0 )
@@ -60,11 +59,47 @@ public async Task<IActionResult> GetAllStudents()
6059 {
6160 try
6261 {
62+ /*
63+
64+ //This code assumes that the token is in the "Bearer <token>" format in the Authorization header.
65+ //It splits the header and takes the last part as the token for validation. If your token format is
66+ //different, adjust the code accordingly.
67+
68+ // Retrieve the user name from the claims
69+ var userName = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
70+
71+ // Retrieve the token from the Authorization header
72+ var token = HttpContext.Request.Headers.Authorization.FirstOrDefault()?.Split(" ").Last();
73+
74+ if (string.IsNullOrEmpty(token))
75+ {
76+ return Unauthorized("Token not provided");
77+ }
78+
79+ // Validate the token
80+ var tokenHandler = new JwtSecurityTokenHandler();
81+ var validationParameters = new TokenValidationParameters
82+ {
83+ ValidateIssuer = true,
84+ ValidateAudience = true,
85+ ValidateLifetime = true,
86+ ValidateIssuerSigningKey = true,
87+ ValidIssuer = configuration?["Jwt:Issuer"]?.ToString(),
88+ ValidAudience = configuration?["Jwt:Audience"]?.ToString(),
89+ IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration?["Jwt:Key"]?.PadRight(32)))
90+ };
91+
92+ var principal = tokenHandler.ValidateToken(token, validationParameters, out SecurityToken validatedToken);
93+
94+ // At this point, the token is valid, and you can retrieve additional claims
95+ var dateOfJoin = principal.FindFirst("DateOfJoin")?.Value;
96+
97+ */
98+
6399 using IDbConnection dbConnection = new SqlConnection ( _connectionString ) ;
64100 dbConnection . Open ( ) ;
65101
66- // Assuming your table name is 'Students'
67- string query = "SELECT * FROM Students" ;
102+ string query = "SELECT * FROM TBLB_Student" ;
68103 var students = await dbConnection . QueryAsync < Student > ( query ) ;
69104
70105 return Ok ( students ) ;
@@ -75,7 +110,6 @@ public async Task<IActionResult> GetAllStudents()
75110 }
76111 }
77112
78-
79113 [ HttpPost ( "insertstudent" ) ]
80114 public async Task < IActionResult > InsertStudent ( Student student )
81115 {
@@ -87,12 +121,12 @@ public async Task<IActionResult> InsertStudent(Student student)
87121 if ( student . rollNumber . HasValue && student . rollNumber > 0 )
88122 {
89123 // Update record if rollNumber is greater than 0
90- string updateQuery = "UPDATE Students SET name = @Name, email = @Email, phone = @Phone, image = @Image WHERE rollNumber = @rollNumber;" ;
124+ string updateQuery = "UPDATE TBLB_Student SET name = @Name, email = @Email, phone = @Phone, image = @Image WHERE rollNumber = @rollNumber;" ;
91125 int rowsAffected = await dbConnection . ExecuteAsync ( updateQuery , student ) ;
92126
93127 if ( rowsAffected > 0 )
94128 {
95- string query = "SELECT * FROM Students " ;
129+ string query = "SELECT * FROM TBLB_Student " ;
96130 var students = await dbConnection . QueryAsync < Student > ( query ) ;
97131
98132 return Ok ( students ) ;
@@ -105,12 +139,12 @@ public async Task<IActionResult> InsertStudent(Student student)
105139 else
106140 {
107141 // Insert record if rollNumber is not provided or less than or equal to 0
108- string insertQuery = "INSERT INTO Students (name, email, phone, image) VALUES (@Name, @Email, @Phone, @Image);" ;
142+ string insertQuery = "INSERT INTO TBLB_Student (name, email, phone, image) VALUES (@Name, @Email, @Phone, @Image);" ;
109143 int rowsAffected = await dbConnection . ExecuteAsync ( insertQuery , student ) ;
110144
111145 if ( rowsAffected > 0 )
112146 {
113- string query = "SELECT * FROM Students " ;
147+ string query = "SELECT * FROM TBLB_Student " ;
114148 var students = await dbConnection . QueryAsync < Student > ( query ) ;
115149
116150 return Ok ( students ) ;
@@ -135,8 +169,7 @@ public async Task<IActionResult> DeleteStudent(int rollNumber)
135169 using IDbConnection dbConnection = new SqlConnection ( _connectionString ) ;
136170 dbConnection . Open ( ) ;
137171
138- // Assuming your table name is 'Students'
139- string query = "DELETE FROM Students WHERE rollNumber = @rollNumber" ;
172+ string query = "DELETE FROM TBLB_Student WHERE rollNumber = @rollNumber" ;
140173 int rowsAffected = await dbConnection . ExecuteAsync ( query , new { RollNumber = rollNumber } ) ;
141174
142175 if ( rowsAffected > 0 )
0 commit comments