Updated the domain model with entities, repos and domain services. Looks good. I think I addressed all the user stories however if I fell short we'll revise as needed.
This commit was merged in pull request #6.
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
using LibraryApp.Domain.Entities;
|
||||
using LibraryApp.Domain.Repositories;
|
||||
|
||||
namespace LibraryApp.Domain.Services;
|
||||
|
||||
public class LendingService
|
||||
{
|
||||
private readonly ILendRecordRepository _lendRecords;
|
||||
|
||||
public LendingService(ILendRecordRepository lendRecords)
|
||||
{
|
||||
_lendRecords = lendRecords;
|
||||
}
|
||||
|
||||
public async Task<Result<LendRecord>> LendBookAsync(BookInstance bookInstance, Member member)
|
||||
{
|
||||
if (bookInstance.Status != BookInstanceStatus.OnShelf)
|
||||
return Result<LendRecord>.Failure($"Book is not available for lending. Current status: {bookInstance.Status}.");
|
||||
|
||||
var code = await GenerateUniqueLendCodeAsync();
|
||||
bookInstance.MarkAsLent();
|
||||
var record = new LendRecord(bookInstance.Id, member.Id, code);
|
||||
return Result<LendRecord>.Success(record);
|
||||
}
|
||||
|
||||
public Task<Result> ReturnBookAsync(BookInstance bookInstance, LendRecord lendRecord)
|
||||
{
|
||||
if (bookInstance.Status != BookInstanceStatus.Lent)
|
||||
return Task.FromResult(Result.Failure($"Book is not currently lent out. Current status: {bookInstance.Status}."));
|
||||
|
||||
bookInstance.ReturnToShelf();
|
||||
lendRecord.MarkAsReturned();
|
||||
return Task.FromResult(Result.Success());
|
||||
}
|
||||
|
||||
private async Task<LendCode> GenerateUniqueLendCodeAsync()
|
||||
{
|
||||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
var random = Random.Shared;
|
||||
|
||||
while (true)
|
||||
{
|
||||
var code = new string(Enumerable.Range(0, 6).Select(_ => chars[random.Next(chars.Length)]).ToArray());
|
||||
var lendCode = new LendCode(code);
|
||||
if (!await _lendRecords.ExistsWithCodeAsync(lendCode))
|
||||
return lendCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user