Skip to content
Merged
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
114 changes: 114 additions & 0 deletions API/Controllers/ReviewController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using Application.Commands.ReviewCommands.AddReview;
using Application.Commands.ReviewCommands.DeleteReview;
using Application.Commands.ReviewCommands.UpdateReview;
using Application.Dtos.ReviewDtos;
using Application.Queries.ReviewQueries.GetAllReviews;
using Application.Queries.ReviewQueries.GetReviewById;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ReviewController : ControllerBase
{
private readonly IMediator _mediator;

public ReviewController(IMediator mediator)
{
_mediator = mediator;
}

[HttpGet("GetAll")]
public async Task<IActionResult> GetAllReviews()
{
try
{
var operationResult = await _mediator.Send(new GetAllReviewsQuery());
if (operationResult is not null)
{
return Ok(operationResult.Data);
}
return BadRequest(new { message = operationResult.Message, errors = operationResult.ErrorMessage });
}
catch (Exception ex)
{
return BadRequest(ex.InnerException);
}
}

[HttpGet("GetById/{id}")]
public async Task<IActionResult> GetReviewById(int id)
{
try
{
var operationResult = await _mediator.Send(new GetReviewByIdQuery(id));
if (operationResult is not null)
{
return Ok(operationResult.Data);
}
return BadRequest(new { message = operationResult.Message, errors = operationResult.ErrorMessage });
}
catch (Exception ex)
{
return BadRequest(ex.InnerException);
}
}

[HttpPost("Create")]
public async Task<IActionResult> AddReview(AddReviewDto dto)
{
try
{
var operationResult = await _mediator.Send(new AddReviewCommand(dto));
if (operationResult is not null)
{
return Ok(operationResult.Data);
}
return BadRequest(new { message = operationResult.Message, errors = operationResult.ErrorMessage });
}
catch (Exception ex)
{
return BadRequest(ex.InnerException);
}
}

[HttpPut("Update")]
public async Task<IActionResult> UpdateReview(UpdateReviewDto dto)
{
try
{
var operationResult = await _mediator.Send(new UpdateReviewCommand(dto));
if (operationResult is not null)
{
return Ok(operationResult.Data);
}
return BadRequest(new { message = operationResult.Message, errors = operationResult.ErrorMessage });
}
catch (Exception ex)
{
return BadRequest(ex.InnerException);
}
}

[HttpDelete("Delete/{id}")]
public async Task<IActionResult> DeleteReview(int id)
{
try
{
var operationResult = await _mediator.Send(new DeleteReviewCommand(id));
if (operationResult is not null)
{
return Ok(operationResult.Data);
}
return BadRequest(new { message = operationResult.Message, errors = operationResult.ErrorMessage });
}
catch (Exception ex)
{
return BadRequest(ex.InnerException);
}

}
}
}
11 changes: 11 additions & 0 deletions Application/Commands/ReviewCommands/AddReview/AddReviewCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Application.Dtos.ReviewDtos;
using Application.Models;
using MediatR;

