Currently, my setup involves an Angular 12 frontend and a Spring REST backend for a Single Page Application.
I'm facing a challenge in creating entities in Angular when the fields of an entity vary for each CRUD operation. For instance, let's consider a User object with different fields required for each request type:
export interface UserGet {
id: string;
firstName: string;
lastName: string;
hobbies: Hobby[];
}
export interface UserPost {
firstName: string;
lastName: string;
hobbies: number[];
}
export interface UserPut {
id: string;
firstName: string;
lastName: string;
hobbies: number[];
}
export interface Hobby {
id: string;
hobby: string;
}
In this scenario, the ID may be optional depending on the request, and the field type of 'hobby' can fluctuate between object and number.
Should I manage three separate objects in Angular as shown above, or would it be more practical to create a single "common" object like the following?
Data structure for POST-Request:
export interface UserCrud {
id?: string;
firstName: string;
lastName: string;
hobbies: Hobby[] | number;
}
export interface Hobby {
id: string;
hobby: string;
}
Which approach is better for long-term maintenance within the context of Angular 12? Any insights will be greatly appreciated.