Skip to content

Commit a3c0966

Browse files
committed
main finish
1 parent fe02a3e commit a3c0966

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

src/main/java/org/ma5d/javamock/controller/MockController.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import jakarta.servlet.http.HttpServletRequest;
55
import org.ma5d.javamock.dto.SaveParam;
66
import org.ma5d.javamock.service.JavaMockServiceImpl;
7-
import org.springframework.beans.factory.annotation.Autowired;
87
import org.springframework.stereotype.Controller;
98
import org.springframework.ui.Model;
109
import org.springframework.web.bind.annotation.*;
@@ -15,10 +14,9 @@
1514
@Controller
1615
public class MockController {
1716
@Resource
18-
JavaMockServiceImpl javaMockService;
19-
@Autowired
20-
private JavaMockServiceImpl javaMockServiceImpl;
17+
private JavaMockServiceImpl javaMockService;
2118

19+
// region 查
2220
@GetMapping("/")
2321
public String index() {
2422
return "redirect:/index";
@@ -30,26 +28,47 @@ public String index(Model model) throws SQLException {
3028
model.addAttribute("saveParams", saveParams);
3129
return "index";
3230
}
31+
// endregion
32+
33+
// region 增
34+
@GetMapping("/new")
35+
public String newItem(Model model) {
36+
SaveParam saveParam = new SaveParam();
37+
model.addAttribute("saveParam", saveParam);
38+
return "detail";
39+
}
3340

3441
@PostMapping("/save")
3542
public String configure(@ModelAttribute("saveParam") SaveParam saveParam) throws SQLException {
3643
javaMockService.saveConfig(saveParam);
3744
return "redirect:/";
3845
}
46+
// endregion
3947

48+
// region 删
49+
@GetMapping("/delete/{id}")
50+
public String delete(@PathVariable String id) throws SQLException {
51+
javaMockService.deleteByTimeStamp(id);
52+
return "redirect:/";
53+
}
54+
// endregion
55+
56+
// region 改
4057
@GetMapping("/detail/{timeStamp}")
4158
public String detail(@PathVariable String timeStamp, Model model) throws SQLException {
4259
SaveParam saveParam = javaMockService.queryLine(timeStamp);
4360
model.addAttribute("saveParam", saveParam);
4461
return "detail";
4562
}
63+
// endregion
4664

65+
// region 主要逻辑
4766
@ResponseBody
4867
@GetMapping("/p/**")
4968
public String proxyGet(HttpServletRequest request) throws SQLException {
5069
String requestURI = request.getRequestURI();
5170
String pathWithParam = "/" + requestURI.split("/p/")[1];
52-
SaveParam saveParam = javaMockServiceImpl.getSaveParamByPathWithParam(pathWithParam);
71+
SaveParam saveParam = javaMockService.getSaveParamByPathWithParam(pathWithParam);
5372
return saveParam.getResponse();
5473
}
5574

@@ -58,8 +77,8 @@ public String proxyGet(HttpServletRequest request) throws SQLException {
5877
public String proxyPost(HttpServletRequest request) throws SQLException {
5978
String requestURI = request.getRequestURI();
6079
String pathWithParam = "/" + requestURI.split("/p/")[1];
61-
SaveParam saveParam = javaMockServiceImpl.getSaveParamByPathWithParam(pathWithParam);
80+
SaveParam saveParam = javaMockService.getSaveParamByPathWithParam(pathWithParam);
6281
return saveParam.getResponse();
6382
}
64-
83+
// endregion
6584
}

src/main/java/org/ma5d/javamock/service/JavaMockServiceImpl.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,25 @@
1313
import java.util.List;
1414

1515
@Service
16+
@SuppressWarnings("UnusedReturnValue")
1617
public class JavaMockServiceImpl {
1718

1819
@Resource
1920
HikariDataSource dataSource;
2021

22+
2123
public boolean saveConfig(SaveParam saveParam) throws SQLException {
2224
Connection connection = dataSource.getConnection();
2325
// language=SQLite
24-
String query = "insert into java_mock(time_stamp, adomain, path_param, response) values(?, ?, ?, ?)";
26+
String query = "replace into java_mock(time_stamp, adomain, path_param, response) values(?, ?, ?, ?)";
2527
PreparedStatement stmt = connection.prepareStatement(query);
2628
stmt.setString(1, saveParam.getTimeStamp());
2729
stmt.setString(2, saveParam.getDomainCom());
2830
stmt.setString(3, saveParam.getPathWithParam());
2931
stmt.setString(4, saveParam.getResponse());
30-
return stmt.execute();
32+
boolean execute = stmt.execute();
33+
connection.close();
34+
return execute;
3135
}
3236

3337
public List<SaveParam> queryAllLines() throws SQLException {
@@ -73,4 +77,15 @@ public SaveParam getSaveParamByPathWithParam(String pathWithParam) throws SQLExc
7377
connection.close();
7478
return saveParam;
7579
}
80+
81+
public boolean deleteByTimeStamp(String id) throws SQLException {
82+
Connection connection = dataSource.getConnection();
83+
// language=SQLite
84+
String query = "delete from java_mock where time_stamp = ?";
85+
PreparedStatement preparedStatement = connection.prepareStatement(query);
86+
preparedStatement.setString(1, id);
87+
boolean execute = preparedStatement.execute();
88+
connection.close();
89+
return execute;
90+
}
7691
}

src/main/resources/templates/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<th>域名</th>
2222
<th>路径及参数</th>
2323
<th>返回数据</th>
24+
<th>删除</th>
2425
</tr>
2526
</thead>
2627
<tbody>
@@ -41,6 +42,12 @@
4142

4243
<td th:text="${#strings.length(saveParam.response) > 100 ? saveParam.response.substring(0, 100) + '...' : saveParam.response}"></td>
4344

45+
<td>
46+
<a th:href="@{http://{domainCom}/delete/{timeStamp}(domainCom=${saveParam.domainCom}, timeStamp=${saveParam.timeStamp})}"
47+
th:text="${saveParam.timeStamp}">
48+
</a>
49+
</td>
50+
4451
</tr>
4552
</tbody>
4653
</table>

0 commit comments

Comments
 (0)