func (this *User) Save() error { this.CreateTime = time.Now().UnixNano() this.LastUpdateTime = this.CreateTime _, err := repo.Run().Exec(`INSERT INTO "user"(username, password, nickname, status, email, createtime, lastupdatetime, salt, authority) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9);`, this.Username, this.Password, this.Nickname, this.Status, this.Email, this.CreateTime, this.LastUpdateTime, this.Salt, this.Authority) return err }
func (userUtil) GetAllUsers() ([]*User, error) { rows, err := repo.Run().Query(`SELECT id, username, password, nickname, status, email, createtime, lastupdatetime, salt, authority FROM "user";`) if err != nil { return nil, err } defer func() { e := rows.Close() if e != nil { log.Println(e) } }() l := list.New() for rows.Next() { user, err := wrapUser(rows) if err != nil { return nil, err } l.PushBack(user) } if l.Len() > 0 { result := make([]*User, l.Len()) idx := 0 for e := l.Front(); e != nil; e = e.Next() { result[idx] = e.Value.(*User) idx++ } return result, nil } else { return []*User{}, nil } }
func (postUtil) GetIndexPosts() ([]*Post, error) { // record limit is 10 rows, err := repo.Run().Query(` SELECT id, title, abstract, content, status, posttime, lastupdatetime, userid FROM post where status = 0 order by posttime desc limit 10; `) if err != nil { return nil, err } defer func() { e := rows.Close() if e != nil { log.Println(e) } }() l := list.New() for rows.Next() { post, err := WrapPost(rows) if err != nil { return nil, err } l.PushBack(post) } if l.Len() > 0 { result := make([]*Post, l.Len()) idx := 0 for e := l.Front(); e != nil; e = e.Next() { result[idx] = e.Value.(*Post) idx++ } return result, nil } else { return []*Post{}, nil } }
func (this *Post) FillPostById() error { var userId int64 = -1 r := repo.Run().QueryRow(` SELECT title, abstract, content, status, posttime, lastupdatetime, userid FROM post where id = $1; `, this.Id) e := r.Scan(&this.Title, &this.Abstract, &this.Content, &this.Status, &this.PostTime, &this.LastUpdateTime, &userId) if e != nil { if e == sql.ErrNoRows { return NO_SUCH_RECORD } else { return e } } if userId <= 0 { return NO_USER_RELATED } user := NewUser() user.Id = userId err := user.FillUserById() if err != nil { return err } this.PostUser = user return nil }
func (this *Post) Save() error { this.PostTime = time.Now().UnixNano() this.LastUpdateTime = this.PostTime _, err := repo.Run().Exec(` INSERT INTO post(title, abstract, content, status, posttime, lastupdatetime, userid) VALUES ($1, $2, $3, $4, $5, $6, $7); `, this.Title, this.Abstract, this.Content, this.Status, this.PostTime, this.LastUpdateTime, this.PostUser.Id) return err }
func (this *User) FillUserByUsername() error { r := repo.Run().QueryRow(`SELECT id, password, nickname, status, email, createtime, lastupdatetime, salt, authority FROM "user" where username = $1;`, this.Username) e := r.Scan(&this.Id, &this.Password, &this.Nickname, &this.Status, &this.Email, &this.CreateTime, &this.LastUpdateTime, &this.Salt, &this.Authority) if e != nil { if e == sql.ErrNoRows { return NO_SUCH_RECORD } else { return e } } else { return nil } }
func (userUtil) UserExists(username string) (bool, error) { id := int64(-1) r := repo.Run().QueryRow(`SELECT id FROM "user" where username = $1;`, username) e := r.Scan(&id) if e != nil { if e == sql.ErrNoRows { return false, nil } else { return false, e } } else { return true, nil } }