-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSupportersController.cs
More file actions
96 lines (86 loc) · 3.57 KB
/
SupportersController.cs
File metadata and controls
96 lines (86 loc) · 3.57 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
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/supporters")]
[Authorize(Policy = AuthPolicies.StaffOrAdmin)]
public class SupportersController(AppDbContext db) : ControllerBase
{
[HttpGet]
public async Task<IActionResult> GetAll(
[FromQuery] string? type,
[FromQuery] string? status,
[FromQuery] string? search)
{
var query = db.Supporters.AsQueryable();
if (!string.IsNullOrEmpty(type)) query = query.Where(s => s.SupporterType == type);
if (!string.IsNullOrEmpty(status)) query = query.Where(s => s.Status == status);
if (!string.IsNullOrEmpty(search)) query = query.Where(s =>
(s.DisplayName != null && s.DisplayName.Contains(search)) ||
(s.FirstName != null && s.FirstName.Contains(search)) ||
(s.LastName != null && s.LastName.Contains(search)) ||
(s.Email != null && s.Email.Contains(search)));
return Ok(await query.OrderBy(s => s.SupporterId).ToListAsync());
}
[HttpGet("{id}")]
public async Task<IActionResult> GetById(int id)
{
var supporter = await db.Supporters.FindAsync(id);
return supporter is null ? NotFound() : Ok(supporter);
}
[HttpPost]
[Authorize(Roles = AuthRoles.Admin)]
public async Task<IActionResult> Create([FromBody] SupporterWriteRequest request)
{
if (!RequestValidation.TryValidate(request, out var validationProblem, "Unable to save supporter."))
return BadRequest(validationProblem);
var supporter = new Supporter();
CrudWriteMapper.ApplySupporter(supporter, request);
supporter.SupporterId = await db.Supporters.AnyAsync() ? await db.Supporters.MaxAsync(s => s.SupporterId) + 1 : 1;
supporter.CreatedAt = DateTime.UtcNow;
supporter.Status ??= "Active";
db.Supporters.Add(supporter);
await db.SaveChangesAsync();
return CreatedAtAction(nameof(GetById), new { id = supporter.SupporterId }, supporter);
}
[HttpPut("{id}")]
[Authorize(Roles = AuthRoles.Admin)]
public async Task<IActionResult> Update(int id, [FromBody] JsonElement body)
{
if (!JsonRequestPatch<SupporterWriteRequest>.TryParse(body, out var patch, out var parseProblem))
return BadRequest(parseProblem);
if (!RequestValidation.TryValidate(patch!.Model, out var validationProblem, "Unable to update supporter."))
return BadRequest(validationProblem);
var existing = await db.Supporters.FindAsync(id);
if (existing is null) return NotFound();
CrudWriteMapper.ApplySupporter(existing, patch.Model, patch);
existing.SupporterId = id;
await db.SaveChangesAsync();
return Ok(existing);
}
[HttpDelete("{id}")]
[Authorize(Roles = AuthRoles.Admin)]
public async Task<IActionResult> Delete(int id)
{
var supporter = await db.Supporters.FindAsync(id);
if (supporter is null) return NotFound();
db.Supporters.Remove(supporter);
await db.SaveChangesAsync();
return NoContent();
}
[HttpGet("{id}/donations")]
public async Task<IActionResult> GetDonations(int id)
{
var donations = await db.Donations
.Where(d => d.SupporterId == id)
.OrderByDescending(d => d.DonationDate)
.ToListAsync();
return Ok(donations);
}
}