package com.sourcefellows.patterns.outbox;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RegistrationService {

    private final RegistrationRepository registrationRepository;
    private final OutboxRepository outboxRepository;

    @Autowired
    RegistrationService(OutboxRepository outboxRepository,
                        RegistrationRepository registrationRepository) {
        this.outboxRepository = outboxRepository;
        this.registrationRepository = registrationRepository;
    }

    @Transactional
    public void registerNewAttendee(Registration registration) {

        Registration saved = this.registrationRepository.save(registration);

        Message message = new Message("new registration " + saved.toString());
        this.outboxRepository.save(message);

    }

}