001package usecase.follow;
002
003import model.entity.Follow;
004import model.entity.Section;
005import model.entity.User;
006import model.repository.GenericRepository;
007import model.validation.SectionExists;
008import usecase.auth.AuthenticationRequired;
009import usecase.auth.CurrentUser;
010
011import javax.enterprise.context.ApplicationScoped;
012import javax.inject.Inject;
013import javax.transaction.Transactional;
014
015/**
016 * Classe che fornisce i servizi relativi ai follow.
017 */
018@ApplicationScoped
019@Transactional
020public class FollowService {
021    private GenericRepository genericRepository;
022    private CurrentUser currentUser;
023
024    protected FollowService(){}
025
026    @Inject
027    protected FollowService(GenericRepository genericRepository, CurrentUser currentUser){
028        this.genericRepository = genericRepository;
029        this.currentUser = currentUser;
030    }
031
032    /**
033     * Permette di seguire una sezione
034     * @param sectionId identificativo sezione
035     */
036    @AuthenticationRequired
037    public void follow(@SectionExists int sectionId){
038        User user = genericRepository.findById(User.class, currentUser.getId());
039        Section section = genericRepository.findById(Section.class,sectionId);
040        genericRepository.insert(new Follow(user,section));
041    }
042
043    /**
044     * Permette di togliere il follow ad una sezione
045     * @param sectionId identificativo sezione
046     */
047    @AuthenticationRequired
048    public void unFollow(@SectionExists int sectionId){
049        User user = genericRepository.findById(User.class, currentUser.getId());
050        Section section = genericRepository.findById(Section.class,sectionId);
051        Follow follow = section.getFollow(user);
052
053        if(follow != null)
054            genericRepository.remove(follow);
055    }
056
057}