kamikat / moshi-jsonapi

JSON API v1.0 Specification in Moshi.
MIT License
156 stars 34 forks source link

Resource.getDocument() #76

Closed dreamsbond closed 6 years ago

dreamsbond commented 6 years ago
@JsonApi(type = "articles")
public class Article extends Resource {
    private String title;
    private HasOne<Person> author;
    private HasMany<Comment> comments;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Person getAuthor() {
        return author.get(getDocument());
    }

    public List<Comment> getComments() {
        return comments.get(getDocument());
    }
}

I have idea where to find the getDocument()

the getDocument() was a unresolved method.

kamikat commented 6 years ago

Thanks for issuing the problem. The getContext is replaced by getDocument for clarity in 3.3.0. I've checked the CI to find it fails to build 3.3.0 due to some changes to the build environment.

I've requested another build for 3.3.1. It'll soon be available at bintray :-)

kamikat commented 6 years ago

3.3.1 is available now 🎉

dreamsbond commented 6 years ago

and how to get the relationship attributes as like as getting resource attributes?

        mPlaceApi = BaseApi.getClient().create(PlacesApi.class);
        Call<List<Place>> getPlaces = mPlacesApi.getPlaces();
        getPlaces.enqueue(new Callback<List<Place>>() {
            @Override
            public void onResponse(Call<List<Place>> call, Response<List<Place>> response) {
                mPlaces = response.body();

                for (int i = 0; i < mPlaces.size(); i++) {
                    System.out.println(mPlaces.get(i).getId();
                    System.out.println(mPlaces.get(i).getName();

                   // how to get attributes from the relationship?
                    System.out.println(mPlaces.get(i).getPlaceType().getAttributeName());
                }
            }

            @Override
            public void onFailure(Call<List<Place>> call, Throwable t) {

            }
        });

thanks

kamikat commented 6 years ago

How is the Place implemented?

dreamsbond commented 6 years ago

This is the Place Model


import com.squareup.moshi.Json;

import moe.banana.jsonapi2.HasOne;
import moe.banana.jsonapi2.JsonApi;
import moe.banana.jsonapi2.Resource;

@JsonApi(type = "places")
public class Place extends Resource {
    private String name;
    @Json(name = "created-at")
    private String createdAt;
    @Json(name = "updated-at")
    private String updatedAt;
    @Json(name = "deleted-at")
    private String deletedAt;

    @Json(name = "type")
    public HasOne<PlaceType> placeType;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(String createdAt) {
        this.createdAt = createdAt;
    }

    public String getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(String updatedAt) {
        this.updatedAt = updatedAt;
    }

    public String getDeletedAt() {
        return deletedAt;
    }

    public void setDeletedAt(String deletedAt) {
        this.deletedAt = deletedAt;
    }

    public HasOne<PlaceType> getPlaceType() {
        return placeType;
    }

    public void setPlaceType(HasOne<PlaceType> placeType) {
        this.placeType = placeType;
    }
}
dreamsbond commented 6 years ago

and this is PlaceType


import com.squareup.moshi.Json;

import moe.banana.jsonapi2.JsonApi;
import moe.banana.jsonapi2.Resource;
import moe.banana.jsonapi2.HasMany;

@JsonApi(type = "place-types")
public class PlaceType extends Resource {
    private String name;
    @Json(name = "created-at")
    private String createdAt;
    @Json(name = "updated-at")
    private String updatedAt;
    @Json(name = "deleted-at")
    private String deletedAt;

    private HasMany<Place> places;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(String createdAt) {
        this.createdAt = createdAt;
    }

    public String getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(String updatedAt) {
        this.updatedAt = updatedAt;
    }

    public String getDeletedAt() {
        return deletedAt;
    }

    public void setDeletedAt(String deletedAt) {
        this.deletedAt = deletedAt;
    }

    public HasMany<Place> getPlaces() {
        return places;
    }

    public void setPlaces(HasMany<Place> places) {
        this.places = places;
    }
}
kamikat commented 6 years ago

Here's the example https://github.com/kamikat/moshi-jsonapi#relationships.