From dffe3c80bd45fbf8bff3b55d8e779580edb0d041 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ya=C4=9Fmur=20Aslan?= Date: Mon, 2 Jun 2025 09:27:07 +0300 Subject: [PATCH 1/3] Author Kontrol --- .../Commands/CreateAuthorCommandRequest.cs | 13 +++++ .../Commands/RemoveAuthorCommandRequest.cs | 15 ++++++ .../Commands/UpdateAuthorCommandRequest.cs | 14 +++++ .../Handlers/CreateAuthorCommandHandler.cs | 36 +++++++++++++ .../Handlers/GetAllAuthorsQueryHandler.cs | 28 ++++++++++ .../Handlers/GetAuthorByIdQueryHandler.cs | 31 +++++++++++ .../Handlers/RemoveAuthorCommandHandler.cs | 28 ++++++++++ .../Handlers/UpdateAuthorCommandHandler.cs | 33 ++++++++++++ .../Queries/GetAllAuthorsQueryRequest.cs | 10 ++++ .../Queries/GetAuthorByIdQueryRequest.cs | 16 ++++++ .../Results/GetAllAuthorsQueryResult.cs | 11 ++++ .../Results/GetAuthorByIdQueryResult.cs | 11 ++++ .../Models/Authors/AuthorModel.cs | 16 ++++++ .../Controllers/AuthorController.cs | 53 +++++++++++++++++++ 14 files changed, 315 insertions(+) create mode 100644 Core/LibPoint.Application/Features/Authors/Commands/CreateAuthorCommandRequest.cs create mode 100644 Core/LibPoint.Application/Features/Authors/Commands/RemoveAuthorCommandRequest.cs create mode 100644 Core/LibPoint.Application/Features/Authors/Commands/UpdateAuthorCommandRequest.cs create mode 100644 Core/LibPoint.Application/Features/Authors/Handlers/CreateAuthorCommandHandler.cs create mode 100644 Core/LibPoint.Application/Features/Authors/Handlers/GetAllAuthorsQueryHandler.cs create mode 100644 Core/LibPoint.Application/Features/Authors/Handlers/GetAuthorByIdQueryHandler.cs create mode 100644 Core/LibPoint.Application/Features/Authors/Handlers/RemoveAuthorCommandHandler.cs create mode 100644 Core/LibPoint.Application/Features/Authors/Handlers/UpdateAuthorCommandHandler.cs create mode 100644 Core/LibPoint.Application/Features/Authors/Queries/GetAllAuthorsQueryRequest.cs create mode 100644 Core/LibPoint.Application/Features/Authors/Queries/GetAuthorByIdQueryRequest.cs create mode 100644 Core/LibPoint.Application/Features/Authors/Results/GetAllAuthorsQueryResult.cs create mode 100644 Core/LibPoint.Application/Features/Authors/Results/GetAuthorByIdQueryResult.cs create mode 100644 Core/LibPoint.Domain/Models/Authors/AuthorModel.cs create mode 100644 Presentation/LibPoint.API/Controllers/AuthorController.cs diff --git a/Core/LibPoint.Application/Features/Authors/Commands/CreateAuthorCommandRequest.cs b/Core/LibPoint.Application/Features/Authors/Commands/CreateAuthorCommandRequest.cs new file mode 100644 index 0000000..dc889a0 --- /dev/null +++ b/Core/LibPoint.Application/Features/Authors/Commands/CreateAuthorCommandRequest.cs @@ -0,0 +1,13 @@ +using LibPoint.Domain.Models.Responses; +using MediatR; + +namespace LibPoint.Application.Features.Authors.Commands; + +public class CreateAuthorCommandRequest:IRequest> +{ + public string Name { get; set; } + public string Surname { get; set; } + public string? Bio { get; set; } + public DateTime? DateOfBirth { get; set; } + public DateTime? DateOfDeath { get; set; } +} \ No newline at end of file diff --git a/Core/LibPoint.Application/Features/Authors/Commands/RemoveAuthorCommandRequest.cs b/Core/LibPoint.Application/Features/Authors/Commands/RemoveAuthorCommandRequest.cs new file mode 100644 index 0000000..d3b70f6 --- /dev/null +++ b/Core/LibPoint.Application/Features/Authors/Commands/RemoveAuthorCommandRequest.cs @@ -0,0 +1,15 @@ +using LibPoint.Domain.Models.Responses; +using MediatR; + +namespace LibPoint.Application.Features.Authors.Commands; + +public class RemoveAuthorCommandRequest:IRequest> +{ + public RemoveAuthorCommandRequest(Guid id) + { + Id = id; + } + public Guid Id { get; set; } + + +} \ No newline at end of file diff --git a/Core/LibPoint.Application/Features/Authors/Commands/UpdateAuthorCommandRequest.cs b/Core/LibPoint.Application/Features/Authors/Commands/UpdateAuthorCommandRequest.cs new file mode 100644 index 0000000..329ce52 --- /dev/null +++ b/Core/LibPoint.Application/Features/Authors/Commands/UpdateAuthorCommandRequest.cs @@ -0,0 +1,14 @@ +using LibPoint.Domain.Models.Responses; +using MediatR; + +namespace LibPoint.Application.Features.Authors.Commands; + +public class UpdateAuthorCommandRequest: IRequest > +{ + public Guid Id { get; set; } + public string Name { get; set; } + public string Surname { get; set; } + public string? Bio { get; set; } + public DateTime? DateOfBirth { get; set; } + public DateTime? DateOfDeath { get; set; } +} \ No newline at end of file diff --git a/Core/LibPoint.Application/Features/Authors/Handlers/CreateAuthorCommandHandler.cs b/Core/LibPoint.Application/Features/Authors/Handlers/CreateAuthorCommandHandler.cs new file mode 100644 index 0000000..e742f1e --- /dev/null +++ b/Core/LibPoint.Application/Features/Authors/Handlers/CreateAuthorCommandHandler.cs @@ -0,0 +1,36 @@ +using LibPoint.Application.Abstractions; +using LibPoint.Domain.Entities; +using LibPoint.Domain.Models.Responses; +using MediatR; + +namespace LibPoint.Application.Features.Authors.Commands; + +public class CreateAuthorCommandHandler:IRequestHandler> +{ + private readonly IRepository _repository; + + public CreateAuthorCommandHandler(IRepository repository) + { + _repository = repository; + } + + public async Task> Handle(CreateAuthorCommandRequest request, CancellationToken cancellationToken) + { + var author = new Author + { + Name = request.Name, + Surname = request.Surname, + Bio = request.Bio, + DateOfBirth = request.DateOfBirth, + DateOfDeath = request.DateOfDeath, Books = new System.Collections.Generic.HashSet() + + }; + var addResult=await _repository.AddAsync(author); + + if (!addResult) + return new ResponseModel("Author could not be created", 400); + + await _repository.SaveChangesAsync(); + return new ResponseModel(author.Id); + } +} \ No newline at end of file diff --git a/Core/LibPoint.Application/Features/Authors/Handlers/GetAllAuthorsQueryHandler.cs b/Core/LibPoint.Application/Features/Authors/Handlers/GetAllAuthorsQueryHandler.cs new file mode 100644 index 0000000..91fdfa6 --- /dev/null +++ b/Core/LibPoint.Application/Features/Authors/Handlers/GetAllAuthorsQueryHandler.cs @@ -0,0 +1,28 @@ +using AutoMapper; +using LibPoint.Application.Abstractions; +using LibPoint.Domain.Entities; +using LibPoint.Domain.Models.Author; +using LibPoint.Domain.Models.Responses; +using MediatR; + +namespace LibPoint.Application.Features.Authors.Queries; + +public class GetAllAuthorsQueryHandler:IRequestHandler>> +{ + private readonly IRepository _repository; + private readonly IMapper _mapper; + + public GetAllAuthorsQueryHandler(IRepository repository, IMapper mapper) + { + _repository = repository; + _mapper = mapper; + } + + public async Task>> Handle(GetAllAuthorsQueryRequest request, CancellationToken cancellationToken) + { + var authors = await _repository.GetAllAsync(); + + var mappedAuthors = _mapper.Map>(authors); + return new ResponseModel>(mappedAuthors, 200); + } +} \ No newline at end of file diff --git a/Core/LibPoint.Application/Features/Authors/Handlers/GetAuthorByIdQueryHandler.cs b/Core/LibPoint.Application/Features/Authors/Handlers/GetAuthorByIdQueryHandler.cs new file mode 100644 index 0000000..4a322a3 --- /dev/null +++ b/Core/LibPoint.Application/Features/Authors/Handlers/GetAuthorByIdQueryHandler.cs @@ -0,0 +1,31 @@ +using AutoMapper; +using LibPoint.Application.Abstractions; +using LibPoint.Domain.Entities; +using LibPoint.Domain.Models.Author; +using LibPoint.Domain.Models.Responses; +using MediatR; + +namespace LibPoint.Application.Features.Authors.Queries; + +public class GetAuthorByIdQueryHandler:IRequestHandler> +{ + private readonly IRepository _repository; + private readonly IMapper _mapper; + + public GetAuthorByIdQueryHandler(IRepository repository, IMapper mapper) + { + _repository = repository; + _mapper = mapper; + } + + + public async Task> Handle(GetAuthorByIdQueryRequest request, CancellationToken cancellationToken) + { + var author = await _repository.GetByIdAsync(request.Id); + if (author == null) + return new ResponseModel("Author not found", 404 ); + + var mappedAuthor = _mapper.Map(author); + return new ResponseModel(mappedAuthor, 200); + } +} \ No newline at end of file diff --git a/Core/LibPoint.Application/Features/Authors/Handlers/RemoveAuthorCommandHandler.cs b/Core/LibPoint.Application/Features/Authors/Handlers/RemoveAuthorCommandHandler.cs new file mode 100644 index 0000000..40cde18 --- /dev/null +++ b/Core/LibPoint.Application/Features/Authors/Handlers/RemoveAuthorCommandHandler.cs @@ -0,0 +1,28 @@ +using LibPoint.Application.Abstractions; +using LibPoint.Domain.Entities; +using LibPoint.Domain.Models.Responses; +using MediatR; + +namespace LibPoint.Application.Features.Authors.Commands; + +public class RemoveAuthorCommandHandler:IRequestHandler> +{ + private readonly IRepository _repository; + + public RemoveAuthorCommandHandler(IRepository repository) + { + _repository = repository; + } + public async Task> Handle(RemoveAuthorCommandRequest request, CancellationToken cancellationToken) + { + var author =await _repository.GetByIdAsync(request.Id); + + if (author==null || author.IsDeleted) + return new ResponseModel("Author not found or already removed", 404); + + author.IsDeleted = true; + _repository.Update(author); + await _repository.SaveChangesAsync(); + return new ResponseModel(author.Id); + } +} \ No newline at end of file diff --git a/Core/LibPoint.Application/Features/Authors/Handlers/UpdateAuthorCommandHandler.cs b/Core/LibPoint.Application/Features/Authors/Handlers/UpdateAuthorCommandHandler.cs new file mode 100644 index 0000000..f866421 --- /dev/null +++ b/Core/LibPoint.Application/Features/Authors/Handlers/UpdateAuthorCommandHandler.cs @@ -0,0 +1,33 @@ +using LibPoint.Application.Abstractions; +using LibPoint.Domain.Entities; +using LibPoint.Domain.Models.Responses; +using MediatR; + +namespace LibPoint.Application.Features.Authors.Commands; + +public class UpdateAuthorCommandHandler:IRequestHandler> +{ + private readonly IRepository _repository; + + public UpdateAuthorCommandHandler(IRepository repository) + { + _repository = repository; + } + + public async Task> Handle(UpdateAuthorCommandRequest request, CancellationToken cancellationToken) + { + var author = await _repository.GetByIdAsync(request.Id); + if (author == null) + return new ResponseModel("Author not found",404); + + author.Name = request.Name; + author.Surname = request.Surname; + author.Bio = request.Bio; + author.DateOfBirth = request.DateOfBirth; + author.DateOfDeath = request.DateOfDeath; + + await _repository.SaveChangesAsync(); + return new ResponseModel(author.Id); + } + +} \ No newline at end of file diff --git a/Core/LibPoint.Application/Features/Authors/Queries/GetAllAuthorsQueryRequest.cs b/Core/LibPoint.Application/Features/Authors/Queries/GetAllAuthorsQueryRequest.cs new file mode 100644 index 0000000..4ed85ca --- /dev/null +++ b/Core/LibPoint.Application/Features/Authors/Queries/GetAllAuthorsQueryRequest.cs @@ -0,0 +1,10 @@ +using LibPoint.Domain.Models.Author; +using LibPoint.Domain.Models.Responses; +using MediatR; + +namespace LibPoint.Application.Features.Authors.Queries; + +public class GetAllAuthorsQueryRequest:IRequest>> +{ + +} \ No newline at end of file diff --git a/Core/LibPoint.Application/Features/Authors/Queries/GetAuthorByIdQueryRequest.cs b/Core/LibPoint.Application/Features/Authors/Queries/GetAuthorByIdQueryRequest.cs new file mode 100644 index 0000000..16c90fc --- /dev/null +++ b/Core/LibPoint.Application/Features/Authors/Queries/GetAuthorByIdQueryRequest.cs @@ -0,0 +1,16 @@ +using LibPoint.Domain.Models.Author; +using LibPoint.Domain.Models.Responses; +using MediatR; + +namespace LibPoint.Application.Features.Authors.Queries; + +public class GetAuthorByIdQueryRequest:IRequest> +{ + public Guid Id { get; set; } + + public GetAuthorByIdQueryRequest(Guid id) + { + Id = id; + } + +} \ No newline at end of file diff --git a/Core/LibPoint.Application/Features/Authors/Results/GetAllAuthorsQueryResult.cs b/Core/LibPoint.Application/Features/Authors/Results/GetAllAuthorsQueryResult.cs new file mode 100644 index 0000000..0234fb5 --- /dev/null +++ b/Core/LibPoint.Application/Features/Authors/Results/GetAllAuthorsQueryResult.cs @@ -0,0 +1,11 @@ +namespace LibPoint.Application.Features.Authors.Results; + +public class GetAllAuthorsQueryResult +{ + public Guid Id { get; set; } + public string Name { get; set; } + public string Surname { get; set; } + public string? Bio { get; set; } + public DateTime? DateOfBirth { get; set; } + public DateTime? DateOfDeath { get; set; } +} \ No newline at end of file diff --git a/Core/LibPoint.Application/Features/Authors/Results/GetAuthorByIdQueryResult.cs b/Core/LibPoint.Application/Features/Authors/Results/GetAuthorByIdQueryResult.cs new file mode 100644 index 0000000..0450adb --- /dev/null +++ b/Core/LibPoint.Application/Features/Authors/Results/GetAuthorByIdQueryResult.cs @@ -0,0 +1,11 @@ +namespace LibPoint.Application.Features.Authors.Results; + +public class GetAuthorByIdQueryResult +{ + public Guid Id { get; set; } + public string Name { get; set; } + public string Surname { get; set; } + public string? Bio { get; set; } + public DateTime? DateOfBirth { get; set; } + public DateTime? DateOfDeath { get; set; } +} \ No newline at end of file diff --git a/Core/LibPoint.Domain/Models/Authors/AuthorModel.cs b/Core/LibPoint.Domain/Models/Authors/AuthorModel.cs new file mode 100644 index 0000000..781a1c3 --- /dev/null +++ b/Core/LibPoint.Domain/Models/Authors/AuthorModel.cs @@ -0,0 +1,16 @@ +using LibPoint.Domain.Entities; + +namespace LibPoint.Domain.Models.Author; + +public class AuthorModel +{ + public Guid Id { get; set; } // Yazarın benzersiz kimliği + public string Name { get; set; } + public string Surname { get; set; } + public string? Bio { get; set; } + public DateTime? DateOfBirth { get; set; } + public DateTime? DateOfDeath { get; set; } + + public ICollectionBooks { get; set; } +} + diff --git a/Presentation/LibPoint.API/Controllers/AuthorController.cs b/Presentation/LibPoint.API/Controllers/AuthorController.cs new file mode 100644 index 0000000..126aecb --- /dev/null +++ b/Presentation/LibPoint.API/Controllers/AuthorController.cs @@ -0,0 +1,53 @@ +using LibPoint.Application.Features.Authors.Commands; +using LibPoint.Application.Features.Authors.Queries; +using MediatR; +using Microsoft.AspNetCore.Mvc; + +namespace LibPoint.API.Controllers; + +[Route("api/[controller]")] +[ApiController] +public class AuthorController: ControllerBase +{ + private readonly IMediator _mediator; + + public AuthorController(IMediator mediator) + { + _mediator = mediator; + } + [HttpPost("create-author")] + public async Task CreateAuthor([FromBody] CreateAuthorCommandRequest request) + { + var response = await _mediator.Send(request); + return response.Success ? Ok(response) : BadRequest(response); + } + + [HttpPut("update-author")] + public async Task UpdateAuthor([FromBody] UpdateAuthorCommandRequest request) + { + var response = await _mediator.Send(request); + return response.Success ? Ok(response) : BadRequest(response); + } + + [HttpDelete("remove-author")] + public async Task RemoveAuthor([FromQuery] Guid id) + { + var response = await _mediator.Send(new RemoveAuthorCommandRequest(id)); + return response.Success ? Ok(response) : BadRequest(response); + } + + [HttpGet("get-author-by-id")] + public async Task GetAuthorById([FromQuery] Guid id) + { + var response = await _mediator.Send(new GetAuthorByIdQueryRequest(id)); + return response.Success ? Ok(response) : NotFound(response); + } + + [HttpGet("get-all-authors")] + public async Task GetAllAuthors() + { + var response = await _mediator.Send(new GetAllAuthorsQueryRequest()); + return response.Success ? Ok(response) : BadRequest(response); + } + +} \ No newline at end of file From 39852fab501ad82283530cbdf18de79040aaded6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ya=C4=9Fmur=20Aslan?= Date: Mon, 2 Jun 2025 09:43:37 +0300 Subject: [PATCH 2/3] =?UTF-8?q?D=C3=BCzenleme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/LibPoint.Application/Abstractions/IRepository.cs | 2 +- Infrastructure/LibPoint.Persistence/Services/Repository.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/LibPoint.Application/Abstractions/IRepository.cs b/Core/LibPoint.Application/Abstractions/IRepository.cs index c20aa50..ddf11d0 100644 --- a/Core/LibPoint.Application/Abstractions/IRepository.cs +++ b/Core/LibPoint.Application/Abstractions/IRepository.cs @@ -13,7 +13,7 @@ public interface IRepository where T : class DbSet Table { get; } Task> GetAllAsync(Expression> expression = null, bool tracking = false, params Expression>[] includeEntity); Task GetAsync(Expression> expression = null, bool tracking = false, params Expression>[] includeEntity); - Task GetByIdAsync(int id, bool tracking = false); + Task GetByIdAsync(Guid id, bool tracking = false); Task AddAsync(T entity); Task AddRangeAsync(List entites); bool Update(T entity); diff --git a/Infrastructure/LibPoint.Persistence/Services/Repository.cs b/Infrastructure/LibPoint.Persistence/Services/Repository.cs index 54b85e0..b9a64c1 100644 --- a/Infrastructure/LibPoint.Persistence/Services/Repository.cs +++ b/Infrastructure/LibPoint.Persistence/Services/Repository.cs @@ -97,7 +97,7 @@ public async Task GetAsync(Expression> expression = null, bool return await query.SingleOrDefaultAsync(); } - public async Task GetByIdAsync(int id, bool tracking = false) + public async Task GetByIdAsync(Guid id, bool tracking = false) { var query = Table.AsQueryable(); From 606807dcac146bdb84d510d30fb0cdcf70293375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ya=C4=9Fmur=20Aslan?= Date: Mon, 2 Jun 2025 23:10:00 +0300 Subject: [PATCH 3/3] Author Test Kontrol --- .../Handlers/GetAllAuthorsQueryHandler.cs | 20 ++++++++++++++----- .../Handlers/GetAuthorByIdQueryHandler.cs | 18 +++++++++++------ .../Handlers/RemoveAuthorCommandHandler.cs | 5 ++--- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/Core/LibPoint.Application/Features/Authors/Handlers/GetAllAuthorsQueryHandler.cs b/Core/LibPoint.Application/Features/Authors/Handlers/GetAllAuthorsQueryHandler.cs index 91fdfa6..eea3f4b 100644 --- a/Core/LibPoint.Application/Features/Authors/Handlers/GetAllAuthorsQueryHandler.cs +++ b/Core/LibPoint.Application/Features/Authors/Handlers/GetAllAuthorsQueryHandler.cs @@ -10,19 +10,29 @@ namespace LibPoint.Application.Features.Authors.Queries; public class GetAllAuthorsQueryHandler:IRequestHandler>> { private readonly IRepository _repository; - private readonly IMapper _mapper; - public GetAllAuthorsQueryHandler(IRepository repository, IMapper mapper) + public GetAllAuthorsQueryHandler(IRepository repository) { _repository = repository; - _mapper = mapper; } public async Task>> Handle(GetAllAuthorsQueryRequest request, CancellationToken cancellationToken) { var authors = await _repository.GetAllAsync(); - var mappedAuthors = _mapper.Map>(authors); - return new ResponseModel>(mappedAuthors, 200); + if(authors == null) + return new ResponseModel>("No authors found", 404 ); + + var authorModels = authors.Select(author => new AuthorModel + { + Id = author.Id, + Name = author.Name, + Surname = author.Surname, + Bio = author.Bio, + DateOfBirth = author.DateOfBirth, + DateOfDeath = author.DateOfDeath, + }).ToList(); + + return new ResponseModel>(authorModels, 200); } } \ No newline at end of file diff --git a/Core/LibPoint.Application/Features/Authors/Handlers/GetAuthorByIdQueryHandler.cs b/Core/LibPoint.Application/Features/Authors/Handlers/GetAuthorByIdQueryHandler.cs index 4a322a3..489f24b 100644 --- a/Core/LibPoint.Application/Features/Authors/Handlers/GetAuthorByIdQueryHandler.cs +++ b/Core/LibPoint.Application/Features/Authors/Handlers/GetAuthorByIdQueryHandler.cs @@ -10,12 +10,9 @@ namespace LibPoint.Application.Features.Authors.Queries; public class GetAuthorByIdQueryHandler:IRequestHandler> { private readonly IRepository _repository; - private readonly IMapper _mapper; - - public GetAuthorByIdQueryHandler(IRepository repository, IMapper mapper) + public GetAuthorByIdQueryHandler(IRepository repository) { _repository = repository; - _mapper = mapper; } @@ -24,8 +21,17 @@ public async Task> Handle(GetAuthorByIdQueryRequest r var author = await _repository.GetByIdAsync(request.Id); if (author == null) return new ResponseModel("Author not found", 404 ); + + var authorModel = new AuthorModel + { + Id = author.Id, + Name = author.Name, + Surname = author.Surname, + Bio = author.Bio, + DateOfBirth = author.DateOfBirth, + DateOfDeath = author.DateOfDeath, + }; - var mappedAuthor = _mapper.Map(author); - return new ResponseModel(mappedAuthor, 200); + return new ResponseModel(authorModel, 200); } } \ No newline at end of file diff --git a/Core/LibPoint.Application/Features/Authors/Handlers/RemoveAuthorCommandHandler.cs b/Core/LibPoint.Application/Features/Authors/Handlers/RemoveAuthorCommandHandler.cs index 40cde18..e27ada5 100644 --- a/Core/LibPoint.Application/Features/Authors/Handlers/RemoveAuthorCommandHandler.cs +++ b/Core/LibPoint.Application/Features/Authors/Handlers/RemoveAuthorCommandHandler.cs @@ -17,11 +17,10 @@ public async Task> Handle(RemoveAuthorCommandRequest request { var author =await _repository.GetByIdAsync(request.Id); - if (author==null || author.IsDeleted) + if (author==null) return new ResponseModel("Author not found or already removed", 404); - author.IsDeleted = true; - _repository.Update(author); + var deleteauthor=_repository.Delete(author); await _repository.SaveChangesAsync(); return new ResponseModel(author.Id); }