Skip to content

Commit

Permalink
addPet function
Browse files Browse the repository at this point in the history
  • Loading branch information
Hao Zhang committed Oct 22, 2024
1 parent d4d2f44 commit 3f63cbb
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 2 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")
.functions("queryOwners", "addOwner", "updateOwner", "queryVets", "addPet")
.call()
.content();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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.Owner;
import org.springframework.samples.petclinic.agent.model.Pet;
import org.springframework.samples.petclinic.agent.model.PetRequest;
import org.springframework.samples.petclinic.agent.service.OwnerService;
import org.springframework.samples.petclinic.agent.service.PetService;

import java.util.Date;
import java.util.function.BiFunction;
import java.util.function.Function;

@Configuration
public class PetTools {

private final OwnerService ownerService;

private final PetService petService;

public PetTools(OwnerService ownerService, PetService petService) {
this.ownerService = ownerService;
this.petService = petService;
}

@Bean
@Description("Add a new pet for an owner by providing the owner's first name, pet name, pet's birth date, and pet type")
public Function<PetCreateRequest, PetRequest> addPet() {
return petCreateRequest -> {
var ownerOptional = ownerService.findByFirstName(petCreateRequest.firstName).stream().findFirst();
if (ownerOptional.isEmpty()) {
return null;
}
Owner owner = ownerOptional.get();

PetRequest pet = new PetRequest();
pet.setName(petCreateRequest.name);
pet.setBirthDate(petCreateRequest.birthDate);
pet.setTypeId(
petService.findPetTypeByName(petCreateRequest.typeName).getId()
);
petService.addPet(owner.getId(), pet);
return pet;
};
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonClassDescription("Pet Create Request.")
public record PetCreateRequest(
@JsonProperty(required = true,
value = "firstName") @JsonPropertyDescription("The Owner first name") String firstName,
@JsonProperty(required = false,
value = "petId") @JsonPropertyDescription("The Pet Id") Integer petId,
@JsonProperty(required = true,
value = "name") @JsonPropertyDescription("The Pet Name") String name,
@JsonProperty(required = true,
value = "birthDate") @JsonPropertyDescription("The Pet Birth Date") Date birthDate,
@JsonProperty(required = true,
value = "petType") @JsonPropertyDescription("The Pet Type") String typeName) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.validation.constraints.Size;
import lombok.Data;

import java.util.Date;

/**
* @author [email protected] on 2016-12-05.
*/
@Data
public class PetRequest {
private int id;

@JsonFormat(pattern = "yyyy-MM-dd")
private Date birthDate;

@Size(min = 1)
private String name;

private int typeId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.springframework.samples.petclinic.agent.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.agent.model.Pet;
import org.springframework.samples.petclinic.agent.model.PetRequest;
import org.springframework.samples.petclinic.agent.model.PetType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class PetService {

private final RestTemplate restTemplate;

@Autowired
public PetService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

public void addPet(int ownerId, PetRequest pet) {
restTemplate.postForEntity("http://customers-service/owners/" + ownerId + "/pets", pet, Pet.class);
}

public PetType findPetTypeByName(String typeName) {
return restTemplate.getForObject("http://customers-service/petType?typeName=" + typeName, PetType.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public interface PetRepository extends JpaRepository<Pet, Integer> {
@Query("FROM PetType ptype WHERE ptype.id = :typeId")
Optional<PetType> findPetTypeById(@Param("typeId") int typeId);


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

Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public List<PetType> getPetTypes() {
return petRepository.findPetTypes();
}

@GetMapping("/petType")
public PetType findPetTypeByName(@RequestParam("typeName") String typeName) {
Optional<PetType> petType = petRepository.findPetTypeByName(typeName);
return petType.orElse(null);
}

@PostMapping("/owners/{ownerId}/pets")
@ResponseStatus(HttpStatus.CREATED)
public Pet processCreationForm(
Expand Down

0 comments on commit 3f63cbb

Please sign in to comment.