Skip to content

Commit d7020c5

Browse files
committed
feat: add create like api POST /users/:address/items/:item_id/likes
1 parent a967322 commit d7020c5

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

Diff for: app/controller/user.js

+19
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,25 @@ class UserController extends Controller {
3939
ctx.body = { address };
4040
}
4141

42+
async createLike() {
43+
const ctx = this.ctx;
44+
45+
const to_address = ctx.params.address;
46+
const item_id = ctx.params.item_id;
47+
const { from_user_id, value, message, referrer } = ctx.request.body;
48+
49+
// TODO: 检查权限 只有本人 或者 管理员 才有权限
50+
// ctx.throw();
51+
52+
ctx.body = await ctx.service.user.createLike({
53+
from_user_id,
54+
to_address,
55+
item_id,
56+
value,
57+
message,
58+
referrer,
59+
});
60+
}
4261
}
4362

4463
module.exports = UserController;

Diff for: app/router.js

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ module.exports = app => {
3737
router.get('/users/:address', controller.user.getUser);
3838
router.patch('/users/:address', controller.user.updateUser);
3939
router.post('/users/:id/wallet', controller.user.createWallet);
40+
// 创建打赏
41+
router.post('/users/:address/items/:item_id/likes', controller.user.createLike);
4042

4143
router.get('/likes', controller.like.list);
4244
router.post('/likes', isUser, controller.like.create);

Diff for: app/service/user.js

+23
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,29 @@ class UserService extends Service {
127127
};
128128
}
129129

130+
// 创建一个打赏
131+
async createLike({ from_user_id, to_address, item_id, value, message, referrer }) {
132+
const ctx = this.ctx;
133+
134+
const from_user = await ctx.model.User.find({ where: { id: from_user_id } });
135+
if (!from_user) {
136+
ctx.throw(400, `用户(id: ${from_user_id})不存在`, { code: 'USER_NOT_FOUND', errors: { from_user_id } });
137+
}
138+
if (!from_user.address || !from_user.private_key) {
139+
ctx.throw(400, `用户(id: ${from_user_id})没有钱包`, { code: 'USER_HAS_NO_WALLET', errors: { from_user_id } });
140+
}
141+
142+
const tx_hash = await ctx.web3.callContract({
143+
value,
144+
method: 'like',
145+
from: from_user.address,
146+
to: ctx.app.config.blockchain.contract_address,
147+
privateKey: from_user.private_key,
148+
args: [ from_user.address, to_address, item_id, message, referrer ],
149+
});
150+
151+
return { tx_hash, tx_link: ctx.app.config.blockchain.etherscan_host + '/tx/' + tx_hash };
152+
}
130153
}
131154

132155
module.exports = UserService;

Diff for: config/config.default.js

+5
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,10 @@ module.exports = appInfo => {
6060
// https://ropsten.etherscan.io/address/0x7e0ca9A3c1Bb74C7FD860453e5292b391866aa41
6161
config.contract = '0x7e0ca9A3c1Bb74C7FD860453e5292b391866aa41';
6262

63+
config.blockchain = {
64+
contract_address: '0xed608ed37a7635a175be1a183bfc87f3b4a69be3',
65+
etherscan_host: 'https://ropsten.etherscan.io',
66+
};
67+
6368
return config;
6469
};

0 commit comments

Comments
 (0)