英文:
Api endpoint that edits one element in list
问题
以下是翻译好的部分:
我目前正在构建一个 REST API,允许用户输入食谱并对其进行描述。我正在使用 Spring Boot 作为后端,AngularJS 作为前端。
这是 Spring Boot 的食谱文件:
// 省略部分导入和注释
@Entity // 这告诉 Hibernate 将此类创建为表
public class Recipe {
// 构造函数和属性
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
private String description;
private String type;
private Integer preptime;
// 其他属性省略
// Getter 和 Setter 方法省略
}
我创建了一个端点,用户可以在 recipes/edit/{id}
端点中编辑整个食谱。用户可以编辑食谱的名称、描述、内容等。端点如下:
@PutMapping("/recipes/edit/{id}")
void updateRecipe(@PathVariable int id, @RequestBody Recipe recipe ) {
// 更新逻辑
}
现在,我只想创建一个仅用于重命名食谱名称的端点。这个 PutMapping
应该接受一个列表作为输入,然后仅重命名食谱的名称:
@PutMapping("/recipes/rename")
public List<Recipe> renameRecipes(@RequestBody List<Recipe> recipes) {
// 重命名逻辑
}
以上是您所提供内容的翻译。如果您有进一步的问题或需要更多帮助,请随时提问。
英文:
I am currently building a rest api that lets the user enter a recipe and describe it. I am using spring-boot as backend and angularjs as frontend.
This is springboot recipe file
package com.example.springboot;
import com.example.springboot.Recipe;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@Entity // This tells Hibernate to make a table out of this class
public class Recipe {
public Recipe(){
}
public Recipe(Integer id, String name, String description, String type, Integer preptime, Integer cooktime, String content, Integer difficulty){
this.id = id;
this.name = name;
this.description = description;
this.type = type;
this.preptime = preptimee;
this.cooktime = cooktime;
this.content = content;
this.difficulty = difficulty;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
private String description;
private String type;
private Integer preptime;
@Column(columnDefinition = "TEXT")
private String content;
private Integer difficulty;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Integer getDifficulty() {
return difficulty;
}
public void setDifficulty(Integer difficulty) {
this.difficulty = difficulty;
}
public Integer getpreptime {
return preptime;
}
public void setpreptime(Integer preptime) {
this.preptime = preptime;
}
}
I created an Endpoint where the user can edit the whole recipe. The user can edit the name , description, content and so on in the recipes/edit/{id} endpoint.
The Endpoint looks like this.
@PutMapping("/recipes/edit/{id}")
void updateRecipe(@PathVariable int id, @RequestBody Recipe recipe ) {
System.out.println("entering");
Recipe recipe_ = recipeRepository.findById(id).get();
recipe_.setName(recipe.getName());
recipe_.setDescription(recipe.getDescription());
recipe_.setType(recipe.getType());
recipe_.setpreptime(recipe.getpreptime());
recipe_.setContent(recipe.getContent());
System.out.println("entering " + recipe.getTitle());
System.out.println("entering" + recipe.getType());
System.out.println("entering" + recipe.getDescription());
System.out.println("adding");
recipeRepository.save(recipe_);
}
Now I just want to create an Endpoint which only serves the purpose for renaming the name of the recipe. This putmapping should accept a list as its input then only rename the name of the recipe.
@PutMapping("/recipes/rename")
public List<Recipe> {
System.out.println("entering renaming");
// recipe_.setName(recipe.getName()); ?
}
I don't know how I can implement this. This is what I have come up with so far. An endpoint which takes a list as a parameter.
This is the service.ts file that updates the Recipes in the edit function
service.ts:
updateRecipe (id: number, recipe: any): Observable<any > {
const url = `${this.usersUrl}/edit/${id}`;
return this.http.put(url ,recipe);
}
where the updateRecipe gets called:
save(): void {
const id = +this.route.snapshot.paramMap.get('name');
this.recipeService.updateRecipe2(id, this.recipes)
.subscribe(() => this.gotoUserList());
}
This implementation work , I don't know how I can get it work or how I can rewrite the functions so that it can update only the name of the recipe and not the whole file.
Could someone help me?
答案1
得分: 0
Your update the name
method should look like that:
@PutMapping("...{id}")
public void updateName(@PathVariable Integer id, @RequestParam String name){
Recipe recipe = repository.findById(id).orElseThrow(...);
recipe.setName(name);
}
if you want to rename list of recipes
public void renameRecipes(String oldName, String newName){
repository.findByName(oldName)
.forEach(r -> r.setName(newName));
}
@PutMapping("recipes/rename")
public void updateNames(@PequestParam String oldName, @RequestParam String newName){
renameRecipes(oldName, newName);
}
Try that.
英文:
Your update the name
method should look like that:
@PutMapping("...{id}")
public void updateName(@PathVariable Integer id, @RequestParam String name){
Recipe recipe = repository.findById(id).orElseThrow(...);
recipe.setName(name);
}
if you want to rename list of recipes
public void renameRecipes(String oldName, String newName){
repository.findByName(oldName)
.forEach(r -> r.setName(newName));
}
@PutMapping("recipes/rename")
public void updateNames(@PequestParam String oldName, @RequestParam String newName){
renameRecipes(oldName, newName);
}
Try that.
专注分享java语言的经验与见解,让所有开发者获益!
评论