ticket-system/source/ticketAPI/api/Controllers/TicketController.cs
2024-12-04 15:57:49 -05:00

84 lines
2.2 KiB
C#

using api.Interfaces;
using Microsoft.AspNetCore.Mvc;
using models.Core;
using models.Request;
using models.Response;
namespace api.Controllers;
/// <summary>
/// Endpoints for Ticket Management
/// </summary>
/// <param name="qr">Injected QR Code Service</param>
/// <param name="ticketManager">Injected Ticket Manager Service</param>
[ApiController]
[Route("[controller]")]
public class TicketController(
IQrCodeGenerator qr,
ITicketManager ticketManager) : ControllerBase
{
/// <summary>
/// Generates a Base64 String Qr Code and Saves Qr Code and Ticket to DB
/// </summary>
/// <returns>Base64 String Qr Code</returns>
[HttpPost]
public ActionResult<MintResponse> Post([FromBody] AddTicket mintRequest)
{
//TODO: Protect Endpoint
//generate ticket id
var ticketId = Guid.NewGuid();
try
{
//generate the qr code
var qrCode = qr.GenerateQrCode(ticketId.ToString());
//build the ticket
var ticket = new Ticket
{
Id = ticketId,
QrCode = qrCode,
Type = mintRequest.Type,
EventId = mintRequest.EventId
};
//save the minted ticket
ticketManager.SaveMintedTicket(ticket);
//return
var response = new MintResponse
{
QrCode = ticket.QrCode,
Type = ticket.Type
};
return Ok(response);
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
/// <summary>
/// Searches for a ticket with a given ticketId and validates it against the event associated with it.
/// </summary>
/// <param name="ticketId">A string representing a GUID value</param>
/// <returns>Ticket Search Result</returns>
[HttpGet]
public ActionResult<TicketSearch> Get(string ticketId)
{
//TODO: Protect Endpoint
try
{
var result = ticketManager.SearchTicket(ticketId);
return Ok(result);
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
}