source: iknow-api/Controllers/DbHealthController.cs

Last change on this file was ea40556, checked in by Stefan-Saveski <stefansaveski19@…>, 8 days ago
  • ready for presentation
  • Property mode set to 100644
File size: 1.5 KB
Line 
1using iknow_api.Data;
2using Microsoft.AspNetCore.Mvc;
3using Microsoft.EntityFrameworkCore;
4
5namespace iknow_api.Controllers;
6
7[ApiController]
8[Route("[controller]")]
9public class DbHealthController(AppDbContext db) : ControllerBase
10{
11 /// <summary>
12 /// Verifies that the SSH tunnel is up, the DB credentials work, and that
13 /// the entity mapping matches the schema created by sql/ddl.sql.
14 /// </summary>
15 [HttpGet]
16 public async Task<IActionResult> Get()
17 {
18 try
19 {
20 // EF projects SqlQuery onto a column named "Value", hence the alias.
21 var version = await db.Database
22 .SqlQuery<string>($"SELECT version() AS \"Value\"")
23 .SingleAsync();
24
25 return Ok(new
26 {
27 connected = true,
28 server = version,
29 counts = new
30 {
31 users = await db.User.CountAsync(),
32 subjects = await db.Subjects.CountAsync(),
33 enrolledSemesters = await db.EnrolledSemesters.CountAsync(),
34 semesterSubjects = await db.SemesterSubjects.CountAsync(),
35 passedSubjects = await db.PassedSubjects.CountAsync(),
36 }
37 });
38 }
39 catch (Exception ex)
40 {
41 return StatusCode(503, new
42 {
43 connected = false,
44 error = ex.Message,
45 hint = "Is tunnel_scripta.cmd running and listening on localhost:9999?"
46 });
47 }
48 }
49}
Note: See TracBrowser for help on using the repository browser.