package de.spqrinfo.bookstore.backend.dao;

import com.google.common.collect.Maps;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Repository
public class BookAuthorDao extends AbstractDao {

    @Transactional
    public void insert(final long bookId, final long authorId) {
        final String sql = "INSERT INTO book_author (book_id, author_id) VALUES (:bookId, :authorId)";

        Map<String, Object> parameters = Maps.newHashMap();
        parameters.put("bookId", bookId);
        parameters.put("authorId", authorId);

        getJdbcTemplate().update(sql, new MapSqlParameterSource(parameters));
    }

    @Transactional
    public void deleteByBookId(final long bookId) {
        final String sql = "DElETE FROM book_author WHERE book_id = :bookId";
        getJdbcTemplate().update(sql, new MapSqlParameterSource("bookId", bookId));
    }
}
