Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from ualibweb/chris-hellen-03-databases
Browse files Browse the repository at this point in the history
Completed training module 03-databases
  • Loading branch information
chris-hellen authored Apr 16, 2024
2 parents b5a2b80 + 24dfc97 commit 50b323b
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 11 deletions.
13 changes: 12 additions & 1 deletion src/main/java/org/folio/sample/controller/BookController.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public ResponseEntity<BookDTO> getBook(UUID bookId) {
HttpStatus.OK
);
}

/** {@inheritDoc} */
@Override
public ResponseEntity<BookDTO> updateBook(UUID bookId, BookForCreationDTO newBook) {
log.info("Called PUT /books/{}", bookId);

Expand All @@ -85,4 +86,14 @@ public ResponseEntity<BookDTO> updateBook(UUID bookId, BookForCreationDTO newBoo
HttpStatus.OK
);
}
/** {@inheritDoc} */
@Override
public ResponseEntity<List<BookDTO>> getAllAvailable() {
log.info("Called GET /books/available");

return new ResponseEntity<>(
bookService.findAllAvailable().stream().map(bookMapper::toDto).toList(),
HttpStatus.OK
);
}
}
7 changes: 7 additions & 0 deletions src/main/java/org/folio/sample/domain/entity/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ public class Book implements Serializable {
@Column(name = "published_date")
private LocalDate publishedDate;

/**
* If the book is available
*/
@NotNull
@Column(name = "available")
private Boolean available;

public boolean isPublishedInLeapYear() {
if (this.getPublishedDate().getYear() % 4 != 0) {
return false;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/folio/sample/domain/mapper/BookMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
public interface BookMapper {
BookMapper INSTANCE = Mappers.getMapper(BookMapper.class);

@Mapping(source = "available", target = "isAvailable")
BookDTO toDto(Book source);

@Mapping(target = "id", ignore = true)
@Mapping(source = "isAvailable", target = "available")
Book fromDto(BookForCreationDTO source);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package org.folio.sample.repository;

import java.util.List;
import java.util.UUID;
import org.folio.sample.domain.entity.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

/**
* A {@link org.springframework.data.jpa.repository.JpaRepository} of {@link org.folio.sample.domain.entity.Book} objects
*/
@Repository
public interface BookRepository extends JpaRepository<Book, UUID> {}
public interface BookRepository extends JpaRepository<Book, UUID> {
@Query("SELECT b FROM Book b WHERE b.available = true")
List<Book> findAllAvailable();
}
4 changes: 4 additions & 0 deletions src/main/java/org/folio/sample/service/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ public Book updateBook (UUID id, Book newBook){

return bookRepository.save(book);
}

public List<Book> findAllAvailable(){
return bookRepository.findAllAvailable();
}
}
14 changes: 14 additions & 0 deletions src/main/resources/api/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,17 @@ paths:
$ref: "./schemas/book.yaml"
"404":
description: The book was not found
/books/available:
get:
summary: Get available books
description: Get all available books
operationId: getAllAvailable
responses:
"200":
description: All available books
content:
application/json:
schema:
type: array
items:
$ref: "./schemas/book.yaml"
4 changes: 4 additions & 0 deletions src/main/resources/api/schemas/bookForCreation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ properties:
type: string
format: date
description: When the book was published
isAvailable:
type: boolean
description: The books availability
required:
- name
- publishedDate
- isAvailable
15 changes: 15 additions & 0 deletions src/main/resources/db/changes/0010-add-available-column.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
databaseChangeLog:
- changeSet:
id: 0010-add-available-column
author: chellen
comment: add available column to books table
changes:
- addColumn:
tableName: books
columns:
- column:
name: available
type: boolean
constraints:
nullable: true
defaultValueBoolean: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.folio.sample.integration.api.books;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;

import io.restassured.common.mapper.TypeRef;
import io.restassured.response.Response;
import java.time.LocalDate;
import java.util.List;
import org.folio.sample.domain.dto.BookDTO;
import org.folio.sample.domain.dto.BookForCreationDTO;
import org.folio.sample.integration.AbstractBaseApiTest;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;

class GetAllAvailableTest extends AbstractBaseApiTest {

@Test
void testEmptyGet(){

Response response = ra()
.get(getRequestUrl("books/available"));
response.then().statusCode(is(HttpStatus.OK.value()));

List<BookDTO> collection = response
.body()
.as(new TypeRef<List<BookDTO>>() {});
assertThat(collection, hasSize(0));
}

@Test
void testGetWithBooks(){
ra()
.body(
BookForCreationDTO
.builder()
.name("book 1")
.publishedDate(LocalDate.of(2020, 1, 1))
.isAvailable(true)
.build()
)
.post(getRequestUrl("books"))
.then()
.statusCode(is(HttpStatus.CREATED.value()));

ra()
.body(
BookForCreationDTO
.builder()
.name("book 2")
.publishedDate(LocalDate.of(2020, 1, 1))
.isAvailable(false)
.build()
)
.post(getRequestUrl("books"))
.then()
.statusCode(is(HttpStatus.CREATED.value()));

Response response = ra()
.get(getRequestUrl("books/available"));
response.then().statusCode(is(HttpStatus.OK.value()));

List<BookDTO> collection = response
.body()
.as(new TypeRef<List<BookDTO>>() {});
assertThat(collection, hasSize(1));
assertThat(collection.get(0).getName(), is(equalTo("book 1")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.hamcrest.Matchers.is;

import io.restassured.response.Response;
import net.minidev.json.JSONObject;
import java.time.LocalDate;
import java.util.UUID;

Expand Down Expand Up @@ -39,20 +40,25 @@ void testUpdate(){

UUID createdId = postResponse.as(BookDTO.class).getId();

JSONObject updateInfo = new JSONObject();

updateInfo.put("name", "updated book");
updateInfo.put("publishedDate", LocalDate.of(2024, 1, 1));
updateInfo.put("isAvailable", true);

Response putResponse = ra()
.body(
BookForCreationDTO
.builder()
.name("updated book")
.publishedDate(LocalDate.of(2020, 1, 1))
.build()
)
.body(updateInfo)
.pathParam("id", createdId)
.put(getRequestUrl("books/{id}"));
putResponse.then().statusCode(is(HttpStatus.OK.value()));

Response getResponse = ra()
.pathParam("id", createdId)
.get(getRequestUrl("books/{id}"));
getResponse.then().statusCode(is(HttpStatus.OK.value()));

BookDTO book = putResponse.getBody().as(BookDTO.class);
BookDTO book = getResponse.getBody().as(BookDTO.class);
assertThat(book.getName(), is(equalTo("updated book")));
assertThat(book.getPublishedDate(), is(equalTo(LocalDate.of(2020, 1, 1))));
assertThat(book.getPublishedDate(), is(equalTo(LocalDate.of(2024, 1, 1))));
}
}

0 comments on commit 50b323b

Please sign in to comment.