diff --git a/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java b/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java index dbf68d02972..d2dfb437f54 100644 --- a/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java @@ -1,58 +1,69 @@ -/* - * Copyright 2012-2025 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ package org.springframework.samples.petclinic.vet; -import org.springframework.cache.annotation.Cacheable; -import org.springframework.dao.DataAccessException; +import java.util.List; + import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; -import org.springframework.data.repository.Repository; -import org.springframework.transaction.annotation.Transactional; - -import java.util.Collection; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; /** - * Repository class for Vet domain objects All method names are compliant - * with Spring Data naming conventions so this interface can easily be extended for Spring - * Data. See: - * https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation - * - * @author Ken Krebs * @author Juergen Hoeller - * @author Sam Brannen - * @author Michael Isvy + * @author Mark Fisher + * @author Ken Krebs + * @author Arjen Poutsma */ -public interface VetRepository extends Repository { - - /** - * Retrieve all Vets from the data store. - * @return a Collection of Vets - */ - @Transactional(readOnly = true) - @Cacheable("vets") - Collection findAll() throws DataAccessException; - - /** - * Retrieve all Vets from data store in Pages - * @param pageable - * @return - * @throws DataAccessException - */ - @Transactional(readOnly = true) - @Cacheable("vets") - Page findAll(Pageable pageable) throws DataAccessException; - -} +@Controller +class VetController { + + private static final int PAGE_SIZE = 5; + + private final VetRepository vetRepository; + + public VetController(VetRepository vetRepository) { + this.vetRepository = vetRepository; + } + + @GetMapping("/vets.html") + public String showVetList(@RequestParam(defaultValue = "1") int page, Model model) { + int currentPage = Math.max(page, 1); + + // Here we are returning an object of type 'Vets' rather than a collection of Vet + // objects so it is simpler for Object-Xml mapping + Vets vets = new Vets(); + Page paginated = findPaginated(currentPage); + vets.getVetList().addAll(paginated.toList()); + + return addPaginationModel(currentPage, paginated, model); + } + + private String addPaginationModel(int page, Page paginated, Model model) { + List listVets = paginated.getContent(); + model.addAttribute("currentPage", page); + model.addAttribute("totalPages", paginated.getTotalPages()); + model.addAttribute("totalItems", paginated.getTotalElements()); + model.addAttribute("listVets", listVets); + model.addAttribute("hasNext", paginated.hasNext()); + model.addAttribute("hasPrevious", paginated.hasPrevious()); + return "vets/vetList"; + } + + private Page findPaginated(int page) { + Pageable pageable = PageRequest.of(page - 1, PAGE_SIZE); + return vetRepository.findAll(pageable); + } + + @GetMapping({ "/vets" }) + public @ResponseBody Vets showResourcesVetList() { + // Here we are returning an object of type 'Vets' rather than a collection of Vet + // objects so it is simpler for JSon/Object mapping + Vets vets = new Vets(); + vets.getVetList().addAll(this.vetRepository.findAll()); + return vets; + } + +} \ No newline at end of file diff --git a/src/main/resources/templates/vets/vetList.html b/src/main/resources/templates/vets/vetList.html index e40fd654e47..3e8ba811227 100644 --- a/src/main/resources/templates/vets/vetList.html +++ b/src/main/resources/templates/vets/vetList.html @@ -4,13 +4,13 @@ -

Veterinarians

+

Veterinarian Directory

- - + +
NameSpecialtiesVeterinarian NameAreas of Expertise