Ignore:
Timestamp:
07/29/22 21:31:30 (2 years ago)
Author:
unknown <mlviktor23@…>
Branches:
main
Children:
3a44163
Parents:
7cb8c3c
Message:

implemented addOpinion and replyToOpinion functions, reorganized api

Location:
springapp/src/main/java/mk/profesori/springapp
Files:
2 added
6 edited
1 moved

Legend:

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

    r7cb8c3c r4a64cf0  
    2020
    2121@RestController
    22 @CrossOrigin(origins = "http://192.168.0.17:3000")
    23 public class MainController {
    24    
     22@RequestMapping("/public")
     23@CrossOrigin(origins = { "http://192.168.0.17:3000", "http://192.168.0.24:3000" })
     24public class PublicController {
     25
    2526    @Autowired
    2627    private MainService mainService;
     
    2930    public List<Professor> getProfessorsByFaculty(@RequestParam Optional<Long> facultyId) {
    3031
    31         if(!facultyId.isPresent()) return mainService.getAllProfessors(); //ako nema parametar facultyId gi vrakja site profesori
     32        if (!facultyId.isPresent())
     33            return mainService.getAllProfessors(); // ako nema parametar facultyId gi vrakja site profesori
    3234        return mainService.getProfessorsByFacultyId(facultyId.get());
    3335    }
     
    3537    @RequestMapping(value = "/professor/{professorId}", method = RequestMethod.GET)
    3638    public Professor getProfessorById(@PathVariable Long professorId) {
    37         return mainService.getProfessorById(professorId);   //vrakja profesor spored id
     39        return mainService.getProfessorById(professorId); // vrakja profesor spored id
    3840    }
    3941
     
    4143    public List<StudyProgramme> getStudyProgrammesByFaculty(@RequestParam Optional<Long> facultyId) {
    4244
    43         if(!facultyId.isPresent()) return mainService.getAllStudyProgrammes(); //ako nema parametar facultyId gi vrakja site studiski programi
     45        if (!facultyId.isPresent())
     46            return mainService.getAllStudyProgrammes(); // ako nema parametar facultyId gi vrakja site studiski programi
    4447        return mainService.getStudyProgrammesByFacultyId(facultyId.get());
    4548    }
     
    4750    @RequestMapping(value = "/study_programme/{studyProgrammeId}", method = RequestMethod.GET)
    4851    public StudyProgramme getStudyProgrammeById(@PathVariable Long studyProgrammeId) {
    49         return mainService.getStudyProgrammeById(studyProgrammeId);   //vrakja studiska programa spored id
     52        return mainService.getStudyProgrammeById(studyProgrammeId); // vrakja studiska programa spored id
    5053    }
    5154
     
    5356    public List<Faculty> getFacultiesByUniversity(@RequestParam Optional<Long> universityId) {
    5457
    55         if(!universityId.isPresent()) return mainService.getAllFaculties(); //ako nema parametar universityId gi vrakja site fakulteti
     58        if (!universityId.isPresent())
     59            return mainService.getAllFaculties(); // ako nema parametar universityId gi vrakja site fakulteti
    5660        return mainService.getFacultiesByUniversityId(universityId.get());
    5761    }
     
    5963    @RequestMapping(value = "/faculty/{facultyId}", method = RequestMethod.GET)
    6064    public Faculty getFacultyById(@PathVariable Long facultyId) {
    61         return mainService.getFacultyById(facultyId); //vrakja fakultet spored id
     65        return mainService.getFacultyById(facultyId); // vrakja fakultet spored id
    6266    }
    6367
     
    6569    public List<University> getUniversitiesByCity(@RequestParam Optional<Long> cityId) {
    6670
    67         if(!cityId.isPresent()) return mainService.getAllUniversities(); //ako nema parametar cityId gi vrakja site univerziteti
     71        if (!cityId.isPresent())
     72            return mainService.getAllUniversities(); // ako nema parametar cityId gi vrakja site univerziteti
    6873        return mainService.getUniversitiesByCityId(cityId.get());
    6974    }
     
    7176    @RequestMapping(value = "/university/{universityId}", method = RequestMethod.GET)
    7277    public University getUniversityById(@PathVariable Long universityId) {
    73         return mainService.getUniversityById(universityId); //vrakja univerzitet spored id
     78        return mainService.getUniversityById(universityId); // vrakja univerzitet spored id
    7479    }
    7580
    7681    @RequestMapping(value = "/cities", method = RequestMethod.GET)
    7782    public List<City> getCities() {
    78         return mainService.getAllCities();  //gi vrakja site gradovi
     83        return mainService.getAllCities(); // gi vrakja site gradovi
    7984    }
    8085
    8186    @RequestMapping(value = "/city/{cityId}", method = RequestMethod.GET)
    8287    public City getCityById(@PathVariable Long cityId) {
    83         return mainService.getCityById(cityId); //vrakja grad spored id
     88        return mainService.getCityById(cityId); // vrakja grad spored id
    8489    }
    85 
    8690}
  • springapp/src/main/java/mk/profesori/springapp/Model/Opinion.java

    r7cb8c3c r4a64cf0  
    99import javax.persistence.ManyToOne;
    1010
     11import lombok.NoArgsConstructor;
     12
    1113@Entity
    1214@DiscriminatorValue("opinion")
     15@NoArgsConstructor
    1316public class Opinion extends Post {
    1417
    1518    @ManyToOne
    16     @JoinColumn(name = "professor_id", nullable = false)
     19    @JoinColumn(name = "professor_id")
    1720    private Professor targetProfessor;
    1821
    19     public Opinion(Long postId, String title, String content, CustomUserDetails author, LocalDateTime timePosted,
    20             LocalDateTime timeLastEdited, Integer upvoteCount, Integer downvoteCount, Post parent,
    21             List<Post> children, Professor targetProfessor) {
    22         super(postId, title, content, author, timePosted, timeLastEdited, upvoteCount, downvoteCount, parent, children);
     22    // konstruktor so parent (koga e reply)
     23    public Opinion(String title, String content, CustomUserDetails author, LocalDateTime timePosted,
     24            LocalDateTime timeLastEdited,
     25            Integer upvoteCount, Integer downvoteCount, Post parent, List<Post> children, Professor targetProfessor) {
     26        super(title, content, author, timePosted, timeLastEdited, upvoteCount, downvoteCount, parent, children);
     27        this.targetProfessor = targetProfessor;
     28    }
     29
     30    // konstruktor bez parent (koga NE e reply)
     31    public Opinion(String title, String content, CustomUserDetails author, LocalDateTime timePosted,
     32            LocalDateTime timeLastEdited,
     33            Integer upvoteCount, Integer downvoteCount, List<Post> children, Professor targetProfessor) {
     34        super(title, content, author, timePosted, timeLastEdited, upvoteCount, downvoteCount, children);
    2335        this.targetProfessor = targetProfessor;
    2436    }
  • springapp/src/main/java/mk/profesori/springapp/Model/Post.java

    r7cb8c3c r4a64cf0  
    2222import com.fasterxml.jackson.annotation.ObjectIdGenerators;
    2323
     24import lombok.NoArgsConstructor;
     25
    2426@Entity(name = "post")
    2527@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
    2628@DiscriminatorColumn(name = "post_type", discriminatorType = DiscriminatorType.STRING)
    2729@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "postId")
     30@NoArgsConstructor
    2831public class Post {
    2932
     
    143146    }
    144147
    145     public Post(Long postId, String title, String content, CustomUserDetails author, LocalDateTime timePosted,
    146             LocalDateTime timeLastEdited, Integer upvoteCount, Integer downvoteCount, Post parent,
    147             List<Post> children) {
    148         this.postId = postId;
     148    // konstruktor so parent (koga e reply)
     149    public Post(String title, String content, CustomUserDetails author, LocalDateTime timePosted,
     150            LocalDateTime timeLastEdited,
     151            Integer upvoteCount, Integer downvoteCount, Post parent, List<Post> children) {
    149152        this.title = title;
    150153        this.content = content;
    151154        this.author = author;
    152         this.timePosted = timePosted;
    153         this.timeLastEdited = timeLastEdited;
    154         this.upvoteCount = upvoteCount;
    155         this.downvoteCount = downvoteCount;
     155        this.timePosted = LocalDateTime.now();
     156        this.timeLastEdited = LocalDateTime.now();
     157        this.upvoteCount = 0;
     158        this.downvoteCount = 0;
    156159        this.parent = parent;
    157         this.children = children;
     160        this.children = new ArrayList<>();
     161    }
     162
     163    // konstruktor bez parent (koga NE e reply)
     164    public Post(String title, String content, CustomUserDetails author, LocalDateTime timePosted,
     165            LocalDateTime timeLastEdited,
     166            Integer upvoteCount, Integer downvoteCount, List<Post> children) {
     167        this.title = title;
     168        this.content = content;
     169        this.author = author;
     170        this.timePosted = LocalDateTime.now();
     171        this.timeLastEdited = LocalDateTime.now();
     172        this.upvoteCount = 0;
     173        this.downvoteCount = 0;
     174        this.parent = null;
     175        this.children = new ArrayList<>();
    158176    }
    159177
  • springapp/src/main/java/mk/profesori/springapp/Model/_Thread.java

    r7cb8c3c r4a64cf0  
    11package mk.profesori.springapp.Model;
    22
    3 import java.time.LocalDateTime;
    43import java.util.ArrayList;
    54import java.util.List;
     
    2827    private Subject targetSubject;
    2928
    30     public _Thread(Long postId, String title, String content, CustomUserDetails author, LocalDateTime timePosted,
    31             LocalDateTime timeLastEdited, Integer upvoteCount, Integer downvoteCount, Post parent,
    32             List<Post> children, List<String> tags, Section parentSection, Subject targetSubject) {
    33         super(postId, title, content, author, timePosted, timeLastEdited, upvoteCount, downvoteCount, parent, children);
    34         this.tags = tags;
    35         this.parentSection = parentSection;
    36         this.targetSubject = targetSubject;
    37     }
     29    // TODO
     30    /*
     31     * public _Thread(String title, String content, List<String> tags, Section
     32     * parentSection, Subject targetSubject) {
     33     * super(title, content);
     34     * this.tags = tags;
     35     * this.parentSection = parentSection;
     36     * this.targetSubject = targetSubject;
     37     * }
     38     */
    3839
    3940    // getters
  • springapp/src/main/java/mk/profesori/springapp/Security/SecurityConfiguration.java

    r7cb8c3c r4a64cf0  
    1212
    1313import lombok.RequiredArgsConstructor;
    14 import mk.profesori.springapp.Service.CustomUserDetailsService;
    1514
    1615@Configuration
     
    1817@EnableWebSecurity
    1918public class SecurityConfiguration {
    20 
    21     private final CustomUserDetailsService customUserDetailsService;
    2219
    2320    @Bean
     
    3532    protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    3633        http
     34                .httpBasic() // neenkriptirani credentials osven ako ne e preky https/tls
     35                .and()
    3736                .cors()
    3837                .and()
    3938                .csrf().disable() // PRIVREMENO
    4039                .authorizeRequests()
    41                 .antMatchers("/registration/**")
    42                 .permitAll()
    43                 .anyRequest()
    44                 .permitAll().and() // bese .authenticated()
     40                .antMatchers("/secure/**").hasAnyAuthority("REGULAR", "MODERATOR")
     41                .antMatchers("/public/**").permitAll()
     42                .antMatchers("/registration/**").permitAll()
     43                .and()
    4544                .formLogin();
    4645
  • springapp/src/main/java/mk/profesori/springapp/Service/CustomUserDetailsService.java

    r7cb8c3c r4a64cf0  
    6464        return userRepository.enableUser(email);
    6565    }
    66    
    6766}
  • springapp/src/main/java/mk/profesori/springapp/Service/MainService.java

    r7cb8c3c r4a64cf0  
    99import mk.profesori.springapp.Repository.CityRepository;
    1010import mk.profesori.springapp.Repository.FacultyRepository;
     11import mk.profesori.springapp.Repository.OpinionRepository;
    1112import mk.profesori.springapp.Repository.ProfessorRepository;
    1213import mk.profesori.springapp.Repository.StudyProgrammeRepository;
    1314import mk.profesori.springapp.Repository.UniversityRepository;
    1415import mk.profesori.springapp.Model.City;
     16import mk.profesori.springapp.Model.CustomUserDetails;
    1517import mk.profesori.springapp.Model.Faculty;
     18import mk.profesori.springapp.Model.Opinion;
    1619import mk.profesori.springapp.Model.Professor;
    1720import mk.profesori.springapp.Model.StudyProgramme;
    1821import mk.profesori.springapp.Model.University;
    1922
    20 @Service       
     23@Service
    2124public class MainService {
    22    
     25
    2326    @Autowired
    2427    private ProfessorRepository professorRepository;
     
    3134    @Autowired
    3235    private CityRepository cityRepository;
     36    @Autowired
     37    private OpinionRepository opinionRepository;
    3338
    3439    public List<Professor> getAllProfessors() {
     
    4550
    4651    public List<Professor> getProfessorsByFacultyId(Long facultyId) {
    47        
     52
    4853        Faculty faculty = facultyRepository.findByFacultyId(facultyId);
    4954
     
    6671
    6772    public List<StudyProgramme> getStudyProgrammesByFacultyId(Long facultyId) {
    68        
     73
    6974        Faculty faculty = facultyRepository.findByFacultyId(facultyId);
    7075
     
    8590
    8691    public List<Faculty> getFacultiesByUniversityId(Long universityId) {
    87        
     92
    8893        University university = universityRepository.findByUniversityId(universityId);
    8994
     
    104109
    105110    public List<University> getUniversitiesByCityId(Long cityId) {
    106        
     111
    107112        City city = cityRepository.findByCityId(cityId);
    108113
     
    121126        return cityRepository.findByCityId(id);
    122127    }
     128
     129    public void addOpinion(String title, String content, Long professorId, CustomUserDetails currentUser) {
     130
     131        Professor targetProfessor = professorRepository.findByProfessorId(professorId);
     132
     133        Opinion opinionToAdd = new Opinion(title, content, currentUser, null, null,
     134                null, null, null, targetProfessor);
     135
     136        opinionRepository.save(opinionToAdd);
     137    }
     138
     139    public void replyToOpinion(String content, Long professorId, Long postId, CustomUserDetails currentUser) {
     140
     141        Professor targetProfessor = professorRepository.findByProfessorId(professorId);
     142        Opinion targetOpinion = opinionRepository.findByPostId(postId);
     143
     144        Opinion opinionToAdd = new Opinion(null, content, currentUser, null, null,
     145                null, null, targetOpinion, null, targetProfessor);
     146        opinionRepository.save(opinionToAdd);
     147
     148        targetOpinion.getChildren().add(opinionToAdd);
     149        opinionRepository.save(targetOpinion);
     150    }
    123151}
Note: See TracChangeset for help on using the changeset viewer.