Skip to content
Permalink
Browse files

08-Follower improve data model

  • Loading branch information...
bonfy committed Sep 30, 2018
1 parent a0a50df commit ef4e3aaab2a4ba077edf687e6af7dc2a907df3db
Showing with 98 additions and 28 deletions.
  1. +13 −27 cmd/db_init/main.go
  2. +85 −1 model/user.go
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"log"

"github.com/bonfy/go-mega-code/model"
@@ -14,32 +13,19 @@ func main() {
defer db.Close()
model.SetDB(db)

db.DropTableIfExists(model.User{}, model.Post{})
db.DropTableIfExists(model.User{}, model.Post{}, "follower")
db.CreateTable(model.User{}, model.Post{})

users := []model.User{
{
Username: "bonfy",
PasswordHash: model.GeneratePasswordHash("abc123"),
Email: "i@bonfy.im",
Avatar: fmt.Sprintf("https://www.gravatar.com/avatar/%s?d=identicon", model.Md5("i@bonfy.im")),
Posts: []model.Post{
{Body: "Beautiful day in Portland!"},
},
},
{
Username: "rene",
PasswordHash: model.GeneratePasswordHash("abc123"),
Email: "rene@test.com",
Avatar: fmt.Sprintf("https://www.gravatar.com/avatar/%s?d=identicon", model.Md5("rene@test.com")),
Posts: []model.Post{
{Body: "The Avengers movie was so cool!"},
{Body: "Sun shine is beautiful"},
},
},
}

for _, u := range users {
db.Debug().Create(&u)
}
model.AddUser("bonfy", "abc123", "i@bonfy.im")
model.AddUser("rene", "abc123", "rene@test.com")

u1, _ := model.GetUserByUsername("bonfy")
u1.CreatePost("Beautiful day in Portland!")
model.UpdateAboutMe(u1.Username, `I'm the author of Go-Mega Tutorial you are reading now!`)

u2, _ := model.GetUserByUsername("rene")
u2.CreatePost("The Avengers movie was so cool!")
u2.CreatePost("Sun shine is beautiful")

u1.Follow(u2.Username)
}
@@ -2,6 +2,7 @@ package model

import (
"fmt"
"log"
"time"
)

@@ -33,6 +34,86 @@ func (u *User) CheckPassword(password string) bool {
return GeneratePasswordHash(password) == u.PasswordHash
}

// Follow func
// follow someone usr_id other.id follow_id u.id
func (u *User) Follow(username string) error {
other, err := GetUserByUsername(username)
if err != nil {
return err
}
return db.Model(other).Association("Followers").Append(u).Error
}

// Unfollow func
func (u *User) Unfollow(username string) error {
other, err := GetUserByUsername(username)
if err != nil {
return err
}
return db.Model(other).Association("Followers").Delete(u).Error
}

// FollowSelf func
func (u *User) FollowSelf() error {
return db.Model(u).Association("Followers").Append(u).Error
}

// FollowersCount func
func (u *User) FollowersCount() int {
return db.Model(u).Association("Followers").Count()
}

// FollowingIDs func
func (u *User) FollowingIDs() []int {
var ids []int
rows, err := db.Table("follower").Where("follower_id = ?", u.ID).Select("user_id, follower_id").Rows()
if err != nil {
log.Println("Counting Following error:", err)
return ids
}
defer rows.Close()
for rows.Next() {
var id, followerID int
rows.Scan(&id, &followerID)
ids = append(ids, id)
}
return ids
}

// FollowingCount func
func (u *User) FollowingCount() int {
ids := u.FollowingIDs()
return len(ids)
}

// FollowingPosts func
func (u *User) FollowingPosts() (*[]Post, error) {
var posts []Post
ids := u.FollowingIDs()
if err := db.Preload("User").Order("timestamp desc").Where("user_id in (?)", ids).Find(&posts).Error; err != nil {
return nil, err
}
return &posts, nil
}

// IsFollowedByUser func
func (u *User) IsFollowedByUser(username string) bool {
user, _ := GetUserByUsername(username)
ids := user.FollowingIDs()
for _, id := range ids {
if u.ID == id {
return true
}
}
return false
}

// CreatePost func
func (u *User) CreatePost(body string) error {
post := Post{Body: body, UserID: u.ID}
return db.Create(&post).Error
}

// GetUserByUsername func
func GetUserByUsername(username string) (*User, error) {
var user User
@@ -47,7 +128,10 @@ func AddUser(username, password, email string) error {
user := User{Username: username, Email: email}
user.SetPassword(password)
user.SetAvatar(email)
return db.Create(&user).Error
if err := db.Create(&user).Error; err != nil {
return err
}
return user.FollowSelf()
}

// UpdateUserByUsername func

0 comments on commit ef4e3aa

Please sign in to comment.
You can’t perform that action at this time.