Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import jakarta.persistence.Column;
import jakarta.persistence.MappedSuperclass;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

/**
* Simple JavaBean domain object adds a name property to <code>BaseEntity</code>. Used as
Expand All @@ -32,6 +33,7 @@ public class NamedEntity extends BaseEntity {

@Column(name = "name")
@NotBlank
@Size(min = 3, max = 50)
private String name;

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.List;
import java.util.Optional;

import jakarta.validation.Valid;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand All @@ -32,8 +34,6 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import jakarta.validation.Valid;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
Expand Down Expand Up @@ -169,5 +169,4 @@ public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
mav.addObject(owner);
return mav;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
package org.springframework.samples.petclinic.owner;

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

import jakarta.annotation.Nonnull;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
Expand Down Expand Up @@ -61,5 +61,8 @@ public interface OwnerRepository extends JpaRepository<Owner, Integer> {
* input for id)
*/
Optional<Owner> findById(@Nonnull Integer id);

@Query("SELECT COUNT(o) FROM Owner o")
long countOwners();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.springframework.samples.petclinic.owner;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/owners")
public class OwnerRestController {

private final OwnerService ownerService;

public OwnerRestController(OwnerService ownerService) {
this.ownerService = ownerService;
}

@GetMapping("/count")
public long getOwnerCount() {
return ownerService.getOwnerCount();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.springframework.samples.petclinic.owner;

import org.springframework.stereotype.Service;

@Service
public class OwnerService {

private final OwnerRepository ownerRepository;

public OwnerService(OwnerRepository ownerRepository) {
this.ownerRepository = ownerRepository;
}

public long getOwnerCount() {
return ownerRepository.countOwners();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
import java.util.LinkedHashSet;
import java.util.Set;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.samples.petclinic.model.NamedEntity;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand All @@ -33,6 +30,9 @@
import jakarta.persistence.OrderBy;
import jakarta.persistence.Table;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.samples.petclinic.model.NamedEntity;

/**
* Simple business object representing a pet.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,25 @@

package org.springframework.samples.petclinic.system;

import org.springframework.samples.petclinic.owner.OwnerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
class WelcomeController {

private final OwnerService ownerService;

@GetMapping("/")
public String welcome() {
return "welcome";
}
public WelcomeController(OwnerService ownerService) {
this.ownerService = ownerService;
}

@GetMapping("/")
public String welcome(Model model) {
long ownerCount = ownerService.getOwnerCount();
model.addAttribute("ownerCount", ownerCount);
return "welcome"; // welcome.html を表示
}

}
1 change: 1 addition & 0 deletions src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ visitDate=Visit Date
editOwner=Edit Owner
addNewPet=Add New Pet
petsAndVisits=Pets and Visits
pet.name.size=ペットの名前は3文字以上50文字以内で入力してください
4 changes: 3 additions & 1 deletion src/main/resources/templates/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

<body>

<h2 th:text="#{welcome}">Welcome</h2>
<h2 th:text="#{welcome}">Welcome</h2><p>登録されているオーナー数: <span th:text="${ownerCount}"></span></p>
<div class="row">
<div class="col-md-12">
<img class="img-responsive" src="../static/resources/images/pets.png" th:src="@{/resources/images/pets.png}" />
</div>
</div>



</body>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.springframework.samples.petclinic.owner;

import static org.assertj.core.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

@DataJpaTest
public class OwnerRepositoryTest {

@Autowired
private OwnerRepository ownerRepository;

@Test
void testCountOwners() {
long count = ownerRepository.count();
assertThat(count).isGreaterThan(0); // データがあることを確認
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.springframework.samples.petclinic.owner;

import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;

@WebMvcTest(OwnerRestController.class)
public class OwnerRestControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private OwnerService ownerService;

@Test
void testGetOwnerCountEndpoint() throws Exception {
when(ownerService.getOwnerCount()).thenReturn(10L);

mockMvc.perform(get("/api/owners/count"))
.andExpect(status().isOk())
.andExpect(content().string("10"));

verify(ownerService, times(1)).getOwnerCount();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.springframework.samples.petclinic.service;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.samples.petclinic.owner.OwnerRepository;
import org.springframework.samples.petclinic.owner.OwnerService;

@ExtendWith(MockitoExtension.class)
public class OwnerServiceTest {

@Mock
private OwnerRepository ownerRepository;

@InjectMocks
private OwnerService ownerService;

@Test
void testGetOwnerCount() {
when(ownerRepository.count()).thenReturn(5L);

long count = ownerService.getOwnerCount();

assertThat(count).isEqualTo(5L);
verify(ownerRepository, times(1)).count();
}
}