// query all func AllCategories(orderBy bool) ([]*Category, error) { orm := orm.NewOrm() categories := make([]*Category, 0) var err error if orderBy { _, err = orm.QueryTable("category").OrderBy("-createTime").All(&categories) } else { _, err = orm.QueryTable("category").All(&categories) } return categories, err }
func GetUserProfile(id int64) (*UserProfile, error) { orm := GetOrm() user := new(User) if err := orm.QueryTable("user").Filter("id", id).One(user, "UserProfile"); err != nil { return nil, err } profile := new(UserProfile) if err := orm.QueryTable("user_profile").Filter("id", user.UserProfile.Id).One(profile); err != nil { return nil, err } return profile, nil }
func GetTerminalProfile(id int64) (*TerminalProfile, error) { orm := GetOrm() terminal := new(Terminal) if err := orm.QueryTable("terminal").Filter("id", id).One(terminal, "TerminalProfile"); err != nil { return nil, err } profile := new(TerminalProfile) if err := orm.QueryTable("terminal_profile").Filter("id", terminal.TerminalProfile.Id).One(profile); err != nil { return nil, err } return profile, nil }
func GetTerminalCarrier(id int64) (*TerminalCarrier, error) { orm := GetOrm() terminal := new(Terminal) if err := orm.QueryTable("terminal").Filter("id", id).One(terminal, "TerminalProfile"); err != nil { return nil, err } carrier := new(TerminalCarrier) if err := orm.QueryTable("terminal_carrier").Filter("id", terminal.TerminalProfile.Id).One(carrier); err != nil { return nil, err } return carrier, nil }
func GetAllTerminals(cond *orm.Condition, pageIndex, pageSize int, order ...string) (*[]Terminal, int64, error) { orm := GetOrm() var terminals *[]Terminal = new([]Terminal) total, err := orm.QueryTable("terminal").SetCond(cond).Count() if err != nil { return nil, 0, err } _, err = orm.QueryTable("terminal").SetCond(cond).Limit(pageSize, (pageIndex-1)*pageSize).OrderBy(order...).All(terminals) if err != nil { return nil, 0, err } return terminals, total, nil }
func GetAllRights(cond *orm.Condition, pageIndex, pageSize int, order ...string) (*[]Right, int64, error) { orm := GetOrm() var roles *[]Right = new([]Right) total, err := orm.QueryTable("right").SetCond(cond).Count() if err != nil { return nil, 0, err } _, err = orm.QueryTable("right").SetCond(cond).Limit(pageSize, (pageIndex-1)*pageSize).OrderBy(order...).All(roles) if err != nil { return nil, 0, err } return roles, total, nil }
func GetAllMessages(cond *orm.Condition, pageIndex, pageSize int, order ...string) (*[]Message, int64, error) { orm := GetOrm() var messages *[]Message = new([]Message) total, err := orm.QueryTable("message").SetCond(cond).Count() if err != nil { return nil, 0, err } _, err = orm.QueryTable("message").SetCond(cond).Limit(pageSize, (pageIndex-1)*pageSize).OrderBy(order...).All(messages) if err != nil { return nil, 0, err } return messages, total, nil }
func GetAllLogs(cond *orm.Condition, pageIndex, pageSize int, order ...string) (*[]Log, int64, error) { orm := GetOrm() var logs *[]Log = new([]Log) total, err := orm.QueryTable("log").SetCond(cond).Count() if err != nil { return nil, 0, err } _, err = orm.QueryTable("log").SetCond(cond).Limit(pageSize, (pageIndex-1)*pageSize).OrderBy(order...).All(logs) if err != nil { return nil, 0, err } return logs, total, nil }
func GetAllUsers(cond *orm.Condition, pageIndex, pageSize int, order ...string) (*[]User, int64, error) { orm := GetOrm() var users *[]User = new([]User) total, err := orm.QueryTable("user").SetCond(cond).Count() if err != nil { return nil, 0, err } _, err = orm.QueryTable("user").SetCond(cond).Limit(pageSize, (pageIndex-1)*pageSize).OrderBy(order...).All(users) if err != nil { return nil, 0, err } return users, total, nil }
func Articles(page int) ([]entities.Article, error) { var err error var articles []entities.Article orm := orm.NewOrm() _, err = orm.QueryTable("article").All(&articles, "id", "user_id", "title", "tag", "content", "created_at", "updated_at") return articles, err }
func LastArticle() (entities.Article, error) { var err error var article entities.Article orm := orm.NewOrm() err = orm.QueryTable("article").OrderBy("-id").One(&article, "id", "user_id", "title", "tag", "content", "created_at", "updated_at") return article, err }
func DeleteComment(cid, tid string) error { commentId, err := strconv.ParseInt(cid, 10, 64) if err != nil { return err } orm := orm.NewOrm() comment := &Comment{Id: commentId} _, err = orm.Delete(comment) if err != nil { return err } topicId, err := strconv.ParseInt(tid, 10, 64) if err != nil { return err } comments := make([]*Comment, 0) _, err = orm.QueryTable("comment").Filter("tid", topicId).OrderBy("-replyTime").All(&comments) if err != nil { return err } topic := &Topic{Id: topicId} if orm.Read(topic) == nil && len(comments) > 0 { topic.ReplyCount-- topic.LastReplyTime = comments[0].ReplyTime // TODO set last user id // topic.ReplyLastUserId = comments[0].UserId orm.Update(topic) } return err }
func GetUser(id int64) (*User, error) { orm := GetOrm() user := new(User) if err := orm.QueryTable("user").Filter("id", id).RelatedSel().One(user); err != nil { return nil, err } return user, nil }
func FundUser(name string, password string) (entities.User, error) { var err error var user entities.User orm := orm.NewOrm() err = orm.QueryTable("user").Filter("name", name).Filter("password", password).One(&user, "id", "name", "nick", "password", "age", "cell", "mail", "sex", "CreatedAt", "UpdatedAt") return user, err }
func GetLog(id int64) (*Log, error) { orm := GetOrm() log := new(Log) if err := orm.QueryTable("log").Filter("Id", id).RelatedSel().One(log); err != nil { return nil, err } return log, nil }
func GetTerminal(id int64) (*Terminal, error) { orm := GetOrm() terminal := new(Terminal) if err := orm.QueryTable("terminal").Filter("Id", id).RelatedSel().One(terminal); err != nil { return nil, err } return terminal, nil }
func GetMessage(id int64) (*Message, error) { orm := GetOrm() message := new(Message) if err := orm.QueryTable("message").Filter("Id", id).One(message); err != nil { return nil, err } return message, nil }
func OpenUser(openId string, openType int) (*entities.OpenUser, error) { var err error var openUser entities.OpenUser orm := orm.NewOrm() err = orm.QueryTable("open_user").Filter("open_id", openId).Filter("type", openType).One(&openUser) return &openUser, err }
func QueryCommentsByTid(tid string) ([]*Comment, error) { titleId, err := strconv.ParseInt(tid, 10, 64) if err != nil { return nil, err } orm := orm.NewOrm() comments := make([]*Comment, 0) _, err = orm.QueryTable("comment").Filter("tid", titleId).All(&comments) return comments, err }
func DeleteLog(id int64) error { orm := GetOrm() rows, err := orm.QueryTable("log").Filter("Id", id).Delete() if err != nil { return err } if rows == 0 { return ERROR_NOT_FOUND } return nil }
func IncreaseTagCount(name string) error { orm := orm.NewOrm() tag := &Tag{Name: name} // 没有主键,无法使用Read方法 err := orm.QueryTable("tag").Filter("name", name).One(tag) if err != nil { return err } tag.Count++ _, err = orm.Update(tag) return err }
func Categories() ([]entities.Category, error) { var err error var categories []entities.Category var categoriesKey = constants.CATEGORY_KEY err = utils.Get(categoriesKey, &categories) if err == nil { return categories, nil } orm := orm.NewOrm() _, err = orm.QueryTable("category").OrderBy("order").All(&categories, "id", "name", "order", "created_at", "updated_at") fmt.Print("**************err", err, "******categories", len(categories)) return categories, err }
func User(id int64) (entities.User, error) { var err error var user entities.User /*var userKey = constants.USER_KEY + string(id); err = redis_util.Get(userKey, &user) if err == nil { return user, nil; }*/ orm := orm.NewOrm() err = orm.QueryTable("user").Filter("id", id).One(&user, "id", "name", "nick", "age", "cell", "mail", "sex", "CreatedAt", "UpdatedAt") /*if err == nil { redis_util.Set(userKey, user, 1000) }*/ return user, err }
func User(id int) (entities.User, error) { var err error var user entities.User var userKey = constants.USER_KEY + string(id) err = utils.Get(userKey, &user) fmt.Println("************err:", err, "user:"******"user").Filter("id", id).One(&user, "id", "nick", "age", "cell", "mail", "sex", "CreatedAt", "UpdatedAt") if err == nil { utils.Set(userKey, user, 1000) } return user, err }
func AllTopics(orderBy bool, category, tag string) ([]*Topic, error) { orm := orm.NewOrm() topics := make([]*Topic, 0) var err error query := orm.QueryTable("topic") if len(category) > 0 { query = query.Filter("category", category) } if len(tag) > 0 { query = query.Filter("tags__contains", "$"+tag+"#") } if orderBy { query = query.OrderBy("-createTime") } _, err = query.All(&topics) return topics, err }
func CheckUserMail(mail string) error { valid := validation.Validation{} if !valid.Email(mail, "email").Ok { return errors.New("邮箱格式错误") } orm := orm.NewOrm() count, err := orm.QueryTable("user").Filter("mail", mail).Count() if nil != err || count > 0 { return exception.USER_MAIL_EXISTENT } return nil }
func FundUser(name string, password string) (entities.User, error) { var err error var user entities.User orm := orm.NewOrm() querySetter := orm.QueryTable("user").Filter("password", password) valid := validation.Validation{} if valid.Email(name, "email").Ok { querySetter = querySetter.Filter("mail", name) } else { querySetter = querySetter.Filter("name", name) } err = querySetter.One(&user, "id", "name", "nick", "password", "age", "cell", "mail", "sex", "CreatedAt", "UpdatedAt") return user, err }
func CheckUserName(name string) error { minNameLength := ParameterIntValue("user_name_min_length") maxNameLength := ParameterIntValue("user_name_max_length") beego.Error("minNameLength:", minNameLength, "maxNameLength:", maxNameLength) nameLength := len(name) if nameLength < minNameLength || nameLength > maxNameLength { return errors.New("用户名长度只能在" + strconv.Itoa(minNameLength) + "-" + strconv.Itoa(maxNameLength) + "字符之间") } orm := orm.NewOrm() count, err := orm.QueryTable("user").Filter("name", name).Count() if nil != err || count > 0 { return exception.USER_NAME_EXISTENT } return nil }
func GetTopic(id string) (*Topic, error) { tid, err := strconv.ParseInt(id, 10, 64) if err != nil { return nil, err } orm := orm.NewOrm() topic := &Topic{ Id: tid, } err = orm.QueryTable("topic").Filter("id", tid).One(topic) if err != nil { return nil, err } // update views topic.Views++ orm.Update(topic) topic.Tags = strings.Replace(strings.Replace(topic.Tags, "#", " ", -1), "$", "", -1) return topic, nil }
func AddCategory(title string) error { orm := orm.NewOrm() category := &Category{ Title: title, CreateTime: CurrentTime(), TopicTime: CurrentTime(), } // query if duplicated err := orm.QueryTable("category").Filter("title", title).One(category) if err == nil { return err } // insert new one _, err = orm.Insert(category) if err != nil { return err } return nil }