Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Core/LibPoint.Application/Abstractions/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IRepository<T> where T : class
DbSet<T> Table { get; }
Task<List<T>> GetAllAsync(Expression<Func<T, bool>> expression = null, bool tracking = false, params Expression<Func<T, object>>[] includeEntity);
Task<T> GetAsync(Expression<Func<T, bool>> expression = null, bool tracking = false, params Expression<Func<T, object>>[] includeEntity);
Task<T> GetByIdAsync(int id, bool tracking = false);
Task<T> GetByIdAsync(Guid id, bool tracking = false);
Task<bool> AddAsync(T entity);
Task AddRangeAsync(List<T> entites);
bool Update(T entity);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using LibPoint.Domain.Models.Responses;
using MediatR;

namespace LibPoint.Application.Features.Authors.Commands;

public class CreateAuthorCommandRequest:IRequest<ResponseModel<Guid>>
{
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; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using LibPoint.Domain.Models.Responses;
using MediatR;

namespace LibPoint.Application.Features.Authors.Commands;

public class RemoveAuthorCommandRequest:IRequest<ResponseModel<Guid>>
{
public RemoveAuthorCommandRequest(Guid id)
{
Id = id;
}
public Guid Id { get; set; }


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using LibPoint.Domain.Models.Responses;
using MediatR;

namespace LibPoint.Application.Features.Authors.Commands;

public class UpdateAuthorCommandRequest: IRequest <ResponseModel<Guid>>
{
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; }
}
Original file line number Diff line number Diff line change
@@ -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<CreateAuthorCommandRequest, ResponseModel<Guid>>
{
private readonly IRepository<Author> _repository;

public CreateAuthorCommandHandler(IRepository<Author> repository)
{
_repository = repository;
}

public async Task<ResponseModel<Guid>> 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<Book>()

};
var addResult=await _repository.AddAsync(author);

if (!addResult)
return new ResponseModel<Guid>("Author could not be created", 400);

await _repository.SaveChangesAsync();
return new ResponseModel<Guid>(author.Id);
}
}
Original file line number Diff line number Diff line change
@@ -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<GetAllAuthorsQueryRequest, ResponseModel<List<AuthorModel>>>
{
private readonly IRepository<Author> _repository;

public GetAllAuthorsQueryHandler(IRepository<Author> repository)
{
_repository = repository;
}

public async Task<ResponseModel<List<AuthorModel>>> Handle(GetAllAuthorsQueryRequest request, CancellationToken cancellationToken)
{
var authors = await _repository.GetAllAsync();

if(authors == null)
return new ResponseModel<List<AuthorModel>>("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<List<AuthorModel>>(authorModels, 200);
}
}
Original file line number Diff line number Diff line change
@@ -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<GetAuthorByIdQueryRequest, ResponseModel<AuthorModel>>
{
private readonly IRepository<Author> _repository;
public GetAuthorByIdQueryHandler(IRepository<Author> repository)
{
_repository = repository;
}


public async Task<ResponseModel<AuthorModel>> Handle(GetAuthorByIdQueryRequest request, CancellationToken cancellationToken)
{
var author = await _repository.GetByIdAsync(request.Id);
if (author == null)
return new ResponseModel<AuthorModel>("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>(authorModel, 200);
}
}
Original file line number Diff line number Diff line change
@@ -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<RemoveAuthorCommandRequest, ResponseModel<Guid>>
{
private readonly IRepository<Author> _repository;

public RemoveAuthorCommandHandler(IRepository<Author> repository)
{
_repository = repository;
}
public async Task<ResponseModel<Guid>> Handle(RemoveAuthorCommandRequest request, CancellationToken cancellationToken)
{
var author =await _repository.GetByIdAsync(request.Id);

if (author==null)
return new ResponseModel<Guid>("Author not found or already removed", 404);

var deleteauthor=_repository.Delete(author);
await _repository.SaveChangesAsync();
return new ResponseModel<Guid>(author.Id);
}
}
Original file line number Diff line number Diff line change
@@ -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<UpdateAuthorCommandRequest, ResponseModel<Guid>>
{
private readonly IRepository<Author> _repository;

public UpdateAuthorCommandHandler(IRepository<Author> repository)
{
_repository = repository;
}

public async Task<ResponseModel<Guid>> Handle(UpdateAuthorCommandRequest request, CancellationToken cancellationToken)
{
var author = await _repository.GetByIdAsync(request.Id);
if (author == null)
return new ResponseModel<Guid>("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<Guid>(author.Id);
}

}
Original file line number Diff line number Diff line change
@@ -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<ResponseModel<List<AuthorModel>>>
{

}
Original file line number Diff line number Diff line change
@@ -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<ResponseModel<AuthorModel>>
{
public Guid Id { get; set; }

public GetAuthorByIdQueryRequest(Guid id)
{
Id = id;
}

}
Original file line number Diff line number Diff line change
@@ -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; }
}
Original file line number Diff line number Diff line change
@@ -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; }
}
16 changes: 16 additions & 0 deletions Core/LibPoint.Domain/Models/Authors/AuthorModel.cs
Original file line number Diff line number Diff line change
@@ -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 ICollection<Book>Books { get; set; }
}

2 changes: 1 addition & 1 deletion Infrastructure/LibPoint.Persistence/Services/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public async Task<T> GetAsync(Expression<Func<T, bool>> expression = null, bool
return await query.SingleOrDefaultAsync();
}

public async Task<T> GetByIdAsync(int id, bool tracking = false)
public async Task<T> GetByIdAsync(Guid id, bool tracking = false)
{
var query = Table.AsQueryable();

Expand Down
53 changes: 53 additions & 0 deletions Presentation/LibPoint.API/Controllers/AuthorController.cs
Original file line number Diff line number Diff line change
@@ -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<IActionResult> CreateAuthor([FromBody] CreateAuthorCommandRequest request)
{
var response = await _mediator.Send(request);
return response.Success ? Ok(response) : BadRequest(response);
}

[HttpPut("update-author")]
public async Task<IActionResult> UpdateAuthor([FromBody] UpdateAuthorCommandRequest request)
{
var response = await _mediator.Send(request);
return response.Success ? Ok(response) : BadRequest(response);
}

[HttpDelete("remove-author")]
public async Task<IActionResult> 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<IActionResult> 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<IActionResult> GetAllAuthors()
{
var response = await _mediator.Send(new GetAllAuthorsQueryRequest());
return response.Success ? Ok(response) : BadRequest(response);
}

}