Skip to content

Commit

Permalink
add Visit
Browse files Browse the repository at this point in the history
  • Loading branch information
Hao Zhang committed Oct 22, 2024
1 parent 3f63cbb commit 839d9c5
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<VisitCreateRequest, Visit> 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) {
}
}
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,8 @@ public interface PetRepository extends JpaRepository<Pet, Integer> {

@Query("FROM PetType ptype WHERE LOWER(ptype.name) = LOWER(:typeName)")
Optional<PetType> findPetTypeByName(@Param("typeName") String typeName);

@Query("FROM Pet pet WHERE LOWER(pet.name) = LOWER(:petName)")
Optional<Pet> findPetByName(@Param("petName") String petName);
}

Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,10 @@ private Pet findPetById(int petId) {
return pet.get();
}

@GetMapping("owners/*/pets")
public Pet findPetByName(@RequestParam("petName") String petName) {
Optional<Pet> pet = petRepository.findPetByName(petName);
return pet.orElse(null);
}

}

0 comments on commit 839d9c5

Please sign in to comment.