コード例 #1
0
ファイル: resource.go プロジェクト: studygolang/studygolang
// FindByCatid 获得某个分类的资源列表,分页
func (ResourceLogic) FindByCatid(ctx context.Context, paginator *Paginator, catid int) (resources []map[string]interface{}, total int64) {
	objLog := GetLogger(ctx)

	var (
		count         = paginator.PerPage()
		resourceInfos = make([]*model.ResourceInfo, 0)
	)

	err := MasterDB.Join("INNER", "resource_ex", "resource.id=resource_ex.id").Where("catid=?", catid).
		Desc("resource.mtime").Limit(count, paginator.Offset()).Find(&resourceInfos)
	if err != nil {
		objLog.Errorln("ResourceLogic FindByCatid error:", err)
		return
	}

	total, err = MasterDB.Where("catid=?", catid).Count(new(model.Resource))
	if err != nil {
		objLog.Errorln("ResourceLogic FindByCatid count error:", err)
		return
	}

	uidSet := set.New(set.NonThreadSafe)
	for _, resourceInfo := range resourceInfos {
		uidSet.Add(resourceInfo.Uid)
	}

	usersMap := DefaultUser.FindUserInfos(ctx, set.IntSlice(uidSet))

	resources = make([]map[string]interface{}, len(resourceInfos))

	for i, resourceInfo := range resourceInfos {
		dest := make(map[string]interface{})

		structs.FillMap(resourceInfo.Resource, dest)
		structs.FillMap(resourceInfo.ResourceEx, dest)

		dest["user"] = usersMap[resourceInfo.Uid]

		// 链接的host
		if resourceInfo.Form == model.LinkForm {
			urlObj, err := url.Parse(resourceInfo.Url)
			if err == nil {
				dest["host"] = urlObj.Host
			}
		} else {
			dest["url"] = "/resources/" + strconv.Itoa(resourceInfo.Resource.Id)
		}

		resources[i] = dest
	}

	return
}
コード例 #2
0
ファイル: topic.go プロジェクト: studygolang/studygolang
// FindAll 支持多页翻看
func (TopicLogic) FindAll(ctx context.Context, paginator *Paginator, orderBy string, querystring string, args ...interface{}) []map[string]interface{} {
	objLog := GetLogger(ctx)

	topicInfos := make([]*model.TopicInfo, 0)

	session := MasterDB.Join("INNER", "topics_ex", "topics.tid=topics_ex.tid")
	if querystring != "" {
		session.Where(querystring, args...)
	}
	err := session.OrderBy(orderBy).Limit(paginator.PerPage(), paginator.Offset()).Find(&topicInfos)
	if err != nil {
		objLog.Errorln("TopicLogic FindAll error:", err)
		return nil
	}

	uidSet := set.New(set.NonThreadSafe)
	nidSet := set.New(set.NonThreadSafe)
	for _, topicInfo := range topicInfos {
		uidSet.Add(topicInfo.Uid)
		if topicInfo.Lastreplyuid != 0 {
			uidSet.Add(topicInfo.Lastreplyuid)
		}
		nidSet.Add(topicInfo.Nid)
	}

	usersMap := DefaultUser.FindUserInfos(ctx, set.IntSlice(uidSet))
	// 获取节点信息
	nodes := GetNodesName(set.IntSlice(nidSet))

	data := make([]map[string]interface{}, len(topicInfos))

	for i, topicInfo := range topicInfos {
		dest := make(map[string]interface{})

		// 有人回复
		if topicInfo.Lastreplyuid != 0 {
			if user, ok := usersMap[topicInfo.Lastreplyuid]; ok {
				dest["lastreplyusername"] = user.Username
			}
		}

		structs.FillMap(topicInfo.Topic, dest)
		structs.FillMap(topicInfo.TopicEx, dest)

		dest["user"] = usersMap[topicInfo.Uid]
		dest["node"] = nodes[topicInfo.Nid]

		data[i] = dest
	}

	return data
}
コード例 #3
0
ファイル: topic.go プロジェクト: studygolang/studygolang
// FindByTid 获得主题详细信息(包括详细回复)
func (self TopicLogic) FindByTid(ctx context.Context, tid int) (topicMap map[string]interface{}, replies []map[string]interface{}, err error) {
	objLog := GetLogger(ctx)

	topicInfo := &model.TopicInfo{}
	_, err = MasterDB.Join("INNER", "topics_ex", "topics.tid=topics_ex.tid").Where("topics.tid=?", tid).Get(topicInfo)
	if err != nil {
		objLog.Errorln("TopicLogic FindByTid get error:", err)
		return
	}

	topic := &topicInfo.Topic

	if topic.Tid == 0 {
		err = errors.New("The topic of tid is not exists")
		objLog.Errorln("TopicLogic FindByTid get error:", err)
		return
	}

	topicMap = make(map[string]interface{})
	structs.FillMap(topic, topicMap)
	structs.FillMap(topicInfo.TopicEx, topicMap)

	// 解析内容中的 @
	topicMap["content"] = self.decodeTopicContent(ctx, topic)

	// 节点名字
	topicMap["node"] = GetNodeName(topic.Nid)

	// 回复信息(评论)
	replies, owerUser, lastReplyUser := DefaultComment.FindObjComments(ctx, topic.Tid, model.TypeTopic, topic.Uid, topic.Lastreplyuid)
	topicMap["user"] = owerUser
	// 有人回复
	if topic.Lastreplyuid != 0 {
		topicMap["lastreplyusername"] = lastReplyUser.Username
	}

	if topic.EditorUid != 0 {
		editorUser := DefaultUser.FindOne(ctx, "uid", topic.EditorUid)
		topicMap["editor_username"] = editorUser.Username
	}

	return
}
コード例 #4
0
ファイル: resource.go プロジェクト: studygolang/studygolang
// 获得资源详细信息
func (ResourceLogic) FindById(ctx context.Context, id int) (resourceMap map[string]interface{}, comments []map[string]interface{}) {
	objLog := GetLogger(ctx)

	resourceInfo := &model.ResourceInfo{}
	_, err := MasterDB.Join("INNER", "resource_ex", "resource.id=resource_ex.id").Where("resource.id=?", id).Get(resourceInfo)
	if err != nil {
		objLog.Errorln("ResourceLogic FindById error:", err)
		return
	}

	resource := &resourceInfo.Resource
	if resource.Id == 0 {
		objLog.Errorln("ResourceLogic FindById get error:", err)
		return
	}

	resourceMap = make(map[string]interface{})
	structs.FillMap(resource, resourceMap)
	structs.FillMap(resourceInfo.ResourceEx, resourceMap)

	resourceMap["catname"] = GetCategoryName(resource.Catid)
	// 链接的host
	if resource.Form == model.LinkForm {
		urlObj, err := url.Parse(resource.Url)
		if err == nil {
			resourceMap["host"] = urlObj.Host
		}
	} else {
		resourceMap["url"] = "/resources/" + strconv.Itoa(resource.Id)
	}

	// 评论信息
	comments, ownerUser, _ := DefaultComment.FindObjComments(ctx, id, model.TypeResource, resource.Uid, 0)
	resourceMap["user"] = ownerUser
	return
}