我想编写更少的代码,所以我将创建和更新代码组合到一个方法中,看来它不起作用。
@Post('user')
@Put('user')
async createOrUpdateUser(@Res() res, @Req() req) {
if (req.method == 'POST') {
//do user creating work
} else {
//do user updating work
}
}
我已经尝试过,但是只有@Post
装饰器在这里起作用。
答案 0 :(得分:1)
它似乎并非如此,但是您可以按照以下步骤进行操作,以避免重复。
const response = await fetch(url, {
mode: 'cors',
});
const blob = await response.blob();
答案 1 :(得分:0)
您可以使用public void setLogoPosition(String pos)
{
//_Watermark is ImageView Object
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams) _Watermark.getLayoutParams();
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 0);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
switch (pos) {
case "topleft":
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
break;
case "topright":
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
break;
case "bottomleft":
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
break;
case "bottomright":
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
break;
case "center":
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
break;
case "topcenter":
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
break;
case "bottomcenter":
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
break;
}
_Watermark.setLayoutParams(layoutParams);
}
装饰器在一种方法中处理所有请求方法:
@All
如果在@All('user')
async createOrUpdateUser(@Req() req) {
switch (req.method) {
case 'POST':
return 'new user';
case 'PUT':
return 'updated user';
default:
return throw new NotFoundException();
}
}
处理程序之前 定义一个@Get('user')
处理程序,它将处理get请求。 (我建议不要这样做,请参见下文。)
1)通过使用@All('user')
注入特定于框架的响应对象,您将失去使嵌套如此出色的大多数功能,例如拦截器或序列化。仅在确实需要时这样做。
2)我总是喜欢将通用逻辑提取到方法或类中,而不是在方法中创建条件分支。我认为,这是一种更具可读性和可维护性的方法。它还使其他嵌套集成更加容易,例如使用swagger轻松记录您的API。
@Res
在此示例中,公共部分被提取到@Put('user')
async update(@Body() body) {
const user = await this.service.prepare(body);
return this.service.updateUser(user);
}
@Post('user')
async update(@Body() body) {
const user = await this.service.prepare(body);
return this.service.createUser(user);
}
中。