Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
Expand Down Expand Up @@ -56,6 +57,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.csrf(AbstractHttpConfigurer::disable)
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(auth -> auth
.requestMatchers(HttpMethod.GET, "/api/resources", "/api/resources/**").permitAll()
.requestMatchers(publicEndpoints).permitAll()
.anyRequest().authenticated()
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.thughari.jobtrackerpro.controller;

import com.thughari.jobtrackerpro.dto.CareerResourceDTO;
import com.thughari.jobtrackerpro.dto.CareerResourcePageResponse;
import com.thughari.jobtrackerpro.dto.CreateCareerResourceRequest;
import com.thughari.jobtrackerpro.dto.UpdateCareerResourceRequest;
import com.thughari.jobtrackerpro.service.CareerResourceService;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;
import java.util.List;

@RestController
@RequestMapping("/api/resources")
public class CareerResourceController {

private final CareerResourceService careerResourceService;

public CareerResourceController(CareerResourceService careerResourceService) {
this.careerResourceService = careerResourceService;
}

@GetMapping
public ResponseEntity<CareerResourcePageResponse> getResources(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size,
@RequestParam(required = false) String query,
@RequestParam(required = false) String category,
@RequestParam(required = false) String type
) {
return ResponseEntity.ok(careerResourceService.getResourcePage(page, size, query, category, type, getAuthenticatedEmailOrNull()));
}


@GetMapping("/categories")
public ResponseEntity<List<String>> getCategories() {
return ResponseEntity.ok(careerResourceService.getAllCategories());
}

@PostMapping
public ResponseEntity<CareerResourceDTO> addResource(@RequestBody CreateCareerResourceRequest request) {
String email = getAuthenticatedEmail();
return ResponseEntity.ok(careerResourceService.createResource(email, request));
}

@GetMapping("/mine")
public ResponseEntity<List<CareerResourceDTO>> getMyResources() {
String email = getAuthenticatedEmail();
return ResponseEntity.ok(careerResourceService.getMyResources(email));
}

@PutMapping("/{id}")
public ResponseEntity<CareerResourceDTO> updateResource(@PathVariable UUID id,
@RequestBody UpdateCareerResourceRequest request) {
String email = getAuthenticatedEmail();
return ResponseEntity.ok(careerResourceService.updateResource(email, id, request));
}

@PostMapping(path = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<CareerResourceDTO> uploadResource(
@RequestParam String title,
@RequestParam String category,
@RequestParam(required = false) String description,
@RequestParam MultipartFile file
) {
String email = getAuthenticatedEmail();
return ResponseEntity.ok(careerResourceService.createResourceFromFile(email, title, category, description, file));
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteResource(@PathVariable UUID id) {
String email = getAuthenticatedEmail();
careerResourceService.deleteResource(email, id);
return ResponseEntity.noContent().build();
}

private String getAuthenticatedEmail() {
return ((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).toLowerCase();
}

private String getAuthenticatedEmailOrNull() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || !authentication.isAuthenticated() || authentication instanceof AnonymousAuthenticationToken) {
return null;
}

Object principal = authentication.getPrincipal();
if (principal instanceof String email && !"anonymousUser".equalsIgnoreCase(email)) {
return email.toLowerCase();
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.thughari.jobtrackerpro.dto;

import lombok.Data;

import java.time.LocalDateTime;
import java.util.UUID;

@Data
public class CareerResourceDTO {
private UUID id;
private String title;
private String url;
private String category;
private String description;
private String resourceType;
private String originalFileName;
private Long fileSizeBytes;
private boolean ownedByCurrentUser;
private String submittedByName;
private LocalDateTime createdAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.thughari.jobtrackerpro.dto;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.List;

@Data
@AllArgsConstructor
public class CareerResourcePageResponse {
private List<CareerResourceDTO> content;
private int page;
private int size;
private long totalElements;
private int totalPages;
private boolean hasNext;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.thughari.jobtrackerpro.dto;

import lombok.Data;

@Data
public class CreateCareerResourceRequest {
private String title;
private String url;
private String category;
private String description;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.thughari.jobtrackerpro.dto;

import lombok.Data;

@Data
public class UpdateCareerResourceRequest {
private String title;
private String url;
private String category;
private String description;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.thughari.jobtrackerpro.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Table;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.UUID;

@Data
@Entity
@Table(name = "career_resources")
public class CareerResource {

@Id
@GeneratedValue(strategy = GenerationType.UUID)
@Column(columnDefinition = "uuid")
private UUID id;

@Column(nullable = false, length = 180)
private String title;

@Column(nullable = false, length = 2048)
private String url;

@Column(nullable = false, length = 16)
private String resourceType = "LINK";

@Column(nullable = false, length = 80)
private String category;

@Column(length = 1200)
private String description;

@Column(length = 255)
private String originalFileName;

private Long fileSizeBytes;

@Column(nullable = false)
private String submittedByEmail;

@Column(nullable = false)
private String submittedByName;

@Column(nullable = false)
private LocalDateTime createdAt;

@PrePersist
public void prePersist() {
if (createdAt == null) {
createdAt = LocalDateTime.now();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public interface StorageService {

String uploadFile(MultipartFile file, String userId);
String uploadFromUrl(String externalUrl, String userId);
String uploadResourceFile(MultipartFile file, String userId);
void deleteFile(String fileUrl);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.thughari.jobtrackerpro.repo;

import com.thughari.jobtrackerpro.entity.CareerResource;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.UUID;

public interface CareerResourceRepository extends JpaRepository<CareerResource, UUID>, JpaSpecificationExecutor<CareerResource> {
List<CareerResource> findAllByOrderByCreatedAtDesc();

Page<CareerResource> findAllByOrderByCreatedAtDesc(Pageable pageable);

List<CareerResource> findAllBySubmittedByEmailOrderByCreatedAtDesc(String email);

@Query("select distinct r.category from CareerResource r where r.category is not null and trim(r.category) <> '' order by r.category asc")
List<String> findDistinctCategories();

boolean existsByUrl(String url);
}
Loading