View Javadoc
1   package usecase.follow;
2   
3   import model.entity.Follow;
4   import model.entity.Section;
5   import model.entity.User;
6   import model.repository.GenericRepository;
7   import model.validation.SectionExists;
8   import usecase.auth.AuthenticationRequired;
9   import usecase.auth.CurrentUser;
10  
11  import javax.enterprise.context.ApplicationScoped;
12  import javax.inject.Inject;
13  import javax.transaction.Transactional;
14  
15  /**
16   * Classe che fornisce i servizi relativi ai follow.
17   */
18  @ApplicationScoped
19  @Transactional
20  public class FollowService {
21      private GenericRepository genericRepository;
22      private CurrentUser currentUser;
23  
24      protected FollowService(){}
25  
26      @Inject
27      protected FollowService(GenericRepository genericRepository, CurrentUser currentUser){
28          this.genericRepository = genericRepository;
29          this.currentUser = currentUser;
30      }
31  
32      /**
33       * Permette di seguire una sezione
34       * @param sectionId identificativo sezione
35       */
36      @AuthenticationRequired
37      public void follow(@SectionExists int sectionId){
38          User user = genericRepository.findById(User.class, currentUser.getId());
39          Section section = genericRepository.findById(Section.class,sectionId);
40          genericRepository.insert(new Follow(user,section));
41      }
42  
43      /**
44       * Permette di togliere il follow ad una sezione
45       * @param sectionId identificativo sezione
46       */
47      @AuthenticationRequired
48      public void unFollow(@SectionExists int sectionId){
49          User user = genericRepository.findById(User.class, currentUser.getId());
50          Section section = genericRepository.findById(Section.class,sectionId);
51          Follow follow = section.getFollow(user);
52  
53          if(follow != null)
54              genericRepository.remove(follow);
55      }
56  
57  }