package de.jottyfan.camporganizer.module.business.business.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.jooq.exception.DataAccessException;
import org.keycloak.KeycloakSecurityContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import de.jottyfan.camporganizer.module.business.business.IBusinessService;

/**
 *
 * @author jotty
 *
 */
@Service
public class BusinessService implements IBusinessService {
	@Autowired
	private BusinessGateway gateway;

	@Override
	public String getCurrentUser(HttpServletRequest request) {
		KeycloakSecurityContext ksc = (KeycloakSecurityContext) request
				.getAttribute(KeycloakSecurityContext.class.getName());
		if (ksc == null) {
			throw new DataAccessException("no keycloak user in session");
		}
		return ksc.getIdToken().getPreferredUsername();
	}

	@Override
	public List<CampBudgetBean> getCampBudgets(String username) {
		return gateway.getCampBudgets(username);
	}
}
