Skip to content
Closed
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
@@ -1,16 +1,27 @@
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
Expand All @@ -24,8 +35,14 @@ public CareerResourceController(CareerResourceService careerResourceService) {
}

@GetMapping
public ResponseEntity<List<CareerResourceDTO>> getResources() {
return ResponseEntity.ok(careerResourceService.getAllResources());
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()));
}

@PostMapping
Expand All @@ -34,7 +51,52 @@ public ResponseEntity<CareerResourceDTO> addResource(@RequestBody CreateCareerRe
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
Expand Up @@ -12,6 +12,10 @@ public class CareerResourceDTO {
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 UpdateCareerResourceRequest {
private String title;
private String url;
private String category;
private String description;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,20 @@ public class CareerResource {
@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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

import com.thughari.jobtrackerpro.entity.CareerResource;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
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> {
public interface CareerResourceRepository extends JpaRepository<CareerResource, UUID>, JpaSpecificationExecutor<CareerResource> {
List<CareerResource> findAllByOrderByCreatedAtDesc();

Page<CareerResource> findAllByOrderByCreatedAtDesc(Pageable pageable);

List<CareerResource> findAllBySubmittedByEmailOrderByCreatedAtDesc(String email);

boolean existsByUrl(String url);
}
Loading