码农笔录博客源码
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

78 rindas
2.3 KiB

pirms 6 gadiem
package service
import (
"github.com/astaxie/beego/orm"
"beeblog/models"
)
type UserService struct {
}
pirms 6 gadiem
func (this *UserService) GetUser(id int64) (*models.User, error) {
pirms 6 gadiem
o := orm.NewOrm()
pirms 6 gadiem
user := &models.User{Id: id}
pirms 6 gadiem
err := o.Read(user)
pirms 6 gadiem
return user, err
pirms 6 gadiem
}
pirms 6 gadiem
func (this *UserService) FindByUserName(username string) (*models.User, error) {
pirms 6 gadiem
o := orm.NewOrm()
qs := o.QueryTable(&models.User{})
var users []*models.User
_, err := qs.Filter("UserName", username).All(&users)
if err != nil {
return nil, err
}
if len(users) != 0 {
return users[0], nil
}
return nil, nil
}
pirms 6 gadiem
func (this *UserService) SaveUser(user *models.User) error {
pirms 6 gadiem
o := orm.NewOrm()
id, eror := o.Insert(user)
if eror != nil {
return eror
} else {
user.Id = id
o.Commit()
}
return nil
}
pirms 6 gadiem
func (this *UserService) CountBlog(uid int64) {
o := orm.NewOrm()
pirms 6 gadiem
browses := 0
pirms 6 gadiem
o.Raw("UPDATE `auth_user` SET `blog_count` = (SELECT count(id) num FROM blog where delflag = 0 and user_id =? ) WHERE `id` = ? ", uid, uid).QueryRow(&browses)
return
}
pirms 6 gadiem
func (this *UserService) CountBrows(uid int64) {
o := orm.NewOrm()
browses := 0
pirms 6 gadiem
o.Raw("UPDATE `auth_user` SET `blog_browes` = (select SUM(browses) browses from blog where user_id = ?) WHERE `id` = ? ", uid, uid).QueryRow(&browses) //获取总条数
return
}
pirms 6 gadiem
func (this *UserService) CountComments(uid int64,bid int64) {
pirms 6 gadiem
o := orm.NewOrm()
browses := 0
pirms 6 gadiem
o.Raw("UPDATE `auth_user` SET `blog_comment` = (select count(id) num from comment where cuser_id = ?) WHERE `id` = ? ", uid, uid).QueryRow(&browses) //获取总条数
o.Raw("UPDATE `blog` SET `comments` = (select count(id) num from comment where blog_id = ?) WHERE `id` = ? ", bid, bid).QueryRow(&browses) //获取总条数
pirms 6 gadiem
return
}
pirms 6 gadiem
func (this *UserService) CountLike(uid int64,bid int64) {
pirms 6 gadiem
o := orm.NewOrm()
browses := 0
pirms 6 gadiem
o.Raw("UPDATE `auth_user` SET `blog_like` = (select count(id) num from tb_like where user_id = ?) WHERE `id` = ?", uid, uid).QueryRow(&browses)
o.Raw("UPDATE `blog` SET `likes` = (select count(id) num from tb_like where blog_id = ?) WHERE `id` = ?", bid, bid).QueryRow(&browses)
pirms 6 gadiem
return
}
pirms 6 gadiem
pirms 6 gadiem
func (this *UserService) EditUser(user *models.User) (int64, error) {
pirms 6 gadiem
return orm.NewOrm().Update(user)
}
pirms 6 gadiem
pirms 6 gadiem
func (this *UserService) EditHeadImg(user *models.User) (int64, error) {
pirms 6 gadiem
return orm.NewOrm().Update(user,"Headimg")
}