Browse Source

pagination & validation for GET entity partner

master
art.dambrine 3 years ago
parent
commit
ae917c54f8
  1. 6
      build.gradle
  2. 13
      src/main/java/com/example/apispringgradleb2boost/controller/PartnerController.java
  3. 13
      src/main/java/com/example/apispringgradleb2boost/service/PartnerService.java
  4. 13
      src/main/resources/data.sql

6
build.gradle

@ -21,12 +21,16 @@ repositories {
ext { ext {
set('snippetsDir', file("build/generated-snippets")) set('snippetsDir', file("build/generated-snippets"))
// Added depedencies version after spring init
gsonVersion = '2.8.9'
} }
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
compileOnly 'org.projectlombok:lombok' compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools' developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.h2database:h2' runtimeOnly 'com.h2database:h2'
@ -35,7 +39,7 @@ dependencies {
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc' testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
// Added after spring init // Added after spring init
implementation 'com.google.code.gson:gson:2.8.9' implementation "com.google.code.gson:gson:$gsonVersion"
} }

13
src/main/java/com/example/apispringgradleb2boost/controller/PartnerController.java

@ -5,32 +5,41 @@ import com.example.apispringgradleb2boost.model.Partner;
import com.example.apispringgradleb2boost.service.PartnerService; import com.example.apispringgradleb2boost.service.PartnerService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.query.Param;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.google.gson.Gson; import com.google.gson.Gson;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Min;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.Optional; import java.util.Optional;
@RestController @RestController
@Validated
public class PartnerController { public class PartnerController {
@Autowired @Autowired
PartnerService partnerService; PartnerService partnerService;
@GetMapping("/partners") @GetMapping("/partners")
public Iterable<Partner> getPartners() { public Iterable<Partner> getPartners(@Param("from") @Min(0) final Integer from, @Param("size") @Min(1) final Integer size) {
if (from != null && size != null) {
return partnerService.getPartners(from, size);
}
return partnerService.getPartners(); return partnerService.getPartners();
} }
@GetMapping("/partner/{id}") @GetMapping("/partner/{id}")
public Partner getPartnerById(@PathVariable("id") final Long Id, HttpServletResponse response) throws IOException { public Partner getPartnerById(@PathVariable("id") @Min(1) final Long Id, HttpServletResponse response) throws IOException {
Optional<Partner> partner = partnerService.getPartnerById(Id); Optional<Partner> partner = partnerService.getPartnerById(Id);
if (partner.isPresent()) { if (partner.isPresent()) {

13
src/main/java/com/example/apispringgradleb2boost/service/PartnerService.java

@ -4,8 +4,12 @@ import com.example.apispringgradleb2boost.model.Partner;
import com.example.apispringgradleb2boost.repository.PartnerRepository; import com.example.apispringgradleb2boost.repository.PartnerRepository;
import lombok.Data; import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Optional; import java.util.Optional;
@Data @Data
@ -16,11 +20,16 @@ public class PartnerService {
@Autowired @Autowired
private PartnerRepository partnerRepository; private PartnerRepository partnerRepository;
public Iterable<Partner> getPartners(){ public Iterable<Partner> getPartners() {
return partnerRepository.findAll(); return partnerRepository.findAll();
} }
public Optional<Partner> getPartnerById(Long Id){ public Iterable<Partner> getPartners(int from, int size) {
Pageable pageable = PageRequest.of(from, size);
return partnerRepository.findAll(pageable);
}
public Optional<Partner> getPartnerById(Long Id) {
return partnerRepository.findById(Id); return partnerRepository.findById(Id);
} }
} }

13
src/main/resources/data.sql

@ -1,4 +1,13 @@
INSERT INTO partners (company_name, ref, locale, expires) INSERT INTO partners (company_name, ref, locale, expires)
VALUES ('B2boost', 'FYI1', 'en_BE', TIMESTAMP '2022-05-23 12:18:46'), VALUES ('B2boost', 'FYI1', 'en_BE', TIMESTAMP '2022-05-23 12:18:46'),
('Spotify', 'FYI2', 'en_GB', TIMESTAMP '2022-05-23 12:18:46'), ('Proximus', 'FYI2', 'en_BE', TIMESTAMP '2022-05-23 12:18:46'),
('AMSOM-Habitat', 'FYI3', 'fr_FR', TIMESTAMP '2022-05-23 12:18:46'); ('KBC Bank', 'FYI3', 'en_BE', TIMESTAMP '2022-05-23 12:18:46'),
('AB InBev', 'FYI4', 'en_BE', TIMESTAMP '2022-05-23 12:18:46'),
('Spotify', 'FYI5', 'sv_SE', TIMESTAMP '2022-05-23 12:18:46'),
('AMSOM-Habitat', 'FYI6', 'fr_FR', TIMESTAMP '2022-05-23 12:18:46'),
('Microsoft', 'FYI7', 'en_US', TIMESTAMP '2022-05-23 12:18:46'),
('Sony', 'FYI8', 'ja_JP', TIMESTAMP '2022-05-23 12:18:46'),
('Intel', 'FYI9', 'en_US', TIMESTAMP '2022-05-23 12:18:46'),
('Cisco', 'FYI10', 'en_US', TIMESTAMP '2022-05-23 12:18:46'),
('Dell', 'FYI11', 'en_US', TIMESTAMP '2022-05-23 12:18:46'),
('Canonical', 'FYI12', 'en_US', TIMESTAMP '2022-05-23 12:18:46');

Loading…
Cancel
Save