Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added retrieving specific loans by searching id. #26

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 15 additions & 7 deletions src/main/java/com/avengers/example/controller/LoanController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@AllArgsConstructor
@RestController
Expand All @@ -19,8 +18,8 @@ public class LoanController
/**
* Creates a new loan object and saves it to the database.
*
* @param loan the loan instance to be added to the database.
* @return The response from the server.
* //@param loan the loan instance to be added to the database.
* //@return The response from the server.
*/
@PostMapping("/loan")
public ResponseEntity<?> save(@RequestBody Loan loan)
Expand All @@ -29,11 +28,20 @@ public ResponseEntity<?> save(@RequestBody Loan loan)
}

/**
* @return A response object containing all loans from the database.
* //@return A response object containing all loans from the database.
*/
@GetMapping("/loans")
public ResponseEntity<?> findAll()
{
return new ResponseEntity<>(loanService.findAll(), HttpStatus.OK);
}

@GetMapping("/{id}") public ResponseEntity<Loan> getLoanById(@PathVariable Long id) {
Optional<Loan> loan = loanService.getLoanById(id);
if (loan.isPresent()) {
return ResponseEntity.ok(loan.get());
} else {
return ResponseEntity.notFound().build();
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/avengers/example/service/LoanService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@AllArgsConstructor
@Service
Expand All @@ -24,4 +25,8 @@ public List<Loan> findAll()
{
return loanRepository.findAll();
}

public Optional<Loan> getLoanById(Long id) {
return loanRepository.findById(id);
}
}