Changeset c68150f for springapp


Ignore:
Timestamp:
10/27/22 17:35:03 (2 years ago)
Author:
unknown <mlviktor23@…>
Branches:
main
Children:
3b6962d
Parents:
8d83180
Message:

left: moderation, oAuth, messaging

Location:
springapp
Files:
5 added
1 deleted
15 edited

Legend:

Unmodified
Added
Removed
  • springapp/pom.xml

    r8d83180 rc68150f  
    3030                        <artifactId>spring-boot-starter-web</artifactId>
    3131                </dependency>
    32 
    3332                <dependency>
    3433                        <groupId>org.postgresql</groupId>
     
    6362                        <scope>compile</scope>
    6463                </dependency>
     64               
    6565        </dependencies>
    6666
  • springapp/src/main/java/mk/profesori/springapp/Controller/PublicController.java

    r8d83180 rc68150f  
    1818import mk.profesori.springapp.Model.Professor;
    1919import mk.profesori.springapp.Model.StudyProgramme;
     20import mk.profesori.springapp.Model.Subject;
    2021import mk.profesori.springapp.Model.University;
     22import mk.profesori.springapp.Model._Thread;
    2123import mk.profesori.springapp.Service.MainService;
    2224
    2325@RestController
    2426@RequestMapping("/public")
    25 @CrossOrigin(origins = { "http://192.168.0.17:3000", "http://192.168.0.28:3000" })
     27@CrossOrigin(origins = { "http://192.168.0.17:3000", "http://192.168.0.39:3000" })
    2628public class PublicController {
    2729
     
    98100    }
    99101
     102    @RequestMapping(value = "/subject/{subjectId}", method = RequestMethod.GET)
     103    public Subject getSubjectById(@PathVariable Long subjectId) {
     104        return mainService.getSubjectById(subjectId); // vrakja predmet spored id
     105    }
     106
     107    @RequestMapping(value = "/thread/{postId}", method = RequestMethod.GET)
     108    public _Thread getThreadById(@PathVariable Long postId) {
     109        return mainService.get_ThreadById(postId); // vrakja thread (tema) spored id
     110    }
     111
    100112    @RequestMapping(value = "/loginSuccessRegular", method = RequestMethod.GET)
    101113    public Map<String, String> loginSuccessRegular(@RequestParam String sessionId) {
  • springapp/src/main/java/mk/profesori/springapp/Controller/SecureController.java

    r8d83180 rc68150f  
    11package mk.profesori.springapp.Controller;
    22
    3 import java.util.Collections;
    4 import java.util.Map;
    5 
    6 import org.springframework.beans.factory.annotation.Autowired;
     3import com.fasterxml.jackson.databind.node.ObjectNode;
     4import mk.profesori.springapp.Model.CustomUserDetails;
     5import mk.profesori.springapp.Service.CustomUserDetailsService;
     6import mk.profesori.springapp.Service.MainService;
    77import org.springframework.security.core.Authentication;
    88import org.springframework.security.core.annotation.CurrentSecurityContext;
    99import org.springframework.security.core.context.SecurityContext;
    1010import org.springframework.security.core.userdetails.UserDetails;
    11 import org.springframework.web.bind.annotation.CrossOrigin;
    12 import org.springframework.web.bind.annotation.PathVariable;
    13 import org.springframework.web.bind.annotation.RequestBody;
    14 import org.springframework.web.bind.annotation.RequestMapping;
    15 import org.springframework.web.bind.annotation.RequestMethod;
    16 import org.springframework.web.bind.annotation.RequestParam;
    17 import org.springframework.web.bind.annotation.RestController;
    18 
    19 import com.fasterxml.jackson.databind.node.ObjectNode;
    20 
    21 import mk.profesori.springapp.Model.CustomUserDetails;
    22 import mk.profesori.springapp.Service.CustomUserDetailsService;
    23 import mk.profesori.springapp.Service.MainService;
     11import org.springframework.web.bind.annotation.*;
    2412
    2513@RestController
    2614@RequestMapping("/secure")
    27 @CrossOrigin(origins = { "http://192.168.0.17:3000", "http://192.168.0.28:3000" })
     15@CrossOrigin(origins = { "http://192.168.0.17:3000", "http://192.168.0.39:3000" })
    2816public class SecureController {
    2917
    30     @Autowired
    31     private MainService mainService;
    32     @Autowired
     18    private final MainService mainService;
     19    final
    3320    CustomUserDetailsService customUserDetailsService;
     21
     22    public SecureController(MainService mainService, CustomUserDetailsService customUserDetailsService) {
     23        this.mainService = mainService;
     24        this.customUserDetailsService = customUserDetailsService;
     25    }
    3426
    3527    @RequestMapping(value = "/professor/{professorId}/addOpinion", method = RequestMethod.POST)
     
    3931        Authentication authentication = context.getAuthentication();
    4032
    41         if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails) {
    42             CustomUserDetails currentUser = (CustomUserDetails) authentication.getPrincipal();
    43             String title = objectNode.get("title").asText();
     33        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
    4434            String content = objectNode.get("content").asText();
    45             mainService.addOpinion(title, content, professorId, currentUser);
     35            mainService.addOpinion(content, professorId, currentUser);
    4636        }
    4737    }
     
    5343        Authentication authentication = context.getAuthentication();
    5444
    55         if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails) {
    56             CustomUserDetails currentUser = (CustomUserDetails) authentication.getPrincipal();
     45        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
    5746            String content = objectNode.get("content").asText();
    5847            mainService.replyToOpinion(content, professorId, postId, currentUser);
     48        }
     49    }
     50
     51    @RequestMapping(value = "/subject/{subjectId}/addThread", method = RequestMethod.POST)
     52    public void addThread(@RequestBody ObjectNode objectNode, @PathVariable Long subjectId,
     53            @CurrentSecurityContext SecurityContext context) {
     54
     55        Authentication authentication = context.getAuthentication();
     56
     57        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
     58            String title = objectNode.get("title").asText();
     59            String content = objectNode.get("content").asText();
     60            mainService.addThread(title, content, subjectId, currentUser);
     61        }
     62    }
     63
     64    @RequestMapping(value = "/subject/{subjectId}/replyToThread/{postId}", method = RequestMethod.POST)
     65    public void replyToThread(@RequestBody ObjectNode objectNode, @PathVariable Long subjectId,
     66            @PathVariable Long postId, @CurrentSecurityContext SecurityContext context) {
     67
     68        Authentication authentication = context.getAuthentication();
     69
     70        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
     71            String content = objectNode.get("content").asText();
     72            mainService.replyToThread(content, subjectId, postId, currentUser);
    5973        }
    6074    }
     
    6478
    6579        Authentication authentication = context.getAuthentication();
    66         if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails) {
    67             CustomUserDetails currentUser = (CustomUserDetails) authentication.getPrincipal();
     80        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
    6881            return customUserDetailsService.loadUserByUsername(currentUser.getEmail());
    6982        }
     
    7285    }
    7386
    74     @RequestMapping(value = "/professor/{professorId}/upvoteOpinion/{postId}", method = RequestMethod.GET)
    75     public void upvoteOpinion(@PathVariable Long professorId,
    76             @PathVariable Long postId, @CurrentSecurityContext SecurityContext context) {
     87    @RequestMapping(value = "/upvoteOpinion/{postId}", method = RequestMethod.GET)
     88    public void upvoteOpinion(@PathVariable Long postId, @CurrentSecurityContext SecurityContext context) {
    7789
    7890        Authentication authentication = context.getAuthentication();
    7991
    80         if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails) {
    81             CustomUserDetails currentUser = (CustomUserDetails) authentication.getPrincipal();
     92        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
    8293            mainService.upvoteOpinion(postId, currentUser);
    8394        }
    8495    }
    8596
    86     @RequestMapping(value = "/professor/{professorId}/downvoteOpinion/{postId}", method = RequestMethod.GET)
    87     public void downvoteOpinion(@PathVariable Long professorId,
    88             @PathVariable Long postId, @CurrentSecurityContext SecurityContext context) {
     97    @RequestMapping(value = "/downvoteOpinion/{postId}", method = RequestMethod.GET)
     98    public void downvoteOpinion(@PathVariable Long postId, @CurrentSecurityContext SecurityContext context) {
    8999
    90100        Authentication authentication = context.getAuthentication();
    91101
    92         if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails) {
    93             CustomUserDetails currentUser = (CustomUserDetails) authentication.getPrincipal();
     102        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
    94103            mainService.downvoteOpinion(postId, currentUser);
    95104        }
    96105    }
    97106
     107    @RequestMapping(value = "/upvoteThread/{postId}", method = RequestMethod.GET)
     108    public void upvoteThread(@PathVariable Long postId, @CurrentSecurityContext SecurityContext context) {
     109
     110        Authentication authentication = context.getAuthentication();
     111
     112        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
     113            mainService.upvote_Thread(postId, currentUser);
     114        }
     115    }
     116
     117    @RequestMapping(value = "/downvoteThread/{postId}", method = RequestMethod.GET)
     118    public void downvoteThread(@PathVariable Long postId, @CurrentSecurityContext SecurityContext context) {
     119
     120        Authentication authentication = context.getAuthentication();
     121
     122        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
     123            mainService.downvote_Thread(postId, currentUser);
     124        }
     125    }
     126
    98127}
  • springapp/src/main/java/mk/profesori/springapp/Model/ConfirmationToken.java

    r8d83180 rc68150f  
    1212import javax.persistence.SequenceGenerator;
    1313
     14import com.fasterxml.jackson.annotation.JsonIdentityInfo;
     15import com.voodoodyne.jackson.jsog.JSOGGenerator;
     16
    1417import lombok.Getter;
    1518import lombok.NoArgsConstructor;
     
    2023@NoArgsConstructor
    2124@Entity
     25@JsonIdentityInfo(generator = JSOGGenerator.class)
    2226public class ConfirmationToken {
    23    
     27
    2428    @Id
    2529    @SequenceGenerator(name = "confirmation_token_sequence", sequenceName = "confirmation_token_sequence", allocationSize = 1)
     
    4246    private CustomUserDetails customUserDetails;
    4347
    44     public ConfirmationToken(String token, LocalDateTime createdAt, LocalDateTime expiredAt, 
    45     CustomUserDetails customUserDetails) {
    46        
     48    public ConfirmationToken(String token, LocalDateTime createdAt, LocalDateTime expiredAt,
     49            CustomUserDetails customUserDetails) {
     50
    4751        this.token = token;
    4852        this.createdAt = createdAt;
  • springapp/src/main/java/mk/profesori/springapp/Model/CustomUserDetails.java

    r8d83180 rc68150f  
    11package mk.profesori.springapp.Model;
    22
    3 import java.util.ArrayList;
    4 import java.util.Collection;
    5 import java.util.Collections;
    6 import java.util.HashSet;
    7 import java.util.List;
    8 import java.util.Set;
    9 
    10 import javax.persistence.CascadeType;
    11 import javax.persistence.Entity;
    12 import javax.persistence.EnumType;
    13 import javax.persistence.Enumerated;
    14 import javax.persistence.FetchType;
    15 import javax.persistence.GeneratedValue;
    16 import javax.persistence.GenerationType;
    17 import javax.persistence.Id;
    18 import javax.persistence.JoinTable;
    19 import javax.persistence.JoinColumn;
    20 import javax.persistence.ManyToMany;
    21 import javax.persistence.OneToMany;
    22 import javax.persistence.SequenceGenerator;
    23 
     3import com.fasterxml.jackson.annotation.JsonIdentityInfo;
     4import com.voodoodyne.jackson.jsog.JSOGGenerator;
     5import lombok.EqualsAndHashCode;
     6import lombok.Getter;
     7import lombok.NoArgsConstructor;
     8import lombok.Setter;
    249import org.springframework.security.core.GrantedAuthority;
    2510import org.springframework.security.core.authority.SimpleGrantedAuthority;
    2611import org.springframework.security.core.userdetails.UserDetails;
    2712
    28 import com.fasterxml.jackson.annotation.JsonIdentityInfo;
    29 import com.voodoodyne.jackson.jsog.JSOGGenerator;
    30 
    31 import lombok.EqualsAndHashCode;
    32 import lombok.Getter;
    33 import lombok.NoArgsConstructor;
    34 import lombok.Setter;
     13import javax.persistence.*;
     14import java.util.Collection;
     15import java.util.Collections;
     16import java.util.HashSet;
     17import java.util.Set;
    3518
    3619@Getter
     
    5942    private Set<Post> authoredPosts = new HashSet<>();
    6043    private Integer karma = 0;
    61     @ManyToMany(fetch = FetchType.EAGER)
    62     @JoinTable(name = "post_like", joinColumns = @JoinColumn(name = "custom_user_details_id"), inverseJoinColumns = @JoinColumn(name = "post_id"))
    63     Set<Post> likedPosts;
    64     @ManyToMany(fetch = FetchType.EAGER)
    65     @JoinTable(name = "post_dislike", joinColumns = @JoinColumn(name = "custom_user_details_id"), inverseJoinColumns = @JoinColumn(name = "post_id"))
    66     Set<Post> dislikedPosts;
     44
     45    public Set<PostVote> getVotes() {
     46        return votes;
     47    }
     48
     49    public void setVotes(Set<PostVote> votes) {
     50        this.votes = votes;
     51    }
     52
     53    @OneToMany(mappedBy = "user")
     54    private Set<PostVote> votes = new HashSet<>();
    6755
    6856    public CustomUserDetails(String fullName, String username, String email, String password, UserRole userRole) {
     
    122110    }
    123111
    124     public Set<Post> getLikedPosts() {
    125         return this.likedPosts;
     112    @Override
     113    public String toString() {
     114        return this.id.toString();
    126115    }
    127116
    128     public void setLikedPosts(Set<Post> likedPosts) {
    129         this.likedPosts = likedPosts;
    130     }
    131 
    132     public Set<Post> getDislikedPosts() {
    133         return this.dislikedPosts;
    134     }
    135 
    136     public void setDislikedPosts(Set<Post> dislikedPosts) {
    137         this.likedPosts = dislikedPosts;
    138     }
    139117}
  • springapp/src/main/java/mk/profesori/springapp/Model/Post.java

    r8d83180 rc68150f  
    33import java.time.LocalDateTime;
    44import java.util.ArrayList;
     5import java.util.HashSet;
    56import java.util.List;
    67import java.util.Set;
     
    5556
    5657    @ManyToOne
    57     @JoinColumn(name = "parent_post_id", nullable = true)
     58    @JoinColumn(name = "parent_post_id")
    5859    private Post parent;
     60
     61    @OneToMany(mappedBy = "post")
     62    private Set<PostVote> votes = new HashSet<>();
     63
     64    public Set<PostVote> getVotes() {
     65        return votes;
     66    }
     67
     68    public void setVotes(Set<PostVote> votes) {
     69        this.votes = votes;
     70    }
    5971
    6072    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
    6173    private List<Post> children = new ArrayList<>();
    62 
    63     @ManyToMany(mappedBy = "likedPosts")
    64     Set<CustomUserDetails> likes;
    65 
    66     @ManyToMany(mappedBy = "dislikedPosts")
    67     Set<CustomUserDetails> dislikes;
    6874
    6975    // getters and setters
     
    132138    }
    133139
    134     public Set<CustomUserDetails> getLikes() {
    135         return likes;
    136     }
    137 
    138     public void setLikes(Set<CustomUserDetails> likes) {
    139         this.likes = likes;
    140     }
    141 
    142     public Set<CustomUserDetails> getDislikes() {
    143         return dislikes;
    144     }
    145 
    146     public void setDislikes(Set<CustomUserDetails> dislikes) {
    147         this.dislikes = dislikes;
    148     }
    149 
    150140    // konstruktor so parent (koga e reply)
    151141    public Post(String title, String content, CustomUserDetails author, LocalDateTime timePosted,
     
    174164    }
    175165
     166    @Override
     167    public String toString() {
     168        return this.postId.toString();
     169    }
     170
    176171}
  • springapp/src/main/java/mk/profesori/springapp/Model/StudyProgramme.java

    r8d83180 rc68150f  
    1212import javax.persistence.ManyToOne;
    1313import javax.persistence.OneToMany;
    14 import javax.persistence.OneToOne;
    1514import javax.persistence.Table;
    1615
     
    4140    private Set<Subject> subjects = new HashSet<>();
    4241
    43     @OneToOne(mappedBy = "relatedStudyProgramme")
    44     private Section relatedSection;
    45 
    4642    // getters
    4743    public Long getStudyProgrammeId() {
     
    6561    }
    6662
    67     public Section getRelatedSection() {
    68         return relatedSection;
    69     }
    7063}
  • springapp/src/main/java/mk/profesori/springapp/Model/Subject.java

    r8d83180 rc68150f  
    1515
    1616import com.fasterxml.jackson.annotation.JsonIdentityInfo;
    17 import com.fasterxml.jackson.annotation.ObjectIdGenerators;
     17import com.voodoodyne.jackson.jsog.JSOGGenerator;
    1818
    1919@Entity
    2020@Table(name = "subject")
    21 @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "subjectId")
     21@JsonIdentityInfo(generator = JSOGGenerator.class)
    2222public class Subject {
    2323
  • springapp/src/main/java/mk/profesori/springapp/Model/University.java

    r8d83180 rc68150f  
    1515
    1616import com.fasterxml.jackson.annotation.JsonIdentityInfo;
    17 import com.fasterxml.jackson.annotation.ObjectIdGenerators;
     17import com.voodoodyne.jackson.jsog.JSOGGenerator;
    1818
    1919@Entity
    2020@Table(name = "university")
    21 @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "universityId")
     21@JsonIdentityInfo(generator = JSOGGenerator.class)
    2222public class University {
    2323
  • springapp/src/main/java/mk/profesori/springapp/Model/_Thread.java

    r8d83180 rc68150f  
    22
    33import java.time.LocalDateTime;
    4 import java.util.ArrayList;
     4
    55import java.util.List;
    66
    7 import javax.persistence.Column;
    87import javax.persistence.DiscriminatorValue;
    9 import javax.persistence.ElementCollection;
     8
    109import javax.persistence.Entity;
    1110import javax.persistence.JoinColumn;
    1211import javax.persistence.ManyToOne;
    1312
     13import lombok.NoArgsConstructor;
     14
    1415@Entity
    1516@DiscriminatorValue("thread")
     17@NoArgsConstructor
    1618public class _Thread extends Post {
    17 
    18     @Column(name = "tags") // unused
    19     @ElementCollection
    20     private List<String> tags = new ArrayList<>();
    21 
    22     @ManyToOne
    23     @JoinColumn(name = "section_id")
    24     private Section parentSection;
    2519
    2620    @ManyToOne
     
    3125    public _Thread(String title, String content, CustomUserDetails author, LocalDateTime timePosted,
    3226            LocalDateTime timeLastEdited,
    33             Post parent, List<Post> children, Section parentSection, Subject targetSubject) {
     27            Post parent, List<Post> children, Subject targetSubject) {
    3428        super(title, content, author, timePosted, timeLastEdited, parent, children);
    35         this.parentSection = parentSection;
    3629        this.targetSubject = targetSubject;
    3730    }
     
    3932    // konstruktor bez parent (koga NE e reply)
    4033    public _Thread(String title, String content, CustomUserDetails author, LocalDateTime timePosted,
    41             LocalDateTime timeLastEdited, List<Post> children, Section parentSection, Subject targetSubject) {
     34            LocalDateTime timeLastEdited, List<Post> children, Subject targetSubject) {
    4235        super(title, content, author, timePosted, timeLastEdited, children);
    43         this.parentSection = parentSection;
    4436        this.targetSubject = targetSubject;
    45     }
    46 
    47     // getters
    48     public List<String> getTags() {
    49         return tags;
    50     }
    51 
    52     public Section getParentSection() {
    53         return parentSection;
    5437    }
    5538
  • springapp/src/main/java/mk/profesori/springapp/Repository/OpinionRepository.java

    r8d83180 rc68150f  
    11package mk.profesori.springapp.Repository;
    22
     3import mk.profesori.springapp.Model.Opinion;
    34import org.springframework.data.repository.CrudRepository;
    45import org.springframework.stereotype.Repository;
    56
    6 import mk.profesori.springapp.Model.Opinion;
    7 
    87@Repository
    9 public interface OpinionRepository extends CrudRepository<Opinion, Long> {
    10     public Opinion findByPostId(Long id);
     8public interface OpinionRepository extends CrudRepository<Opinion,Long> {
     9    Opinion findByPostId(Long postId);
    1110}
  • springapp/src/main/java/mk/profesori/springapp/Security/SecurityConfiguration.java

    r8d83180 rc68150f  
    3737            @Override
    3838            public void addCorsMappings(CorsRegistry registry) {
    39                 registry.addMapping("/**").allowedOrigins("http://192.168.0.17:3000", "http://192.168.0.28:3000")
     39                registry.addMapping("/**").allowedOrigins("http://192.168.0.17:3000", "http://192.168.0.39:3000")
    4040                        .allowCredentials(true);
    4141            }
  • springapp/src/main/java/mk/profesori/springapp/Service/EmailValidator.java

    r8d83180 rc68150f  
    1313    public boolean test(String email) {
    1414       
    15         String regex = "^(.+)@(.+)$";
     15        String regex = "^(.+)@(.+)$"; //TODO bilosto@@ ->> illegal address exception od mailsender
    1616
    1717        Pattern pattern = Pattern.compile(regex);
  • springapp/src/main/java/mk/profesori/springapp/Service/MainService.java

    r8d83180 rc68150f  
    11package mk.profesori.springapp.Service;
     2
     3import mk.profesori.springapp.Model.*;
     4import mk.profesori.springapp.Repository.*;
     5import org.springframework.stereotype.Service;
    26
    37import java.util.ArrayList;
    48import java.util.List;
    59
    6 import org.springframework.beans.factory.annotation.Autowired;
    7 import org.springframework.stereotype.Service;
    8 
    9 import mk.profesori.springapp.Repository.CityRepository;
    10 import mk.profesori.springapp.Repository.FacultyRepository;
    11 import mk.profesori.springapp.Repository.OpinionRepository;
    12 import mk.profesori.springapp.Repository.ProfessorRepository;
    13 import mk.profesori.springapp.Repository.StudyProgrammeRepository;
    14 import mk.profesori.springapp.Repository.UniversityRepository;
    15 import mk.profesori.springapp.Repository.UserRepository;
    16 import mk.profesori.springapp.Model.City;
    17 import mk.profesori.springapp.Model.CustomUserDetails;
    18 import mk.profesori.springapp.Model.Faculty;
    19 import mk.profesori.springapp.Model.Opinion;
    20 import mk.profesori.springapp.Model.Professor;
    21 import mk.profesori.springapp.Model.StudyProgramme;
    22 import mk.profesori.springapp.Model.University;
    23 
    2410@Service
    2511public class MainService {
    2612
    27     @Autowired
    28     private ProfessorRepository professorRepository;
    29     @Autowired
    30     private StudyProgrammeRepository studyProgrammeRepository;
    31     @Autowired
    32     private FacultyRepository facultyRepository;
    33     @Autowired
    34     private UniversityRepository universityRepository;
    35     @Autowired
    36     private CityRepository cityRepository;
    37     @Autowired
    38     private OpinionRepository opinionRepository;
    39     @Autowired
    40     private UserRepository userRepository;
     13    private final ProfessorRepository professorRepository;
     14    private final StudyProgrammeRepository studyProgrammeRepository;
     15    private final FacultyRepository facultyRepository;
     16    private final UniversityRepository universityRepository;
     17    private final CityRepository cityRepository;
     18    private final OpinionRepository opinionRepository;
     19    private final _ThreadRepository _threadRepository;
     20    private final SubjectRepository subjectRepository;
     21    private final PostVoteRepository postVoteRepository;
     22    private final UserRepository userRepository;
     23
     24    public MainService(ProfessorRepository professorRepository, StudyProgrammeRepository studyProgrammeRepository, FacultyRepository facultyRepository, UniversityRepository universityRepository, CityRepository cityRepository, OpinionRepository opinionRepository, _ThreadRepository _threadRepository, SubjectRepository subjectRepository, PostVoteRepository postVoteRepository, UserRepository userRepository) {
     25        this.professorRepository = professorRepository;
     26        this.studyProgrammeRepository = studyProgrammeRepository;
     27        this.facultyRepository = facultyRepository;
     28        this.universityRepository = universityRepository;
     29        this.cityRepository = cityRepository;
     30        this.opinionRepository = opinionRepository;
     31        this._threadRepository = _threadRepository;
     32        this.subjectRepository = subjectRepository;
     33        this.postVoteRepository = postVoteRepository;
     34        this.userRepository = userRepository;
     35    }
    4136
    4237    public List<Professor> getAllProfessors() {
    4338
    44         List<Professor> list = new ArrayList<>();
    45         professorRepository.findAll().forEach(list::add);
    46         return list;
     39        return new ArrayList<>(professorRepository.findAll());
    4740    }
    4841
     
    5649        Faculty faculty = facultyRepository.findByFacultyId(facultyId);
    5750
    58         List<Professor> list = new ArrayList<>();
    59         professorRepository.findByFaculty(faculty).forEach(list::add);
    60         return list;
     51        return new ArrayList<>(professorRepository.findByFaculty(faculty));
    6152    }
    6253
    6354    public List<Professor> getProfessorsByNameContains(String contained) {
    64         List<Professor> list = new ArrayList<>();
    65         professorRepository.findByProfessorNameContainingIgnoreCase(contained).forEach(list::add);
    66         return list;
     55        return new ArrayList<>(professorRepository.findByProfessorNameContainingIgnoreCase(contained));
    6756    }
    6857
    6958    public List<StudyProgramme> getAllStudyProgrammes() {
    7059
    71         List<StudyProgramme> list = new ArrayList<>();
    72         studyProgrammeRepository.findAll().forEach(list::add);
    73         return list;
     60        return new ArrayList<>(studyProgrammeRepository.findAll());
    7461    }
    7562
     
    8370        Faculty faculty = facultyRepository.findByFacultyId(facultyId);
    8471
    85         List<StudyProgramme> list = new ArrayList<>();
    86         studyProgrammeRepository.findByFaculty(faculty).forEach(list::add);
    87         return list;
     72        return new ArrayList<>(studyProgrammeRepository.findByFaculty(faculty));
    8873    }
    8974
    9075    public List<Faculty> getAllFaculties() {
    91         List<Faculty> list = new ArrayList<>();
    92         facultyRepository.findAll().forEach(list::add);
    93         return list;
     76        return new ArrayList<>(facultyRepository.findAll());
    9477    }
    9578
     
    10285        University university = universityRepository.findByUniversityId(universityId);
    10386
    104         List<Faculty> list = new ArrayList<>();
    105         facultyRepository.findByUniversity(university).forEach(list::add);
    106         return list;
     87        return new ArrayList<>(facultyRepository.findByUniversity(university));
    10788    }
    10889
    10990    public List<University> getAllUniversities() {
    110         List<University> list = new ArrayList<>();
    111         universityRepository.findAll().forEach(list::add);
    112         return list;
     91        return new ArrayList<>(universityRepository.findAll());
    11392    }
    11493
     
    121100        City city = cityRepository.findByCityId(cityId);
    122101
    123         List<University> list = new ArrayList<>();
    124         universityRepository.findByCity(city).forEach(list::add);
    125         return list;
     102        return new ArrayList<>(universityRepository.findByCity(city));
    126103    }
    127104
    128105    public List<City> getAllCities() {
    129         List<City> list = new ArrayList<>();
    130         cityRepository.findAll().forEach(list::add);
    131         return list;
     106        return new ArrayList<>(cityRepository.findAll());
    132107    }
    133108
     
    136111    }
    137112
    138     public void addOpinion(String title, String content, Long professorId, CustomUserDetails currentUser) {
     113    public void addOpinion(String content, Long professorId, CustomUserDetails currentUser) {
    139114
    140115        Professor targetProfessor = professorRepository.findByProfessorId(professorId);
    141116
    142         Opinion opinionToAdd = new Opinion(title, content, currentUser, null, null,
     117        Opinion opinionToAdd = new Opinion(null, content, currentUser, null, null,
    143118                null, targetProfessor);
    144119
     
    155130        opinionRepository.save(opinionToAdd);
    156131
     132        //mozda ne treba
    157133        targetOpinion.getChildren().add(opinionToAdd);
    158134        opinionRepository.save(targetOpinion);
    159135    }
    160136
    161     public void upvoteOpinion(Long postId, CustomUserDetails currentUser) {
    162         Opinion targetOpinion = opinionRepository.findByPostId(postId);
     137    public void addThread(String title, String content, Long subjectId, CustomUserDetails currentUser) {
     138        Subject targetSubject = subjectRepository.findBySubjectId(subjectId);
    163139
    164         if (!targetOpinion.getLikes().contains(currentUser)) {
    165 
    166             targetOpinion.getLikes().add(currentUser);
    167             // opinionRepository.save(targetOpinion);
    168 
    169             targetOpinion.getAuthor().setKarma(targetOpinion.getAuthor().getKarma() + 1);
    170             userRepository.save(targetOpinion.getAuthor());
    171 
    172             currentUser.getLikedPosts().add(targetOpinion);
    173             userRepository.save(currentUser);
    174         }
     140        _Thread _threadToAdd = new _Thread(title, content, currentUser, null, null, null, targetSubject);
     141        _threadRepository.save(_threadToAdd);
    175142    }
    176143
     144    public void replyToThread(String content, Long subjectId, Long postId, CustomUserDetails currentUser) {
     145        Subject targetSubject = subjectRepository.findBySubjectId(subjectId);
     146        _Thread target_Thread = _threadRepository.findByPostId(postId);
     147
     148        _Thread _threadToAdd = new _Thread(null, content, currentUser, null, null, target_Thread, null, targetSubject);
     149        _threadRepository.save(_threadToAdd);
     150
     151        //mozda ne treba
     152        target_Thread.getChildren().add(_threadToAdd);
     153        _threadRepository.save(target_Thread);
     154    }
     155
     156    public Subject getSubjectById(Long subjectId) {
     157        return subjectRepository.findBySubjectId(subjectId);
     158    }
     159
     160    public _Thread get_ThreadById(Long postId) {
     161        return _threadRepository.findByPostId(postId);
     162    }
     163
     164    public void upvoteOpinion(Long postId, CustomUserDetails currentUser) {
     165        Post targetPost = opinionRepository.findByPostId(postId);
     166        PostVote voteToAdd = new PostVote(currentUser, targetPost, VoteType.UPVOTE);
     167        postVoteRepository.save(voteToAdd);
     168        targetPost.getAuthor().setKarma(targetPost.getAuthor().getKarma()+1);
     169        userRepository.save(targetPost.getAuthor());
     170    }
    177171    public void downvoteOpinion(Long postId, CustomUserDetails currentUser) {
    178         Opinion targetOpinion = opinionRepository.findByPostId(postId);
     172        Post targetPost = opinionRepository.findByPostId(postId);
     173        PostVote voteToAdd = new PostVote(currentUser, targetPost, VoteType.DOWNVOTE);
     174        postVoteRepository.save(voteToAdd);
     175        targetPost.getAuthor().setKarma(targetPost.getAuthor().getKarma()-1);
     176        userRepository.save(targetPost.getAuthor());
     177    }
    179178
    180         if (!targetOpinion.getDislikes().contains(currentUser)) {
     179    public void upvote_Thread(Long postId, CustomUserDetails currentUser) {
     180        Post targetPost = _threadRepository.findByPostId(postId);
     181        PostVote voteToAdd = new PostVote(currentUser, targetPost, VoteType.UPVOTE);
     182        postVoteRepository.save(voteToAdd);
     183        targetPost.getAuthor().setKarma(targetPost.getAuthor().getKarma()+1);
     184        userRepository.save(targetPost.getAuthor());
    181185
    182             targetOpinion.getDislikes().add(currentUser);
    183             // opinionRepository.save(targetOpinion);
    184 
    185             targetOpinion.getAuthor().setKarma(targetOpinion.getAuthor().getKarma() - 1);
    186             userRepository.save(targetOpinion.getAuthor());
    187 
    188             currentUser.getDislikedPosts().add(targetOpinion);
    189             userRepository.save(currentUser);
    190         }
     186    }
     187    public void downvote_Thread(Long postId, CustomUserDetails currentUser) {
     188        Post targetPost = _threadRepository.findByPostId(postId);
     189        PostVote voteToAdd = new PostVote(currentUser, targetPost, VoteType.DOWNVOTE);
     190        postVoteRepository.save(voteToAdd);
     191        targetPost.getAuthor().setKarma(targetPost.getAuthor().getKarma()-1);
     192        userRepository.save(targetPost.getAuthor());
    191193    }
    192194}
  • springapp/src/main/resources/application.properties

    r8d83180 rc68150f  
    44spring.datasource.password=1win7337
    55spring.jpa.hibernate.ddl-auto=update
     6spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
    67spring.jpa.show-sql=false
    7 spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
    88spring.jpa.properties.hibernate.format_sql=true
    99server.address=192.168.0.17
    10 spring.mail.host=192.168.0.28
     10spring.mail.host=192.168.0.39
    1111spring.mail.username=mailuser
    1212spring.mail.password=mailpass
Note: See TracChangeset for help on using the changeset viewer.