-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDonationsController.cs
More file actions
157 lines (143 loc) · 5.51 KB
/
DonationsController.cs
File metadata and controls
157 lines (143 loc) · 5.51 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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/donations")]
[Authorize(Policy = AuthPolicies.AdminOnly)]
public class DonationsController(AppDbContext db) : ControllerBase
{
[HttpGet]
public async Task<IActionResult> GetAll(
[FromQuery] string? campaignName,
[FromQuery] string? donationType,
[FromQuery] DateOnly? from,
[FromQuery] DateOnly? to)
{
var query = db.Donations.AsQueryable();
if (!string.IsNullOrEmpty(campaignName)) query = query.Where(d => d.CampaignName == campaignName);
if (!string.IsNullOrEmpty(donationType)) query = query.Where(d => d.DonationType == donationType);
if (from.HasValue) query = query.Where(d => d.DonationDate >= from);
if (to.HasValue) query = query.Where(d => d.DonationDate <= to);
return Ok(await query.OrderBy(d => d.DonationId).ToListAsync());
}
[HttpGet("summary")]
public async Task<IActionResult> GetSummary()
{
var donations = await db.Donations.ToListAsync();
var summary = new
{
totalCount = donations.Count,
totalAmount = donations.Where(d => d.Amount.HasValue).Sum(d => d.Amount!.Value),
recurringCount = donations.Count(d => d.IsRecurring == true),
campaigns = donations
.GroupBy(d => d.CampaignName)
.Select(g => new
{
campaignName = g.Key,
count = g.Count(),
totalAmount = g.Where(d => d.Amount.HasValue).Sum(d => d.Amount!.Value)
})
.ToList()
};
return Ok(summary);
}
[HttpGet("trends")]
public async Task<IActionResult> GetTrends()
{
var donations = await db.Donations.Where(d => d.DonationDate.HasValue).ToListAsync();
var trends = donations
.GroupBy(d => new { d.DonationDate!.Value.Month, d.DonationDate!.Value.Year })
.Select(g => new
{
month = g.Key.Month,
year = g.Key.Year,
totalAmount = g.Where(d => d.Amount.HasValue).Sum(d => d.Amount!.Value),
count = g.Count(),
recurringCount = g.Count(d => d.IsRecurring == true)
})
.OrderBy(t => t.year).ThenBy(t => t.month)
.ToList();
return Ok(trends);
}
[HttpGet("channels")]
public async Task<IActionResult> GetChannels()
{
var donations = await db.Donations.ToListAsync();
var channels = donations
.GroupBy(d => d.ChannelSource)
.Select(g => new
{
channel = g.Key,
count = g.Count(),
totalAmount = g.Where(d => d.Amount.HasValue).Sum(d => d.Amount!.Value)
})
.ToList();
return Ok(channels);
}
[HttpGet("{id}")]
public async Task<IActionResult> GetById(int id)
{
var donation = await db.Donations.FindAsync(id);
return donation is null ? NotFound() : Ok(donation);
}
[HttpPost]
[Authorize(Roles = AuthRoles.Admin)]
public async Task<IActionResult> Create([FromBody] DonationWriteRequest request)
{
if (!RequestValidation.TryValidate(request, out var validationProblem, "Unable to save donation."))
return BadRequest(validationProblem);
var donation = new Donation();
CrudWriteMapper.ApplyDonation(donation, request);
donation.DonationId = await db.Donations.AnyAsync() ? await db.Donations.MaxAsync(d => d.DonationId) + 1 : 1;
db.Donations.Add(donation);
await db.SaveChangesAsync();
return CreatedAtAction(nameof(GetById), new { id = donation.DonationId }, donation);
}
[HttpPut("{id}")]
[Authorize(Roles = AuthRoles.Admin)]
public async Task<IActionResult> Update(int id, [FromBody] JsonElement body)
{
if (!JsonRequestPatch<DonationWriteRequest>.TryParse(body, out var patch, out var parseProblem))
return BadRequest(parseProblem);
if (!RequestValidation.TryValidate(patch!.Model, out var validationProblem, "Unable to update donation."))
return BadRequest(validationProblem);
var existing = await db.Donations.FindAsync(id);
if (existing is null) return NotFound();
CrudWriteMapper.ApplyDonation(existing, patch.Model, patch);
existing.DonationId = id;
await db.SaveChangesAsync();
return Ok(existing);
}
[HttpDelete("{id}")]
[Authorize(Roles = AuthRoles.Admin)]
public async Task<IActionResult> Delete(int id)
{
var donation = await db.Donations.FindAsync(id);
if (donation is null) return NotFound();
db.Donations.Remove(donation);
await db.SaveChangesAsync();
return NoContent();
}
[HttpGet("{id}/allocations")]
public async Task<IActionResult> GetAllocations(int id)
{
var allocations = await db.DonationAllocations
.Where(a => a.DonationId == id)
.ToListAsync();
return Ok(allocations);
}
[HttpGet("{id}/in-kind-items")]
public async Task<IActionResult> GetInKindItems(int id)
{
var items = await db.InKindDonationItems
.Where(i => i.DonationId == id)
.ToListAsync();
return Ok(items);
}
}