Skip to content

Commit

Permalink
Final Retuouch, antoher update will be available soon
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdullah Ahmed committed Nov 12, 2024
1 parent 543109e commit 1b62d89
Show file tree
Hide file tree
Showing 19 changed files with 1,586 additions and 68 deletions.
23 changes: 10 additions & 13 deletions src/Application.BLL/Services/Implementation/LoanService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,20 @@ public async Task<ApplicationResult> ReturnLoan(Guid loanId)
using (var transaction = await _unitOfWork.BeginTransactionAsync())
{
var loan = await _unitOfWork.LoanRepository.GetByIdAsync(loanId);

if (loan == null || loan.IsDeleted)
return new ApplicationResult() { Succeeded= false, Errors = new List<string>() { "Loan doesn't exist" } };


if (loan.IsReturned)
return new ApplicationResult() { Succeeded = false, Errors = new List<string>() { "Loan has already been returned" } };

List<string> errors = new List<string>();

if (loan.DueDate < DateTime.UtcNow)
{
// Calculate penalty
var penaltyAmount = (decimal)((DateTime.UtcNow - loan.DueDate).TotalDays * 1.5);
var penaltyAmount = new decimal(((DateTime.UtcNow.Day - loan.DueDate.Day) * 1.9));
var penalty = new Penalty
{
Loan = loan,
Expand All @@ -235,35 +238,29 @@ public async Task<ApplicationResult> ReturnLoan(Guid loanId)

// Add penalty
await _unitOfWork.PenaltyRepository.AddAsync(penalty);
await _unitOfWork.CompleteAsync();

// Throw to let middleware handle it if penalty is due
return new ApplicationResult()
{
Succeeded = false,
Errors = new List<string>() { $"Please pay the penalty of {penaltyAmount:C} before returning this loan." }
};
errors.Add($"Pay {penaltyAmount} as a penalty due to you late return");
}

// Mark loan as returned
// Mark loan as returned
loan.IsReturned = true;

// Increase available copies
if (loan.Book != null)
{
loan.Book.AvailableCopies += 1;
await _unitOfWork.BookRepository.UpdateAsync(loan.Book);

}

// Update loan and save changes
// Update e loan and save changes
await _unitOfWork.LoanRepository.UpdateAsync(loan);
await _unitOfWork.CompleteAsync();

// Commit transaction
await transaction.CommitAsync();
return new ApplicationResult()
{
Succeeded = true
Succeeded = true,
Errors = errors
};
}
}
Expand Down
37 changes: 22 additions & 15 deletions src/Application.BLL/Services/Implementation/PenaltyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,30 @@ public async Task<ApplicationResult> PayPenalty(Guid penaltyId, decimal amount)
{
var penalty = await _unitOfWork.PenaltyRepository.GetByIdAsync(penaltyId);
if (penalty == null || penalty.IsPaid)
return new ApplicationResult() {
Succeeded = false,
Errors = new List<string>
{
await transaction.CommitAsync();
return new ApplicationResult()
{
Succeeded = false,
Errors = new List<string>
{
"No penalty to pay or penalty is already paid."
}
};

"No penalty to pay or penalty is already paid."
}
};
}
if (amount < penalty.Amount)
return new ApplicationResult() {
Succeeded = false,
Errors = new List<string>
{
$"Payment is less than the penalty amount. Required: {penalty.Amount:C}"
}
};

{
await transaction.CommitAsync();
return new ApplicationResult()
{
Succeeded = false,
Errors = new List<string>
{
$"Payment is less than the penalty amount. Required: {penalty.Amount:C}"
}
};

}
// Mark penalty as paid
penalty.IsPaid = true;
await _unitOfWork.PenaltyRepository.UpdateAsync(penalty);
Expand Down
3 changes: 2 additions & 1 deletion src/Application.DAL/Entities/Penalty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -13,7 +14,7 @@ public class Penalty : ISoftDeleteable
{
public Guid PenaltyId { get; set; } = Guid.NewGuid();

[Precision(3,3), DataType(DataType.Currency)]
[Column(TypeName = "decimal(5,2)")]
public decimal? Amount { get; set; }
public bool IsPaid { set; get; } = false;
public bool IsDeleted { set; get; } = false;
Expand Down
Loading

0 comments on commit 1b62d89

Please sign in to comment.