auth-server/src/main/java/com/stktrk/app/application/profile/ProfileController.java

75 lines
2.2 KiB
Java

package com.stktrk.app.application.profile;
import com.stktrk.app.domain.profile.ProfileService;
import com.stktrk.app.domain.profile.types.Profile;
import jakarta.annotation.Nonnull;
import jakarta.validation.Valid;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import java.security.InvalidKeyException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("profile")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class ProfileController {
@Autowired
@Nonnull
private final ProfileService profileService;
// TODO figure out how to handle the exception.
@Nonnull
@GetMapping("/")
public List<?> findAll() throws InvalidKeyException {
return profileService.findAll();
}
@PostMapping ("/")
public void addProfile (@Nonnull @Valid @RequestBody Profile profile){
profileService.addProfile(profile);
}
@GetMapping("/_delete")
@Nonnull
public ResponseEntity<String> delete() {
profileService.deleteAll ();
return ResponseEntity.status(HttpStatus.OK)
.body("Deleted all Profiles");
}
@GetMapping ("/_fake")
@Nonnull
public ResponseEntity<String> _fake() {
profileService.createFakeProfile();
return ResponseEntity.status(HttpStatus.OK)
.body("Created Profile");
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
@Nonnull
public Map<String, String> handleValidationExceptions(
@Nonnull MethodArgumentNotValidException ex) {
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getAllErrors().forEach((error) -> {
String fieldName = ((FieldError) error).getField();
String errorMessage = error.getDefaultMessage();
errors.put(fieldName, errorMessage);
});
return errors;
}
}