lagoshny / ngx-hal-client

Spring HAL client for Angular
21 stars 15 forks source link

getRelation for embedded object in Entity #10

Closed tmichalec closed 4 years ago

tmichalec commented 4 years ago

Hello,

is it possible to use getRelation for embedded object in entity?

SERVER:

@Entity
public class Cart {

    @ElementCollection
    private List<CartItem> items = new ArrayList<>();

}

@Embeddable
public class CartItem {

    @ManyToOne
    private Product product;
}

CLIENT:

export class Cart extends Resource {
    items: CartItem[];
}
export class Product extends Resource {
  name: string;
}
export class CartItem { //it is not Resource, just embedded object
    product: Product;
}

GET /cart/1 looks like

{
  "items": [
    {
      "_links": {
        "product": {
          "href": "http://localhost:8080/api/product/1"
        }
      }
    },
  ]
}
cartService.get(1).subscribe(cart => {
    cart.items[0].getRelation(Product, 'product')
        .subscribe(product => {
            console.log(product;)
        })
    })
});

show ERROR TypeError: cart.items[0].getRelation is not a function

Or is there some workaround to do this?

lagoshny commented 4 years ago

Hi @tmichalec ,

Yes, you can't do postRelation for embeddable entity. I'll see what I can do to make it work.

lagoshny commented 4 years ago

@tmichalec , I've added support for embeddable entities in 1.0.18 version that already available in npm.

After update hal-client you need only extend CartItem from EmbeddedResource that adds ability to do links operations like getRealtion on this model class for more information see here and here:

export class Cart extends Resource {
    items: CartItem[];
}
export class Product extends Resource {
  name: string;
}
export class CartItem extends EmbeddedResource {
    product: Product;
}