save updated
This commit is contained in:
@@ -67,7 +67,7 @@ public class RecipeController : ControllerBase
|
||||
}
|
||||
|
||||
[HttpPut("update/{id}")]
|
||||
public async Task<IActionResult> UpdateRecipe(int id, [FromBody] Recipe updatedRecipe)
|
||||
public async Task<IActionResult> UpdateRecipe(int id, [FromBody] RecipeUpdateDto updatedRecipe)
|
||||
{
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
|
||||
@@ -76,7 +76,7 @@ public class RecipeController : ControllerBase
|
||||
|
||||
if (existingRecipe == null)
|
||||
{
|
||||
return NotFound("Recipe not found or you do not have permission to edit it.");
|
||||
return NotFound("Recipe not found or permission denied.");
|
||||
}
|
||||
|
||||
existingRecipe.Title = updatedRecipe.Title;
|
||||
@@ -88,6 +88,9 @@ public class RecipeController : ControllerBase
|
||||
existingRecipe.ImageUrl = updatedRecipe.ImageUrl;
|
||||
}
|
||||
|
||||
var fullText = $"{updatedRecipe.Title} {string.Join(" ", updatedRecipe.Ingredients)} {string.Join(" ", updatedRecipe.Instructions)}";
|
||||
existingRecipe.Embedding = await _recipeService.GetEmbeddingAsync(fullText);
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return Ok(new { message = "Recipe updated successfully!" });
|
||||
|
||||
7
Seasoned.Backend/DTOs/RecipeUpdateDto.cs
Normal file
7
Seasoned.Backend/DTOs/RecipeUpdateDto.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
public class RecipeUpdateDto
|
||||
{
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public List<string> Ingredients { get; set; } = new();
|
||||
public List<string> Instructions { get; set; } = new();
|
||||
public string? ImageUrl { get; set; }
|
||||
}
|
||||
@@ -390,27 +390,28 @@ const closeDetails = () => {
|
||||
|
||||
const saveChanges = async () => {
|
||||
try {
|
||||
const { embedding, ...recipeData } = selectedRecipe.value;
|
||||
const { embedding, user, ...recipeData } = selectedRecipe.value;
|
||||
|
||||
const payload = {
|
||||
...selectedRecipe.value,
|
||||
createdAt: selectedRecipe.value.createdAt || new Date().toISOString(),
|
||||
ingredients: typeof selectedRecipe.value.ingredients === 'string'
|
||||
? selectedRecipe.value.ingredients.split('\n').filter(i => i.trim())
|
||||
: selectedRecipe.value.ingredients,
|
||||
instructions: typeof selectedRecipe.value.instructions === 'string'
|
||||
? selectedRecipe.value.instructions.split('\n').filter(i => i.trim())
|
||||
: selectedRecipe.value.instructions
|
||||
title: recipeData.title,
|
||||
imageUrl: recipeData.imageUrl,
|
||||
ingredients: typeof recipeData.ingredients === 'string'
|
||||
? recipeData.ingredients.split('\n').filter(i => i.trim())
|
||||
: recipeData.ingredients,
|
||||
instructions: typeof recipeData.instructions === 'string'
|
||||
? recipeData.instructions.split('\n').filter(i => i.trim())
|
||||
: recipeData.instructions
|
||||
};
|
||||
|
||||
await $fetch(`${config.public.apiBase}api/recipe/update/${payload.id}`, {
|
||||
await $fetch(`${config.public.apiBase}api/recipe/update/${selectedRecipe.value.id}`, {
|
||||
method: 'PUT',
|
||||
body: payload,
|
||||
credentials: 'include'
|
||||
});
|
||||
|
||||
const index = recipes.value.findIndex(r => r.id === payload.id);
|
||||
const index = recipes.value.findIndex(r => r.id === selectedRecipe.value.id);
|
||||
if (index !== -1) {
|
||||
recipes.value[index] = { ...payload };
|
||||
recipes.value[index] = { ...recipes.value[index], ...payload };
|
||||
}
|
||||
|
||||
closeDetails();
|
||||
|
||||
Reference in New Issue
Block a user