Don't duplicated updates activities
This commit is contained in:
parent
769e05ee0e
commit
ecb9e1f540
3 changed files with 186 additions and 24 deletions
|
|
@ -307,20 +307,21 @@ public class ActivityController {
|
|||
|
||||
UUID userId = getUserId(userDetails);
|
||||
|
||||
Activity activity = fitFileService.getActivity(id, userId);
|
||||
if (activity == null) {
|
||||
try {
|
||||
Activity updated = fitFileService.updateActivity(
|
||||
id,
|
||||
userId,
|
||||
request.getTitle(),
|
||||
request.getDescription(),
|
||||
request.getVisibility()
|
||||
);
|
||||
|
||||
ActivityDTO dto = ActivityDTO.fromEntity(updated);
|
||||
return ResponseEntity.ok(dto);
|
||||
} catch (IllegalArgumentException e) {
|
||||
log.warn("Activity update failed: {}", e.getMessage());
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
// Update fields
|
||||
activity.setTitle(request.getTitle());
|
||||
activity.setDescription(request.getDescription());
|
||||
activity.setVisibility(request.getVisibility());
|
||||
|
||||
Activity updated = fitFileService.updateActivity(activity);
|
||||
|
||||
ActivityDTO dto = ActivityDTO.fromEntity(updated);
|
||||
return ResponseEntity.ok(dto);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -382,25 +382,26 @@ public class FitFileService {
|
|||
/**
|
||||
* Update an existing activity's metadata.
|
||||
*
|
||||
* @param activity the activity with updated fields
|
||||
* @param activityId the activity ID
|
||||
* @param userId the user ID (for authorization)
|
||||
* @param title new title
|
||||
* @param description new description
|
||||
* @param visibility new visibility
|
||||
* @return the updated activity
|
||||
* @throws IllegalArgumentException if activity doesn't exist or user doesn't own it
|
||||
*/
|
||||
@Transactional
|
||||
public Activity updateActivity(Activity activity) {
|
||||
// Verify activity exists and belongs to the user
|
||||
Activity existing = activityRepository.findById(activity.getId())
|
||||
.orElseThrow(() -> new IllegalArgumentException("Activity not found: " + activity.getId()));
|
||||
|
||||
if (!existing.getUserId().equals(activity.getUserId())) {
|
||||
throw new IllegalArgumentException("User does not own this activity");
|
||||
}
|
||||
public Activity updateActivity(UUID activityId, UUID userId, String title, String description, Activity.Visibility visibility) {
|
||||
// Fetch the existing activity within the transaction
|
||||
Activity existing = activityRepository.findByIdAndUserId(activityId, userId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Activity not found or user does not own it: " + activityId));
|
||||
|
||||
// Update allowed fields
|
||||
existing.setTitle(activity.getTitle());
|
||||
existing.setDescription(activity.getDescription());
|
||||
existing.setVisibility(activity.getVisibility());
|
||||
existing.setTitle(title);
|
||||
existing.setDescription(description);
|
||||
existing.setVisibility(visibility);
|
||||
|
||||
// Save will UPDATE because the entity is already managed by the persistence context
|
||||
return activityRepository.save(existing);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue