Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ public interface IShoppingCartRepository
public Task<Result<long>> IncrementAmountOfTicketTypeAsync(Guid ticketTypeId, long amount);
public Task<Result<long>> DecrementAmountOfTicketTypeAsync(Guid ticketTypeId, long amount);
public Task<Result> RemoveAmountOfTicketTypeAsync(Guid ticketTypeId);
public Task<Result> AddResellTicketToCartAsync(string customerEmail, Guid ticketId);
public Task<Result<bool>> CheckResellTicketAvailabilityAsync(Guid ticketId);
public Task<Result> RemoveResellTicketFromCartAsync(string customerEmail, Guid ticketId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ namespace TickAPI.ShoppingCarts.Abstractions;
public interface IShoppingCartService
{
public Task<Result> AddNewTicketsToCartAsync(Guid ticketTypeId, uint amount, string customerEmail);
public Task<Result> AddResellTicketToCartAsync(Guid ticketId, string customerEmail);
public Task<Result<GetShoppingCartTicketsResponseDto>> GetTicketsFromCartAsync(string customerEmail);
public Task<Result> RemoveNewTicketsFromCartAsync(Guid ticketTypeId, uint amount, string customerEmail);
public Task<Result> RemoveResellTicketFromCartAsync(Guid ticketId, string customerEmail);
public Task<Result<Dictionary<string, decimal>>> GetDueAmountAsync(string customerEmail);
public Task<Result<PaymentResponsePG>> CheckoutAsync(string customerEmail, decimal amount, string currency,
string cardNumber, string cardExpiry, string cvv);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ await _shoppingCartService.AddNewTicketsToCartAsync(addTicketDto.TicketTypeId, a

return addTicketResult.ToObjectResult();
}

[AuthorizeWithPolicy(AuthPolicies.CustomerPolicy)]
[HttpPost("{ticketId:guid}")]
public async Task<ActionResult> AddResellTicket([FromRoute] Guid ticketId)
{
var emailResult = _claimsService.GetEmailFromClaims(User.Claims);
if (emailResult.IsError)
{
return emailResult.ToObjectResult();
}
var email = emailResult.Value!;

var addTicketResult = await _shoppingCartService.AddResellTicketToCartAsync(ticketId, email);

return addTicketResult.ToObjectResult();
}

[AuthorizeWithPolicy(AuthPolicies.CustomerPolicy)]
[HttpGet]
Expand Down Expand Up @@ -74,6 +90,22 @@ await _shoppingCartService.RemoveNewTicketsFromCartAsync(removeTicketDto.TicketT
return removeTicketResult.ToObjectResult();
}

[AuthorizeWithPolicy(AuthPolicies.CustomerPolicy)]
[HttpDelete("{ticketId:guid}")]
public async Task<ActionResult> RemoveResellTicket([FromRoute] Guid ticketId)
{
var emailResult = _claimsService.GetEmailFromClaims(User.Claims);
if (emailResult.IsError)
{
return emailResult.ToObjectResult();
}
var email = emailResult.Value!;

var removeTicketResult = await _shoppingCartService.RemoveResellTicketFromCartAsync(ticketId, email);

return removeTicketResult.ToObjectResult();
}

[AuthorizeWithPolicy(AuthPolicies.CustomerPolicy)]
[HttpGet("due")]
public async Task<ActionResult<Dictionary<string, decimal>>> GetDueAmount()
Expand Down
4 changes: 2 additions & 2 deletions TickAPI/TickAPI/ShoppingCarts/Mappers/ShoppingCartMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public static GetShoppingCartTicketsResellTicketDetailsResponseDto
ticket.Type.Description,
ticket.Type.Event.Organizer.DisplayName,
ticket.Owner.Email,
ticket.Type.Price,
ticket.Type.Currency
ticket.ResellPrice ?? ticket.Type.Price,
ticket.ResellCurrency ?? ticket.Type.Currency
);
}
}
130 changes: 130 additions & 0 deletions TickAPI/TickAPI/ShoppingCarts/Repositories/ShoppingCartRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,131 @@ public async Task<Result> RemoveAmountOfTicketTypeAsync(Guid ticketTypeId)
return Result.Success();
}

