Angular frontend with REST backend: entity design for CRUD-operations

  angular, rest, spring-rest, typescript

I’m using an Angular 12 frontend, Spring REST backend for a Single Page Application.

What is the best way to create entities in Angular if the fields of an entity are different for every CRUD operation?

Let’s take an User-object for example with different fields for every 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;
}

As you can see, Id can be optional dependant on the request, and the field type of hobby can either be object or number.

Is it better to keep three objects in Angular like above, or should I create one "common" object like this:

Data of POST-Request:

export interface UserCrud {
   id?: string;
   firstName: string;
   lastName: string;
   hobbies: Hobby[] | number;
}

export interface Hobby {
   id: string;
   hobby: string;
}

What’s the better approach on the long run with Angular 12?

Thx in advance

Source: Angular Questions

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.