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
2 changes: 1 addition & 1 deletion API/Controllers/BookController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public async Task<IActionResult> AddBook([FromBody, Required] AddBookDto bookToA
[SwaggerResponse(200, "Successfully Updated Book.", typeof(BookDto))]
[SwaggerResponse(400, "Invalid input data.")]
[SwaggerResponse(404, "Book not found.")]
public async Task<IActionResult> UpdateBook([FromBody, Required] BookDto book)
public async Task<IActionResult> UpdateBook([FromBody, Required] UpdateBookDto book)
{
if (!ModelState.IsValid)
{
Expand Down
3 changes: 2 additions & 1 deletion Application/Commands/BookCommands/AddBook/AddBookCommand.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Application.Dtos.BookDtos;
using Application.Models;
using Domain.Entities.Core;
using MediatR;

namespace Application.Commands.BookCommands.AddBook
{
public class AddBookCommand(AddBookDto bookToCreate) : IRequest<OperationResult<AddBookDto>>
public class AddBookCommand(AddBookDto bookToCreate) : IRequest<OperationResult<GetBookDto>>
{
public AddBookDto NewBook { get; } = bookToCreate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

namespace Application.Commands.BookCommands.AddBook
{
public class AddBookCommandHandler(IBookRepository bookRepository, IMapper mapper) : IRequestHandler<AddBookCommand, OperationResult<AddBookDto>>
public class AddBookCommandHandler(IBookRepository bookRepository, IMapper mapper) : IRequestHandler<AddBookCommand, OperationResult<GetBookDto>>
{
private readonly IBookRepository _bookRepository = bookRepository;
public readonly IMapper _mapper = mapper;

public async Task<OperationResult<AddBookDto>> Handle(AddBookCommand request, CancellationToken cancellationToken)
public async Task<OperationResult<GetBookDto>> Handle(AddBookCommand request, CancellationToken cancellationToken)
{
// var existingAuthor = _database.Authors.Where(author => author.Id == request.NewBook.AuthorId.Id);
// Kolla om det finns existerande author eller om en ny ska läggas till

if (request == null || request.NewBook == null || string.IsNullOrEmpty(request.NewBook.Title))
{
return OperationResult<AddBookDto>.Failure("Not valid input"); //Is this if-statement really needed with my ModelState now ?
return OperationResult<GetBookDto>.Failure("Not valid input"); //Is this if-statement really needed with my ModelState now ?
}
try
{
Expand All @@ -33,9 +33,9 @@ public async Task<OperationResult<AddBookDto>> Handle(AddBookCommand request, Ca
};

var createdBook = await _bookRepository.AddBook(bookToCreate);
var mappedBook = _mapper.Map<AddBookDto>(createdBook);
var mappedBook = _mapper.Map<GetBookDto>(createdBook);

return OperationResult<AddBookDto>.Success(mappedBook);
return OperationResult<GetBookDto>.Success(mappedBook);
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

namespace Application.Commands.BookCommands.UpdateBook
{
public class UpdateBookCommand(BookDto bookToUpdate) : IRequest<OperationResult<Book>>
public class UpdateBookCommand(UpdateBookDto bookToUpdate) : IRequest<OperationResult<GetBookDto>>
{
public BookDto NewBook { get; set; } = bookToUpdate;
public UpdateBookDto NewBook { get; set; } = bookToUpdate;
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
using Application.Interfaces.RepositoryInterfaces;
using Application.Dtos.BookDtos;
using Application.Interfaces.RepositoryInterfaces;
using Application.Models;
using AutoMapper;
using Domain.Entities.Core;
using MediatR;

namespace Application.Commands.BookCommands.UpdateBook
{
public class UpdateBookCommandHandler(IBookRepository bookRepository, IMapper mapper) : IRequestHandler<UpdateBookCommand, OperationResult<Book>>
public class UpdateBookCommandHandler(IBookRepository bookRepository, IMapper mapper) : IRequestHandler<UpdateBookCommand, OperationResult<GetBookDto>>
{
private readonly IBookRepository _bookRepository = bookRepository;
public IMapper _mapper = mapper;

public async Task<OperationResult<Book>> Handle(UpdateBookCommand request, CancellationToken cancellationToken)
public async Task<OperationResult<GetBookDto>> Handle(UpdateBookCommand request, CancellationToken cancellationToken)
{
if (request == null || request.NewBook == null || string.IsNullOrEmpty(request.NewBook.Title))
{
return OperationResult<Book>.Failure("Invalid input");
return OperationResult<GetBookDto>.Failure("Invalid input");
}

try
Expand All @@ -24,15 +25,16 @@ public async Task<OperationResult<Book>> Handle(UpdateBookCommand request, Cance
{
BookId = request.NewBook.Id,
Title = request.NewBook.Title,
Genre = request.NewBook.Genre,
Description = request.NewBook.Description,
AuthorId = request.NewBook.AuthorId,
Description = request.NewBook.Description
};

var updatedBook = await _bookRepository.UpdateBook(bookToUpdate);

var mappedBook = _mapper.Map<Book>(updatedBook);
var mappedBook = _mapper.Map<GetBookDto>(updatedBook);

return OperationResult<Book>.Success(mappedBook);
return OperationResult<GetBookDto>.Success(mappedBook);
}

catch (Exception ex)
Expand Down
2 changes: 1 addition & 1 deletion Application/Dtos/BookDtos/BookDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class BookDto(Guid id, string title, Guid authorId, string description)
{
public Guid Id { get; set; } = id;
public string Title { get; set; } = title;
public Guid AuthorId { get; set; } = authorId;
public string Description { get; set; } = description;
public Guid AuthorId { get; set; } = authorId;
}
}
12 changes: 12 additions & 0 deletions Application/Dtos/BookDtos/UpdateBookDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

namespace Application.Dtos.BookDtos
{
public class UpdateBookDto()
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Genre { get; set; }
public string Description { get; set; }
public Guid AuthorId { get; set; }
}
}
1 change: 1 addition & 0 deletions Application/Mappings/BookMappingProfiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public BookMappingProfiles()
CreateMap<BookDto, Book>();
CreateMap<GetBookDto, Book>();
CreateMap<Book, GetBookDto>();
CreateMap<Book, UpdateBookDto>();


// For Testing:
Expand Down
2 changes: 1 addition & 1 deletion TestProject/BookIntegrationTests/AddBookIntegrationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void Setup()
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<AddBookDto, Book>();
cfg.CreateMap<Book, AddBookDto>();
cfg.CreateMap<Book, GetBookDto>();
});
_mapper = config.CreateMapper();

Expand Down
22 changes: 19 additions & 3 deletions TestProject/BookIntegrationTests/UpdateBookIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ public void Setup()
{
cfg.CreateMap<BookDto, Book>()
.ForMember(dest => dest.BookId, opt => opt.MapFrom(src => ExampleBookId));
cfg.CreateMap<Book, GetBookDto>();
});
_mapper = config.CreateMapper();

Book book = new()
{
BookId = ExampleBookId,
Title = "Test",
Genre = "Fantasy",
AuthorId = Guid.NewGuid(),
Description = "Description"
};
Expand All @@ -68,7 +70,14 @@ public void TearDown()
public async Task Handle_ValidInput_ReturnsBook()
{
// Arrange
BookDto bookToTest = new(ExampleBookId, "Test", Guid.NewGuid(), "New Description");
UpdateBookDto bookToTest = new UpdateBookDto
{
Id = ExampleBookId,
Title = "Test",
Description = "New Description",
Genre = "Fantasy",
AuthorId = Guid.NewGuid()
};
var command = new UpdateBookCommand(bookToTest);

// Act
Expand All @@ -84,7 +93,7 @@ public async Task Handle_ValidInput_ReturnsBook()
public async Task Handle_NullInput_ReturnsNull()
{
// Arrange
BookDto bookToTest = null!; // Use null-forgiving operator to explicitly indicate null
UpdateBookDto bookToTest = null!; // Use null-forgiving operator to explicitly indicate null
var command = new UpdateBookCommand(bookToTest);

// Act
Expand All @@ -99,7 +108,14 @@ public async Task Handle_NullInput_ReturnsNull()
public async Task Handle_MissingTitle_ReturnsNull()
{
// Arrange
BookDto bookToTest = new(new Guid("12345678-1234-5678-1234-567812345678"), null!, Guid.NewGuid(), "BookService for Testing"); // Use null-forgiving operator to explicitly indicate null
UpdateBookDto bookToTest = new UpdateBookDto
{
Id = new Guid("12345678-1234-5678-1234-567812345678"),
Title = null!,
Genre = "Fantasy",
Description = "BookService for Testing",
AuthorId = Guid.NewGuid()
};
var command = new UpdateBookCommand(bookToTest);

// Act
Expand Down
6 changes: 3 additions & 3 deletions TestProject/BookUnitTests/AddBookUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public void SetUp()
AuthorId = dto.AuthorId
});

// Set up mock to map from Book to AddBookDto
_mockMapper.Setup(mapper => mapper.Map<AddBookDto>(It.IsAny<Book>()))
.Returns((Book book) => new AddBookDto
// Set up mock to map from Book to GetBookDto
_mockMapper.Setup(mapper => mapper.Map<GetBookDto>(It.IsAny<Book>()))
.Returns((Book book) => new GetBookDto
{
Title = book.Title,
Genre = book.Genre,
Expand Down
53 changes: 39 additions & 14 deletions TestProject/BookUnitTests/UpdateBookUnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Application.Commands.BookCommands.UpdateBook;
using Application.Dtos;
using Application.Dtos.BookDtos;
using Application.Interfaces.RepositoryInterfaces;
using AutoMapper;
Expand All @@ -21,8 +20,9 @@ public class UpdateBookUnitTest
{
BookId = ExampleBookId,
Title = "Test",
Genre = "Fantasy",
Description = "Test",
AuthorId = Guid.NewGuid(),
Description = "Test"
};

[SetUp]
Expand All @@ -34,28 +34,44 @@ public void Setup()
_mockRepository.Setup(repo => repo.UpdateBook(It.Is<Book>(obj => obj.BookId == ExampleBookId)))
.ReturnsAsync(ExampleBook);

_mockRepository.Setup(repo => repo.GetBookById(It.Is<Guid>(id => id != ExampleBookId)))
.ReturnsAsync((Book)null!);
_mockRepository.Setup(repo => repo.GetBookById(It.Is<Guid>(id => id == ExampleBookId)))
.ReturnsAsync(ExampleBook);

_mockMapper.Setup(mapper => mapper.Map<Book>(It.IsAny<UpdateBookDto>()))
.Returns((UpdateBookDto dto) => new Book
{
BookId = dto.Id,
Title = dto.Title,
Genre = dto.Genre,
Description = dto.Description,
AuthorId = dto.AuthorId
});

_mockMapper.Setup(mapper => mapper.Map<Book>(It.IsAny<Book>()));
_mockMapper.Setup(mapper => mapper.Map<Book>(It.IsAny<Book>()))
.Returns((Book book) => new Book
_mockMapper.Setup(mapper => mapper.Map<GetBookDto>(It.IsAny<Book>()))
.Returns((Book book) => new GetBookDto
{
BookId = ExampleBookId,
BookId = book.BookId,
Title = book.Title,
AuthorId = book.AuthorId,
Description = book.Description
Genre = book.Genre,
Description = book.Description,
AuthorId = (Guid)book.AuthorId
});

_handler = new UpdateBookCommandHandler(_mockRepository.Object, _mockMapper.Object);
}


[Test]
public async Task Handle_ValidInput_ReturnsBook()
{
// Arrange
BookDto bookToTest = new(new Guid("3e2e66cf-5ba6-4cd0-88a1-c37b71cca899"), "Test", Guid.NewGuid(), "Test");
UpdateBookDto bookToTest = new UpdateBookDto
{
Id = ExampleBookId,
Title = "Test",
Genre = "Fantasy",
Description = "Test",
AuthorId = Guid.NewGuid(),
};
var command = new UpdateBookCommand(bookToTest);

// Act
Expand All @@ -67,11 +83,14 @@ public async Task Handle_ValidInput_ReturnsBook()
}





[Test]
public async Task Handle_NullInput_ReturnsNull()
{
// Arrange
BookDto bookToTest = null!; // Use null-forgiving operator to explicitly indicate null
UpdateBookDto bookToTest = null!; // Use null-forgiving operator to explicitly indicate null
var command = new UpdateBookCommand(bookToTest);

// Act
Expand All @@ -86,7 +105,13 @@ public async Task Handle_NullInput_ReturnsNull()
public async Task Handle_MissingTitle_ReturnsNull()
{
// Arrange
BookDto bookToTest = new(new Guid("12345678-1234-5678-1234-567812345678"), null!, Guid.NewGuid(), "BookService for Testing"); // Use null-forgiving operator to explicitly indicate null
UpdateBookDto bookToTest = new UpdateBookDto
{
Id = new Guid("12345678-1234-5678-1234-567812345678"),
Title = null!,
AuthorId = Guid.NewGuid(),
Description = "BookService for Testing"
};
var command = new UpdateBookCommand(bookToTest);

// Act
Expand Down
Loading