File size: 3,013 Bytes
ec4d503
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.cs102.attendance.controller;

import com.cs102.attendance.entity.TestConnection;
import com.cs102.attendance.repository.TestConnectionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/test")
public class TestController {

    @Autowired
    private TestConnectionRepository testConnectionRepository;

    @GetMapping("/database-operations")
    public ResponseEntity<Map<String, Object>> testDatabaseOperations() {
        Map<String, Object> response = new HashMap<>();
        
        try {
            // Test 1: Simple query
            Integer queryResult = testConnectionRepository.testQuery();
            response.put("simpleQuery", queryResult == 1 ? "PASS" : "FAIL");
            
            // Test 2: Insert operation
            TestConnection testEntity = new TestConnection("Connection test from Spring Boot");
            TestConnection saved = testConnectionRepository.save(testEntity);
            response.put("insertOperation", saved.getId() != null ? "PASS" : "FAIL");
            
            // Test 3: Read operation
            List<TestConnection> allRecords = testConnectionRepository.findAll();
            response.put("readOperation", !allRecords.isEmpty() ? "PASS" : "FAIL");
            response.put("recordCount", allRecords.size());
            
            // Test 4: Delete operation
            testConnectionRepository.deleteById(saved.getId());
            response.put("deleteOperation", "PASS");
            
            response.put("overallStatus", "SUCCESS");
            response.put("message", "All database operations completed successfully");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("overallStatus", "FAILED");
            response.put("error", e.getMessage());
            response.put("errorClass", e.getClass().getSimpleName());
            return ResponseEntity.status(500).body(response);
        }
    }
    
    @PostMapping("/insert-test-data")
    public ResponseEntity<TestConnection> insertTestData(@RequestParam(defaultValue = "Test message") String message) {
        try {
            TestConnection testEntity = new TestConnection(message);
            TestConnection saved = testConnectionRepository.save(testEntity);
            return ResponseEntity.ok(saved);
        } catch (Exception e) {
            return ResponseEntity.status(500).build();
        }
    }
    
    @GetMapping("/all-test-data")
    public ResponseEntity<List<TestConnection>> getAllTestData() {
        try {
            List<TestConnection> allRecords = testConnectionRepository.findAll();
            return ResponseEntity.ok(allRecords);
        } catch (Exception e) {
            return ResponseEntity.status(500).build();
        }
    }
}