Skip to content

Commit

Permalink
added document-upload-support to volunteers
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-urner committed Apr 3, 2024
1 parent 83ebcf1 commit 3853770
Show file tree
Hide file tree
Showing 16 changed files with 162 additions and 176 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@
<artifactId>spring-boot-docker-compose</artifactId>
</dependency>

<!-- Open API -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.5.0</version>
</dependency>

</dependencies>


Expand Down
24 changes: 24 additions & 0 deletions src/main/java/dev/urner/volodb/configuration/OpenApiConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.urner.volodb.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;

@Configuration
public class OpenApiConfig {

@Bean
public OpenAPI api() {
return new OpenAPI()
.info(
new Info()
.title("VoloDB API")
.version("v0.0.1")
.license(new License()
.name("MIT License")
.url("https://opensource.org/licenses/MIT")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class VolunteerDocument {
private VolunteerDocumentType documentType;

@Column(name = "size")
private int size;
private Long size;

@Column(name = "path")
private String path;
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/dev/urner/volodb/rest/AuthRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

import com.auth0.jwt.exceptions.TokenExpiredException;

import dev.urner.volodb.configuration.MinioConfig;
import dev.urner.volodb.entity.User;
import dev.urner.volodb.entity.UserNotFoundException;
import dev.urner.volodb.model.LoginRequest;
import dev.urner.volodb.model.LoginResponse;
import dev.urner.volodb.security.JwtIssuer;
import dev.urner.volodb.security.UserPrincipal;
import dev.urner.volodb.service.UserService;
import io.minio.MinioClient;
import lombok.RequiredArgsConstructor;

import org.springframework.http.HttpStatus;
Expand All @@ -28,7 +26,6 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@RestController
@RequiredArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.springframework.web.bind.annotation.PathVariable;

@RestController
@RequestMapping("/api/v1/contracts")
@RequestMapping("/contracts")
@RequiredArgsConstructor
public class ContractRestController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.springframework.web.bind.annotation.PathVariable;

@RestController
@RequestMapping("api/v1/files")
@RequestMapping("/files")
@RequiredArgsConstructor
public class FileRestController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
import java.util.Map;

@RestController
@RequestMapping("/api/v1/projects")
@RequestMapping("/projects")
@RequiredArgsConstructor
public class ProjectRestController {

private final ProjectService projectService;

// expose "/project" and return a list of volunteers
@GetMapping("")
@GetMapping
public List<Project> findAll() {
return projectService.findAll();
}
Expand All @@ -39,7 +39,7 @@ public Project getProjectById(@PathVariable int projectId) {
return theProject;
}

@PostMapping("")
@PostMapping
public Project postProject(@RequestBody Project newProject) {
newProject.setId(0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.springframework.web.bind.annotation.PathVariable;

@RestController
@RequestMapping("/api/v1/documents")
@RequestMapping("/documents")
@RequiredArgsConstructor
public class VolunteerDocumentRestController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/api/v1/documents/types")
@RequestMapping("/documents/types")
@RequiredArgsConstructor
public class VolunteerDocumentTypeRestController {

private final VolunteerDocumentTypeService volunteerDocumentTypeService;

@GetMapping("")
@GetMapping
public List<VolunteerDocumentType> getAll() {
return volunteerDocumentTypeService.findAll();
}
Expand All @@ -34,7 +34,7 @@ public VolunteerDocumentType getById(@PathVariable int documentTypeId) {
return volunteerDocumentTypeService.findById(documentTypeId);
}

@PostMapping("")
@PostMapping
public VolunteerDocumentType postNewVolunteerDocumentType(@RequestBody VolunteerDocumentType documentType) {
return volunteerDocumentTypeService.save(documentType);
}
Expand Down

This file was deleted.

This file was deleted.

62 changes: 58 additions & 4 deletions src/main/java/dev/urner/volodb/rest/VolunteerRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,39 @@

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;

import dev.urner.volodb.entity.Volunteer;
import dev.urner.volodb.entity.VolunteerDocument;
import dev.urner.volodb.entity.VolunteerInvalidFormatException;
import dev.urner.volodb.entity.VolunteerNotFoundException;
import dev.urner.volodb.entity.VolunteerNote;
import dev.urner.volodb.security.UserPrincipal;
import dev.urner.volodb.service.VolunteerNoteService;
import dev.urner.volodb.service.VolunteerService;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Map;
import java.time.LocalDateTime;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/api/v1/volunteers")
@RequestMapping("/volunteers")
@RequiredArgsConstructor
public class VolunteerRestController {

private final VolunteerService volunteerService;
private final VolunteerNoteService volunteerNoteService;

// expose "/volunteers" and return a list of volunteers
@GetMapping("")
@GetMapping
public List<Volunteer> findAll() {
return volunteerService.findAll();
}
Expand All @@ -45,7 +54,7 @@ public Volunteer getVolunteer(@PathVariable int volunteerId) throws JsonMappingE

// add mapping for POST /volunteers - add new volunteer

@PostMapping("")
@PostMapping
public Volunteer addVolunteer(@RequestBody Volunteer theVolunteer) {

// also just in case they pass an id in JSON ... set id to 0
Expand All @@ -59,7 +68,7 @@ public Volunteer addVolunteer(@RequestBody Volunteer theVolunteer) {
}

// add mapping for PUT /volunteers - update existing volunteer
@PutMapping("")
@PutMapping
public Volunteer updateVolunteer(@RequestBody Volunteer theVolunteer) {
theVolunteer.setId(0);

Expand Down Expand Up @@ -89,11 +98,56 @@ public String deleteVolunteer(@PathVariable int volunteerId) {
return "Deleted volunteer id - " + volunteerId;
}

// Avatar

@PatchMapping("/{volunteerId}/avatar")
public String setVolunteerAvatar(@PathVariable int volunteerId, @RequestParam MultipartFile avatar) {
return volunteerService.SetAvatar(avatar, volunteerId);
}

// Volunteer-Notes:

// GET all notes BY VoloID
@GetMapping("/{volunteerId}/notes")
public List<VolunteerNote> getAllNotesFromVolo(@PathVariable int volunteerId) {
return volunteerNoteService.findAllByVolunteerId(volunteerId);
}

// POST note BY VoloID
@PostMapping("/{volunteerId}/notes")
public VolunteerNote postNewNote(@PathVariable int volunteerId, @RequestBody VolunteerNote note,
@AuthenticationPrincipal UserPrincipal principal) {
note.setVolunteerId(volunteerId);
note.setTimestamp(LocalDateTime.now());
note.setUsername(principal.getUsername());
return volunteerNoteService.save(note, principal.getUsername());
}

// PATCH
@PatchMapping("/{volunteerId}/notes/{noteId}")
public VolunteerNote updateVolunteerNote(@RequestBody Map<String, Object> fields, @PathVariable int volunteerId,
@PathVariable int noteId) {
return volunteerNoteService.update(noteId, fields);
}

// DELTE BY NoteID
@DeleteMapping("/{volunteerId}/notes/{noteId}")
public String deleteByNoteId(@PathVariable int volunteerId, @PathVariable int noteId,
@AuthenticationPrincipal UserPrincipal principal) {
volunteerNoteService.deleteById(noteId, principal.getUsername());
return "Volunteer-Note with Id '" + noteId + "' deleted.";
}

@PostMapping("/{volunteerId}/documents")
public VolunteerDocument postNewDocument(@PathVariable int volunteerId, @RequestParam MultipartFile document,
@RequestParam int documentTypeId, @AuthenticationPrincipal UserPrincipal principal) {
return volunteerService.saveDocument(document, documentTypeId, volunteerId, principal.getUsername());
}

// **********************************
// Exceptionhandler:
// **********************************

@ExceptionHandler
public ResponseEntity<VolunteerErrorResponse> handleException(VolunteerNotFoundException exc) {
VolunteerErrorResponse error = new VolunteerErrorResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ SecurityFilterChain applicationSecurity(HttpSecurity http) throws Exception {
.authorizeHttpRequests(registry -> registry
.requestMatchers("/auth/login").permitAll()
.requestMatchers("/error").permitAll()
.requestMatchers("/api/v1/files/**").permitAll()
.requestMatchers("/docs/**").permitAll()
.anyRequest()
.authenticated());

Expand Down
Loading

0 comments on commit 3853770

Please sign in to comment.