Couldnt request by friendcode, because it was in controller and therefore not transactional
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good

This commit is contained in:
2026-03-05 17:26:03 +01:00
parent 85b427f0d5
commit bfcb1597ba
2 changed files with 14 additions and 3 deletions
@@ -73,9 +73,7 @@ public class FriendshipController {
@PostMapping("/request/by-friendcode/{friendCode}")
public ResponseEntity<Void> requestFriendshipByFriendcode(@PathVariable String friendCode) {
User user = currentUserService.get();
User requestedFriend = userRepository.findByFriendCode(friendCode).orElseThrow();
checkSelfAction(user.getId(), requestedFriend.getId());
friendshipService.requestFriendship(user, requestedFriend.getId());
friendshipService.requestFriendshipByFriendcode(user, friendCode);
return ResponseEntity.ok().build();
}
@@ -11,7 +11,9 @@ import SuMoC.backend.user.repository.UserRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.jspecify.annotations.NonNull;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import java.util.ArrayList;
import java.util.List;
@@ -344,6 +346,17 @@ public class FriendshipService {
return friends;
}
public void requestFriendshipByFriendcode(User sender, String friendCode) {
User requestedFriend = userRepository.findByFriendCode(friendCode)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found"));
if (sender.getId().equals(requestedFriend.getId())) {
throw new IllegalArgumentException("Action cannot be performed on oneself.");
}
requestFriendship(sender, requestedFriend.getId());
}
public record NameWithFriendship(
Long id,
String name,