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
20 changes: 20 additions & 0 deletions Core/LibPoint.Application/Abstractions/IBookRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using LibPoint.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace LibPoint.Application.Abstractions
{
public interface IBookRepository<T> where T : class
{
Task<List<T>> GetAllAsync();
Task<T> GetByIdAsync(Guid id);
Task CreateAsync(T entity);
Task UpdateAsync(T entity);
Task RemoveAsync(T entity);

}
}
17 changes: 17 additions & 0 deletions Core/LibPoint.Application/Abstractions/IReviewRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibPoint.Application.Abstractions
{
public interface IReviewRepository<T> where T : class
{
Task<List<T>> GetAllAsync();
Task<T> GetByIdAsync(int id);
Task CreateAsync(T entity);
Task UpdateAsync(T entity);
Task RemoveAsync(T entity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using LibPoint.Domain.Models.Responses;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibPoint.Application.Features.Books.Commands
{
public class CreateBookCommandRequest: IRequest<ResponseModel<Guid>>
{
public string Name { get; set; }
public string ISBN { get; set; } // kitap numarası
public bool IsAvailable { get; set; } = true;
public string? Publisher { get; set; }
public int? PublishedYear { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using LibPoint.Domain.Models.Responses;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibPoint.Application.Features.Books.Commands
{
public class RemoveBookCommandRequest: IRequest<ResponseModel<Guid>>
{
public Guid Id { get; set; }

public RemoveBookCommandRequest(Guid id)
{
Id = id;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using LibPoint.Domain.Models.Responses;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibPoint.Application.Features.Books.Commands
{
public class UpdateBookCommandRequest: IRequest<ResponseModel<Guid>>
{
public Guid Id { get; set; }
public string Name { get; set; }
public string ISBN { get; set; } // kitap numarası
public bool IsAvailable { get; set; } = true;
public string? Publisher { get; set; }
public int? PublishedYear { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using LibPoint.Application.Abstractions;
using LibPoint.Application.Features.Books.Commands;
using LibPoint.Application.Features.Review.Commands;
using LibPoint.Domain.Entities;
using LibPoint.Domain.Models.Responses;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibPoint.Application.Features.Books.Handlers
{
public class CreateBookCommandHandler : IRequestHandler<CreateBookCommandRequest, ResponseModel<Guid>>
{
private readonly IRepository<Book> _repository;

public CreateBookCommandHandler(IRepository<Book> repository)
{
_repository = repository;
}

public async Task<ResponseModel<Guid>> Handle(CreateBookCommandRequest request, CancellationToken cancellationToken)
{
var newBook = new Book
{
IsAvailable = request.IsAvailable,
ISBN = request.ISBN,
Name = request.Name,
PublishedYear = request.PublishedYear,
Publisher = request.Publisher,

};

var result = await _repository.AddAsync(newBook);

var saveResult = await _repository.SaveChangesAsync();

if(saveResult == false)
{
return new ResponseModel<Guid>("Book could not be created", 500);
}
else
{
return new ResponseModel<Guid>(newBook.Id);
}





}




}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using LibPoint.Application.Abstractions;
using LibPoint.Application.Features.Books.Queries;
using LibPoint.Application.Features.Books.Results;
using LibPoint.Application.Features.Review.Queries;
using LibPoint.Application.Features.Review.Results;
using LibPoint.Domain.Entities;
using LibPoint.Domain.Models.Books;
using LibPoint.Domain.Models.Responses;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace LibPoint.Application.Features.Books.Handlers
{
public class GetBookByIdQueryHandler: IRequestHandler<GetBookByIdQueryRequest, ResponseModel<BookModel>>
{
private readonly IRepository<Book> _repository;

public GetBookByIdQueryHandler(IRepository<Book> repository)
{
_repository = repository;
}

public async Task<ResponseModel<BookModel>> Handle(GetBookByIdQueryRequest request, CancellationToken cancellationToken)
{
var values = await _repository.GetAsync(b => b.Id == request.Id, false, b => b.Author, b => b.Categories);
if (values == null)
{
return new ResponseModel<BookModel>("hata", 400);


}
else
{
var bookmodel = new BookModel
{
Id = values.Id,
Name = values.Name,
ISBN = values.ISBN,
IsAvailable = values.IsAvailable,
PublishedYear = values.PublishedYear,
Publisher = values.Publisher,


};

return new ResponseModel<BookModel>(bookmodel);

}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using LibPoint.Application.Abstractions;
using LibPoint.Application.Features.Books.Queries;
using LibPoint.Application.Features.Books.Results;
using LibPoint.Application.Features.Review.Results;
using LibPoint.Domain.Entities;
using LibPoint.Domain.Models.Books;
using LibPoint.Domain.Models.Responses;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibPoint.Application.Features.Books.Handlers
{
public class GetBookQueryHandler: IRequestHandler<GetBooksQueryRequest, ResponseModel<List<BookModel>>>
{
private readonly IRepository<Book> _repository;
public GetBookQueryHandler(IRepository<Book> repository)
{
_repository = repository;
}

public async Task<ResponseModel<List<BookModel>>> Handle(GetBooksQueryRequest request, CancellationToken cancellationToken )
{
var values = await _repository.GetAllAsync();
if (values == null)
{
return new ResponseModel<List<BookModel>>("No books found", 404);
}
else
{
var bookmodel = values.Select(Book => new BookModel
{
Id = Book.Id,
Name = Book.Name,
ISBN = Book.ISBN,
IsAvailable = Book.IsAvailable,
PublishedYear = Book.PublishedYear,
Publisher = Book.Publisher,
Categories = Book.Categories

}).ToList();
return new ResponseModel<List<BookModel>>(bookmodel);

}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using LibPoint.Application.Abstractions;
using LibPoint.Application.Features.Books.Commands;
using LibPoint.Application.Features.Review.Commands;
using LibPoint.Domain.Entities;
using LibPoint.Domain.Models.Responses;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace LibPoint.Application.Features.Books.Handlers
{
public class RemoveBookCommandHandler : IRequestHandler<RemoveBookCommandRequest, ResponseModel<Guid>>
{
private readonly IRepository<Book> _repository;

public RemoveBookCommandHandler(IRepository<Book> repository)
{
_repository = repository;
}

public async Task<ResponseModel<Guid>> Handle(RemoveBookCommandRequest request, CancellationToken cancellationToken)
{
var deletingBook = await _repository.GetAsync(x => x.Id == request.Id);
if (deletingBook == null)
{
return new ResponseModel<Guid>
{
Success = false,
Data = Guid.Empty,
Messages = new[] { "Book not found." },
StatusCode = 404
};
}

var deleteResult = _repository.Delete(deletingBook);
if (!deleteResult)
{
return new ResponseModel<Guid>
{
Success = false,
Data = Guid.Empty,
Messages = new[] { "Failed to delete the book." },
StatusCode = 500
};
}

await _repository.SaveChangesAsync();

return new ResponseModel<Guid>
{
Success = true,
Data = deletingBook.Id,
Messages = new[] { "Book deleted successfully." },
StatusCode = 200
};
}
}
}
Loading