@Summary List template versions by template ID @ID list-template-versions-by-template-id @Security CoderSessionToken @Produce json @Tags Templates @Param template path string true "Template ID" format(uuid) @Param after_id query string false "After ID" format(uuid) @Param include_archived query bool
(rw http.ResponseWriter, r *http.Request)
| 817 | // @Success 200 {array} codersdk.TemplateVersion |
| 818 | // @Router /api/v2/templates/{template}/versions [get] |
| 819 | func (api *API) templateVersionsByTemplate(rw http.ResponseWriter, r *http.Request) { |
| 820 | ctx := r.Context() |
| 821 | template := httpmw.TemplateParam(r) |
| 822 | |
| 823 | paginationParams, ok := ParsePagination(rw, r) |
| 824 | if !ok { |
| 825 | return |
| 826 | } |
| 827 | |
| 828 | // If this throws an error, the boolean is false. Which is the default we want. |
| 829 | parser := httpapi.NewQueryParamParser() |
| 830 | includeArchived := parser.Boolean(r.URL.Query(), false, "include_archived") |
| 831 | if len(parser.Errors) > 0 { |
| 832 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 833 | Message: "Invalid query parameters.", |
| 834 | Validations: parser.Errors, |
| 835 | }) |
| 836 | return |
| 837 | } |
| 838 | |
| 839 | var err error |
| 840 | apiVersions := []codersdk.TemplateVersion{} |
| 841 | err = api.Database.InTx(func(store database.Store) error { |
| 842 | if paginationParams.AfterID != uuid.Nil { |
| 843 | // See if the record exists first. If the record does not exist, the pagination |
| 844 | // query will not work. |
| 845 | _, err := store.GetTemplateVersionByID(ctx, paginationParams.AfterID) |
| 846 | if err != nil && xerrors.Is(err, sql.ErrNoRows) { |
| 847 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 848 | Message: fmt.Sprintf("Record at \"after_id\" (%q) does not exists.", paginationParams.AfterID.String()), |
| 849 | }) |
| 850 | return err |
| 851 | } else if err != nil { |
| 852 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 853 | Message: "Internal error fetching template version at after_id.", |
| 854 | Detail: err.Error(), |
| 855 | }) |
| 856 | return err |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | // Exclude archived templates versions |
| 861 | archiveFilter := sql.NullBool{ |
| 862 | Bool: false, |
| 863 | Valid: true, |
| 864 | } |
| 865 | if includeArchived { |
| 866 | archiveFilter = sql.NullBool{Valid: false} |
| 867 | } |
| 868 | |
| 869 | versions, err := store.GetTemplateVersionsByTemplateID(ctx, database.GetTemplateVersionsByTemplateIDParams{ |
| 870 | TemplateID: template.ID, |
| 871 | AfterID: paginationParams.AfterID, |
| 872 | // #nosec G115 - Pagination limits are small and fit in int32 |
| 873 | LimitOpt: int32(paginationParams.Limit), |
| 874 | // #nosec G115 - Pagination offsets are small and fit in int32 |
| 875 | OffsetOpt: int32(paginationParams.Offset), |
| 876 | Archived: archiveFilter, |
nothing calls this directly
no test coverage detected