我对Retrofit 2和Android / Backend编程相对较新,我想知道如何正确地将我的JSON模式转换为Java学校项目的Java。
所以情况是我在我的应用程序中需要一些“挑战”。 目前的结构有点复杂,我甚至不确定我们是否正常工作,但我会在这里试一试。
这些是我的REST后端的mongoose架构:
挑战:所以这个对象包含所有正常属性和对challengeType aswel的引用,因为我们的挑战是由多态性定义的。
var ChallengeSchema = new Schema({
_id: ObjectId,
name: String,
description: [String],
challengeType:{
type: Schema.ObjectId, ref: 'ChallengeType'
},
state: Number,
difficulty: Number
});
现在我们的challengeTypes被定义为:
var ChallengeTypeSchema = new Schema({
_id: ObjectId
}),
RecipeChallengeSchema = new Schema({
_id: ObjectId,
joined: Number,
dietTypeMaker: {
type: Schema.ObjectId, ref: 'DietTypeMaker'
}
}),
FastfoodChallengeSchema = new Schema({
_id: ObjectId,
joined: Number
}),
ThursdayVeggieDayChallengeSchema = new Schema({
_id: ObjectId,
joined: Number,
dietTypeMaker1: {
type: Schema.ObjectId, ref: 'DietTypeMaker'
},
dietTypeMaker2: {
type: Schema.ObjectId, ref: 'DietTypeMaker'
}
}),
RestaurantChallengeSchema = new Schema({
_id: ObjectId,
joined: Number,
city: {
type: Schema.ObjectId, ref: 'City'
},
restaurantTypeMaker: {
type: Schema.ObjectId, ref: 'RestaurantTypeMaker'
}
});
module.exports = mongoose.model('ChallengeType',ChallengeTypeSchema,'challengeTypes');
module.exports = mongoose.model('RecipeChallenge', RecipeChallengeSchema, 'challengeTypes', ['ChallengeType']);
module.exports = mongoose.model('FastfoodChallenge', FastfoodChallengeSchema, 'challengeTypes', ['RecipeChallenge']);
module.exports = mongoose.model('ThursdayVeggieDayChallenge', ThursdayVeggieDayChallengeSchema, 'challengeTypes', ['RecipeChallenge']);
module.exports = mongoose.model('RestaurantChallenge', RestaurantChallengeSchema, 'challengeTypes', ['ChallengeType']);
这些与DietTypeMaker或RestaurantTypeMaker相关联,根据活跃的挑战确定我们的食谱或餐馆。
var RestaurantTypeMakerSchema = new Schema({
_id: ObjectId,
eatingPlace: {
restaurants: [
{
type: Schema.ObjectId, ref: 'Restaurant'
}
]
},
gastronomic: {
restaurants: [
{
type: Schema.ObjectId, ref: 'Restaurant'
}
]
},
snackbar: {
restaurants: [
{
type: Schema.ObjectId, ref: 'Restaurant'
}
]
},
catering: {
restaurants: [
{
type: Schema.ObjectId, ref: 'Restaurant'
}
]
}
});
和
var DietTypeMakerSchema = new Schema({
_id: ObjectId,
vegetarian: {
recipe: {
type: Schema.ObjectId, ref: 'Recipe'
}
},
glutenFree: {
recipe: {
type: Schema.ObjectId, ref: 'Recipe'
}
},
sugarFree: {
recipe: {
type: Schema.ObjectId, ref: 'Recipe'
}
},
childFriendly: {
recipe: {
type: Schema.ObjectId, ref: 'Recipe'
}
}
});
每个都指食谱或餐馆:
var RecipeSchema = new Schema({
_id: ObjectId,
title: String,
difficultyRate: Number,
ingredients: [
{
name: String,
qty: String
}
],
description: [String],
extraInfo: {
preparationTime: Number,
cookDifficulty: Number,
recipeType: Number,
dietTypes: [Number],
isFastFood: Boolean
},
image: {
data: Buffer, contentType: String
}
});
和
var RestaurantSchema = new Schema({
_id: ObjectId,
name: String,
streetName: String,
houseNumber: Number,
city: {
type: Schema.ObjectId, ref: 'City'
},
restaurantTypes: [Number],
preferences: [Number],
websiteLink: String,
emailaddress: String,
description: [String]
});
这一切都需要转换为我们的应用程序的Java代码。现在我知道如何制作pojos,我已经为食谱和餐厅制作了它们。我可以使用Retrofit 2从我的后端正确地获取它们。现在我坚持的是我们对所有模式引用的复杂结构...
我们是在制造复杂的东西还是甚至可能...... ???