func (r *GormWorkItemLinkTypeRepository) listLinkTypes(ctx context.Context, fetchFunc fetchLinkTypesFunc) (*app.WorkItemLinkTypeList, error) { rows, err := fetchFunc() if err != nil { return nil, errs.WithStack(err) } res := app.WorkItemLinkTypeList{} res.Data = make([]*app.WorkItemLinkTypeData, len(rows)) for index, value := range rows { lt := ConvertLinkTypeFromModel(value) res.Data[index] = lt.Data } // TODO: When adding pagination, this must not be len(rows) but // the overall total number of elements from all pages. res.Meta = &app.WorkItemLinkTypeListMeta{ TotalCount: len(rows), } return &res, nil }
// List returns all work item link types // TODO: Handle pagination func (r *GormWorkItemLinkTypeRepository) List(ctx context.Context) (*app.WorkItemLinkTypeList, error) { // We don't have any where clause or paging at the moment. var rows []WorkItemLinkType db := r.db.Find(&rows) if db.Error != nil { return nil, db.Error } res := app.WorkItemLinkTypeList{} res.Data = make([]*app.WorkItemLinkTypeData, len(rows)) for index, value := range rows { linkType := ConvertLinkTypeFromModel(value) res.Data[index] = linkType.Data } // TODO: When adding pagination, this must not be len(rows) but // the overall total number of elements from all pages. res.Meta = &app.WorkItemLinkTypeListMeta{ TotalCount: len(rows), } return &res, nil }
// enrichLinkTypeList includes related resources in the list's "included" array func enrichLinkTypeList(ctx *workItemLinkContext, list *app.WorkItemLinkTypeList) error { // Add "links" element for _, data := range list.Data { selfURL := rest.AbsoluteURL(ctx.RequestData, ctx.LinkFunc(*data.ID)) data.Links = &app.GenericLinks{ Self: &selfURL, } } // Build our "set" of distinct category IDs already converted as strings categoryIDMap := map[string]bool{} for _, typeData := range list.Data { categoryIDMap[typeData.Relationships.LinkCategory.Data.ID] = true } // Now include the optional link category data in the work item link type "included" array for categoryID := range categoryIDMap { linkCat, err := ctx.Application.WorkItemLinkCategories().Load(ctx.Context, categoryID) if err != nil { jerrors, httpStatusCode := jsonapi.ErrorToJSONAPIErrors(err) return ctx.ResponseData.Service.Send(ctx.Context, httpStatusCode, jerrors) } list.Included = append(list.Included, linkCat.Data) } return nil }