Changeset 19398ad for jobvista-backend


Ignore:
Timestamp:
05/16/24 23:09:21 (6 months ago)
Author:
223021 <daniel.ilievski.2@…>
Branches:
main
Children:
28b3398
Parents:
d8b6c91
Message:

Implemented backend and frontend CRUD operations for job advertisements

Location:
jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend
Files:
12 added
18 edited

Legend:

Unmodified
Added
Removed
  • jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/config/JwtAuthFilter.java

    rd8b6c91 r19398ad  
    4444                token.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
    4545
    46 //                SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
    47 //                securityContext.setAuthentication(token);
    48 //                SecurityContextHolder.setContext(securityContext);
    49 
    5046                SecurityContextHolder.getContext().setAuthentication(token);
    5147            }
  • jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/config/SecurityConfiguration.java

    rd8b6c91 r19398ad  
    22
    33import lombok.RequiredArgsConstructor;
    4 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.enumerations.Role;
    54import org.springframework.context.annotation.Bean;
    65import org.springframework.context.annotation.Configuration;
     
    2524
    2625    private final JwtAuthFilter jwtAuthFilter;
    27     //private final UserService userService;
    2826    private final UserDetailsService userDetailsService;
    2927
     
    3129    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    3230        http.csrf(AbstractHttpConfigurer::disable)
    33                 .authorizeHttpRequests(request -> request.requestMatchers("/api/auth/**")
    34                         .permitAll()
    35                         .requestMatchers("/api/admin").hasAnyAuthority(Role.ROLE_ADMIN.name())
    36                         .requestMatchers("/api/job-seeker").hasAnyAuthority(Role.ROLE_JOBSEEKER.name())
    37                         .requestMatchers("/api/recruiter").hasAnyAuthority(Role.ROLE_RECRUITER.name())
     31                .authorizeHttpRequests(request -> request
     32                        .requestMatchers("/api/job-advertisements/all","/api/job-advertisements/view/**","/api/recruiter/info/**", "/api/auth/**").permitAll()
     33                        //.requestMatchers("/api/job-advertisements/**").hasAnyAuthority(Role.ROLE_RECRUITER.name())
    3834                        .anyRequest().authenticated())
    3935                .sessionManagement(manager -> manager.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
  • jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/controllers/AdminController.java

    rd8b6c91 r19398ad  
    22
    33import lombok.RequiredArgsConstructor;
    4 import org.springframework.http.ResponseEntity;
    5 import org.springframework.web.bind.annotation.GetMapping;
    64import org.springframework.web.bind.annotation.RequestMapping;
    75import org.springframework.web.bind.annotation.RestController;
     
    1210public class AdminController {
    1311
    14 
    15     @GetMapping
    16     public ResponseEntity<String> sayHello() {
    17         return ResponseEntity.ok("Hi Admin");
    18     }
    1912}
  • jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/controllers/AuthController.java

    rd8b6c91 r19398ad  
    1212import org.springframework.web.bind.annotation.*;
    1313
    14 @CrossOrigin(origins = "*")
    1514@RestController
    1615@RequestMapping("/api/auth")
    1716@AllArgsConstructor
     17@CrossOrigin(origins = "*")
    1818public class AuthController {
    1919
  • jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/controllers/JobSeekerController.java

    rd8b6c91 r19398ad  
    22
    33
    4 import lombok.RequiredArgsConstructor;
     4import lombok.AllArgsConstructor;
     5import org.springframework.web.bind.annotation.CrossOrigin;
    56import org.springframework.web.bind.annotation.RequestMapping;
    67import org.springframework.web.bind.annotation.RestController;
     
    89@RestController
    910@RequestMapping("/api/job-seeker")
    10 @RequiredArgsConstructor
     11@AllArgsConstructor
     12@CrossOrigin(origins = "*")
    1113public class JobSeekerController {
    1214}
  • jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/controllers/RecruiterController.java

    rd8b6c91 r19398ad  
    11package mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.controllers;
    22
    3 import lombok.RequiredArgsConstructor;
     3import lombok.AllArgsConstructor;
     4import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.DTO.RecruiterDetailsDTO;
     5import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.service.intef.RecruiterService;
     6import org.springframework.http.HttpStatus;
    47import org.springframework.http.ResponseEntity;
    5 import org.springframework.web.bind.annotation.GetMapping;
    6 import org.springframework.web.bind.annotation.RequestMapping;
    7 import org.springframework.web.bind.annotation.RestController;
     8import org.springframework.web.bind.annotation.*;
    89
    910@RestController
    1011@RequestMapping("/api/recruiter")
    11 @RequiredArgsConstructor
     12@AllArgsConstructor
     13@CrossOrigin(origins = "*")
    1214public class RecruiterController {
    1315
    14     @GetMapping
    15     public ResponseEntity<String> sayHello() {
    16         return ResponseEntity.ok("Hi Recruiter");
     16    private final RecruiterService recruiterService;
     17
     18    @GetMapping("/info/{id}")
     19    public ResponseEntity<?> getRecruiterDetailsById(@PathVariable("id") Long id) {
     20        RecruiterDetailsDTO recruiterDetailsDTO = recruiterService.getRecruiterDetailsById(id);
     21        return new ResponseEntity<>(recruiterDetailsDTO, HttpStatus.OK);
    1722    }
    1823}
  • jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/models/users/Admin.java

    rd8b6c91 r19398ad  
    44import jakarta.persistence.Table;
    55import lombok.AllArgsConstructor;
    6 import lombok.Builder;
    76import lombok.Data;
    87import lombok.NoArgsConstructor;
    98
    109@Entity
    11 //@Data
    12 //@NoArgsConstructor
    13 //@AllArgsConstructor
    14 //@Builder
     10@Data
     11@NoArgsConstructor
    1512@Table(name = "admins")
    1613public class Admin extends User {
     14
     15//    private String name;
     16//    private String surname;
     17
    1718    @Override
    1819    public String getName() {
    1920        return "Admin";
    2021    }
    21 //    private String name;
    22 //    private String surname;
    2322}
  • jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/models/users/DTO/JwtAuthResponse.java

    rd8b6c91 r19398ad  
    1010@NoArgsConstructor
    1111public class JwtAuthResponse {
     12    private Long id;
    1213    private String email;
    1314    private String name;
  • jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/models/users/JobSeeker.java

    rd8b6c91 r19398ad  
    55import jakarta.persistence.Table;
    66import lombok.AllArgsConstructor;
    7 import lombok.Builder;
    87import lombok.Data;
    98import lombok.NoArgsConstructor;
     
    1413@NoArgsConstructor
    1514@AllArgsConstructor
    16 @Builder
    1715@Table(name = "job_seekers")
    1816public class JobSeeker extends User {
  • jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/models/users/User.java

    rd8b6c91 r19398ad  
    1515
    1616@Entity
    17 @Inheritance(strategy = InheritanceType.JOINED)
    18 @Table(name = "users")
    1917@Data
    2018@NoArgsConstructor
    2119@AllArgsConstructor
     20@Inheritance(strategy = InheritanceType.JOINED)
     21@Table(name = "users")
    2222public abstract class User implements UserDetails {
    2323
  • jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/models/users/mappers/JobSeekerMapper.java

    rd8b6c91 r19398ad  
    2626    }
    2727
    28 
    2928}
  • jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/models/users/mappers/RecruiterMapper.java

    rd8b6c91 r19398ad  
    22
    33import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.DTO.RecruiterDTO;
     4import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.DTO.RecruiterDetailsDTO;
    45import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.Recruiter;
    56
     
    1112                recruiter.getPassword(),
    1213                recruiter.getCompanyName(),
     14                recruiter.getPhoneNumber()
     15        );
     16    }
     17
     18    public static RecruiterDetailsDTO mapToRecruiterDetailsDTO(Recruiter recruiter) {
     19        return new RecruiterDetailsDTO(
     20                recruiter.getEmail(),
     21                recruiter.getCompanyName(),
     22                recruiter.getCompanyDescription(),
    1323                recruiter.getPhoneNumber()
    1424        );
  • jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/repositories/RecruiterRepository.java

    rd8b6c91 r19398ad  
    22
    33import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.Recruiter;
     4import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.mappers.RecruiterMapper;
    45import org.springframework.data.jpa.repository.JpaRepository;
    56
    67public interface RecruiterRepository extends JpaRepository<Recruiter, Long> {
     8    Recruiter findRecruiterByEmail(String email);
    79}
  • jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/service/impl/AuthServiceImpl.java

    rd8b6c91 r19398ad  
    3131    private final JwtService jwtService;
    3232
    33 
    3433    @Override
    3534    public User signUpJobSeeker(JobSeeker jobSeeker) {
     
    5049        String refreshJwt = jwtService.generateRefreshToken(new HashMap<>(), user);
    5150
    52         return new JwtAuthResponse(user.getEmail(), user.getName(), user.getRole().name(), jwt, refreshJwt);
     51        return new JwtAuthResponse(user.getId(), user.getEmail(), user.getName(), user.getRole().name(), jwt, refreshJwt);
    5352    }
    5453   
     
    5958            String jwt = jwtService.generateToken(user);
    6059
    61             return new JwtAuthResponse(user.getEmail(), user.getName(), user.getRole().name(), jwt, refreshTokenRequest.getToken());
     60            return new JwtAuthResponse(user.getId(), user.getEmail(), user.getName(), user.getRole().name(), jwt, refreshTokenRequest.getToken());
    6261        }
    6362        return null;
  • jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/service/impl/JwtServiceImpl.java

    rd8b6c91 r19398ad  
    4141    }
    4242
    43     public String extractUsername(String token) {
    44         return extractClaim(token, Claims::getSubject);
    45     }
    46 
    4743    private Claims extractAllClaims(String token) {
    4844        return Jwts.parserBuilder().setSigningKey(getSigninKey()).build().parseClaimsJws(token).getBody();
     
    5450    }
    5551
     52    @Override
     53    public String extractUsername(String token) {
     54        return extractClaim(token, Claims::getSubject);
     55    }
     56
     57    @Override
    5658    public boolean isTokenValid(String token, UserDetails userDetails) {
    5759        final String username = extractUsername(token);
     
    5961    }
    6062
    61 
    62     private boolean isTokenExpired(String token) {
     63    @Override
     64    public boolean isTokenExpired(String token) {
    6365        return extractClaim(token, Claims::getExpiration).before(new Date());
    6466    }
  • jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/service/impl/UserServiceImpl.java

    rd8b6c91 r19398ad  
    11package mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.service.impl;
    22
    3 import lombok.AllArgsConstructor;
    4 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.repositories.JobSeekerRepository;
    5 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.repositories.RecruiterRepository;
    6 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.repositories.UserRepository;
    7 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.service.intef.JwtService;
    8 import org.springframework.security.authentication.AuthenticationManager;
    9 import org.springframework.security.crypto.password.PasswordEncoder;
     3import lombok.RequiredArgsConstructor;
    104import org.springframework.stereotype.Service;
     5import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.service.intef.UserService;
    116
    127@Service
    13 @AllArgsConstructor
     8@RequiredArgsConstructor
     9public class UserServiceImpl implements UserService {
    1410
    15 public class UserServiceImpl  {
    16     private final UserRepository userRepository;
    17     private final RecruiterRepository recruiterRepository;
    18     private final JobSeekerRepository jobSeekerRepository;
    19     private final PasswordEncoder passwordEncoder;
    20     private final AuthenticationManager authenticationManager;
    21     private final JwtService jwtService;
    22 
    23 
    24 //    @Override
    25 //    public User registerJobSeeker(JobSeeker jobSeeker) {
    26 //        jobSeeker.setPassword(passwordEncoder.encode(jobSeeker.getPassword()));
    27 //        return jobSeekerRepository.save(jobSeeker);
    28 //    }
    29 //
    30 //    @Override
    31 //    public User registerRecruiter(Recruiter recruiter) {
    32 //        recruiter.setPassword(passwordEncoder.encode(recruiter.getPassword()));
    33 //        return recruiterRepository.save(recruiter);
    34 //    }
    35 //
    36 //    @Override
    37 //    public JwtAuthenticationResponse login(JwtAuthenticationRequest jwtAuthenticationRequest) {
    38 //        authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(jwtAuthenticationRequest.getEmail(), jwtAuthenticationRequest.getPassword()));
    39 //
    40 //        var user = userRepository.findByEmail(jwtAuthenticationRequest.getEmail()).orElseThrow(() -> new IllegalArgumentException("Email or password is incorrect"));
    41 //        var jwt = jwtService.generateToken(user);
    42 //        var refreshToken = jwtService.generateRefreshToken(new HashMap<>(), user);
    43 //
    44 //        JwtAuthenticationResponse jwtAuthenticationResponse = new JwtAuthenticationResponse();
    45 //        jwtAuthenticationResponse.setToken(jwt);
    46 //        jwtAuthenticationResponse.setRefreshToken(refreshToken);
    47 //        return jwtAuthenticationResponse;
    48 //    }
    49 //
    50 //    @Override
    51 //    public JwtAuthenticationResponse refreshToken(RefreshTokenRequest refreshTokenRequest) {
    52 //        String userEmail = jwtService.extractUsername(refreshTokenRequest.getToken());
    53 //        User user = userRepository.findByEmail(userEmail).orElseThrow();
    54 //        if(jwtService.isTokenValid(refreshTokenRequest.getToken(), user)) {
    55 //            var jwt = jwtService.generateToken(user);
    56 //
    57 //            JwtAuthenticationResponse jwtAuthenticationResponse = new JwtAuthenticationResponse();
    58 //            jwtAuthenticationResponse.setToken(jwt);
    59 //            jwtAuthenticationResponse.setRefreshToken(refreshTokenRequest.getToken());
    60 //            return jwtAuthenticationResponse;
    61 //        }
    62 //        return null;
    63 //    }
    6411}
  • jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/service/intef/JwtService.java

    rd8b6c91 r19398ad  
    66
    77public interface JwtService {
    8 
    98    String generateToken(UserDetails userDetails);
     9    String generateRefreshToken(Map<String, Object> extraClaims, UserDetails userDetails);
    1010    String extractUsername(String token);
    1111    boolean isTokenValid(String token, UserDetails userDetails);
    12     String generateRefreshToken(Map<String, Object> extraClaims, UserDetails userDetails);
     12    boolean isTokenExpired(String token);
    1313
    1414}
  • jobvista-backend/src/main/java/mk/ukim/finki/predmeti/internettehnologii/jobvistabackend/service/intef/UserService.java

    rd8b6c91 r19398ad  
    11package mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.service.intef;
    22
    3 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.DTO.SignInDTO;
    4 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.DTO.JwtAuthResponse;
    5 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.DTO.RefreshTokenRequest;
    6 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.JobSeeker;
    73import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.Recruiter;
    8 import mk.ukim.finki.predmeti.internettehnologii.jobvistabackend.models.users.User;
    94
    105public interface UserService {
    11     User registerJobSeeker(JobSeeker jobSeeker);
    12     User registerRecruiter(Recruiter recruiter);
    13     JwtAuthResponse login(SignInDTO signInDTO);
    14     JwtAuthResponse refreshToken(RefreshTokenRequest refreshTokenRequest);
     6
    157}
Note: See TracChangeset for help on using the changeset viewer.