-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookingsController.cs
More file actions
97 lines (86 loc) · 3.24 KB
/
BookingsController.cs
File metadata and controls
97 lines (86 loc) · 3.24 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
using Evently.Server.Common.Domains.Entities;
using Evently.Server.Common.Domains.Interfaces;
using Evently.Server.Common.Domains.Models;
using Evently.Server.Common.Extensions;
using Evently.Server.Features.Accounts.Services;
using Microsoft.AspNetCore.Mvc;
using System.Globalization;
using System.Threading.Channels;
namespace Evently.Server.Features.Bookings;
[ApiController]
[Route("api/v1/[controller]")]
public sealed class BookingsController(IBookingService bookingService, ChannelWriter<string> emailQueue, ILogger<BookingsController> logger)
: ControllerBase {
[HttpGet("{bookingId}", Name = "GetBooking")]
public async Task<ActionResult<Booking>> GetBooking(string bookingId) {
Booking? booking = await bookingService.GetBooking(bookingId);
if (booking is null) {
return NotFound();
}
return Ok(booking);
}
[HttpGet("{bookingId}/preview", Name = "PreviewBooking")]
public async Task<ActionResult<Booking>> PreviewBooking(string bookingId) {
string html = await bookingService.RenderTicket(bookingId);
return Content(html, "text/html");
}
[HttpGet("", Name = "GetBookings")]
public async Task<ActionResult<Booking>> GetBookings(
string? attendeeId,
long? gatheringId,
DateTimeOffset? checkInStart,
DateTimeOffset? checkInEnd,
DateTimeOffset? gatheringStartBefore, DateTimeOffset? gatheringStartAfter, DateTimeOffset? gatheringEndBefore, DateTimeOffset? gatheringEndAfter,
bool isCancelled,
int? offset,
int? limit) {
PageResult<Booking> result = await bookingService.GetBookings(attendeeId,
gatheringId,
checkInStart,
checkInEnd,
gatheringStartBefore,
gatheringStartAfter,
gatheringEndBefore,
gatheringEndAfter,
isCancelled,
offset,
limit);
List<Booking> bookingEvents = result.Items;
int total = result.TotalCount;
HttpContext.Response.Headers.Append("Access-Control-Expose-Headers", "X-Total-Count");
HttpContext.Response.Headers.Append("X-Total-Count", value: total.ToString(CultureInfo.InvariantCulture));
return Ok(bookingEvents);
}
[HttpPost("", Name = "CreateBooking")]
public async Task<ActionResult<Booking>> CreateBooking([FromBody] BookingReqDto bookingReqDto) {
Booking booking = await bookingService.CreateBooking(bookingReqDto);
await emailQueue.WriteAsync(booking.BookingId);
return Ok(booking);
}
[HttpPut("{bookingId}", Name = "UpdateBooking")]
public async Task<ActionResult> UpdateBooking(string bookingId,
[FromBody] BookingReqDto bookingReqDto) {
bool isExist = await bookingService.Exists(bookingId);
if (!isExist) {
return NotFound();
}
Booking booking = await bookingService.UpdateBooking(bookingId, bookingReqDto);
return Ok(booking);
}
[HttpPatch("{bookingId}/checkIn", Name = "CheckInBooking")]
public async Task<ActionResult> CheckInBooking(string bookingId) {
Booking? booking = await bookingService.GetBooking(bookingId);
if (booking?.Gathering is null) {
return NotFound();
}
Gathering gathering = booking.Gathering;
bool isAuth = await this.IsResourceOwner(gathering.OrganiserId);
logger.LogInformation("isAuth: {}", isAuth);
if (!isAuth) {
return Forbid();
}
booking.CheckInDateTime = DateTimeOffset.UtcNow;
booking = await bookingService.UpdateBooking(bookingId, bookingReqDto: booking.ToBookingDto());
return Ok(booking);
}
}