package com.sshmanager.controller; import com.sshmanager.dto.ConnectionCreateRequest; import com.sshmanager.dto.ConnectionDto; import com.sshmanager.entity.User; import com.sshmanager.repository.UserRepository; import com.sshmanager.service.ConnectionService; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController @RequestMapping("/api/connections") public class ConnectionController { private final ConnectionService connectionService; private final UserRepository userRepository; public ConnectionController(ConnectionService connectionService, UserRepository userRepository) { this.connectionService = connectionService; this.userRepository = userRepository; } private Long getCurrentUserId(Authentication auth) { User user = userRepository.findByUsername(auth.getName()).orElseThrow(() -> new IllegalStateException("User not found")); return user.getId(); } @GetMapping public ResponseEntity> list(Authentication authentication) { Long userId = getCurrentUserId(authentication); return ResponseEntity.ok(connectionService.listByUserId(userId)); } @GetMapping("/{id}") public ResponseEntity get(@PathVariable Long id, Authentication authentication) { Long userId = getCurrentUserId(authentication); return ResponseEntity.ok(connectionService.getById(id, userId)); } @PostMapping public ResponseEntity create(@RequestBody ConnectionCreateRequest request, Authentication authentication) { Long userId = getCurrentUserId(authentication); return ResponseEntity.ok(connectionService.create(request, userId)); } @PutMapping("/{id}") public ResponseEntity update(@PathVariable Long id, @RequestBody ConnectionCreateRequest request, Authentication authentication) { Long userId = getCurrentUserId(authentication); return ResponseEntity.ok(connectionService.update(id, request, userId)); } @DeleteMapping("/{id}") public ResponseEntity> delete(@PathVariable Long id, Authentication authentication) { Long userId = getCurrentUserId(authentication); connectionService.delete(id, userId); Map result = new HashMap<>(); result.put("message", "Deleted"); return ResponseEntity.ok(result); } }