Initial commit: SSH Manager (backend + frontend)

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
liu
2026-02-03 09:10:06 +08:00
commit 1c5a44ff71
63 changed files with 6946 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
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<ConnectionDto>> list(Authentication authentication) {
Long userId = getCurrentUserId(authentication);
return ResponseEntity.ok(connectionService.listByUserId(userId));
}
@GetMapping("/{id}")
public ResponseEntity<ConnectionDto> get(@PathVariable Long id, Authentication authentication) {
Long userId = getCurrentUserId(authentication);
return ResponseEntity.ok(connectionService.getById(id, userId));
}
@PostMapping
public ResponseEntity<ConnectionDto> create(@RequestBody ConnectionCreateRequest request,
Authentication authentication) {
Long userId = getCurrentUserId(authentication);
return ResponseEntity.ok(connectionService.create(request, userId));
}
@PutMapping("/{id}")
public ResponseEntity<ConnectionDto> 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<Map<String, String>> delete(@PathVariable Long id,
Authentication authentication) {
Long userId = getCurrentUserId(authentication);
connectionService.delete(id, userId);
Map<String, String> result = new HashMap<>();
result.put("message", "Deleted");
return ResponseEntity.ok(result);
}
}