示例#1
0
文件: comment.go 项目: hxdyxd/pugo
func (cs *CommentService) List(v interface{}) (*Result, error) {
	opt, ok := v.(CommentListOption)
	if !ok {
		return nil, ErrServiceFuncNeedType(cs.List, opt)
	}
	opt = prepareCommentListOption(opt)
	sess := core.Db.NewSession().Limit(opt.Size, (opt.Page-1)*opt.Size).OrderBy(opt.Order)
	defer sess.Close()
	if opt.Status > 0 {
		sess.Where("status = ?", opt.Status)
	} else {
		sess.Where("status != ?", model.COMMENT_STATUS_DELETED)
	}
	comments := make([]*model.Comment, 0)
	if err := sess.Find(&comments); err != nil {
		return nil, err
	}

	res := newResult(cs.List, &comments)
	if opt.IsCount {
		if opt.Status > 0 {
			sess.Where("status = ?", opt.Status)
		} else {
			sess.Where("status != ?", model.COMMENT_STATUS_DELETED)
		}
		count, err := sess.Count(new(model.Comment))
		if err != nil {
			return nil, err
		}
		res.Set(utils.CreatePager(opt.Page, opt.Size, int(count)))
	}
	return res, nil
}
示例#2
0
文件: page.go 项目: hxdyxd/pugo
func (ps *PageService) List(v interface{}) (*Result, error) {
	opt, ok := v.(PageListOption)
	if !ok {
		return nil, ErrServiceFuncNeedType(ps.List, opt)
	}
	opt = preparePageListOption(opt)

	sess := core.Db.NewSession().Limit(opt.Size, (opt.Page-1)*opt.Size).OrderBy(opt.Order)
	defer sess.Close()
	if opt.Status == 0 {
		sess.Where("status != ?", model.PAGE_STATUS_DELETE)
	} else {
		sess.Where("status = ?", opt.Status)
	}

	pages := make([]*model.Page, 0)
	if err := sess.Find(&pages); err != nil {
		return nil, err
	}
	res := newResult(ps.List, &pages)
	if opt.IsCount {
		// the session had been used, reset condition to count
		if opt.Status == 0 {
			sess.Where("status != ?", model.ARTICLE_STATUS_DELETE)
		} else {
			sess.Where("status = ?", opt.Status)
		}
		count, err := sess.Count(new(model.Article))
		if err != nil {
			return nil, err
		}
		res.Set(utils.CreatePager(opt.Page, opt.Size, int(count)))
	}
	return res, nil
}
示例#3
0
文件: media.go 项目: hxdyxd/pugo
func (ms *MediaService) List(v interface{}) (*Result, error) {
	opt, ok := v.(MediaListOption)
	if !ok {
		return nil, ErrServiceFuncNeedType(ms.List, opt)
	}
	opt = prepareMediaListOption(opt)

	sess := core.Db.NewSession().Limit(opt.Size, (opt.Page-1)*opt.Size).OrderBy(opt.Order)
	defer sess.Close()
	if opt.Type > 0 {
		sess.Where("file_type = ?", opt.Type)
	}

	mediaFiles := make([]*model.Media, 0)
	if err := sess.Find(&mediaFiles); err != nil {
		return nil, err
	}

	res := newResult(ms.List, &mediaFiles)

	if opt.IsCount {
		if opt.Type > 0 {
			sess.Where("file_type = ?", opt.Type)
		}
		count, err := sess.Count(new(model.Media))
		if err != nil {
			return nil, err
		}
		res.Set(utils.CreatePager(opt.Page, opt.Size, int(count)))
	}
	return res, nil
}
示例#4
0
文件: article.go 项目: hxdyxd/pugo
func (as *ArticleService) List(v interface{}) (*Result, error) {
	opt, ok := v.(ArticleListOption)
	if !ok {
		return nil, ErrServiceFuncNeedType(as.List, opt)
	}
	opt = prepareArticleListOption(opt)

	sess := core.Db.NewSession().Limit(opt.Size, (opt.Page-1)*opt.Size).OrderBy(opt.Order)
	defer sess.Close()
	if opt.Status == 0 {
		sess.Where("status != ?", model.ARTICLE_STATUS_DELETE)
	} else {
		sess.Where("status = ?", opt.Status)
	}

	articles := make([]*model.Article, 0)
	if err := sess.Find(&articles); err != nil {
		return nil, err
	}
	if opt.ReadTime > 0 {
		for _, a := range articles {
			a.IsNewRead = (a.UpdateTime - opt.ReadTime) >= -3600
		}
	} else {
		// set first one as new article
		if len(articles) > 0 {
			articles[0].IsNewRead = true
		}
	}
	res := newResult(as.List, &articles)
	if opt.IsCount {
		// the session had been used, reset condition to count
		if opt.Status == 0 {
			sess.Where("status != ?", model.ARTICLE_STATUS_DELETE)
		} else {
			sess.Where("status = ?", opt.Status)
		}
		count, err := sess.Count(new(model.Article))
		if err != nil {
			return nil, err
		}
		res.Set(utils.CreatePager(opt.Page, opt.Size, int(count)))
	}
	return res, nil
}
示例#5
0
文件: message.go 项目: hxdyxd/pugo
func (ms *MessageService) List(v interface{}) (*Result, error) {
	opt, ok := v.(MessageListOption)
	if !ok {
		return nil, ErrServiceFuncNeedType(ms.List, opt)
	}
	opt = prepareMessageListOption(opt)
	msgs := make([]*model.Message, 0)
	if err := core.Db.Limit(opt.Size, (opt.Page-1)*opt.Size).OrderBy(opt.Order).Find(&msgs); err != nil {
		return nil, err
	}
	res := newResult(ms.List, &msgs)
	if opt.IsCount {
		count, err := core.Db.Count(new(model.Message))
		if err != nil {
			return nil, err
		}
		res.Set(utils.CreatePager(opt.Page, opt.Size, int(count)))
	}
	return res, nil
}