package com.example.todolist.config;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Converter(autoApply = true) // ④
public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, Timestamp> { // ①
	@Override
	public java.sql.Timestamp convertToDatabaseColumn(LocalDateTime localDateTime) { // ②
		// Entity LocalDateTime -> Table Timestamp
		return (localDateTime == null ? null : java.sql.Timestamp.valueOf(localDateTime));
	}

	@Override
	public LocalDateTime convertToEntityAttribute(Timestamp timestamp) { // ③
		// Table Timestamp -> Entity LocalDateTime
		return (timestamp == null ? null : timestamp.toLocalDateTime());
	}
}
