* GET all properties * GET one property by id * CREATE one property * DELETE one property by id
59 lines
1.7 KiB
Java
59 lines
1.7 KiB
Java
package de.iwomm.propify_api.controller;
|
|
|
|
import de.iwomm.propify_api.entity.Property;
|
|
import de.iwomm.propify_api.service.PropertyService;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
import java.util.UUID;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/v1/properties")
|
|
public class PropertyController {
|
|
private final PropertyService propertyService;
|
|
|
|
public PropertyController(PropertyService propertyService) {
|
|
this.propertyService = propertyService;
|
|
}
|
|
|
|
@GetMapping("/info")
|
|
@PreAuthorize("isAuthenticated()")
|
|
public String infoEndpoint() {
|
|
return "Hello, you are authenticated!";
|
|
}
|
|
|
|
@GetMapping
|
|
@PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_USER')")
|
|
public List<Property> getAllProperties() {
|
|
return propertyService.findAll();
|
|
}
|
|
|
|
@GetMapping("/{id}")
|
|
@PreAuthorize("hasAnyRole('ADMIN', 'USER')")
|
|
public Property getPropertyById(@PathVariable UUID id) {
|
|
return propertyService.findById(id).orElse(null);
|
|
}
|
|
|
|
@PostMapping
|
|
@ResponseStatus(HttpStatus.CREATED)
|
|
@PreAuthorize("hasRole('ADMIN')")
|
|
public Property createProperty(@RequestBody Property property) {
|
|
return propertyService.save(property);
|
|
}
|
|
|
|
@DeleteMapping("/{id}")
|
|
@ResponseStatus(HttpStatus.NO_CONTENT)
|
|
@PreAuthorize("hasRole('ADMIN')")
|
|
public void deleteProperty(@PathVariable UUID id) {
|
|
Optional<Property> property = propertyService.findById(id);
|
|
if (property.isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
propertyService.delete(property.get());
|
|
}
|
|
}
|