-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Hao Zhang
committed
Oct 22, 2024
1 parent
d4d2f44
commit 3f63cbb
Showing
6 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...c-chat-agent/src/main/java/org/springframework/samples/petclinic/agent/chat/PetTools.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...hat-agent/src/main/java/org/springframework/samples/petclinic/agent/model/PetRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
27 changes: 27 additions & 0 deletions
27
...t-agent/src/main/java/org/springframework/samples/petclinic/agent/service/PetService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters