Skip to content

Commit 839d9c5

Browse files
author
Hao Zhang
committed
add Visit
1 parent 3f63cbb commit 839d9c5

File tree

8 files changed

+145
-3
lines changed

8 files changed

+145
-3
lines changed

src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/chat/Agent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public String chat(String userMessage, String username) {
7272
.advisors(advisorSpecConsumer)
7373
.system(systemPromptTemplate.render(systemParameters))
7474
.user(userMessage)
75-
.functions("queryOwners", "addOwner", "updateOwner", "queryVets", "addPet")
75+
.functions("queryOwners", "addOwner", "updateOwner", "queryVets", "addPet", "addVisit")
7676
.call()
7777
.content();
7878
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.springframework.samples.petclinic.agent.chat;
2+
3+
import com.fasterxml.jackson.annotation.JsonClassDescription;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.context.annotation.Description;
10+
import org.springframework.samples.petclinic.agent.model.Pet;
11+
import org.springframework.samples.petclinic.agent.model.Visit;
12+
import org.springframework.samples.petclinic.agent.service.PetService;
13+
import org.springframework.samples.petclinic.agent.service.VisitService;
14+
15+
import java.util.Date;
16+
import java.util.function.Function;
17+
18+
@Configuration
19+
public class VisitTools {
20+
21+
private final VisitService visitService;
22+
23+
private final PetService petService;
24+
25+
public VisitTools(VisitService visitService, PetService petService) {
26+
this.visitService = visitService;
27+
this.petService = petService;
28+
}
29+
30+
@Bean
31+
@Description("Add a visit for a pet by providing the pet name, visit date, and description")
32+
public Function<VisitCreateRequest, Visit> addVisit() {
33+
return visitCreateRequest -> {
34+
Pet pet = petService.findPetByName(visitCreateRequest.petName);
35+
36+
Visit visit = new Visit();
37+
visit.setDate(visitCreateRequest.visitDate);
38+
visit.setDescription(visitCreateRequest.description);
39+
visitService.addVisit(pet.getId(), visit);
40+
return visit;
41+
};
42+
}
43+
44+
@JsonInclude(JsonInclude.Include.NON_NULL)
45+
@JsonClassDescription("Visit Create Request.")
46+
public record VisitCreateRequest(
47+
@JsonProperty(required = true,
48+
value = "petName") @JsonPropertyDescription("The Pet Name") String petName,
49+
@JsonProperty(required = true,
50+
value = "visitDate") @JsonPropertyDescription("The Visit Date") Date visitDate,
51+
@JsonProperty(required = true,
52+
value = "description") @JsonPropertyDescription("The Visit Description") String description) {
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.samples.petclinic.agent.model;
17+
18+
import com.fasterxml.jackson.annotation.JsonFormat;
19+
import jakarta.persistence.*;
20+
import jakarta.validation.constraints.Size;
21+
import lombok.AllArgsConstructor;
22+
import lombok.Builder;
23+
import lombok.Data;
24+
import lombok.NoArgsConstructor;
25+
26+
import java.util.Date;
27+
28+
/**
29+
* Simple JavaBean domain object representing a visit.
30+
*
31+
* @author Ken Krebs
32+
* @author Maciej Szarlinski
33+
* @author Ramazan Sakin
34+
*/
35+
@Data
36+
@NoArgsConstructor
37+
@AllArgsConstructor
38+
public class Visit {
39+
40+
private Integer id;
41+
42+
@JsonFormat(pattern = "yyyy-MM-dd")
43+
private Date date = new Date();
44+
45+
@Size(max = 8192)
46+
private String description;
47+
48+
private int petId;
49+
50+
}

src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/service/PetService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public void addPet(int ownerId, PetRequest pet) {
2121
restTemplate.postForEntity("http://customers-service/owners/" + ownerId + "/pets", pet, Pet.class);
2222
}
2323

24+
public Pet findPetByName(String petName) {
25+
return restTemplate.getForObject("http://customers-service/owners/*/pets?petName=" + petName, Pet.class);
26+
}
27+
2428
public PetType findPetTypeByName(String typeName) {
2529
return restTemplate.getForObject("http://customers-service/petType?typeName=" + typeName, PetType.class);
2630
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.springframework.samples.petclinic.agent.service;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.samples.petclinic.agent.model.Visit;
6+
import org.springframework.web.client.RestTemplate;
7+
8+
@Configuration
9+
public class VisitService {
10+
11+
private final RestTemplate restTemplate;
12+
13+
@Autowired
14+
public VisitService(RestTemplate restTemplate) {
15+
this.restTemplate = restTemplate;
16+
}
17+
18+
public void addVisit(int petId, Visit visit) {
19+
restTemplate.postForEntity("http://visits-service/owners/*/pets/" + petId + "/visits", visit, Visit.class);
20+
}
21+
22+
}

src/spring-petclinic-chat-agent/src/main/resources/petclinic-terms-of-use.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ Pet clinic Terms of Use
55
2. Owners
66
2.1 Owner is the petclinic customer
77
2.2 Query all the owners information, the owner information include the owner id, first name, last name, city, telephone, address and their pets
8-
2.3 Can add an owner by providing first name, last name, city, telephone and address. The information of city, telephone and address must provided by customer and please don't use the default value.
8+
2.3 Can add an owner by providing first name, last name, city, telephone and address. The information of city, telephone and address must be provided by customer and please don't use the default value.
9+
2.4 Can update an owner by providing first name.
910

1011
3. Pets
11-
3.1 Can add a pet by input the owner id, pet name, pet type, the pet type should be one cat, dog, lizard,snake, bird and hamster
12+
3.1 Can add a pet by input the owner first name, pet name, pet type, the pet type should be one cat, dog, lizard, snake, bird and hamster.
1213

14+
4. Visits
15+
4.1 Can add a visit for a pet by providing pet name, visit date, and description.

src/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/PetRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,8 @@ public interface PetRepository extends JpaRepository<Pet, Integer> {
4646

4747
@Query("FROM PetType ptype WHERE LOWER(ptype.name) = LOWER(:typeName)")
4848
Optional<PetType> findPetTypeByName(@Param("typeName") String typeName);
49+
50+
@Query("FROM Pet pet WHERE LOWER(pet.name) = LOWER(:petName)")
51+
Optional<Pet> findPetByName(@Param("petName") String petName);
4952
}
5053

src/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/PetResource.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,10 @@ private Pet findPetById(int petId) {
102102
return pet.get();
103103
}
104104

105+
@GetMapping("owners/*/pets")
106+
public Pet findPetByName(@RequestParam("petName") String petName) {
107+
Optional<Pet> pet = petRepository.findPetByName(petName);
108+
return pet.orElse(null);
109+
}
110+
105111
}

0 commit comments

Comments
 (0)