|
1 | 1 | package de.tum.cit.aet.analysis.web; |
2 | 2 |
|
3 | | -import de.tum.cit.aet.analysis.domain.CqiWeightConfiguration; |
4 | | -import de.tum.cit.aet.analysis.repository.CqiWeightConfigurationRepository; |
5 | | -import de.tum.cit.aet.analysis.service.cqi.CQIConfig; |
| 3 | +import de.tum.cit.aet.analysis.dto.cqi.CqiWeightsDTO; |
| 4 | +import de.tum.cit.aet.analysis.service.cqi.CqiWeightService; |
6 | 5 | import lombok.RequiredArgsConstructor; |
7 | 6 | import lombok.extern.slf4j.Slf4j; |
8 | 7 | import org.springframework.http.ResponseEntity; |
9 | | -import org.springframework.transaction.annotation.Transactional; |
10 | 8 | import org.springframework.web.bind.annotation.*; |
11 | 9 |
|
| 10 | +/** |
| 11 | + * REST controller for per-exercise CQI weight configuration. |
| 12 | + */ |
12 | 13 | @RestController |
13 | 14 | @RequestMapping("/api/exercises/{exerciseId}/cqi-weights") |
14 | 15 | @Slf4j |
15 | 16 | @RequiredArgsConstructor |
16 | 17 | public class CqiWeightResource { |
17 | 18 |
|
18 | | - private final CqiWeightConfigurationRepository weightConfigRepository; |
19 | | - private final CQIConfig cqiConfig; |
20 | | - |
21 | | - public record CqiWeightsDTO( |
22 | | - double effortBalance, |
23 | | - double locBalance, |
24 | | - double temporalSpread, |
25 | | - double ownershipSpread, |
26 | | - Boolean isDefault |
27 | | - ) {} |
| 19 | + private final CqiWeightService cqiWeightService; |
28 | 20 |
|
29 | 21 | /** |
30 | | - * Get the CQI weights for an exercise, falling back to defaults if none are configured. |
| 22 | + * Returns the CQI weights for an exercise, falling back to defaults if none are configured. |
31 | 23 | * |
32 | 24 | * @param exerciseId the exercise ID |
33 | | - * @return the current weights configuration |
| 25 | + * @return the current weights |
34 | 26 | */ |
35 | 27 | @GetMapping |
36 | 28 | public ResponseEntity<CqiWeightsDTO> getWeights(@PathVariable Long exerciseId) { |
37 | | - return weightConfigRepository.findByExerciseId(exerciseId) |
38 | | - .map(config -> ResponseEntity.ok(new CqiWeightsDTO( |
39 | | - config.getEffortWeight(), config.getLocWeight(), |
40 | | - config.getTemporalWeight(), config.getOwnershipWeight(), false))) |
41 | | - .orElseGet(() -> { |
42 | | - CQIConfig.Weights w = cqiConfig.getWeights(); |
43 | | - return ResponseEntity.ok(new CqiWeightsDTO( |
44 | | - w.getEffort(), w.getLoc(), w.getTemporal(), w.getOwnership(), true)); |
45 | | - }); |
| 29 | + log.info("GET cqi-weights for exerciseId={}", exerciseId); |
| 30 | + return ResponseEntity.ok(cqiWeightService.getWeights(exerciseId)); |
46 | 31 | } |
47 | 32 |
|
48 | 33 | /** |
49 | | - * Save custom CQI weights for an exercise. Weights must sum to 1.0 and be non-negative. |
| 34 | + * Saves custom CQI weights for an exercise. |
50 | 35 | * |
51 | 36 | * @param exerciseId the exercise ID |
52 | | - * @param request the weights to save |
53 | | - * @return the saved weights configuration |
| 37 | + * @param request the weights to save |
| 38 | + * @return the saved weights |
54 | 39 | */ |
55 | 40 | @PutMapping |
56 | | - @Transactional |
57 | | - public ResponseEntity<?> saveWeights( |
58 | | - @PathVariable Long exerciseId, |
59 | | - @RequestBody CqiWeightsDTO request) { |
60 | | - |
61 | | - if (request.effortBalance() < 0 || request.locBalance() < 0 |
62 | | - || request.temporalSpread() < 0 || request.ownershipSpread() < 0) { |
63 | | - return ResponseEntity.badRequest().body("All weights must be non-negative"); |
| 41 | + public ResponseEntity<?> saveWeights(@PathVariable Long exerciseId, @RequestBody CqiWeightsDTO request) { |
| 42 | + log.info("PUT cqi-weights for exerciseId={}", exerciseId); |
| 43 | + try { |
| 44 | + return ResponseEntity.ok(cqiWeightService.saveWeights(exerciseId, request)); |
| 45 | + } catch (IllegalArgumentException e) { |
| 46 | + return ResponseEntity.badRequest().body(e.getMessage()); |
64 | 47 | } |
65 | | - double sum = request.effortBalance() + request.locBalance() |
66 | | - + request.temporalSpread() + request.ownershipSpread(); |
67 | | - if (Math.abs(sum - 1.0) >= 0.001) { |
68 | | - return ResponseEntity.badRequest().body("Weights must sum to 100% (got " + Math.round(sum * 100) + "%)"); |
69 | | - } |
70 | | - |
71 | | - CqiWeightConfiguration config = weightConfigRepository.findByExerciseId(exerciseId) |
72 | | - .orElseGet(() -> new CqiWeightConfiguration(exerciseId, |
73 | | - request.effortBalance(), request.locBalance(), |
74 | | - request.temporalSpread(), request.ownershipSpread())); |
75 | | - config.setEffortWeight(request.effortBalance()); |
76 | | - config.setLocWeight(request.locBalance()); |
77 | | - config.setTemporalWeight(request.temporalSpread()); |
78 | | - config.setOwnershipWeight(request.ownershipSpread()); |
79 | | - weightConfigRepository.save(config); |
80 | | - |
81 | | - log.info("Saved CQI weights for exercise {}: effort={}, loc={}, temporal={}, ownership={}", |
82 | | - exerciseId, config.getEffortWeight(), config.getLocWeight(), |
83 | | - config.getTemporalWeight(), config.getOwnershipWeight()); |
84 | | - |
85 | | - return ResponseEntity.ok(new CqiWeightsDTO( |
86 | | - config.getEffortWeight(), config.getLocWeight(), |
87 | | - config.getTemporalWeight(), config.getOwnershipWeight(), false)); |
88 | 48 | } |
89 | 49 |
|
90 | 50 | /** |
91 | | - * Reset CQI weights for an exercise back to the application defaults. |
| 51 | + * Resets CQI weights for an exercise back to application defaults. |
92 | 52 | * |
93 | 53 | * @param exerciseId the exercise ID |
94 | | - * @return the default weights configuration |
| 54 | + * @return the default weights |
95 | 55 | */ |
96 | 56 | @DeleteMapping |
97 | | - @Transactional |
98 | 57 | public ResponseEntity<CqiWeightsDTO> resetWeights(@PathVariable Long exerciseId) { |
99 | | - weightConfigRepository.deleteByExerciseId(exerciseId); |
100 | | - CQIConfig.Weights w = cqiConfig.getWeights(); |
101 | | - |
102 | | - log.info("Reset CQI weights to defaults for exercise {}", exerciseId); |
103 | | - |
104 | | - return ResponseEntity.ok(new CqiWeightsDTO( |
105 | | - w.getEffort(), w.getLoc(), w.getTemporal(), w.getOwnership(), true)); |
| 58 | + log.info("DELETE cqi-weights for exerciseId={}", exerciseId); |
| 59 | + return ResponseEntity.ok(cqiWeightService.resetWeights(exerciseId)); |
106 | 60 | } |
107 | 61 | } |
0 commit comments