-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbController.php
More file actions
110 lines (96 loc) · 4.42 KB
/
DbController.php
File metadata and controls
110 lines (96 loc) · 4.42 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
declare(strict_types=1);
// DROP TABLE IF EXISTS `t_user`;
// CREATE TABLE `t_user` (
// `user_id` int(11) NOT NULL AUTO_INCREMENT,
// `account` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
// `nick_name` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
// `password` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
// `phone` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
// `write_time` char(19) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
// PRIMARY KEY (`user_id`) USING BTREE
// ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
// 数据库操作,无非就是各种花式拼接SQL, 就看谁拼的更优雅
// github上优秀的轮子太多了, 没必要重复造轮子
// 所以 phpRest 使用 \Medoo\Medoo
// see https://medoo.in/doc
namespace App\Controller;
use PhpRest2\Controller\Attribute\{Controller, Action, Summary, Param};
use PhpRest2\ApiResult;
use Medoo\Medoo;
use DI\Attribute\Inject;
#[Controller('/db')]
class DbController
{
#[Inject]
private Medoo $db;
/***************************************************************************************
* select 1
**************************************************************************************/
#[Action('GET:/find/{userId}')]
public function getById(int $userId)
{
$row = $this->db->get('t_user', '*', ['user_id' => $userId]);
if ($row === null) return ApiResult::error('记录不存在');
// 把数据库里的下划线规则转成驼峰规则
$row = \App\Utils\Tools::camelizeArrayKey($row);
return ApiResult::success($row);
}
/***************************************************************************************
* select list
**************************************************************************************/
#[Action('GET:/list')]
public function getList(int $page = 1, int $limit = 10)
{
$start = ($page-1) * $limit;
$rows = $this->db->select('t_user', '*', ['LIMIT' => [$start, $limit]]);
$rows = \App\Utils\Tools::camelizeArrayKey($rows);
return ApiResult::success($rows);
}
/***************************************************************************************
* insert
**************************************************************************************/
#[Action('POST:/')]
#[Param(name: 'account', rule: 'slug')]
#[Param(name: 'phone', rule: '/^1[3456789]\d{9}$/')]
#[Param(name: 'password', rule: 'lengthBetween=6,20')]
public function create(string $account, string $phone, string $nickName, string $password)
{
$data = [
'account' => $account,
'phone' => $phone,
'nick_name' => $nickName,
'password' => $password,
'write_time' => date('Y-m-d H:i:s')
];
// https://www.php.net/manual/zh/class.pdostatement.php
$res = $this->db->insert('t_user', $data);
if ($res->rowCount() !== 1) return ApiResult::error('添加失败');
// https://www.php.net/manual/zh/pdo.lastinsertid.php
$autoId = $this->db->id();
return ApiResult::success("autoId = {$autoId}");
}
/***************************************************************************************
* update
**************************************************************************************/
#[Action('PUT:/')]
#[Param(name: 'phone', rule: '/^1[3456789]\d{9}$/')]
public function update(int $userId, string $nickName, string $phone)
{
$this->db->update('t_user', [
'nick_name' => $nickName,
'phone' => $phone
], [ 'user_id' => $userId]);
return ApiResult::success();
}
/***************************************************************************************
* delete
**************************************************************************************/
#[Action('DELETE:/{userId}')]
public function delete(int $userId)
{
$res = $this->db->delete('t_user', ['user_id' => $userId]);
if ($res->rowCount() !== 1) return ApiResult::error('删除失败');
return ApiResult::success();
}
}