gpt4 book ai didi

java - 编辑列表中一个元素的 API 端点

转载 作者:行者123 更新时间:2023-12-01 17:35:46 27 4
gpt4 key购买 nike

我目前正在构建一个rest api,让用户输入菜谱并对其进行描述。我使用 spring-boot 作为后端,使用 angularjs 作为前端。

这是 springboot 配方文件

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;
}
}

我创建了一个端点,用户可以在其中编辑整个配方。用户可以在recipes/edit/{id}端点中编辑名称、描述、内容等。端点看起来像这样。

@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_);
}

现在我只想创建一个端点,它仅用于重命名配方名称。此 putmapping 应接受列表作为其输入,然后仅重命名配方的名称。

   @PutMapping("/recipes/rename")
public List<Recipe> {
System.out.println("entering renaming");
// recipe_.setName(recipe.getName()); ?


}

我不知道如何实现这个。这是我到目前为止所想出的。以列表作为参数的端点。

这是在编辑功能中更新Recipes的service.ts文件服务.ts:

updateRecipe (id: number, recipe: any): Observable<any > {
const url = `${this.usersUrl}/edit/${id}`;
return this.http.put(url ,recipe);
}

调用 updateRecipe 的位置:

 save(): void {
const id = +this.route.snapshot.paramMap.get('name');
this.recipeService.updateRecipe2(id, this.recipes)
.subscribe(() => this.gotoUserList());

}

这个实现工作,我不知道如何让它工作,也不知道如何重写函数,以便它只能更新配方的名称而不是整个文件。

有人可以帮助我吗?

最佳答案

您的更新名称方法应如下所示:

@PutMapping("...{id}")
public void updateName(@PathVariable Integer id, @RequestParam String name){
Recipe recipe = repository.findById(id).orElseThrow(...);
recipe.setName(name);
}

如果你想重命名食谱列表

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);
}

尝试一下。

关于java - 编辑列表中一个元素的 API 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61043803/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com