0

I am trying to make a facebook clone app . And from the very begining ,I will need to define a user class . And I got stuck here , every users ,supose to have followers list and following list .How do it able to make this model ? Could you please take a look my code ,thank you so much in advance !!

@Entity(tableName="user_table")
@Parcelize
data class User(
    @PrimaryKey(autoGenerate = true)
    val userId : Int=0 ,
    val username :String,
    val email :String,
    val password : String,
    var followers: Array<User>?,
    var followings: Array<User>?,


) : Parcelable

data class UserResponse(val user:User?)

Also ,I attach my node.js model schema :

const mongoose = require('mongoose');

const router = require("express").Router();

const userSchema = new mongoose.Schema({
    username:{
        type:String,
        required : true,
        min:3,
        max:50,
    },

    email:{
        type:String,
        required:true,
        unique:true
    },
    password:{
        type:String,
        required : true,
    },
    profilePicture:{
        type:String,
        default:''
    },
    coverPicture:{
        type : String,
        default:''
    },
    followers:{
        type:Array,
        default :[]
    },
    followings:{
        type:Array,
        default:[]
    },
    isAdmin:{
        type:Boolean,
        default : false
    }

},
{timestamps:true}
);

const User = mongoose.model('User',userSchema);
exports.userSchema = userSchema;
exports.User = User;
6
  • What error do you get? Commented Dec 2, 2021 at 20:51
  • Hi Robin ,it didn't appear error ,just appears something like warning like ,it needs to generate hashcode .And after I follow it ,the warning removed .However ,I am not sure if i have writen in the right format,I post it here for asking opinion from you guys .What do you think ?Any problem my code ? Thank you so much ! Commented Dec 2, 2021 at 20:59
  • 1
    I would use List<User>? rather than Array<User>? because they are easier to use and I would probably remove the ? too because the response defaults to an empty list and not null which is okay in Kotlin so List<User> should just suffice Commented Dec 2, 2021 at 21:04
  • 1
    Probably a good idea would be to separate your network response models from the database entities. Currently the way this is setup, making a change that does not concern the DB will force you to migrate Commented Dec 2, 2021 at 21:08
  • Thank you so much for your suggestion Robin ! Commented Dec 2, 2021 at 22:04

1 Answer 1

1

The first thing you should do is to change

var followers: Array<User>?,
var followings: Array<User>?

To

var followers: Array<Int>?,
var followings: Array<Int>?

Int may be the type of your userId or make it to String By doing this you will not be recursively fetching your whole database and looping for getting followers and following.

And if you are using Room to store your data , add TypeConverters.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.