views:

256

answers:

1

Hi!

How would you model a friend - friendship relationship in Grails? Until now my User class had many followers

class User {
//Searchable plugin
static searchable = true

String userId
String password
boolean enabled = true

// For Spring Security plugin's user registration.
String email
String userRealName
boolean emailShow

Date dateCreated
Profile profile

static hasMany = [
        posts : Post,
        tags : Tag,
        following : User,
        authorities : Role,
        notifications: Notification,
     locations: Location,
     incomingLocations:IncomingLocation,

]
static belongsTo = Role


static constraints = {
    userId(blank: false, size:3..20, unique: true)
    password(blank: false)
    dateCreated()
    profile(nullable: true)
    userRealName(nullable: true, blank: true)
    email(nullable: true, blank: true)
}


static mapping = {
    profile lazy:false
}

}

But I would like to change the following:User for something like friendships:Friendship and create a Friendship class as following:

class Friendship {

static belongsTo= [user:User]
User friend2
boolean areFriends

}

Is this an ideal implementation?

How would you implement the handshake (accept/reject a pending friendship)?

+2  A: 

You might not need to model Friendship directly. You can just have a hasMany relationship that relates the Users as friends. You don't create that relationship until someone accepts a FriendRequest. If they no longer want to be friends, then just remove the relationship between the 2 Users.

class User {
    static hasMany = [friends:User]
}

class FriendRequest {
    User fromUser
    User toUser
}

That way Friendship doesn't have to do 2 things (relate users and track statuses). And friends becomes a natural object relationship which can make some things like optimizing fetching a bit easier.

geofflane