package dev.dinauer.maven.maven.core.release;

import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.NotFoundException;

import java.util.Map;

@ApplicationScoped
public class ReleaseVersionRepo implements PanacheRepositoryBase<ReleaseVersion, String>
{
    public ReleaseVersion findByGroupIdAndArtifactIdAndVersion(String groupId, String artifactId, String version)
    {
        return find("version = :version AND artifact.artifactId = :artifactId AND artifact.group.groupId = :groupId", Map.ofEntries(Map.entry("groupId", groupId), Map.entry("artifactId", artifactId), Map.entry("version", version))).firstResult();
    }

    public ReleaseVersion requireByGroupIdAndArtifactIdAndVersion(String groupId, String artifactId, String version)
    {
        ReleaseVersion releaseVersion = find("version = :version AND artifact.artifactId = :artifactId AND artifact.group.groupId = :groupId", Map.ofEntries(Map.entry("groupId", groupId), Map.entry("artifactId", artifactId), Map.entry("version", version))).firstResult();
        if (releaseVersion != null)
        {
            return releaseVersion;
        }
        else
        {
            throw new NotFoundException();
        }
    }
}
