Skip to content

Commit

Permalink
fix: pinned version downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
MiniDigger committed Feb 8, 2025
1 parent 7432022 commit 7b319c0
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,68 @@ public interface HangarVersionsDAO {
// TODO fixup view and this
@SqlQuery("""
(SELECT pv.version_id,
pv."type",
pv.version_string AS name,
pc.name pc_name,
pc.description pc_description,
pc.created_at pc_created_at,
pc.color pc_color,
pc.flags pc_flags
FROM pinned_versions pv
JOIN project_versions p ON pv.version_id = p.id
JOIN project_channels pc ON pc.id = p.channel_id
WHERE pv.project_id = :projectId AND pv.type = 'version' AND p.visibility = 0 LIMIT 5)
pv.version_string,
pv."type",
pv.version_string AS name,
pc.name pc_name,
pc.description pc_description,
pc.created_at pc_created_at,
pc.color pc_color,
pc.flags pc_flags,
(SELECT ARRAY [p.owner_name, p.slug] FROM projects p WHERE p.id = pv.project_id LIMIT 1) AS project_namespace,-- needed for downloads
(SELECT json_agg(json_build_object('file_size', pvd.file_size,
'hash', pvd.hash,
'file_name', pvd.file_name,
'external_url', pvd.external_url,
'platforms', pvd.platforms,
'download_platform', pvd.download_platform)) AS value
FROM project_version_downloads pvd
WHERE pvd.version_id = pv.version_id
GROUP BY pvd.version_id) AS downloads,
(SELECT json_agg(json_build_object(
'platform', plv.platform,
'version', plv.version)) AS value
FROM project_version_platform_dependencies pvpd
JOIN platform_versions plv ON pvpd.platform_version_id = plv.id
WHERE pvpd.version_id = pv.version_id) AS platform_dependencies,
'dum' AS platform_dependencies_formatted
FROM pinned_versions pv
JOIN project_versions p ON pv.version_id = p.id
JOIN project_channels pc ON pc.id = p.channel_id
WHERE pv.project_id = :projectId AND pv.type = 'version' AND p.visibility = 0
LIMIT 5)
UNION ALL
(SELECT pv.version_id,
pv."type",
pv.version_string AS name,
pc.name pc_name,
pc.description pc_description,
pc.created_at pc_created_at,
pc.color pc_color,
pc.flags pc_flags
FROM pinned_versions pv
JOIN project_versions p ON pv.version_id = p.id
JOIN project_channels pc ON pc.id = p.channel_id
WHERE pv.project_id = :projectId AND pv.type = 'channel' AND p.visibility = 0 LIMIT 1)
pv.version_string,
pv."type",
pv.version_string AS name,
pc.name pc_name,
pc.description pc_description,
pc.created_at pc_created_at,
pc.color pc_color,
pc.flags pc_flags,
(SELECT ARRAY [p.owner_name, p.slug] FROM projects p WHERE p.id = pv.project_id LIMIT 1) AS project_namespace,-- needed for downloads
(SELECT json_agg(json_build_object('file_size', pvd.file_size,
'hash', pvd.hash,
'file_name', pvd.file_name,
'external_url', pvd.external_url,
'platforms', pvd.platforms,
'download_platform', pvd.download_platform)) AS value
FROM project_version_downloads pvd
WHERE pvd.version_id = pv.version_id
GROUP BY pvd.version_id) AS downloads,
(SELECT json_agg(json_build_object(
'platform', plv.platform,
'version', plv.version)) AS value
FROM project_version_platform_dependencies pvpd
JOIN platform_versions plv ON pvpd.platform_version_id = plv.id
WHERE pvpd.version_id = pv.version_id) AS platform_dependencies,
'dum' AS platform_dependencies_formatted
FROM pinned_versions pv
JOIN project_versions p ON pv.version_id = p.id
JOIN project_channels pc ON pc.id = p.channel_id
WHERE pv.project_id = :projectId AND pv.type = 'channel' AND p.visibility = 0
LIMIT 1)
""")
@RegisterConstructorMapper(HangarProject.PinnedVersion.class)
List<HangarProject.PinnedVersion> getPinnedVersions(long projectId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ public VersionDownloadsMapper(final ObjectMapper objectMapper, final FileService

@Override
public Map<Platform, PlatformVersionDownload> map(final ResultSet r, final int columnNumber, final StatementContext ctx) throws SQLException {
final String raw = r.getString(columnNumber);
final Map<Platform, PlatformVersionDownload> result = new EnumMap<>(Platform.class);
final String raw = r.getString(columnNumber);

if (raw == null) {
return result;
}

final String[] projectNamespace = (String[]) r.getArray("project_namespace").getArray();
final String version = r.getString("version_string");
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import io.papermc.hangar.model.internal.Joinable;
import io.papermc.hangar.model.internal.user.JoinableMember;
import java.util.Collection;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jdbi.v3.core.enums.EnumByName;
import org.jdbi.v3.core.mapper.Nested;

Expand Down Expand Up @@ -114,64 +114,34 @@ public int noteCount() {
}


public static class PinnedVersion {
private final long versionId;
private final Type type;
private final String name;
private final ProjectChannel channel;
private final Map<Platform, List<String>> platformDependenciesFormatted;
private final Map<Platform, PlatformVersionDownload> downloads;

public PinnedVersion(final long versionId, final Type type, final String name, @Nested("pc") final ProjectChannel channel) {
this.versionId = versionId;
this.type = type;
this.name = name;
this.channel = channel;
this.platformDependenciesFormatted = new EnumMap<>(Platform.class);
this.downloads = new EnumMap<>(Platform.class);
public record PinnedVersion(long versionId,
HangarProject.PinnedVersion.Type type,
String name,
ProjectChannel channel,
Map<Platform, PlatformVersionDownload> downloads,
Map<Platform, Set<String>> platformDependencies,
Map<Platform, List<String>> platformDependenciesFormatted) {
public PinnedVersion(final long versionId,
final Type type,
final String name,
@Nested("pc") final ProjectChannel channel,
final Map<Platform, PlatformVersionDownload> downloads,
final Map<Platform, Set<String>> platformDependencies,
final Map<Platform, List<String>> platformDependenciesFormatted) {
this.versionId = versionId;
this.type = type;
this.name = name;
this.channel = channel;
this.downloads = downloads;
this.platformDependencies = platformDependencies;
this.platformDependenciesFormatted = platformDependenciesFormatted;
}

@EnumByName
@JsonFormat(shape = JsonFormat.Shape.STRING)
public enum Type {
CHANNEL,
VERSION
}
}

public long getVersionId() {
return this.versionId;
}

public Type getType() {
return this.type;
}

public String getName() {
return this.name;
}

public Map<Platform, List<String>> getPlatformDependenciesFormatted() {
return this.platformDependenciesFormatted;
}

public ProjectChannel getChannel() {
return this.channel;
}

public Map<Platform, PlatformVersionDownload> getDownloads() {
return this.downloads;
}

@Override
public String toString() {
return "PinnedVersion{" +
"versionId=" + this.versionId +
", type=" + this.type +
", name='" + this.name + '\'' +
", channel=" + this.channel +
", platformDependenciesFormatted=" + this.platformDependenciesFormatted +
", downloads=" + this.downloads +
'}';
}

@EnumByName
@JsonFormat(shape = JsonFormat.Shape.STRING)
public enum Type {
CHANNEL,
VERSION
}
}
}

0 comments on commit 7b319c0

Please sign in to comment.