package org.codiki.core.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EntityScan("org.codiki.core.entities.persistence")
@EnableTransactionManagement
@EnableJpaRepositories("org.codiki.core.repositories")
@PropertySource("classpath:application.yml")
public class JpaConfiguration {
	@Bean(name = "dataSource")
	public DataSource getDataSource(@Value("${spring.jpa.datasource.driverClassName}") String driverClassName,
									@Value("${spring.jpa.datasource.url}") String url,
									@Value("${spring.jpa.datasource.username}") String username,
									@Value("${spring.jpa.datasource.password}") String password) {
		return DataSourceBuilder.create()
				.username(username)
				.password(password)
				.url(url)
				.driverClassName(driverClassName)
				.build();
	}
}
