Implemented project handling and updated property components according to front-end needs

This commit is contained in:
2025-09-12 23:41:58 +02:00
parent 5eb6b6e738
commit 8b6083c1c0
9 changed files with 576 additions and 7 deletions

View File

@@ -0,0 +1,89 @@
package de.iwomm.propify_api.controller;
import de.iwomm.propify_api.dto.ProjectDTO;
import de.iwomm.propify_api.dto.ProjectStatsDTO;
import de.iwomm.propify_api.dto.PropertyDTO;
import de.iwomm.propify_api.entity.Project;
import de.iwomm.propify_api.entity.Property;
import de.iwomm.propify_api.service.ProjectService;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import java.net.URI;
import java.util.List;
import java.util.UUID;
@RestController
@RequestMapping("/api/v1/projects")
public class ProjectController {
private ProjectService projectService;
public ProjectController(ProjectService projectService) {
this.projectService = projectService;
}
@GetMapping
@PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_USER')")
public ResponseEntity<?> getAll() {
List<ProjectDTO> projectDTOs = projectService.toDTOs(projectService.findAll());
return ResponseEntity
.ok(projectDTOs);
}
@GetMapping("/{id}")
@PreAuthorize("hasAnyRole('ADMIN', 'USER')")
public ResponseEntity<?> getById(@PathVariable UUID id) {
try {
ProjectDTO projectDTO = projectService.toDTO(projectService.findById(id).orElseThrow(EntityNotFoundException::new));
return ResponseEntity
.ok(projectDTO);
} catch (EntityNotFoundException e) {
return ResponseEntity
.notFound()
.build();
} catch (Exception e) {
return ResponseEntity
.internalServerError()
.build();
}
}
@GetMapping("/stats")
@PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_USER')")
public ResponseEntity<?> getProjectStats() {
ProjectStatsDTO projectStatsDTO = this.projectService.getStatsDTO();
List<ProjectDTO> projectDTOs = projectService.toDTOs(projectService.findAll());
return ResponseEntity
.ok(projectStatsDTO);
}
@PostMapping
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<?> create(@RequestBody ProjectDTO projectDTO) {
try {
Project newItem = projectService.save(projectDTO);
URI location = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(newItem.getId())
.toUri();
return ResponseEntity
.created(location)
.body(newItem);
} catch (Exception e) {
return ResponseEntity
.internalServerError()
.build();
}
}
}

View File

@@ -35,7 +35,7 @@ public class PropertyController {
@GetMapping
@PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_USER')")
public ResponseEntity<?> getAllProperties() {
public ResponseEntity<?> getAll() {
List<PropertyDTO> propertiesDTO = propertyService.toDTOs(propertyService.findAll());
return ResponseEntity
@@ -44,7 +44,7 @@ public class PropertyController {
@GetMapping("/{id}")
@PreAuthorize("hasAnyRole('ADMIN', 'USER')")
public ResponseEntity<?> getPropertyById(@PathVariable UUID id) {
public ResponseEntity<?> getById(@PathVariable UUID id) {
try {
return ResponseEntity
.ok(propertyService.findById(id).orElseThrow(EntityNotFoundException::new));
@@ -107,7 +107,7 @@ public class PropertyController {
@PostMapping
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<?> createProperty(@RequestBody PropertyDTO propertyDTO) {
public ResponseEntity<?> create(@RequestBody PropertyDTO propertyDTO) {
try {
Property newItem = propertyService.saveDTO(propertyDTO);
@@ -128,7 +128,7 @@ public class PropertyController {
@PatchMapping("/{id}")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<?> updateProperty(@PathVariable UUID id, @RequestBody PropertyDTO propertyDTO) {
public ResponseEntity<?> update(@PathVariable UUID id, @RequestBody PropertyDTO propertyDTO) {
try {
return ResponseEntity
.ok(propertyService.update(id, propertyDTO));
@@ -145,7 +145,7 @@ public class PropertyController {
@DeleteMapping("/{id}")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<?> deleteProperty(@PathVariable UUID id) {
public ResponseEntity<?> delete(@PathVariable UUID id) {
try {
propertyService.deleteById(id);
@@ -165,7 +165,7 @@ public class PropertyController {
@PostMapping("/bulk-delete")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<?> deleteProperties(@RequestBody BulkDeletePropertyIdsDTO propertiesDTO) {
public ResponseEntity<?> deleteMany(@RequestBody BulkDeletePropertyIdsDTO propertiesDTO) {
try {
propertyService.deleteByIds(propertiesDTO);