public async Task<Result> AddResellTicketToCartAsync(string customerEmail, Guid ticketId)
{
var getShoppingCartResult = await GetShoppingCartByEmailAsync(customerEmail);

if (getShoppingCartResult.IsError)
{
return Result.PropagateError(getShoppingCartResult);
}

var cart = getShoppingCartResult.Value!;

cart.ResellTickets.Add(new ShoppingCartResellTicket {TicketId = ticketId});

var setKeyResult = await SetTicketKeyAsync(ticketId);

if (setKeyResult.IsError)
{
return Result.PropagateError(setKeyResult);
}

var updateShoppingCartResult = await UpdateShoppingCartAsync(customerEmail, cart);

if (updateShoppingCartResult.IsError)
{
return Result.PropagateError(updateShoppingCartResult);
}

return Result.Success();
}

public async Task<Result<bool>> CheckResellTicketAvailabilityAsync(Guid ticketId)
{
bool exists;

try
{
exists = await _redisService.KeyExistsAsync(GetResellTicketKey(ticketId));
}
catch (Exception e)
{
return Result<bool>.Failure(StatusCodes.Status500InternalServerError, e.Message);
}

return Result<bool>.Success(!exists);
}

public async Task<Result> RemoveResellTicketFromCartAsync(string customerEmail, Guid ticketId)
{
var getShoppingCartResult = await GetShoppingCartByEmailAsync(customerEmail);

if (getShoppingCartResult.IsError)
{
return Result.PropagateError(getShoppingCartResult);
}

var cart = getShoppingCartResult.Value!;

var existingEntry = cart.ResellTickets.FirstOrDefault(t => t.TicketId == ticketId);

if (existingEntry is null)
{
return Result.Failure(StatusCodes.Status404NotFound, "the shopping cart does not contain this ticket");
}

cart.ResellTickets.Remove(existingEntry);

var deleteKeyResult = await DeleteTicketKeyAsync(ticketId);

if (deleteKeyResult.IsError)
{
return Result.PropagateError(deleteKeyResult);
}

var updateShoppingCartResult = await UpdateShoppingCartAsync(customerEmail, cart);

if (updateShoppingCartResult.IsError)
{
return Result.PropagateError(updateShoppingCartResult);
}

return Result.Success();
}

private async Task<Result> SetTicketKeyAsync(Guid ticketId)
{
bool success;

try
{
success = await _redisService.SetStringAsync(GetResellTicketKey(ticketId), string.Empty, _defaultExpiry);
}
catch (Exception e)
{
return Result.Failure(StatusCodes.Status500InternalServerError, e.Message);
}

if (!success)
{
return Result.Failure(StatusCodes.Status500InternalServerError, "the ticket key could not be updated");
}

return Result.Success();
}

private async Task<Result> DeleteTicketKeyAsync(Guid ticketId)
{
bool success;

try
{
success = await _redisService.DeleteKeyAsync(GetResellTicketKey(ticketId));
}
catch (Exception e)
{
return Result.Failure(StatusCodes.Status500InternalServerError, e.Message);
}

if (!success)
{
return Result.Failure(StatusCodes.Status500InternalServerError, "the ticket key could not be deleted");
}

return Result.Success();
}

private static string GetCartKey(string customerEmail)
{
return $"cart:{customerEmail}";
Expand All @@ -263,4 +388,9 @@ private static string GetAmountKey(Guid ticketTypeId)
{
return $"amount:{ticketTypeId}";
}

private static string GetResellTicketKey(Guid ticketId)
{
return $"resell:{ticketId}";
}
}
Loading
Loading