package com.atlassian.stash.internal.repository;

import com.atlassian.stash.internal.AbstractService;
import com.atlassian.stash.internal.annotation.RewriteExceptions;
import com.atlassian.bitbucket.repository.RepositorySupplier;
import org.springframework.security.access.prepost.PostAuthorize;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @since 3.6
 */
@RewriteExceptions
public class DefaultRepositorySupplier extends AbstractService implements RepositorySupplier {

    protected final RepositoryDao repositoryDao;

    public DefaultRepositorySupplier(RepositoryDao repositoryDao) {
        this.repositoryDao = repositoryDao;
    }

    @Override
    @PostAuthorize("isRepositoryAccessible(returnObject)")
    public InternalRepository getById(int id) {
        return repositoryDao.getById(id);
    }

    @Override
    @PostAuthorize("isRepositoryAccessible(returnObject)")
    public InternalRepository getBySlug(@Nonnull String projectKey, @Nonnull String slug) {
        return getBySlug(null, projectKey, slug);
    }

    @Override
    @PostAuthorize("isRepositoryAccessible(returnObject)")
    public InternalRepository getBySlug(@Nullable String projectNamespace, @Nonnull String projectKey, @Nonnull String slug) {
        checkNotNull(projectKey, "projectKey");
        checkNotNull(slug, "slug");

        return repositoryDao.getBySlug(projectNamespace, projectKey, slug);
    }
}
