Merge branch 'master' into feature/oauth
This commit is contained in:
@@ -5,5 +5,6 @@ namespace Hutopy.Application.Common.Interfaces;
|
||||
public interface IApplicationDbContext
|
||||
{
|
||||
DbSet<FutureCreator> FutureCreators { get; }
|
||||
DbSet<UserTransaction> UserTransactions { get; }
|
||||
Task<int> SaveChangesAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
@@ -3,5 +3,5 @@ namespace Hutopy.Application.Common.Interfaces;
|
||||
|
||||
public interface IStripeService
|
||||
{
|
||||
public Task<string> CreateCheckoutSession(int price, string currency);
|
||||
public Task<string> CreateCheckoutSession(int amount, string currency);
|
||||
}
|
||||
|
||||
24
src/Application/Stripe/Commands/ConfirmStripeTransaction.cs
Normal file
24
src/Application/Stripe/Commands/ConfirmStripeTransaction.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using Hutopy.Application.Common.Interfaces;
|
||||
|
||||
namespace Hutopy.Application.Stripe.Commands;
|
||||
public record ConfirmStripeTransactionCommand : IRequest<string>
|
||||
{
|
||||
public required Guid UserTransactionId { get; init; }
|
||||
public required bool IsConfirmed { get; init; }
|
||||
}
|
||||
|
||||
public class ConfirmStripeTransactionCommandHandler(
|
||||
IApplicationDbContext dbContext
|
||||
)
|
||||
: IRequestHandler<ConfirmStripeTransactionCommand, string>
|
||||
{
|
||||
public async Task<string> Handle(ConfirmStripeTransactionCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var transaction = await dbContext.UserTransactions.FirstOrDefaultAsync(x => x.Id == request.UserTransactionId, cancellationToken);
|
||||
if (transaction is null) return "";
|
||||
transaction.IsConfirmed = request.IsConfirmed;
|
||||
dbContext.UserTransactions.Update(transaction);
|
||||
|
||||
return transaction.Id.ToString();
|
||||
}
|
||||
}
|
||||
@@ -1,25 +1,38 @@
|
||||
using Hutopy.Application.Common.Interfaces;
|
||||
|
||||
using Hutopy.Domain.Entities;
|
||||
|
||||
namespace Hutopy.Application.Stripe.Commands;
|
||||
public abstract record CreateSessionCheckoutCommand : IRequest<string>
|
||||
public record CreateSessionCheckoutCommand : IRequest<string>
|
||||
{
|
||||
public required int Price { get; init; }
|
||||
|
||||
public string Currency { get; init; } = "cad";
|
||||
public required string CreatorId { get; init; }
|
||||
public required int Amount { get; init; }
|
||||
public string Currency { get; init; } = "CAD";
|
||||
public string TipMessage { get; init; } = string.Empty;
|
||||
}
|
||||
|
||||
|
||||
public class CreateSessionCheckoutCommandHandler(
|
||||
IApplicationDbContext context,
|
||||
IStripeService stripeService)
|
||||
IApplicationDbContext dbContext,
|
||||
IStripeService stripeService
|
||||
)
|
||||
: IRequestHandler<CreateSessionCheckoutCommand, string>
|
||||
{
|
||||
private readonly IApplicationDbContext _context = context;
|
||||
|
||||
public async Task<string> Handle(CreateSessionCheckoutCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var stripeSecret = await stripeService.CreateCheckoutSession(request.Price, request.Currency);
|
||||
var stripeSecret = await stripeService.CreateCheckoutSession(request.Amount, request.Currency);
|
||||
|
||||
// ReSharper disable once PossibleLossOfFraction
|
||||
decimal priceInDollars = (request.Amount / 100);
|
||||
|
||||
|
||||
//todo: Need to add this transaction after the confirmation from stripe in the frontEnd ( redirect, re-call backend )
|
||||
var userTransaction = new UserTransaction
|
||||
{
|
||||
Currency = request.Currency, Amount = priceInDollars, TipMessage = request.TipMessage, ApplicationUserId = request.CreatorId
|
||||
};
|
||||
|
||||
await dbContext.UserTransactions.AddAsync(userTransaction, cancellationToken);
|
||||
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return stripeSecret;
|
||||
}
|
||||
|
||||
37
src/Application/Users/Queries/GetCurrentUser.cs
Normal file
37
src/Application/Users/Queries/GetCurrentUser.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using Hutopy.Application.Common.Interfaces;
|
||||
using Hutopy.Domain.Interfaces;
|
||||
|
||||
namespace Hutopy.Application.Users.Queries;
|
||||
|
||||
public record GetCurrentUserQuery : IRequest<UserDto>;
|
||||
|
||||
public class GetCurrentUserQueryHandler(
|
||||
IApplicationDbContext context,
|
||||
IMapper mapper,
|
||||
IUserService userService
|
||||
)
|
||||
: IRequestHandler<GetCurrentUserQuery, UserDto>
|
||||
{
|
||||
public async Task<UserDto> Handle(GetCurrentUserQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var identityUser = await userService.GetCurrentUserAsync();
|
||||
var currentUserId = new Guid(identityUser?.Id ?? "");
|
||||
|
||||
var transactions = await context.UserTransactions
|
||||
.Where(x => x.Id == currentUserId)
|
||||
.OrderBy(x => x.LastModified)
|
||||
.ProjectTo<UserTransactionDto>(mapper.ConfigurationProvider)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
|
||||
var user = new UserDto()
|
||||
{
|
||||
Id = currentUserId,
|
||||
FirstName = identityUser?.FirstName ?? "",
|
||||
LastName = identityUser?.LastName ?? "",
|
||||
UserTransactions = transactions
|
||||
};
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
12
src/Application/Users/Queries/UserDto.cs
Normal file
12
src/Application/Users/Queries/UserDto.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace Hutopy.Application.Users.Queries;
|
||||
|
||||
public class UserDto
|
||||
{
|
||||
public Guid Id { get; init; }
|
||||
|
||||
public required string FirstName { get; init; }
|
||||
|
||||
public required string LastName { get; init; }
|
||||
|
||||
public List<UserTransactionDto> UserTransactions { get; init; } = [];
|
||||
}
|
||||
20
src/Application/Users/Queries/UserTransactionDto.cs
Normal file
20
src/Application/Users/Queries/UserTransactionDto.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Hutopy.Domain.Entities;
|
||||
|
||||
namespace Hutopy.Application.Users.Queries;
|
||||
|
||||
public class UserTransactionDto
|
||||
{
|
||||
public required decimal Amount { get; init; }
|
||||
|
||||
public string Currency { get; init; } = "cad";
|
||||
|
||||
public string TipMessage { get; init; } = string.Empty;
|
||||
|
||||
private class Mapping : Profile
|
||||
{
|
||||
public Mapping()
|
||||
{
|
||||
CreateMap<UserTransaction, UserTransactionDto>();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user