save updated
This commit is contained in:
@@ -67,7 +67,7 @@ public class RecipeController : ControllerBase
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("update/{id}")]
|
[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);
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||||
|
|
||||||
@@ -76,7 +76,7 @@ public class RecipeController : ControllerBase
|
|||||||
|
|
||||||
if (existingRecipe == null)
|
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;
|
existingRecipe.Title = updatedRecipe.Title;
|
||||||
@@ -88,6 +88,9 @@ public class RecipeController : ControllerBase
|
|||||||
existingRecipe.ImageUrl = updatedRecipe.ImageUrl;
|
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();
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
return Ok(new { message = "Recipe updated successfully!" });
|
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 () => {
|
const saveChanges = async () => {
|
||||||
try {
|
try {
|
||||||
const { embedding, ...recipeData } = selectedRecipe.value;
|
const { embedding, user, ...recipeData } = selectedRecipe.value;
|
||||||
|
|
||||||
const payload = {
|
const payload = {
|
||||||
...selectedRecipe.value,
|
title: recipeData.title,
|
||||||
createdAt: selectedRecipe.value.createdAt || new Date().toISOString(),
|
imageUrl: recipeData.imageUrl,
|
||||||
ingredients: typeof selectedRecipe.value.ingredients === 'string'
|
ingredients: typeof recipeData.ingredients === 'string'
|
||||||
? selectedRecipe.value.ingredients.split('\n').filter(i => i.trim())
|
? recipeData.ingredients.split('\n').filter(i => i.trim())
|
||||||
: selectedRecipe.value.ingredients,
|
: recipeData.ingredients,
|
||||||
instructions: typeof selectedRecipe.value.instructions === 'string'
|
instructions: typeof recipeData.instructions === 'string'
|
||||||
? selectedRecipe.value.instructions.split('\n').filter(i => i.trim())
|
? recipeData.instructions.split('\n').filter(i => i.trim())
|
||||||
: selectedRecipe.value.instructions
|
: 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',
|
method: 'PUT',
|
||||||
body: payload,
|
body: payload,
|
||||||
credentials: 'include'
|
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) {
|
if (index !== -1) {
|
||||||
recipes.value[index] = { ...payload };
|
recipes.value[index] = { ...recipes.value[index], ...payload };
|
||||||
}
|
}
|
||||||
|
|
||||||
closeDetails();
|
closeDetails();
|
||||||
|
|||||||
Reference in New Issue
Block a user