package sk.vrto.web.presenters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import sk.vrto.domain.Contact;
import sk.vrto.domain.User;
import sk.vrto.service.dao.ContactRepository;
import sk.vrto.web.base.Presenter;
import sk.vrto.web.events.ContactInsertedEvent;
import sk.vrto.web.views.NewContactView;
@Component
public class NewContactPresenter extends Presenter<NewContactView> {
private final ContactRepository contactRepository;
private final User user;
/**
* @param contactRepository Repository for accessing contacts
* @param user Logged user
*/
@Autowired
public NewContactPresenter(
@Qualifier("dao.contactRepository") ContactRepository contactRepository,
User user) {
this.contactRepository = contactRepository;
this.user = user;
}
/**
* Creates and inserts new contact, and then propagates {@link ContactInsertedEvent}
* @param name Contact's name
* @param email Contact's email
*/
public void createContact(String name, String email) {
Contact contact = new Contact(name, email, user);
contactRepository.insert(contact);
eventBus.post(new ContactInsertedEvent(contact));
}
}