package com.example.todolist.config;

import java.util.Optional;
import javax.servlet.http.HttpSession;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import lombok.RequiredArgsConstructor;

@Configuration // ①
@EnableJpaAuditing // ②
@RequiredArgsConstructor
public class TodolistAuditorAware implements AuditorAware<String> { // ③
	private final HttpSession session;

	@Override
	public Optional<String> getCurrentAuditor() { // ④
		Integer accountId = (Integer) session.getAttribute("accountId");
		if (accountId == null) {
			return null;
		} else {
			return Optional.ofNullable("" + accountId); // ⑤
		}
	}
}