Repository: go-gitea/gitea
File: models/packages/package_version.go (Go)

Code before:
// GetVersionsByPackageType gets all versions of a specific type
func GetVersionsByPackageType(ctx context.Context, ownerID int64, packageType Type) ([]*PackageVersion, error) {
	pvs, _, err := SearchVersions(ctx, &PackageSearchOptions{
		OwnerID:    ownerID,
		Type:       packageType,
		IsInternal: optional.Some(false),
	})
	return pvs, err
}

// GetVersionsByPackageName gets all versions of a specific package
func GetVersionsByPackageName(ctx context.Context, ownerID int64, packageType Type, name string) ([]*PackageVersion, error) {
	pvs, _, err := SearchVersions(ctx, &PackageSearchOptions{
		OwnerID: ownerID,
		Type:    packageType,
		Name: SearchValue{
			ExactMatch: true,
			Value:      name,
		},
		IsInternal: optional.Some(false),
	})

	if len(pvs) == 0 {
		return nil, util.ErrNotExist
	}

	return pvs, err
}

// DeleteVersionByID deletes a version by id
func DeleteVersionByID(ctx context.Context, versionID int64) error {
	_, err := db.GetEngine(ctx).ID(versionID).Delete(&PackageVersion{})
	return err
}

// HasVersionFileReferences checks if there are associated files
func HasVersionFileReferences(ctx context.Context, versionID int64) (bool, error) {
	return db.GetEngine(ctx).Get(&PackageFile{
		VersionID: versionID,
	})
}

// SearchValue describes a value to search
// If ExactMatch is true, the field must match the value otherwise a LIKE search is performed.
type SearchValue struct {
	Value      string
	ExactMatch bool
}

type VersionSort = string


Review comment (suggestion):
Hmm, we shouldn't do that .... and other code doesn't do that either.

When a function returns empty list without error, it already means "empty" (no item exists).


So IMO the fix could be like this:

```go
pvs1, err := packages_model.GetVersionsByPackageName(ctx, ctx.Package.Owner.ID, packages_model.TypeMaven, params.toInternalPackageNameLegacy())
if err != nil {
		apiError(ctx, http.StatusInternalServerError, err)
		return
}
pvs2, err := packages_model.GetVersionsByPackageName(ctx, ctx.Package.Owner.ID, packages_model.TypeMaven, params.toInternalPackageName())
if err != nil {
		apiError(ctx, http.StatusInternalServerError, err)
		return
}
pvs := append(pvs1, pvs2...)
```


Code after:
// GetVersionsByPackageType gets all versions of a specific type
func GetVersionsByPackageType(ctx context.Context, ownerID int64, packageType Type) ([]*PackageVersion, error) {
	pvs, _, err := SearchVersions(ctx, &PackageSearchOptions{
		OwnerID:    ownerID,
		Type:       packageType,
		IsInternal: optional.Some(false),
	})
	return pvs, err
}

// GetVersionsByPackageName gets all versions of a specific package
func GetVersionsByPackageName(ctx context.Context, ownerID int64, packageType Type, name string) ([]*PackageVersion, error) {
	pvs, _, err := SearchVersions(ctx, &PackageSearchOptions{
		OwnerID: ownerID,
		Type:    packageType,
		Name: SearchValue{
			ExactMatch: true,
			Value:      name,
		},
		IsInternal: optional.Some(false),
	})
	return pvs, err
}

// DeleteVersionByID deletes a version by id
func DeleteVersionByID(ctx context.Context, versionID int64) error {
	_, err := db.GetEngine(ctx).ID(versionID).Delete(&PackageVersion{})
	return err
}

// HasVersionFileReferences checks if there are associated files
func HasVersionFileReferences(ctx context.Context, versionID int64) (bool, error) {
	return db.GetEngine(ctx).Get(&PackageFile{
		VersionID: versionID,
	})
}

// SearchValue describes a value to search
// If ExactMatch is true, the field must match the value otherwise a LIKE search is performed.
type SearchValue struct {
	Value      string
	ExactMatch bool
}

type VersionSort = string

const (
	SortNameAsc     VersionSort = "name_asc"
	SortNameDesc    VersionSort = "name_desc"
	SortVersionAsc  VersionSort = "version_asc"
	SortVersionDesc VersionSort = "version_desc"