profileShare

rasmusjy / splitapp-backend-clean-onion

Read-only snapshot

No repository description.

main default branch 429 files Expires Sep 13, 2026, 9:06 AM
SplitPresetsController.cs 4,850 bytes
1 using App.BLL.DTO;
2 using App.BLL.Services;
3 using App.Domain;
4 using App.DTO.Mappers;
5 using App.DTO.v1;
6 using Asp.Versioning;
7 using Microsoft.AspNetCore.Authentication.JwtBearer;
8 using Microsoft.AspNetCore.Authorization;
9 using Microsoft.AspNetCore.Mvc;
10 using System.Net;
11 using System.Security.Claims;
12
13 namespace WebApp.ApiControllers;
14
15 [ApiVersion("1.0")]
16 [Route("api/v{version:apiVersion}/[controller]")]
17 [ApiController]
18 [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
19 public class SplitPresetsController : ControllerBase
20 {
21 private readonly ISplitPresetService _splitPresetService;
22 private readonly ITripService _tripService;
23
24 public SplitPresetsController(ISplitPresetService splitPresetService, ITripService tripService)
25 {
26 _splitPresetService = splitPresetService;
27 _tripService = tripService;
28 }
29
30 private Guid GetUserId() => Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
31
32 // GET: api/v1/splitpresets/trip/{tripId}
33 [HttpGet("trip/{tripId:guid}")]
34 [Produces("application/json")]
35 [ProducesResponseType<List<SplitPresetDto>>((int)HttpStatusCode.OK)]
36 [ProducesResponseType((int)HttpStatusCode.Forbidden)]
37 public async Task<ActionResult<List<SplitPresetDto>>> GetTripPresets(Guid tripId)
38 {
39 var userId = GetUserId();
40 if (!await _tripService.IsParticipantAsync(tripId, userId)) return Forbid();
41
42 var presets = await _splitPresetService.GetByTripIdAsync(tripId, userId);
43
44 return Ok(presets.Select(SplitPresetMapper.MapToDto).ToList());
45 }
46
47 // GET: api/v1/splitpresets/{id}
48 [HttpGet("{id:guid}")]
49 [Produces("application/json")]
50 [ProducesResponseType<SplitPresetDto>((int)HttpStatusCode.OK)]
51 [ProducesResponseType((int)HttpStatusCode.NotFound)]
52 public async Task<ActionResult<SplitPresetDto>> GetPreset(Guid id)
53 {
54 var userId = GetUserId();
55
56 var preset = await _splitPresetService.GetByIdAsync(id, userId);
57 if (preset == null) return NotFound();
58
59 return Ok(SplitPresetMapper.MapToDto(preset));
60 }
61
62 // POST: api/v1/splitpresets
63 [HttpPost]
64 [Produces("application/json")]
65 [Consumes("application/json")]
66 [ProducesResponseType<SplitPresetDto>((int)HttpStatusCode.Created)]
67 [ProducesResponseType((int)HttpStatusCode.Forbidden)]
68 public async Task<ActionResult<SplitPresetDto>> CreatePreset([FromBody] SplitPresetCreateDto dto)
69 {
70 var userId = GetUserId();
71
72 var preset = new SplitPresetBllDto
73 {
74 TripId = dto.TripId,
75 Name = dto.Name,
76 SplitMethod = Enum.Parse<ESplitMethod>(dto.SplitMethod),
77 CreatedById = userId,
78 };
79
80 var members = dto.Members?
81 .Select(m => (m.UserId, m.ShareWeight, m.Percentage))
82 .ToList() ?? new List<(Guid UserId, decimal? ShareWeight, decimal? Percentage)>();
83
84 var (created, errorCode) = await _splitPresetService.CreateAsync(preset, members, userId);
85 if (created == null)
86 {
87 if (errorCode == "forbidden") return Forbid();
88 return NotFound();
89 }
90
91 return CreatedAtAction(nameof(GetPreset), new { id = created.Id }, SplitPresetMapper.MapToDto(created));
92 }
93
94 // PUT: api/v1/splitpresets/{id}
95 [HttpPut("{id:guid}")]
96 [Consumes("application/json")]
97 [ProducesResponseType((int)HttpStatusCode.NoContent)]
98 [ProducesResponseType((int)HttpStatusCode.NotFound)]
99 public async Task<IActionResult> UpdatePreset(Guid id, [FromBody] SplitPresetCreateDto dto)
100 {
101 var userId = GetUserId();
102
103 var members = dto.Members?
104 .Select(m => (m.UserId, m.ShareWeight, m.Percentage))
105 .ToList() ?? new List<(Guid UserId, decimal? ShareWeight, decimal? Percentage)>();
106
107 var (ok, errorCode) = await _splitPresetService.UpdateAsync(
108 id, dto.Name, Enum.Parse<ESplitMethod>(dto.SplitMethod), members, userId);
109
110 if (!ok)
111 {
112 return errorCode switch
113 {
114 "notfound" => NotFound(),
115 "forbidden" => Forbid(),
116 _ => NotFound()
117 };
118 }
119
120 return NoContent();
121 }
122
123 // DELETE: api/v1/splitpresets/{id}
124 [HttpDelete("{id:guid}")]
125 [ProducesResponseType((int)HttpStatusCode.NoContent)]
126 [ProducesResponseType((int)HttpStatusCode.NotFound)]
127 public async Task<IActionResult> DeletePreset(Guid id)
128 {
129 var userId = GetUserId();
130
131 var (ok, errorCode) = await _splitPresetService.DeleteAsync(id, userId);
132 if (!ok)
133 {
134 return errorCode switch
135 {
136 "notfound" => NotFound(),
137 "forbidden" => Forbid(),
138 _ => NotFound()
139 };
140 }
141
142 return NoContent();
143 }
144 }
145