spring-petclinic / spring-petclinic-rest

REST version of the Spring Petclinic sample application
Apache License 2.0
472 stars 874 forks source link

Refactoring aggregates #125

Open arey opened 11 months ago

arey commented 11 months ago

This issue is related to the conversation from the pull request https://github.com/spring-petclinic/spring-petclinic-rest/pull/123 and the 2 issues https://github.com/spring-petclinic/spring-petclinic-rest/issues/122 and https://github.com/spring-petclinic/spring-petclinic-rest/issues/103

To fix the issues without changing the API specification, we temporary had the operations findSpecialtiesByName and findPetTypeByName.

Vet, Specialty (and maybe PetType) are different aggregates and should probably be linked by their ids for writing operations. A better solution should be to change the OpenAPI interface (and the Angular client). We could imagine that for reading operation, in addition to their id, the name property of aggregates is returned to consumers.

dnokov commented 10 months ago

Issue with DTO Usage in Different Contexts


I think at it's core the problem is caused, because the same DTOs are being used in different contexts and both contexts have conflicting requirements.What I've written is regaring PetType, but the same applies to Specialty

PS: Writing this as I don't want to jump straight to a solution, maybe someone will have a better idea.

PetType Creation Context:

Creating a Pet/Modifying a PetType Context:

image

(Taken from PetTypeDTO generated class, with id readOnly: true and not required)

Possible approach:

I see a few, but this one makes the most sense as it will have a consistent API spec and will not require any hacky workarounds.

arey commented 10 months ago

Hi @dnokov Thank you very much for your analysis and your suggestion. I agree with your approach. I prefer to reuse the PetFieldsDTO spec. I've done the exercice on the PR https://github.com/spring-petclinic/spring-petclinic-rest/issues/125 for the PetType

To add a pet to an owner, we could now use the pet id. But we still have to post a pet name even if we don't use this value.

POST /petclinic/api/owners/5/pets
{
  "name": "Leo",
  "birthDate": "2023-12-31",
  "type": {
    "name": "cat",
    "id": 6
  }
}