package com.platform2easy.genesis.domain.service;

import com.platform2easy.genesis.domain.model.Compra;
import com.platform2easy.genesis.domain.repository.CompraRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@AllArgsConstructor
public class CompraService {

    private final CompraRepository repository;

    @Transactional
    public void salvar(Compra compra) {

        compra.getItens().forEach(itemCompra -> itemCompra.setCompra(compra));

        repository.save(compra);
    }

    public List<Compra> listarTodos() {
        return repository.findAll();
    }

    public Compra buscarPorId(Long id) {
        return repository.findById(id).get();
    }

    public void deletarPorId(Long id) {
        repository.deleteById(id);
    }

}
