func (c *APIItemController) ConvertItems(items []models.Item) ([]responses.ItemResponse, error) { itemFacade := facades.NewItemFacade(c.GetContext()) // Get only item ids itemIDs := make([]uint64, len(items)) itemUserIDs := make([]uint64, len(items)) for i, item := range items { itemIDs[i] = item.ID itemUserIDs[i] = item.UserID } itemImpressionMap, err := itemFacade.FindItemImpressionMap(itemIDs) if err != nil { return nil, err } itemTagIDsMap, err := itemFacade.FindItemTagIDsMap(itemIDs) if err != nil { return nil, err } userFacade := facades.NewUserFacade(c.GetContext()) itemUserMap, err := userFacade.FindUserMap(itemUserIDs) if err != nil { return nil, err } tagFacade := facades.NewTagFacade(c.GetContext()) // Make a response itemResponseList := make([]responses.ItemResponse, len(items)) for i, item := range items { itemResponse := responses.NewItemResponse( item.ID, item.GithubURL, item.Author, item.Name, item.Description, item.CreatedAt, ) itemImpression := itemImpressionMap[item.ID] itemResponse.SetImpression(itemImpression.View, itemImpression.Star) itemUser := itemUserMap[item.ID] itemResponse.SetUser(responses.NewUserResponse(itemUser.Name, itemUser.AvatarURL)) itemTagIDs := itemTagIDsMap[item.ID] tags, _ := tagFacade.FindTagByIDs(itemTagIDs) for _, tag := range tags { itemResponse.AppendTag(responses.NewTagResponse(tag.Name, tag.Color)) } itemResponseList[i] = itemResponse } return itemResponseList, nil }
func (c *APIItemController) scoring(meID, itemID uint64) { itemFacade := facades.NewItemFacade(c.GetContext()) itemTagIDsMap, err := itemFacade.FindItemTagIDsMap([]uint64{itemID}) if err != nil { return } tagFacade := facades.NewTagFacade(c.GetContext()) itemTagIDs := itemTagIDsMap[itemID] tags, err := tagFacade.FindTagByIDs(itemTagIDs) if err != nil { return } for _, tag := range tags { tagFacade.ScoringTag(meID, tag.ID) } }
func (c *APITagController) GetTagList() { //// Apply Request Parameters param := parameters.NewGetTagListRequest() if ok := c.API().Preprocess(¶m); !ok { return } tags, err := func() ([]models.Tag, error) { //// Call a facade tagFacade := facades.NewTagFacade(c.GetContext()) meFacade := facades.NewMeFacade(c.GetContext()) me, _ := meFacade.GetMe(param.GetAccessToken()) if me != nil { const scoreCountThreshold = 3 tags, _ := tagFacade.FindTagByScore(me.ID, param.Limit) counter := 0 for _, tag := range tags { if tag.Score > 5 { counter++ } } if scoreCountThreshold >= counter { return tags, nil } } return tagFacade.FindPopularTag(param.Limit) }() if err != nil { c.API().InternalServerError(map[string]interface{}{ "status": "NG", "func": "FindPopularTag::TagFacade", "message": err, }) return } // Render result c.API().OK(map[string]interface{}{ "status": "OK", "instances": tags, }) }