profileShare

rasmusjy / splitapp-backend-clean-onion

Read-only snapshot

No repository description.

main default branch 429 files Expires Sep 13, 2026, 9:06 AM
WishlistController.cs 6,050 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 WishlistController : ControllerBase
20 {
21 private readonly IWishlistService _wishlistService;
22 private readonly ITripService _tripService;
23
24 public WishlistController(IWishlistService wishlistService, ITripService tripService)
25 {
26 _wishlistService = wishlistService;
27 _tripService = tripService;
28 }
29
30 private Guid GetUserId() => Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
31
32 // GET: api/v1/wishlist/trip/{tripId}
33 [HttpGet("trip/{tripId:guid}")]
34 [Produces("application/json")]
35 [ProducesResponseType<List<WishlistItemDto>>((int)HttpStatusCode.OK)]
36 [ProducesResponseType((int)HttpStatusCode.Forbidden)]
37 public async Task<ActionResult<List<WishlistItemDto>>> GetTripWishlistItems(Guid tripId)
38 {
39 var userId = GetUserId();
40
41 if (!await _tripService.IsParticipantAsync(tripId, userId)) return Forbid();
42
43 var items = await _wishlistService.GetByTripIdAsync(tripId, userId);
44
45 return Ok(items.Select(i => WishlistMapper.MapToDto(i, userId)).ToList());
46 }
47
48 // POST: api/v1/wishlist
49 [HttpPost]
50 [Produces("application/json")]
51 [Consumes("application/json")]
52 [ProducesResponseType<WishlistItemDto>((int)HttpStatusCode.Created)]
53 [ProducesResponseType((int)HttpStatusCode.Forbidden)]
54 public async Task<ActionResult<WishlistItemDto>> CreateWishlistItem([FromBody] WishlistItemCreateDto dto)
55 {
56 var userId = GetUserId();
57
58 var item = new TripWishlistItemBllDto
59 {
60 TripId = dto.TripId,
61 AddedByUserId = userId,
62 Title = dto.Title,
63 Description = dto.Description,
64 Category = Enum.Parse<EWishlistCategory>(dto.Category),
65 Priority = Enum.Parse<EWishlistPriority>(dto.Priority),
66 EstimatedCost = dto.EstimatedCost,
67 Url = dto.Url,
68 Location = dto.Location,
69 IsCompleted = false,
70 DisplayOrder = 0
71 };
72
73 var (created, errorCode) = await _wishlistService.CreateAsync(item, userId);
74 if (created == null)
75 {
76 if (errorCode == "forbidden") return Forbid();
77 return NotFound();
78 }
79
80 return CreatedAtAction(null, new { id = created.Id }, WishlistMapper.MapToDto(created, userId));
81 }
82
83 // PUT: api/v1/wishlist/{id}
84 [HttpPut("{id:guid}")]
85 [Consumes("application/json")]
86 [ProducesResponseType((int)HttpStatusCode.NoContent)]
87 [ProducesResponseType((int)HttpStatusCode.NotFound)]
88 [ProducesResponseType((int)HttpStatusCode.Forbidden)]
89 public async Task<IActionResult> UpdateWishlistItem(Guid id, [FromBody] WishlistItemCreateDto dto)
90 {
91 var userId = GetUserId();
92
93 var incoming = new TripWishlistItemBllDto
94 {
95 Title = dto.Title,
96 Description = dto.Description,
97 Category = Enum.Parse<EWishlistCategory>(dto.Category),
98 Priority = Enum.Parse<EWishlistPriority>(dto.Priority),
99 EstimatedCost = dto.EstimatedCost,
100 Url = dto.Url,
101 Location = dto.Location
102 };
103
104 var (ok, errorCode) = await _wishlistService.UpdateAsync(id, incoming, userId);
105 if (!ok)
106 {
107 return errorCode switch
108 {
109 "notfound" => NotFound(),
110 "forbidden" => Forbid(),
111 _ => NotFound()
112 };
113 }
114
115 return NoContent();
116 }
117
118 // DELETE: api/v1/wishlist/{id}
119 [HttpDelete("{id:guid}")]
120 [ProducesResponseType((int)HttpStatusCode.NoContent)]
121 [ProducesResponseType((int)HttpStatusCode.NotFound)]
122 [ProducesResponseType((int)HttpStatusCode.Forbidden)]
123 public async Task<IActionResult> DeleteWishlistItem(Guid id)
124 {
125 var userId = GetUserId();
126
127 var (ok, errorCode) = await _wishlistService.DeleteAsync(id, userId);
128 if (!ok)
129 {
130 return errorCode switch
131 {
132 "notfound" => NotFound(),
133 "forbidden" => Forbid(),
134 _ => NotFound()
135 };
136 }
137
138 return NoContent();
139 }
140
141 // POST: api/v1/wishlist/{id}/vote
142 [HttpPost("{id:guid}/vote")]
143 [Produces("application/json")]
144 [ProducesResponseType((int)HttpStatusCode.OK)]
145 [ProducesResponseType((int)HttpStatusCode.NotFound)]
146 [ProducesResponseType((int)HttpStatusCode.Forbidden)]
147 public async Task<IActionResult> ToggleVote(Guid id)
148 {
149 var userId = GetUserId();
150
151 var (ok, errorCode) = await _wishlistService.ToggleVoteAsync(id, userId);
152 if (!ok)
153 {
154 return errorCode switch
155 {
156 "notfound" => NotFound(),
157 "forbidden" => Forbid(),
158 _ => NotFound()
159 };
160 }
161
162 return Ok();
163 }
164
165 // POST: api/v1/wishlist/{id}/complete
166 [HttpPost("{id:guid}/complete")]
167 [Produces("application/json")]
168 [ProducesResponseType((int)HttpStatusCode.OK)]
169 [ProducesResponseType((int)HttpStatusCode.NotFound)]
170 [ProducesResponseType((int)HttpStatusCode.Forbidden)]
171 public async Task<IActionResult> MarkCompleted(Guid id)
172 {
173 var userId = GetUserId();
174
175 var (ok, errorCode) = await _wishlistService.ToggleCompleteAsync(id, userId);
176 if (!ok)
177 {
178 return errorCode switch
179 {
180 "notfound" => NotFound(),
181 "forbidden" => Forbid(),
182 _ => NotFound()
183 };
184 }
185
186 return Ok();
187 }
188 }
189