From 839d9c597171edf6e7ac109d71ba9e88f990bf44 Mon Sep 17 00:00:00 2001 From: Hao Zhang Date: Tue, 22 Oct 2024 13:56:15 +0800 Subject: [PATCH] add Visit --- .../samples/petclinic/agent/chat/Agent.java | 2 +- .../petclinic/agent/chat/VisitTools.java | 54 +++++++++++++++++++ .../samples/petclinic/agent/model/Visit.java | 50 +++++++++++++++++ .../petclinic/agent/service/PetService.java | 4 ++ .../petclinic/agent/service/VisitService.java | 22 ++++++++ .../main/resources/petclinic-terms-of-use.txt | 7 ++- .../customers/model/PetRepository.java | 3 ++ .../petclinic/customers/web/PetResource.java | 6 +++ 8 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/chat/VisitTools.java create mode 100644 src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/model/Visit.java create mode 100644 src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/service/VisitService.java diff --git a/src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/chat/Agent.java b/src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/chat/Agent.java index 0f1cdab..57b69eb 100644 --- a/src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/chat/Agent.java +++ b/src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/chat/Agent.java @@ -72,7 +72,7 @@ public String chat(String userMessage, String username) { .advisors(advisorSpecConsumer) .system(systemPromptTemplate.render(systemParameters)) .user(userMessage) - .functions("queryOwners", "addOwner", "updateOwner", "queryVets", "addPet") + .functions("queryOwners", "addOwner", "updateOwner", "queryVets", "addPet", "addVisit") .call() .content(); } diff --git a/src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/chat/VisitTools.java b/src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/chat/VisitTools.java new file mode 100644 index 0000000..6d1093f --- /dev/null +++ b/src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/chat/VisitTools.java @@ -0,0 +1,54 @@ +package org.springframework.samples.petclinic.agent.chat; + +import com.fasterxml.jackson.annotation.JsonClassDescription; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyDescription; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Description; +import org.springframework.samples.petclinic.agent.model.Pet; +import org.springframework.samples.petclinic.agent.model.Visit; +import org.springframework.samples.petclinic.agent.service.PetService; +import org.springframework.samples.petclinic.agent.service.VisitService; + +import java.util.Date; +import java.util.function.Function; + +@Configuration +public class VisitTools { + + private final VisitService visitService; + + private final PetService petService; + + public VisitTools(VisitService visitService, PetService petService) { + this.visitService = visitService; + this.petService = petService; + } + + @Bean + @Description("Add a visit for a pet by providing the pet name, visit date, and description") + public Function addVisit() { + return visitCreateRequest -> { + Pet pet = petService.findPetByName(visitCreateRequest.petName); + + Visit visit = new Visit(); + visit.setDate(visitCreateRequest.visitDate); + visit.setDescription(visitCreateRequest.description); + visitService.addVisit(pet.getId(), visit); + return visit; + }; + } + + @JsonInclude(JsonInclude.Include.NON_NULL) + @JsonClassDescription("Visit Create Request.") + public record VisitCreateRequest( + @JsonProperty(required = true, + value = "petName") @JsonPropertyDescription("The Pet Name") String petName, + @JsonProperty(required = true, + value = "visitDate") @JsonPropertyDescription("The Visit Date") Date visitDate, + @JsonProperty(required = true, + value = "description") @JsonPropertyDescription("The Visit Description") String description) { + } +} diff --git a/src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/model/Visit.java b/src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/model/Visit.java new file mode 100644 index 0000000..edb7a83 --- /dev/null +++ b/src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/model/Visit.java @@ -0,0 +1,50 @@ +/* + * Copyright 2002-2021 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 + * + * http://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.agent.model; + +import com.fasterxml.jackson.annotation.JsonFormat; +import jakarta.persistence.*; +import jakarta.validation.constraints.Size; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Date; + +/** + * Simple JavaBean domain object representing a visit. + * + * @author Ken Krebs + * @author Maciej Szarlinski + * @author Ramazan Sakin + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class Visit { + + private Integer id; + + @JsonFormat(pattern = "yyyy-MM-dd") + private Date date = new Date(); + + @Size(max = 8192) + private String description; + + private int petId; + +} diff --git a/src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/service/PetService.java b/src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/service/PetService.java index f2024e6..bee8621 100644 --- a/src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/service/PetService.java +++ b/src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/service/PetService.java @@ -21,6 +21,10 @@ public void addPet(int ownerId, PetRequest pet) { restTemplate.postForEntity("http://customers-service/owners/" + ownerId + "/pets", pet, Pet.class); } + public Pet findPetByName(String petName) { + return restTemplate.getForObject("http://customers-service/owners/*/pets?petName=" + petName, Pet.class); + } + public PetType findPetTypeByName(String typeName) { return restTemplate.getForObject("http://customers-service/petType?typeName=" + typeName, PetType.class); } diff --git a/src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/service/VisitService.java b/src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/service/VisitService.java new file mode 100644 index 0000000..a447b1e --- /dev/null +++ b/src/spring-petclinic-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/service/VisitService.java @@ -0,0 +1,22 @@ +package org.springframework.samples.petclinic.agent.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.samples.petclinic.agent.model.Visit; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class VisitService { + + private final RestTemplate restTemplate; + + @Autowired + public VisitService(RestTemplate restTemplate) { + this.restTemplate = restTemplate; + } + + public void addVisit(int petId, Visit visit) { + restTemplate.postForEntity("http://visits-service/owners/*/pets/" + petId + "/visits", visit, Visit.class); + } + +} diff --git a/src/spring-petclinic-chat-agent/src/main/resources/petclinic-terms-of-use.txt b/src/spring-petclinic-chat-agent/src/main/resources/petclinic-terms-of-use.txt index 6003674..e5a80a9 100644 --- a/src/spring-petclinic-chat-agent/src/main/resources/petclinic-terms-of-use.txt +++ b/src/spring-petclinic-chat-agent/src/main/resources/petclinic-terms-of-use.txt @@ -5,8 +5,11 @@ Pet clinic Terms of Use 2. Owners 2.1 Owner is the petclinic customer 2.2 Query all the owners information, the owner information include the owner id, first name, last name, city, telephone, address and their pets -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. +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. +2.4 Can update an owner by providing first name. 3. Pets -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 +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. +4. Visits +4.1 Can add a visit for a pet by providing pet name, visit date, and description. diff --git a/src/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/PetRepository.java b/src/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/PetRepository.java index 227b274..ccde19f 100644 --- a/src/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/PetRepository.java +++ b/src/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/model/PetRepository.java @@ -46,5 +46,8 @@ public interface PetRepository extends JpaRepository { @Query("FROM PetType ptype WHERE LOWER(ptype.name) = LOWER(:typeName)") Optional findPetTypeByName(@Param("typeName") String typeName); + + @Query("FROM Pet pet WHERE LOWER(pet.name) = LOWER(:petName)") + Optional findPetByName(@Param("petName") String petName); } diff --git a/src/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/PetResource.java b/src/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/PetResource.java index 1e4dac9..288e9c7 100644 --- a/src/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/PetResource.java +++ b/src/spring-petclinic-customers-service/src/main/java/org/springframework/samples/petclinic/customers/web/PetResource.java @@ -102,4 +102,10 @@ private Pet findPetById(int petId) { return pet.get(); } + @GetMapping("owners/*/pets") + public Pet findPetByName(@RequestParam("petName") String petName) { + Optional pet = petRepository.findPetByName(petName); + return pet.orElse(null); + } + }