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/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..eea3f4b --- /dev/null +++ b/Core/LibPoint.Application/Features/Authors/Handlers/GetAllAuthorsQueryHandler.cs @@ -0,0 +1,38 @@ +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; + + public GetAllAuthorsQueryHandler(IRepository repository) + { + _repository = repository; + } + + public async Task>> Handle(GetAllAuthorsQueryRequest request, CancellationToken cancellationToken) + { + var authors = await _repository.GetAllAsync(); + + 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 new file mode 100644 index 0000000..489f24b --- /dev/null +++ b/Core/LibPoint.Application/Features/Authors/Handlers/GetAuthorByIdQueryHandler.cs @@ -0,0 +1,37 @@ +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; + public GetAuthorByIdQueryHandler(IRepository repository) + { + _repository = repository; + } + + + 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 authorModel = new AuthorModel + { + Id = author.Id, + Name = author.Name, + Surname = author.Surname, + Bio = author.Bio, + DateOfBirth = author.DateOfBirth, + DateOfDeath = author.DateOfDeath, + }; + + 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 new file mode 100644 index 0000000..e27ada5 --- /dev/null +++ b/Core/LibPoint.Application/Features/Authors/Handlers/RemoveAuthorCommandHandler.cs @@ -0,0 +1,27 @@ +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) + return new ResponseModel("Author not found or already removed", 404); + + var deleteauthor=_repository.Delete(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/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(); 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