namespace Application.Commands.ReviewCommands.AddReview
{
public class AddReviewCommand(AddReviewDto dto) : IRequest<OperationResult<GetReviewDto>>
{
public AddReviewDto dto { get; set; } = dto;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Application.Dtos.ReviewDtos;
using Application.Interfaces.RepositoryInterfaces;
using Application.Models;
using AutoMapper;
using Domain.Entities.Metadata;
using MediatR;

namespace Application.Commands.ReviewCommands.AddReview
{
public class AddReviewCommandHandler(IGenericRepository<Review, int> repository, IMapper mapper) : IRequestHandler<AddReviewCommand, OperationResult<GetReviewDto>>
{
private readonly IGenericRepository<Review, int> _repository = repository;
private readonly IMapper _mapper = mapper;


public async Task<OperationResult<GetReviewDto>> Handle(AddReviewCommand request, CancellationToken cancellationToken)
{
try
{
Review review = new()
{
Rating = request.dto.Rating,
Comment = request.dto.Comment,
ReviewDate = request.dto.ReviewDate,
BookId = request.dto.BookId
};

var createdReview = await _repository.AddAsync(review);
if (createdReview != null)
{
var mappedReview = _mapper.Map<GetReviewDto>(request.dto);
return OperationResult<GetReviewDto>.Success(mappedReview);
}
return OperationResult<GetReviewDto>.Failure("Review not added");
}
catch (Exception ex)
{
throw new Exception("Review not added", ex);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

using FluentValidation;

namespace Application.Commands.ReviewCommands.AddReview
{
public class AddReviewCommandValidator : AbstractValidator<AddReviewCommand>
{
public AddReviewCommandValidator()
{
RuleFor(x => x.dto.Comment)
.NotEmpty().WithMessage("Comment can not be empty");

RuleFor(x => x.dto.Rating)
.NotEmpty().WithMessage("Rating betwen 1 - 5 is required.");

// More stuff here.
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

using Application.Models;
using MediatR;

namespace Application.Commands.ReviewCommands.DeleteReview
{
public class DeleteReviewCommand(int id) : IRequest<OperationResult<bool>>
{
public int Id { get; set; } = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Application.Interfaces.RepositoryInterfaces;
using Application.Models;
using AutoMapper;
using Domain.Entities.Metadata;
using MediatR;

namespace Application.Commands.ReviewCommands.DeleteReview
{
internal class DeleteReviewCommandHandler(IGenericRepository<Review, int> repository, IMapper mapper) : IRequestHandler<DeleteReviewCommand, OperationResult<bool>>
{
private readonly IGenericRepository<Review, int> _repository = repository;
private readonly IMapper _mapper = mapper;

public async Task<OperationResult<bool>> Handle(DeleteReviewCommand request, CancellationToken cancellationToken)
{
try
{
var deletionSucessful = await _repository.DeleteAsync(request.Id);
if (deletionSucessful is false)
{
return OperationResult<bool>.Failure("Operation failed");
}
return OperationResult<bool>.Success(true);
}
catch (Exception ex)
{
return OperationResult<bool>.Failure(ex.Message, ex.InnerException.Message);
}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using FluentValidation;

namespace Application.Commands.ReviewCommands.DeleteReview
{
public class DeleteReviewCommandValidator : AbstractValidator<DeleteReviewCommand>
{
public DeleteReviewCommandValidator()
{
RuleFor(x => x.Id)
.NotEmpty().WithMessage("Id can not be empty");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Application.Dtos.ReviewDtos;
using Application.Models;
using MediatR;

namespace Application.Commands.ReviewCommands.UpdateReview
{
public class UpdateReviewCommand(UpdateReviewDto dto) : IRequest<OperationResult<GetReviewDto>>
{
public UpdateReviewDto dto { get; set; } = dto;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Application.Dtos.ReviewDtos;
using Application.Interfaces.RepositoryInterfaces;
using Application.Models;
using AutoMapper;
using Domain.Entities.Metadata;
using MediatR;

namespace Application.Commands.ReviewCommands.UpdateReview
{
public class UpdateReviewCommandHandler(IGenericRepository<Review, int> repository, IMapper mapper) : IRequestHandler<UpdateReviewCommand, OperationResult<GetReviewDto>>
{
private readonly IGenericRepository<Review, int> _repository = repository;
private readonly IMapper _mapper = mapper;

public async Task<OperationResult<GetReviewDto>> Handle(UpdateReviewCommand request, CancellationToken cancellationToken)
{
try
{
Review review = new()
{
Id = request.dto.ReviewId,
Rating = request.dto.Rating,
Comment = request.dto.Comment,
ReviewDate = request.dto.ReviewDate,
BookId = request.dto.BookId
};
var updatedReview = await _repository.UpdateAsync(review);
var mappedReview = _mapper.Map<GetReviewDto>(updatedReview);
return OperationResult<GetReviewDto>.Success(mappedReview);
}
catch (Exception ex)
{
return OperationResult<GetReviewDto>.Failure(ex.Message, ex.InnerException.Message);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

using FluentValidation;

namespace Application.Commands.ReviewCommands.UpdateReview
{
public class UpdateReviewCommandValidator : AbstractValidator<UpdateReviewCommand>
{
public UpdateReviewCommandValidator()
{
RuleFor(x => x.dto.ReviewId)
.NotEmpty().WithMessage("ReviewId can not be empty");

RuleFor(x => x.dto.Comment)
.NotEmpty().WithMessage("Comment can not be empty");

RuleFor(x => x.dto.Rating)
.NotEmpty().WithMessage("Rating betwen 1 - 5 is required.");

// More stuff here.
}
}
}
1 change: 1 addition & 0 deletions Application/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static IServiceCollection AddApplication(this IServiceCollection services
services.AddAutoMapper(typeof(BookCopyMappingProfiles).Assembly);
services.AddAutoMapper(typeof(LibraryBranchMappingProfiles).Assembly);
services.AddAutoMapper(typeof(GenreMappingProfiles).Assembly);
services.AddAutoMapper(typeof(ReviewMappingProfiles).Assembly);

// Register validators
services.AddValidatorsFromAssemblyContaining<AddAuthorCommandValidator>();
Expand Down
11 changes: 11 additions & 0 deletions Application/Dtos/ReviewDtos/AddReviewDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

namespace Application.Dtos.ReviewDtos
{
public class AddReviewDto
{
public int Rating { get; set; }
public string Comment { get; set; }
public DateTime ReviewDate { get; set; }
public Guid BookId { get; set; }
}
}
12 changes: 12 additions & 0 deletions Application/Dtos/ReviewDtos/GetReviewDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

namespace Application.Dtos.ReviewDtos
{
public class GetReviewDto
{
public int ReviewId { get; set; }
public int Rating { get; set; }
public string Comment { get; set; }
public DateTime ReviewDate { get; set; }
public Guid BookId { get; set; }
}
}
12 changes: 12 additions & 0 deletions Application/Dtos/ReviewDtos/UpdateReviewDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

namespace Application.Dtos.ReviewDtos
{
public class UpdateReviewDto
{
public int ReviewId { get; set; }
public int Rating { get; set; }
public string Comment { get; set; }
public DateTime ReviewDate { get; set; }
public Guid BookId { get; set; }
}
}
17 changes: 17 additions & 0 deletions Application/Mappings/ReviewMappingProfiles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

using Application.Dtos.ReviewDtos;
using AutoMapper;
using Domain.Entities.Metadata;

namespace Application.Mappings
{
public class ReviewMappingProfiles : Profile
{
public ReviewMappingProfiles()
{
CreateMap<Review, AddReviewDto>();
CreateMap<Review, GetReviewDto>();

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Application.Dtos.ReviewDtos;
using Application.Models;
using MediatR;

namespace Application.Queries.ReviewQueries.GetAllReviews
{
public class GetAllReviewsQuery() : IRequest<OperationResult<List<GetReviewDto>>>
{

}
}
Loading
Loading