-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResidentsController.cs
More file actions
136 lines (122 loc) · 4.86 KB
/
ResidentsController.cs
File metadata and controls
136 lines (122 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using Backend.Data;
using Backend.Contracts;
using Backend.Infrastructure;
using Backend.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Text.Json;
namespace Backend.Controllers;
[ApiController]
[Route("api/residents")]
[Authorize(Policy = AuthPolicies.StaffOrAdmin)]
public class ResidentsController(AppDbContext db) : ControllerBase
{
[HttpGet]
public async Task<IActionResult> GetAll(
[FromQuery] string? status,
[FromQuery] int? safehouseId,
[FromQuery] string? riskLevel,
[FromQuery] string? search)
{
var query = db.Residents.AsQueryable();
if (!string.IsNullOrEmpty(status)) query = query.Where(r => r.CaseStatus == status);
if (safehouseId.HasValue) query = query.Where(r => r.SafehouseId == safehouseId);
if (!string.IsNullOrEmpty(riskLevel)) query = query.Where(r => r.CurrentRiskLevel == riskLevel);
if (!string.IsNullOrEmpty(search)) query = query.Where(r =>
(r.CaseControlNo != null && r.CaseControlNo.Contains(search)) ||
(r.InternalCode != null && r.InternalCode.Contains(search)) ||
(r.AssignedSocialWorker != null && r.AssignedSocialWorker.Contains(search)));
return Ok(await query.OrderBy(r => r.ResidentId).ToListAsync());
}
[HttpGet("case-conferences")]
public async Task<IActionResult> GetCaseConferences()
{
var plans = await db.InterventionPlans
.Where(p => p.CaseConferenceDate.HasValue)
.OrderByDescending(p => p.CaseConferenceDate)
.ToListAsync();
return Ok(plans);
}
[HttpGet("{id}")]
public async Task<IActionResult> GetById(int id)
{
var resident = await db.Residents.FindAsync(id);
return resident is null ? NotFound() : Ok(resident);
}
[HttpPost]
[Authorize(Roles = AuthRoles.Admin)]
public async Task<IActionResult> Create([FromBody] ResidentWriteRequest request)
{
if (!RequestValidation.TryValidate(request, out var validationProblem, "Unable to save resident."))
return BadRequest(validationProblem);
var resident = new Resident();
CrudWriteMapper.ApplyResident(resident, request);
resident.ResidentId = await db.Residents.AnyAsync() ? await db.Residents.MaxAsync(r => r.ResidentId) + 1 : 1;
resident.CreatedAt = DateTime.UtcNow;
db.Residents.Add(resident);
await db.SaveChangesAsync();
return CreatedAtAction(nameof(GetById), new { id = resident.ResidentId }, resident);
}
[HttpPut("{id}")]
[Authorize(Roles = AuthRoles.Admin)]
public async Task<IActionResult> Update(int id, [FromBody] JsonElement body)
{
if (!JsonRequestPatch<ResidentWriteRequest>.TryParse(body, out var patch, out var parseProblem))
return BadRequest(parseProblem);
if (!RequestValidation.TryValidate(patch!.Model, out var validationProblem, "Unable to update resident."))
return BadRequest(validationProblem);
var existing = await db.Residents.FindAsync(id);
if (existing is null) return NotFound();
CrudWriteMapper.ApplyResident(existing, patch.Model, patch);
existing.ResidentId = id;
await db.SaveChangesAsync();
return Ok(existing);
}
[HttpDelete("{id}")]
[Authorize(Roles = AuthRoles.Admin)]
public async Task<IActionResult> Delete(int id)
{
var resident = await db.Residents.FindAsync(id);
if (resident is null) return NotFound();
db.Residents.Remove(resident);
await db.SaveChangesAsync();
return NoContent();
}
[HttpGet("{id}/education-records")]
public async Task<IActionResult> GetEducationRecords(int id)
{
var records = await db.EducationRecords
.Where(e => e.ResidentId == id)
.OrderByDescending(e => e.RecordDate)
.ToListAsync();
return Ok(records);
}
[HttpGet("{id}/health-records")]
public async Task<IActionResult> GetHealthRecords(int id)
{
var records = await db.HealthWellbeingRecords
.Where(h => h.ResidentId == id)
.OrderByDescending(h => h.RecordDate)
.ToListAsync();
return Ok(records);
}
[HttpGet("{id}/incident-reports")]
public async Task<IActionResult> GetIncidentReports(int id)
{
var reports = await db.IncidentReports
.Where(i => i.ResidentId == id)
.OrderByDescending(i => i.IncidentDate)
.ToListAsync();
return Ok(reports);
}
[HttpGet("{id}/intervention-plans")]
public async Task<IActionResult> GetInterventionPlans(int id)
{
var plans = await db.InterventionPlans
.Where(p => p.ResidentId == id)
.OrderByDescending(p => p.CreatedAt)
.ToListAsync();
return Ok(plans);
}
}