Skip to content

Commit

Permalink
added TakeBorrow commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasbozdemir committed Apr 29, 2024
1 parent 36260b4 commit 73022fd
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace LibraryTrackingApp.Application.Features.BorrowLend.Behaviors.Mapping;

public class BorrowLendMappingProfile :Profile
public class BorrowLendMappingProfile : Profile
{
public BorrowLendMappingProfile()
{
Expand All @@ -14,7 +14,6 @@ public BorrowLendMappingProfile()
CreateMap<Domain.Entities.Library.BorrowLend, CreateBorrowLendDTO>()
.ReverseMap();


// BorrowLend ↔ GiveBorrowCommandRequest
CreateMap<Domain.Entities.Library.BorrowLend, GiveBorrowCommandRequest>()
.ReverseMap();
Expand All @@ -23,8 +22,20 @@ public BorrowLendMappingProfile()
CreateMap<GiveBorrowCommandRequest, CreateBorrowLendDTO>()
.ReverseMap();


// GiveBorrowCommandRequest ↔ BorrowLendDTO
CreateMap<GiveBorrowCommandRequest, BorrowLendDTO>().ReverseMap();
CreateMap<GiveBorrowCommandRequest, BorrowLendDTO>()
.ReverseMap();

// BorrowLend ↔ TakeBorrowCommandRequest
CreateMap<Domain.Entities.Library.BorrowLend, TakeBorrowCommandRequest>()
.ReverseMap();

// TakeBorrowCommandRequest ↔ CreateBorrowLendDTO
CreateMap<TakeBorrowCommandRequest, CreateBorrowLendDTO>()
.ReverseMap();

// BorrowCommandRequest ↔ BorrowLendDTO
CreateMap<TakeBorrowCommandRequest, BorrowLendDTO>()
.ReverseMap();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,33 @@ IMediator mediator
_mapper = mapper;
}

/*
*
* Burda aslında library kısmını kiracı başına veritabanı yapılırdı.
* istek atılan header içinde daha sonrasında library id de verilir header'dan da o alınır.
* güncellemek isteyen kullanıcının böylece yetkisi kontrol edilir.
*
*/

/*
*
* ve de IUnitOfWork içinde BeginTransactionAsync, RollbackAsync, CommitAsync ve SaveAsync
* bunlar da kullanılcaktır. BeginTransactionAsync kısmında birtakım hata oldugu için su anlık askıya aldım.
* bunlar da kulllanılarak hata olunca rollback yapılcak.
*
*/


/*
*
* NOT: stok durumlarını işe ona göre de ,
* hata mesajlarını response mesajlarını ayarla
*
*
*/

// seed data ile olusan bookid, memberid,lenderid gibi dataları alıp bu handleri test edebilirsiniz.

public async Task<GiveBorrowCommandResponse> Handle(
GiveBorrowCommandRequest request,
CancellationToken cancellationToken
Expand All @@ -45,8 +72,8 @@ CancellationToken cancellationToken
try
{
var existingBook = await _unitOfWork
.GetReadRepository<Domain.Entities.Library.BorrowLend>()
.ExistsAsync(b => b.BookId == request.BookId);
.GetReadRepository<Domain.Entities.Library.Book>()
.ExistsAsync(b => b.Id == request.BookId);

var existingMember = await _unitOfWork
.GetReadRepository<Domain.Entities.Library.Member>()
Expand All @@ -58,88 +85,73 @@ CancellationToken cancellationToken

if (!existingBook || !existingMember || !existingStaff)
{
return new GiveBorrowCommandResponse
return new ()
{
StatusCode = (int)HttpStatusCode.BadRequest,
Success = false,
StateMessages = new [] { "Bazı girişler geçersiz veya eksik." }
StateMessages = new[] { "Bazı girişler geçersiz veya eksik." }
};
}
else
{
var stockDecreaseResponse = await _mediator.Send(
new StockOperationCommandRequest
{
BookId = request.BookId,
OperationType = StockOperationType.Decrease,
Quantity = 1,
}
);

if (stockDecreaseResponse.Success)
var stockDecreaseResponse = await _mediator.Send(
new StockOperationCommandRequest
{
BookId = request.BookId,
OperationType = StockOperationType.Decrease,
Quantity = 1,
}
);

var givenBookWriteRepository = _unitOfWork.GetWriteRepository<Domain.Entities.Library.BorrowLend>();


var givenBook = _mapper.Map<Domain.Entities.Library.BorrowLend>(request);

givenBook.IsLate = givenBook.IsLate = givenBook.ReturnDate > givenBook.DueDate;
givenBook.BorrowStatus = BorrowStatus.Borrowed;

if (givenBook.ReturnDate.HasValue && givenBook.ReturnDate > givenBook.DueDate)
{
givenBook.LateDurationInDays = (int?)(givenBook.ReturnDate - givenBook.DueDate)?.TotalDays;
}
else
{
givenBook.LateDurationInDays = null;
}
if (stockDecreaseResponse.Success)
{
var givenBookWriteRepository =
_unitOfWork.GetWriteRepository<Domain.Entities.Library.BorrowLend>();

var givenBook = _mapper.Map<Domain.Entities.Library.BorrowLend>(request);

givenBook.BorrowDate = DateTime.Now;

var givenBookResult = await givenBookWriteRepository.AddAsync(givenBook);
givenBook.IsLate = givenBook.IsLate = givenBook.ReturnDate > givenBook.DueDate;
givenBook.BorrowStatus = BorrowStatus.Borrowed;

var givenBookResult = await givenBookWriteRepository.AddAsync(givenBook);

if (givenBookResult)
{
return new GiveBorrowCommandResponse
{
StatusCode = (int)HttpStatusCode.OK,
Success = true,
StateMessages = new[] { "Kitap Başarıyla Ödünç Verildi." }
};
}
else
if (givenBookResult)
{
return new ()
{
return new GiveBorrowCommandResponse
{
StatusCode = (int)HttpStatusCode.BadRequest,
Success = true,
StateMessages = new[] { "Kitap Ödünç Verilirken Hata Oluştu." }
};
}


StatusCode = (int)HttpStatusCode.OK,
Success = true,
StateMessages = new[] { "Kitap Başarıyla Ödünç Verildi." }
};
}
else
{
return new GiveBorrowCommandResponse
return new ()
{
StatusCode = stockDecreaseResponse.StatusCode,
Success = stockDecreaseResponse.Success,
StateMessages = stockDecreaseResponse.StateMessages
StatusCode = (int)HttpStatusCode.BadRequest,
Success = true,
StateMessages = new[] { "Kitap Ödünç Verilirken Hata Oluştu." }
};
}
}
else
{
return new ()
{
StatusCode = stockDecreaseResponse.StatusCode,
Success = stockDecreaseResponse.Success,
StateMessages = stockDecreaseResponse.StateMessages
};
}
}
catch (Exception ex)
{
return new()
{
StatusCode = 500,
Success = false,
StateMessages = new [] { $"Bir hata oluştu: {ex.Message}" }
StateMessages = new[] { $"Bir hata oluştu: {ex.Message}" }
};
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
using LibraryTrackingApp.Application.Features.BorrowLend.Commands.Requests;
using System.Net;
using LibraryTrackingApp.Application.Features.BookStocks.Commands.Requests;
using LibraryTrackingApp.Application.Features.BorrowLend.Commands.Requests;
using LibraryTrackingApp.Application.Features.BorrowLend.Commands.Responses;
using LibraryTrackingApp.Application.Interfaces.UnitOfWork;

namespace LibraryTrackingApp.Application.Features.BorrowLend.Commands.Handlers;

/*
*
*
*
*
*
*
*/

public class TakeBorrowCommandHandler
: IRequestHandler<TakeBorrowCommandRequest, TakeBorrowCommandResponse>
{
Expand All @@ -31,18 +24,83 @@ IMediator mediator
_mapper = mapper;
}


// hata durumlarında geri almak için RollbackAsync olayları da yapılcaktır daha.
public async Task<TakeBorrowCommandResponse> Handle(
TakeBorrowCommandRequest request,
CancellationToken cancellationToken
)
{
try
{
var givenBookWriteRepository =
_unitOfWork.GetWriteRepository<Domain.Entities.Library.BorrowLend>();

var givenBookReadRepository =
_unitOfWork.GetReadRepository<Domain.Entities.Library.BorrowLend>();

var borrowedBook = await givenBookReadRepository.GetSingleAsync(
b => b.Id == request.BorrowId
);

if (borrowedBook == null)
return new()
{
StatusCode = (int)HttpStatusCode.NotFound,
Success = false,
StateMessages = new[] { "Ödünç Kitap Bulunamadı." }
};

if (borrowedBook.BorrowStatus == BorrowStatus.Returned)
return new()
{
StatusCode = (int)HttpStatusCode.BadRequest,
Success = false,
StateMessages = new[] { "Kitap zaten iade edilmiş durumda." }
};

borrowedBook.ReturnDate = DateTime.Now;

borrowedBook.BorrowStatus = BorrowStatus.Returned;

if (borrowedBook.ReturnDate.HasValue && borrowedBook.ReturnDate > borrowedBook.DueDate)
{
borrowedBook.LateDurationInDays = (int?)
(borrowedBook.ReturnDate - borrowedBook.DueDate)?.TotalDays;
}
else
{
borrowedBook.LateDurationInDays = 0;
}

var stockDecreaseResponse = await _mediator.Send(
new StockOperationCommandRequest
{
BookId = borrowedBook.BookId,
OperationType = StockOperationType.Increase,
Quantity = 1,
}
);


if (stockDecreaseResponse.Success)
{

var givenBookResult = await givenBookWriteRepository.UpdateAsync(borrowedBook);

return new()
{
StatusCode = (int)HttpStatusCode.OK,
Success = stockDecreaseResponse.Success,
StateMessages = new[] { "Kitap Başarıyla Teslim alındı." }
};
}

return new()
{
StatusCode = 500,
Success = false,
StateMessages = new string[] { $"Bir hata oluştu: " }
StatusCode = stockDecreaseResponse.StatusCode,
Success = stockDecreaseResponse.Success,
StateMessages = stockDecreaseResponse.StateMessages
};
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class GiveBorrowCommandRequest : IRequest<GiveBorrowCommandResponse>
public Guid MemberId { get; set; }
public Guid LenderId { get; set; }

public DateTime BorrowDate { get; set; }
public DateTime DueDate { get; set; }
public bool HasFee { get; set; }
public decimal FeeAmount { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ namespace LibraryTrackingApp.Application.Features.BorrowLend.Commands.Requests;
public class TakeBorrowCommandRequest : IRequest<TakeBorrowCommandResponse>
{
public Guid BorrowId { get; set; }
public DateTime ReturnDate { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using MediatR;
using System.Security.Cryptography;

namespace LibraryTrackingApp.Domain.Events;

public abstract class BaseEvent : INotification
{
public string EntityId { get; set; }
public string EntityId { get; set; }
public DateTime OccurredOn { get; protected set; } = DateTime.UtcNow;
public bool IsSuccessful { get; set; }
public string[] Errors { get; set; }

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace LibraryTrackingApp.Infrastructure.Configuration.ApiDocs.Swagger.Filters;

public class RequestOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
// burda request sınıflarını kaydedip dokumanda daha güzel hale getirmemiz için
}
}
Loading

0 comments on commit 73022fd

Please sign in to comment.