Skip to content

Commit 3f63cbb

Browse files
author
Hao Zhang
committed
addPet function
1 parent d4d2f44 commit 3f63cbb

File tree

6 files changed

+141
-2
lines changed

6 files changed

+141
-2
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")
75+
.functions("queryOwners", "addOwner", "updateOwner", "queryVets", "addPet")
7676
.call()
7777
.content();
7878
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.Owner;
11+
import org.springframework.samples.petclinic.agent.model.Pet;
12+
import org.springframework.samples.petclinic.agent.model.PetRequest;
13+
import org.springframework.samples.petclinic.agent.service.OwnerService;
14+
import org.springframework.samples.petclinic.agent.service.PetService;
15+
16+
import java.util.Date;
17+
import java.util.function.BiFunction;
18+
import java.util.function.Function;
19+
20+
@Configuration
21+
public class PetTools {
22+
23+
private final OwnerService ownerService;
24+
25+
private final PetService petService;
26+
27+
public PetTools(OwnerService ownerService, PetService petService) {
28+
this.ownerService = ownerService;
29+
this.petService = petService;
30+
}
31+
32+
@Bean
33+
@Description("Add a new pet for an owner by providing the owner's first name, pet name, pet's birth date, and pet type")
34+
public Function<PetCreateRequest, PetRequest> addPet() {
35+
return petCreateRequest -> {
36+
var ownerOptional = ownerService.findByFirstName(petCreateRequest.firstName).stream().findFirst();
37+
if (ownerOptional.isEmpty()) {
38+
return null;
39+
}
40+
Owner owner = ownerOptional.get();
41+
42+
PetRequest pet = new PetRequest();
43+
pet.setName(petCreateRequest.name);
44+
pet.setBirthDate(petCreateRequest.birthDate);
45+
pet.setTypeId(
46+
petService.findPetTypeByName(petCreateRequest.typeName).getId()
47+
);
48+
petService.addPet(owner.getId(), pet);
49+
return pet;
50+
};
51+
}
52+
53+
@JsonInclude(JsonInclude.Include.NON_NULL)
54+
@JsonClassDescription("Pet Create Request.")
55+
public record PetCreateRequest(
56+
@JsonProperty(required = true,
57+
value = "firstName") @JsonPropertyDescription("The Owner first name") String firstName,
58+
@JsonProperty(required = false,
59+
value = "petId") @JsonPropertyDescription("The Pet Id") Integer petId,
60+
@JsonProperty(required = true,
61+
value = "name") @JsonPropertyDescription("The Pet Name") String name,
62+
@JsonProperty(required = true,
63+
value = "birthDate") @JsonPropertyDescription("The Pet Birth Date") Date birthDate,
64+
@JsonProperty(required = true,
65+
value = "petType") @JsonPropertyDescription("The Pet Type") String typeName) {
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.validation.constraints.Size;
20+
import lombok.Data;
21+
22+
import java.util.Date;
23+
24+
/**
25+
* @author [email protected] on 2016-12-05.
26+
*/
27+
@Data
28+
public class PetRequest {
29+
private int id;
30+
31+
@JsonFormat(pattern = "yyyy-MM-dd")
32+
private Date birthDate;
33+
34+
@Size(min = 1)
35+
private String name;
36+
37+
private int typeId;
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.springframework.samples.petclinic.agent.service;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.samples.petclinic.agent.model.Pet;
5+
import org.springframework.samples.petclinic.agent.model.PetRequest;
6+
import org.springframework.samples.petclinic.agent.model.PetType;
7+
import org.springframework.stereotype.Service;
8+
import org.springframework.web.client.RestTemplate;
9+
10+
@Service
11+
public class PetService {
12+
13+
private final RestTemplate restTemplate;
14+
15+
@Autowired
16+
public PetService(RestTemplate restTemplate) {
17+
this.restTemplate = restTemplate;
18+
}
19+
20+
public void addPet(int ownerId, PetRequest pet) {
21+
restTemplate.postForEntity("http://customers-service/owners/" + ownerId + "/pets", pet, Pet.class);
22+
}
23+
24+
public PetType findPetTypeByName(String typeName) {
25+
return restTemplate.getForObject("http://customers-service/petType?typeName=" + typeName, PetType.class);
26+
}
27+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public interface PetRepository extends JpaRepository<Pet, Integer> {
4444
@Query("FROM PetType ptype WHERE ptype.id = :typeId")
4545
Optional<PetType> findPetTypeById(@Param("typeId") int typeId);
4646

47-
47+
@Query("FROM PetType ptype WHERE LOWER(ptype.name) = LOWER(:typeName)")
48+
Optional<PetType> findPetTypeByName(@Param("typeName") String typeName);
4849
}
4950

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
@@ -48,6 +48,12 @@ public List<PetType> getPetTypes() {
4848
return petRepository.findPetTypes();
4949
}
5050

51+
@GetMapping("/petType")
52+
public PetType findPetTypeByName(@RequestParam("typeName") String typeName) {
53+
Optional<PetType> petType = petRepository.findPetTypeByName(typeName);
54+
return petType.orElse(null);
55+
}
56+
5157
@PostMapping("/owners/{ownerId}/pets")
5258
@ResponseStatus(HttpStatus.CREATED)
5359
public Pet processCreationForm(

0 commit comments

Comments
 (0)