public async Task <IActionResult> Subscribe(int id, string token) { CourseSubscribeInputModel inputModel = await courseService.CapturePaymentAsync(id, token); await courseService.SubscribeCourseAsync(inputModel); TempData["ConfirmationMessage"] = "Grazie per esserti iscritto guarda la prima lezione!"; return(RedirectToAction(nameof(Detail), new { id = id })); }
public async Task SubscribeCourseAsync(CourseSubscribeInputModel inputModel) { try { await db.CommandAsync($"INSERT INTO Subscriptions (UserId, CourseId, PaymentDate, PaymentType, Paid_Currency, Paid_Amount, TransactionId) VALUES ({inputModel.UserId}, {inputModel.CourseId}, {inputModel.PaymentDate}, {inputModel.PaymentType}, {inputModel.Paid.Currency.ToString()}, {inputModel.Paid.Amount}, {inputModel.TransactionId})"); } catch (ConstraintViolationException) { throw new CourseSubscriptionException(inputModel.CourseId); } catch (Exception) { await transactionLogger.LogTransactionAsync(inputModel); } }
public async Task LogTransactionAsync(CourseSubscribeInputModel inputModel) { string filePath = Path.Combine(env.ContentRootPath, "Data", "transactions.txt"); string content = $"\r\n{inputModel.TransactionId}\t{inputModel.PaymentDate}\t{inputModel.PaymentType}\t{inputModel.Paid}\t{inputModel.UserId}\t{inputModel.CourseId}"; try { await semaphore.WaitAsync(); await File.AppendAllTextAsync(filePath, content); } finally { semaphore.Release(); } }
public async Task SubscribeCourseAsync(CourseSubscribeInputModel inputModel) { Subscription subscription = new(inputModel.UserId, inputModel.CourseId) { PaymentDate = inputModel.PaymentDate, PaymentType = inputModel.PaymentType, Paid = inputModel.Paid, TransactionId = inputModel.TransactionId }; dbContext.Subscriptions.Add(subscription); try { await dbContext.SaveChangesAsync(); } catch (DbUpdateException) { throw new CourseSubscriptionException(inputModel.CourseId); } catch (Exception) { await transactionLogger.LogTransactionAsync(inputModel); } }
public Task SubscribeCourseAsync(CourseSubscribeInputModel inputModel) { return(courseService.SubscribeCourseAsync(inputModel)); }