- used for tests
- no DB implenention for prod
This commit is contained in:
djmil 2023-07-20 12:31:04 +02:00
parent 198b49dd8f
commit 38f86076c2
6 changed files with 47 additions and 7 deletions

View File

@ -8,7 +8,7 @@ group = 'djmil'
version = '0.0.1-SNAPSHOT' version = '0.0.1-SNAPSHOT'
java { java {
sourceCompatibility = '17' sourceCompatibility = '20'
} }
repositories { repositories {
@ -18,8 +18,26 @@ repositories {
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.data:spring-data-jdbc'
testImplementation 'com.h2database:h2'
} }
tasks.named('test') { tasks.named('test') {
useJUnitPlatform() useJUnitPlatform()
} }
// This section causes useful test output to go to the terminal.
test {
testLogging {
events "passed", "skipped", "failed" //, "standardOut", "standardError"
showExceptions true
exceptionFormat "full"
showCauses true
showStackTraces true
// Change to `true` for more verbose test output
showStandardStreams = false
}
}

View File

@ -1,5 +1,6 @@
package djmil.cashcard; package djmil.cashcard;
public record CashCard(Long id, Double amount) { import org.springframework.data.annotation.Id;
public record CashCard(@Id Long id, Double amount) {
} }

View File

@ -6,15 +6,24 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.Optional;
@RestController @RestController
@RequestMapping("/cashcards") @RequestMapping("/cashcards")
public class CashCardController { public class CashCardController {
private CashCardRepository cashCardRepository;
public CashCardController(CashCardRepository cashCardRepository) {
this.cashCardRepository = cashCardRepository;
}
@GetMapping("/{requestedId}") @GetMapping("/{requestedId}")
public ResponseEntity<CashCard> findById(@PathVariable Long requestedId) { public ResponseEntity<CashCard> findById(@PathVariable Long requestedId) {
if (requestedId.equals(99L)) { Optional<CashCard> cashCardOptional = cashCardRepository.findById(requestedId);
CashCard cashCard = new CashCard(99L, 123.45);
return ResponseEntity.ok(cashCard); if (cashCardOptional.isPresent()) {
return ResponseEntity.ok(cashCardOptional.get());
} else { } else {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }

View File

@ -0,0 +1,6 @@
package djmil.cashcard;
import org.springframework.data.repository.CrudRepository;
public interface CashCardRepository extends CrudRepository<CashCard, Long> {
}

View File

@ -0,0 +1 @@
INSERT INTO CASH_CARD(ID, AMOUNT) VALUES (99, 123.45);

View File

@ -0,0 +1,5 @@
CREATE TABLE cash_card
(
ID BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
AMOUNT NUMBER NOT NULL DEFAULT 0
);