【问题标题】:How to store data owned by User?如何存储用户拥有的数据?
【发布时间】:2015-03-24 22:19:50
【问题描述】:

我正在学习 Google App Engine + Google Cloud Endpoints + Objectify,我正在尝试了解如何创建 REST API,让每个特定用户都可以将他的数据保存在云中。

我目前的难题是如何存储用户拥有的实体 (com.google.appengine.api.users.User)?

到目前为止,我有端点:

@ApiMethod(name = "saveBook")
public void saveBook(Book book, User user) throws OAuthRequestException, IOException {
    if (user == null) {
        throw new OAuthRequestException("User is not authorized");
    }

    ofy().save()
            .entity(BookRecord.fromBook(user, book))
            .now();
}

实体(在这种情况下,我们假设User 写了这本书):

@Entity
public class BookRecord {

    @Parent
    private Key<User> user;
    @Id
    private String id;
    @Index
    private String name;

    public static BookRecord fromBook(User user, Book book) {
        return new BookRecord(
                Key.create(user),
                book.getId(),
                book.getName()
        );
    }

    public BookRecord() {
    }

    private BookRecord(Key<User> user, String id, String name, String author) {
        this.user = user;
        this.id = id;
        this.name = name;
        this.author = author;
    }

}

问题来自User 不是Entity,所以我不能真正使用这个解决方案并直接使用User 作为@Parent。解决此问题并存储用户拥有的数据的一般解决方案是什么?

【问题讨论】:

    标签: google-app-engine objectify


    【解决方案1】:

    创建您自己的 User 实体,这是您存储应用的自定义信息/首选项的地方(正如我建议的 here)。

    您将使用getUserId() 将其与 Google 用户联系起来,然后您就可以根据需要随意使用它了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-22
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 2016-07-17
      相关资源
      最近更新 更多