Ignore:
Timestamp:
11/23/22 12:15:28 (2 years ago)
Author:
unknown <mlviktor23@…>
Branches:
main
Children:
af801e3, e49d1b6
Parents:
c68150f
Message:

moderation/reporting api in spring boot

Location:
springapp/src/main/java/mk/profesori/springapp
Files:
2 added
16 edited

Legend:

Unmodified
Added
Removed
  • springapp/src/main/java/mk/profesori/springapp/Controller/PublicController.java

    rc68150f r3b6962d  
    2525@RestController
    2626@RequestMapping("/public")
    27 @CrossOrigin(origins = { "http://192.168.0.17:3000", "http://192.168.0.39:3000" })
     27@CrossOrigin(origins = { "http://192.168.0.19:3000", "http://192.168.0.39:3000" })
    2828public class PublicController {
    2929
  • springapp/src/main/java/mk/profesori/springapp/Controller/SecureController.java

    rc68150f r3b6962d  
    33import com.fasterxml.jackson.databind.node.ObjectNode;
    44import mk.profesori.springapp.Model.CustomUserDetails;
     5import mk.profesori.springapp.Model.PostReport;
     6import mk.profesori.springapp.Model.UserRole;
    57import mk.profesori.springapp.Service.CustomUserDetailsService;
    68import mk.profesori.springapp.Service.MainService;
     9import org.apache.tomcat.websocket.AuthenticationException;
    710import org.springframework.security.core.Authentication;
    811import org.springframework.security.core.annotation.CurrentSecurityContext;
     
    1114import org.springframework.web.bind.annotation.*;
    1215
     16import java.util.List;
     17
    1318@RestController
    1419@RequestMapping("/secure")
    15 @CrossOrigin(origins = { "http://192.168.0.17:3000", "http://192.168.0.39:3000" })
     20@CrossOrigin(origins = { "http://192.168.0.19:3000", "http://192.168.0.39:3000" })
    1621public class SecureController {
    1722
    1823    private final MainService mainService;
    19     final
    20     CustomUserDetailsService customUserDetailsService;
     24    final CustomUserDetailsService customUserDetailsService;
    2125
    2226    public SecureController(MainService mainService, CustomUserDetailsService customUserDetailsService) {
     
    2832    public void addOpinion(@RequestBody ObjectNode objectNode, @PathVariable Long professorId,
    2933            @CurrentSecurityContext SecurityContext context) {
    30 
    31         Authentication authentication = context.getAuthentication();
    32 
     34        Authentication authentication = context.getAuthentication();
    3335        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
    3436            String content = objectNode.get("content").asText();
     
    4042    public void replyToOpinion(@RequestBody ObjectNode objectNode, @PathVariable Long professorId,
    4143            @PathVariable Long postId, @CurrentSecurityContext SecurityContext context) {
    42 
    43         Authentication authentication = context.getAuthentication();
    44 
     44        Authentication authentication = context.getAuthentication();
    4545        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
    4646            String content = objectNode.get("content").asText();
     
    5252    public void addThread(@RequestBody ObjectNode objectNode, @PathVariable Long subjectId,
    5353            @CurrentSecurityContext SecurityContext context) {
    54 
    55         Authentication authentication = context.getAuthentication();
    56 
     54        Authentication authentication = context.getAuthentication();
    5755        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
    5856            String title = objectNode.get("title").asText();
     
    6563    public void replyToThread(@RequestBody ObjectNode objectNode, @PathVariable Long subjectId,
    6664            @PathVariable Long postId, @CurrentSecurityContext SecurityContext context) {
    67 
    68         Authentication authentication = context.getAuthentication();
    69 
     65        Authentication authentication = context.getAuthentication();
    7066        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
    7167            String content = objectNode.get("content").asText();
     
    7672    @RequestMapping(value = "/currentUser", method = RequestMethod.GET)
    7773    public UserDetails getUserDetails(@CurrentSecurityContext SecurityContext context) {
    78 
    7974        Authentication authentication = context.getAuthentication();
    8075        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
    8176            return customUserDetailsService.loadUserByUsername(currentUser.getEmail());
    8277        }
    83 
    8478        return null;
    8579    }
     
    8781    @RequestMapping(value = "/upvoteOpinion/{postId}", method = RequestMethod.GET)
    8882    public void upvoteOpinion(@PathVariable Long postId, @CurrentSecurityContext SecurityContext context) {
    89 
    90         Authentication authentication = context.getAuthentication();
    91 
     83        Authentication authentication = context.getAuthentication();
    9284        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
    9385            mainService.upvoteOpinion(postId, currentUser);
     
    9789    @RequestMapping(value = "/downvoteOpinion/{postId}", method = RequestMethod.GET)
    9890    public void downvoteOpinion(@PathVariable Long postId, @CurrentSecurityContext SecurityContext context) {
    99 
    100         Authentication authentication = context.getAuthentication();
    101 
     91        Authentication authentication = context.getAuthentication();
    10292        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
    10393            mainService.downvoteOpinion(postId, currentUser);
     
    10797    @RequestMapping(value = "/upvoteThread/{postId}", method = RequestMethod.GET)
    10898    public void upvoteThread(@PathVariable Long postId, @CurrentSecurityContext SecurityContext context) {
    109 
    110         Authentication authentication = context.getAuthentication();
    111 
     99        Authentication authentication = context.getAuthentication();
    112100        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
    113101            mainService.upvote_Thread(postId, currentUser);
     
    119107
    120108        Authentication authentication = context.getAuthentication();
    121 
    122109        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
    123110            mainService.downvote_Thread(postId, currentUser);
     
    125112    }
    126113
     114    @RequestMapping(value = "/deleteOpinion/{postId}", method = RequestMethod.DELETE)
     115    public void deleteOpinion(@PathVariable Long postId, @CurrentSecurityContext SecurityContext context)
     116            throws Exception {
     117        Authentication authentication = context.getAuthentication();
     118        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser
     119                && currentUser.getUserRole().equals(UserRole.MODERATOR)) {
     120            mainService.deleteOpinion(postId);
     121        } else
     122            throw new AuthenticationException("Auth exception");
     123    }
     124
     125    @RequestMapping(value = "/deleteThread/{postId}", method = RequestMethod.DELETE)
     126    public void deleteThread(@PathVariable Long postId, @CurrentSecurityContext SecurityContext context) {
     127        Authentication authentication = context.getAuthentication();
     128        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser
     129                && currentUser.getUserRole().equals(UserRole.MODERATOR)) {
     130            mainService.delete_Thread(postId);
     131        }
     132    }
     133
     134    @RequestMapping(value = "/updateOpinion/{postId}", method = RequestMethod.PUT)
     135    public void updateOpinion(@RequestBody ObjectNode objectNode, @PathVariable Long postId,
     136            @CurrentSecurityContext SecurityContext context) {
     137        Authentication authentication = context.getAuthentication();
     138        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser
     139                && currentUser.getUserRole().equals(UserRole.MODERATOR)) {
     140            String newContent = objectNode.get("newContent").asText();
     141            Long newTargetProfessorId = objectNode.get("newTargetProfessorId").asLong();
     142            mainService.updateOpinion(newContent, newTargetProfessorId, postId);
     143        }
     144    }
     145
     146    @RequestMapping(value = "/updateThread/{postId}", method = RequestMethod.PUT)
     147    public void updateThread(@RequestBody ObjectNode objectNode, @PathVariable Long postId,
     148            @CurrentSecurityContext SecurityContext context) {
     149        Authentication authentication = context.getAuthentication();
     150        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser
     151                && currentUser.getUserRole().equals(UserRole.MODERATOR)) {
     152            String newTitle = objectNode.get("newTitle").asText();
     153            String newContent = objectNode.get("newContent").asText();
     154            Long newTargetSubjectId = objectNode.get("newTargetSubjectId").asLong();
     155
     156            if (objectNode.has("newParentThreadId")) {
     157                Long newParentThreadId = objectNode.get("newParentThreadId").asLong();
     158                mainService.update_Thread(newTitle, newContent, newTargetSubjectId, newParentThreadId, postId);
     159            } else {
     160                mainService.update_Thread(newTitle, newContent, newTargetSubjectId, null, postId);
     161            }
     162        }
     163    }
     164
     165    @RequestMapping(value = "/lockUser/{userId}", method = RequestMethod.GET)
     166    public void lockUser(@PathVariable Long userId, @CurrentSecurityContext SecurityContext context) {
     167        Authentication authentication = context.getAuthentication();
     168        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser
     169                && currentUser.getUserRole().equals(UserRole.MODERATOR)) {
     170            mainService.lockUser(userId);
     171        }
     172    }
     173
     174    @RequestMapping(value = "/deleteUser/{userId}", method = RequestMethod.DELETE)
     175    public void deleteUser(@PathVariable Long userId, @CurrentSecurityContext SecurityContext context) {
     176        Authentication authentication = context.getAuthentication();
     177        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser
     178                && currentUser.getUserRole().equals(UserRole.MODERATOR)) {
     179            mainService.deleteUser(userId);
     180        }
     181    }
     182
     183    @RequestMapping(value = "/updateUserFullName/{userId}", method = RequestMethod.PUT)
     184    public void updateUserFullName(@RequestBody ObjectNode objectNode, @PathVariable Long userId,
     185            @CurrentSecurityContext SecurityContext context) {
     186        Authentication authentication = context.getAuthentication();
     187        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser
     188                && (currentUser.getUserRole().equals(UserRole.MODERATOR) || currentUser.getId().equals(userId))) {
     189            String newFullName = objectNode.get("newFullName").asText();
     190            mainService.updateUserFullName(newFullName, userId);
     191        }
     192    }
     193
     194    @RequestMapping(value = "/updateUserUsername/{userId}", method = RequestMethod.PUT)
     195    public void updateUserUsername(@RequestBody ObjectNode objectNode, @PathVariable Long userId,
     196            @CurrentSecurityContext SecurityContext context) {
     197        Authentication authentication = context.getAuthentication();
     198        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser
     199                && (currentUser.getUserRole().equals(UserRole.MODERATOR) || currentUser.getId().equals(userId))) {
     200            String newUsername = objectNode.get("newUsername").asText();
     201            mainService.updateUserUsername(newUsername, userId);
     202        }
     203    }
     204
     205    @RequestMapping(value = "/reportOpinion/{postId}", method = RequestMethod.POST)
     206    public void reportOpinion(@RequestBody ObjectNode objectNode, @PathVariable Long postId,
     207                              @CurrentSecurityContext SecurityContext context) {
     208        Authentication authentication = context.getAuthentication();
     209        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
     210            String description = objectNode.get("description").asText();
     211            mainService.reportOpinion(postId, currentUser, description);
     212        }
     213    }
     214
     215    @RequestMapping(value = "/reportThread/{postId}", method = RequestMethod.POST)
     216    public void reportThread(@RequestBody ObjectNode objectNode, @PathVariable Long postId,
     217                              @CurrentSecurityContext SecurityContext context) {
     218        Authentication authentication = context.getAuthentication();
     219        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
     220            String description = objectNode.get("description").asText();
     221            mainService.reportThread(postId, currentUser, description);
     222        }
     223    }
     224
     225    @RequestMapping(value = "/markReportResolved/{postReportId}/", method = RequestMethod.GET)
     226    public void markReportResolved(@PathVariable Long postReportId, @PathVariable String action, @CurrentSecurityContext SecurityContext context) {
     227        Authentication authentication = context.getAuthentication();
     228        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser) {
     229            mainService.markReport(postReportId, action);
     230        }
     231    }
     232
     233    @RequestMapping(value = "/getAllPostReports", method = RequestMethod.GET)
     234    public List<PostReport> getAllPostReports(@CurrentSecurityContext SecurityContext context) throws AuthenticationException{
     235        Authentication authentication = context.getAuthentication();
     236        if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails currentUser
     237                && currentUser.getUserRole().equals(UserRole.MODERATOR)) {
     238            return mainService.getAllPostReports();
     239        } else throw new AuthenticationException("Invalid role");
     240     }
     241
     242
    127243}
  • springapp/src/main/java/mk/profesori/springapp/Model/CustomUserDetails.java

    rc68150f r3b6962d  
    2929    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_sequence")
    3030    private Long id;
    31     private String fullName; // opcionalno, smee da e prazno
     31    private String fullName;
    3232    private String username;
    3333    private String email;
    34     private String password; // TODO dont expose password in api
     34    private String password; // TODO
    3535    @Enumerated(EnumType.STRING)
    3636    private UserRole userRole;
    3737    private Boolean locked = false;
    3838    private Boolean enabled = false;
    39     @OneToMany(mappedBy = "customUserDetails", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
     39    @OneToMany(mappedBy = "customUserDetails", fetch = FetchType.EAGER, orphanRemoval = true)
    4040    private Set<ConfirmationToken> confirmationTokens = new HashSet<>();
    41     @OneToMany(mappedBy = "author", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
     41    @OneToMany(mappedBy = "author", fetch = FetchType.EAGER, orphanRemoval = true)
    4242    private Set<Post> authoredPosts = new HashSet<>();
    4343    private Integer karma = 0;
    4444
    45     public Set<PostVote> getVotes() {
    46         return votes;
     45    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
     46    private Set<PostVote> votes = new HashSet<>();
     47
     48    @OneToMany(mappedBy = "user", cascade = {CascadeType.PERSIST})
     49    private Set<PostReport> reportsSubmitted = new HashSet<>();
     50    @PreRemove
     51    public void preRemove() {
     52        reportsSubmitted.forEach(report -> {
     53            report.setUser(null);
     54        });
    4755    }
    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<>();
    5556
    5657    public CustomUserDetails(String fullName, String username, String email, String password, UserRole userRole) {
     
    115116    }
    116117
     118    public void setLocked(Boolean locked) {
     119        this.locked = locked;
     120    }
     121
     122    public Set<PostVote> getVotes() {
     123        return votes;
     124    }
     125
     126    public void setVotes(Set<PostVote> votes) {
     127        this.votes = votes;
     128    }
    117129}
  • springapp/src/main/java/mk/profesori/springapp/Model/Opinion.java

    rc68150f r3b6962d  
    4040        return targetProfessor;
    4141    }
    42 
    4342    public void setTargetProfessor(Professor targetProfessor) {
    4443        this.targetProfessor = targetProfessor;
    4544    }
     45
    4646}
  • springapp/src/main/java/mk/profesori/springapp/Model/Post.java

    rc68150f r3b6962d  
    11package mk.profesori.springapp.Model;
    22
     3import com.fasterxml.jackson.annotation.JsonIdentityInfo;
     4import com.voodoodyne.jackson.jsog.JSOGGenerator;
     5import lombok.NoArgsConstructor;
     6
     7import javax.persistence.*;
    38import java.time.LocalDateTime;
    49import java.util.ArrayList;
     
    611import java.util.List;
    712import java.util.Set;
    8 
    9 import javax.persistence.CascadeType;
    10 import javax.persistence.Column;
    11 import javax.persistence.DiscriminatorColumn;
    12 import javax.persistence.Entity;
    13 import javax.persistence.GeneratedValue;
    14 import javax.persistence.GenerationType;
    15 import javax.persistence.Id;
    16 import javax.persistence.Inheritance;
    17 import javax.persistence.DiscriminatorType;
    18 import javax.persistence.InheritanceType;
    19 import javax.persistence.JoinColumn;
    20 import javax.persistence.ManyToMany;
    21 import javax.persistence.ManyToOne;
    22 import javax.persistence.OneToMany;
    23 
    24 import com.fasterxml.jackson.annotation.JsonIdentityInfo;
    25 import com.voodoodyne.jackson.jsog.JSOGGenerator;
    26 
    27 import lombok.NoArgsConstructor;
    2813
    2914@Entity(name = "post")
     
    5944    private Post parent;
    6045
    61     @OneToMany(mappedBy = "post")
     46    @OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
    6247    private Set<PostVote> votes = new HashSet<>();
    6348
     49    @OneToMany(mappedBy = "post", cascade={CascadeType.PERSIST})
     50    private Set<PostReport> reports = new HashSet<>();
     51    @PreRemove
     52    public void preRemove() {
     53        reports.forEach(report -> {
     54            report.setPost(null);
     55            report.setResolved(true);
     56        });
     57    }
     58
     59
     60    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
     61    private List<Post> children = new ArrayList<>();
     62
     63    // getters and setters
    6464    public Set<PostVote> getVotes() {
    6565        return votes;
     
    6969        this.votes = votes;
    7070    }
    71 
    72     @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
    73     private List<Post> children = new ArrayList<>();
    74 
    75     // getters and setters
    7671    public Long getPostId() {
    7772        return postId;
     
    169164    }
    170165
     166
    171167}
  • springapp/src/main/java/mk/profesori/springapp/Model/_Thread.java

    rc68150f r3b6962d  
    4141    }
    4242
     43    public void setTargetSubject(Subject targetSubject) {
     44        this.targetSubject = targetSubject;
     45    }
    4346}
  • springapp/src/main/java/mk/profesori/springapp/Repository/CityRepository.java

    rc68150f r3b6962d  
    1111public interface CityRepository extends CrudRepository<City, Long>{
    1212
    13     public List<City> findAll();
    14     public City findByCityId(Long id);
     13    List<City> findAll();
     14    City findByCityId(Long id);
    1515}
  • springapp/src/main/java/mk/profesori/springapp/Repository/FacultyRepository.java

    rc68150f r3b6962d  
    1212public interface FacultyRepository extends CrudRepository<Faculty, Long>{
    1313
    14     public List<Faculty> findAll();
    15     public Faculty findByFacultyId(Long id);
    16     public List<Faculty> findByUniversity(University university);
     14    List<Faculty> findAll();
     15    Faculty findByFacultyId(Long id);
     16    List<Faculty> findByUniversity(University university);
    1717}
  • springapp/src/main/java/mk/profesori/springapp/Repository/ProfessorRepository.java

    rc68150f r3b6962d  
    1313public interface ProfessorRepository extends CrudRepository<Professor, Long>, JpaSpecificationExecutor<Professor> {
    1414
    15     public List<Professor> findAll();
     15    List<Professor> findAll();
    1616
    17     public Professor findByProfessorId(Long id);
     17    Professor findByProfessorId(Long id);
    1818
    19     public List<Professor> findByFaculty(Faculty faculty);
     19    List<Professor> findByFaculty(Faculty faculty);
    2020
    21     public List<Professor> findByProfessorNameContainingIgnoreCase(String name);
     21    List<Professor> findByProfessorNameContainingIgnoreCase(String name);
    2222}
  • springapp/src/main/java/mk/profesori/springapp/Repository/StudyProgrammeRepository.java

    rc68150f r3b6962d  
    1313public interface StudyProgrammeRepository extends CrudRepository<StudyProgramme, Long>{
    1414   
    15     public List<StudyProgramme> findAll();
    16     public StudyProgramme findByStudyProgrammeId(Long id);
    17     public List<StudyProgramme> findByFaculty(Faculty faculty);
     15    List<StudyProgramme> findAll();
     16    StudyProgramme findByStudyProgrammeId(Long id);
     17    List<StudyProgramme> findByFaculty(Faculty faculty);
    1818}
  • springapp/src/main/java/mk/profesori/springapp/Repository/SubjectRepository.java

    rc68150f r3b6962d  
    88@Repository
    99public interface SubjectRepository extends CrudRepository<Subject, Long> {
    10     public Subject findBySubjectId(Long id);
     10    Subject findBySubjectId(Long id);
    1111}
  • springapp/src/main/java/mk/profesori/springapp/Repository/UniversityRepository.java

    rc68150f r3b6962d  
    1212public interface UniversityRepository extends CrudRepository<University, Long> {
    1313
    14     public List<University> findAll();
     14    List<University> findAll();
    1515
    16     public University findByUniversityId(Long id);
     16    University findByUniversityId(Long id);
    1717
    18     public List<University> findByCity(City city);
     18    List<University> findByCity(City city);
    1919}
  • springapp/src/main/java/mk/profesori/springapp/Security/SecurityConfiguration.java

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

    rc68150f r3b6962d  
    4343        userRepository.save(customUserDetails);
    4444
    45         String token = createToken(customUserDetails);
    46 
    47         return token;
     45        return createToken(customUserDetails);
    4846    }
    4947
     
    6058    }
    6159
    62     public int enableUser(String email) {
    63         return userRepository.enableUser(email);
     60    public void enableUser(String email) {
     61        userRepository.enableUser(email);
    6462    }
    6563}
  • springapp/src/main/java/mk/profesori/springapp/Service/MainService.java

    rc68150f r3b6962d  
    33import mk.profesori.springapp.Model.*;
    44import mk.profesori.springapp.Repository.*;
     5import org.springframework.security.core.userdetails.UsernameNotFoundException;
    56import org.springframework.stereotype.Service;
    67
     8import java.time.LocalDateTime;
    79import java.util.ArrayList;
    810import java.util.List;
     
    2123    private final PostVoteRepository postVoteRepository;
    2224    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    private final PostReportRepository postReportRepository;
     26
     27    public MainService(ProfessorRepository professorRepository, StudyProgrammeRepository studyProgrammeRepository, FacultyRepository facultyRepository, UniversityRepository universityRepository, CityRepository cityRepository, OpinionRepository opinionRepository, _ThreadRepository _threadRepository, SubjectRepository subjectRepository, PostVoteRepository postVoteRepository, UserRepository userRepository, PostReportRepository postReportRepository) {
    2528        this.professorRepository = professorRepository;
    2629        this.studyProgrammeRepository = studyProgrammeRepository;
     
    3336        this.postVoteRepository = postVoteRepository;
    3437        this.userRepository = userRepository;
     38        this.postReportRepository = postReportRepository;
    3539    }
    3640
     
    183187        targetPost.getAuthor().setKarma(targetPost.getAuthor().getKarma()+1);
    184188        userRepository.save(targetPost.getAuthor());
    185 
    186189    }
    187190    public void downvote_Thread(Long postId, CustomUserDetails currentUser) {
     
    192195        userRepository.save(targetPost.getAuthor());
    193196    }
     197
     198    public void deleteOpinion(Long postId) {opinionRepository.deleteById(postId);}
     199    public void delete_Thread(Long postId) {_threadRepository.deleteById(postId);}
     200
     201    public void updateOpinion(String newContent, Long newTargetProfessorId, Long postId) {
     202        Opinion opinionToUpdate = opinionRepository.findByPostId(postId);
     203
     204        opinionToUpdate.setContent(newContent);
     205
     206        Professor newTargetProfessor = professorRepository.findByProfessorId(newTargetProfessorId);
     207        opinionToUpdate.setTargetProfessor(newTargetProfessor); //opcijava da ja dava samo kaj postovi so parentPost==null
     208        for(Post p : opinionToUpdate.getChildren()) {
     209            Opinion o = (Opinion) p;
     210            o.setTargetProfessor(newTargetProfessor);
     211        }
     212        opinionToUpdate.setTimeLastEdited(LocalDateTime.now());
     213        opinionRepository.save(opinionToUpdate);
     214    }
     215
     216    public void update_Thread(String newTitle, String newContent, Long newTargetSubjectId, Long newParentThreadId, Long postId) {
     217        _Thread _threadToUpdate = _threadRepository.findByPostId(postId);
     218
     219        _threadToUpdate.setContent(newContent);
     220
     221        Subject newTargetSubject = subjectRepository.findBySubjectId(newTargetSubjectId);
     222        _threadToUpdate.setTargetSubject(newTargetSubject);
     223
     224        if(newParentThreadId != null) { //samo ako e specificirano
     225            _Thread newParentThread = _threadRepository.findByPostId(newParentThreadId);
     226
     227            if (_threadToUpdate.getParent() == null || _threadToUpdate.getParent().getPostId().equals(postId)) {
     228                _threadToUpdate.setParent(newParentThread);
     229            }//samo ako e naslovniot post ili directChild
     230            } else if(_threadToUpdate.getParent() != null) {
     231            _threadToUpdate.setParent(null);
     232            }
     233
     234            if(_threadToUpdate.getParent() == null) {
     235             _threadToUpdate.setTitle(newTitle);
     236            } else {
     237             _threadToUpdate.setTitle(null);
     238             }
     239
     240         for(Post p : _threadToUpdate.getChildren()) {
     241                _Thread t = (_Thread) p;
     242                t.setTargetSubject(newTargetSubject);
     243            }
     244         _threadToUpdate.setTimeLastEdited(LocalDateTime.now());
     245          _threadRepository.save(_threadToUpdate);
     246    }
     247
     248    public void lockUser(Long userId) {
     249        CustomUserDetails user = userRepository.findById(userId).orElseThrow(() -> new UsernameNotFoundException("Invalid userId"));
     250        user.setLocked(true);
     251        userRepository.save(user);
     252    }
     253
     254    public void deleteUser(Long userId) {
     255        userRepository.deleteById(userId);
     256    }
     257
     258
     259    public void updateUserFullName(String newFullName, Long userId) {
     260        CustomUserDetails user = userRepository.findById(userId).orElseThrow(() -> new UsernameNotFoundException("Invalid userId"));
     261        user.setFullName(newFullName);
     262        userRepository.save(user);
     263    }
     264
     265    public void updateUserUsername(String newUsername, Long userId) {
     266        CustomUserDetails user = userRepository.findById(userId).orElseThrow(() -> new UsernameNotFoundException("Invalid userId"));
     267        user.setUsername(newUsername);
     268        userRepository.save(user);
     269    }
     270
     271
     272    public void reportOpinion(Long postId, CustomUserDetails currentUser, String description) {
     273            Post targetPost = opinionRepository.findByPostId(postId);
     274            PostReport reportToAdd = new PostReport(currentUser, targetPost, description);
     275            postReportRepository.save(reportToAdd);
     276    }
     277
     278    public void markReport(Long postReportId, String action) {
     279        PostReport report = postReportRepository.findByPostReportId(postReportId);
     280        if (action.equals("resolve")) report.setResolved(true);
     281        else if (action.equals("open")) report.setResolved(false);
     282     }
     283
     284    public List<PostReport> getAllPostReports() {
     285        return postReportRepository.findAll();
     286    }
     287
     288    public void reportThread(Long postId, CustomUserDetails currentUser, String description) {
     289        Post targetPost = _threadRepository.findByPostId(postId);
     290        PostReport reportToAdd = new PostReport(currentUser, targetPost, description);
     291        postReportRepository.save(reportToAdd);
     292    }
    194293}
  • springapp/src/main/java/mk/profesori/springapp/Service/RegistrationService.java

    rc68150f r3b6962d  
    4545                String tokenToResend = customUserDetailsService
    4646                        .createToken(userRepository.findByEmail(request.getEmail()).get());
    47                 String link = "http://192.168.0.17:8080/registration/confirm?token=" + tokenToResend;
     47                String link = "http://192.168.0.19:8080/registration/confirm?token=" + tokenToResend;
    4848                emailSender.send(request.getEmail(), emailSender.buildEmail(request.getUsername(), link));
    4949                return tokenToResend;
     
    6666                        UserRole.REGULAR));
    6767
    68         String link = "http://192.168.0.17:8080/registration/confirm?token=" + token;
     68        String link = "http://192.168.0.19:8080/registration/confirm?token=" + token;
    6969
    7070        emailSender.send(request.getEmail(), emailSender.buildEmail(request.getUsername(), link));
Note: See TracChangeset for help on using the changeset viewer.