Merge branch 'feature/Sort-Logic-Instruction' into 'develop'
Feature/sort logic instruction See merge request francesco.damico/francescos.recipes.world!9
This commit is contained in:
@@ -1,6 +1,69 @@
|
|||||||
namespace Francesco.Recipes.World.Controller.Instruction
|
namespace Francesco.Recipes.World.Controller.Instruction
|
||||||
{
|
{
|
||||||
public class InstructionController
|
using Francesco.Recipes.World.Models;
|
||||||
|
using Francesco.Recipes.World.Repositories.Instruction;
|
||||||
|
using Francesco.Recipes.World.Services.Instruction;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
public class InstructionController : Controller
|
||||||
{
|
{
|
||||||
|
private readonly IInstructionService _instructionService;
|
||||||
|
private readonly IInstructionRepository _instructionRepository;
|
||||||
|
|
||||||
|
public InstructionController(IInstructionService instructionService, IInstructionRepository instructionRepository)
|
||||||
|
{
|
||||||
|
_instructionService = instructionService;
|
||||||
|
_instructionRepository = instructionRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("Recipe/{recipeId}/Instruction/{instructionId}/move-up")]
|
||||||
|
[ValidateAntiForgeryToken]
|
||||||
|
public async Task<IActionResult> MoveUp(Guid recipeId, Guid instructionId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _instructionService.MoveInstructionUpAsync(recipeId, instructionId);
|
||||||
|
return Ok(new { Message = "Instruction moved up successfully." });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return BadRequest(new { Error = ex.Message });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("Recipe/{recipeId}/Instruction/{instructionId}/move-down")]
|
||||||
|
[ValidateAntiForgeryToken]
|
||||||
|
public async Task<IActionResult> MoveDown(Guid recipeId, Guid instructionId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _instructionService.MoveInstructionDownAsync(recipeId, instructionId);
|
||||||
|
return Ok(new { Message = "Instruction moved down successfully." });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return BadRequest(new { Error = ex.Message });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("Recipe/{recipeId}/Instructions")]
|
||||||
|
public async Task<IActionResult> GetInstructions(Guid recipeId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var sortedInstructions = await _instructionService.GetSortedInstructionsAsync(recipeId);
|
||||||
|
var viewModel = new InstructionViewModel
|
||||||
|
{
|
||||||
|
RecipeId = recipeId,
|
||||||
|
Instructions = sortedInstructions,
|
||||||
|
};
|
||||||
|
|
||||||
|
return View("~/Views/Shared/_GetInstructions.cshtml", viewModel);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return BadRequest(new { Error = ex.Message });
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
namespace Francesco.Recipes.World.Controller.MediaFile
|
namespace Francesco.Recipes.World.Controller.MediaFile
|
||||||
{
|
{
|
||||||
using Francesco.Recipes.World.Data;
|
|
||||||
using Francesco.Recipes.World.Repositories.MediaFile;
|
using Francesco.Recipes.World.Repositories.MediaFile;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
@@ -8,12 +7,10 @@
|
|||||||
public class MediaFileController : Controller
|
public class MediaFileController : Controller
|
||||||
{
|
{
|
||||||
private readonly IMediaFileRepository _mediaFileRepository;
|
private readonly IMediaFileRepository _mediaFileRepository;
|
||||||
private readonly FrancescosRecipesWorldDbContext _context;
|
|
||||||
|
|
||||||
public MediaFileController(IMediaFileRepository mediaFileRepository, FrancescosRecipesWorldDbContext context)
|
public MediaFileController(IMediaFileRepository mediaFileRepository)
|
||||||
{
|
{
|
||||||
_mediaFileRepository = mediaFileRepository;
|
_mediaFileRepository = mediaFileRepository;
|
||||||
_context = context;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST: /UploadImage
|
// POST: /UploadImage
|
||||||
|
|||||||
@@ -13,7 +13,6 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||||
|
|
||||||
|
|
||||||
public class RecipeController : Controller
|
public class RecipeController : Controller
|
||||||
{
|
{
|
||||||
private readonly IRecipeRepository _recipeRepository;
|
private readonly IRecipeRepository _recipeRepository;
|
||||||
@@ -189,6 +188,47 @@
|
|||||||
return RedirectToAction("Details", new { id = recipeId });
|
return RedirectToAction("Details", new { id = recipeId });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GET: /Recipe/{recipeId}/RemoveInstruction/{instructionId}
|
||||||
|
[HttpGet("{recipeId}/RemoveInstruction/{instructionId}")]
|
||||||
|
public async Task<IActionResult> RemoveInstruction(Guid recipeId, Guid instructionId)
|
||||||
|
{
|
||||||
|
var recipe = await _recipeRepository.GetRecipeAsync(recipeId);
|
||||||
|
|
||||||
|
if (recipe == null)
|
||||||
|
{
|
||||||
|
return NotFound("Recipe not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var instruction = recipe.Instructions?.FirstOrDefault(i => i.Id == instructionId);
|
||||||
|
if (instruction == null)
|
||||||
|
{
|
||||||
|
return NotFound("Instruction not found in the specified recipe.");
|
||||||
|
}
|
||||||
|
|
||||||
|
ViewBag.RecipeId = recipeId;
|
||||||
|
ViewBag.InstructionId = instructionId;
|
||||||
|
|
||||||
|
return View();
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST: /Recipe/{recipeId}/RemoveInstruction/{instructionId}
|
||||||
|
[HttpPost("{recipeId}/RemoveInstruction/{instructionId}")]
|
||||||
|
[ValidateAntiForgeryToken]
|
||||||
|
public async Task<IActionResult> RemoveInstructionConfirmed(Guid recipeId, Guid instructionId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _instructionRepository.RemoveInstructionFromRecipeAsync(recipeId, instructionId);
|
||||||
|
TempData["SuccessMessage"] = "Instruction removed successfully.";
|
||||||
|
return RedirectToAction("Details", new { recipeId });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
TempData["ErrorMessage"] = $"An error occurred while removing the instruction: {ex.Message}";
|
||||||
|
return RedirectToAction("Details", new { recipeId });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// GET: /Recipe/FilterByDifficulty
|
// GET: /Recipe/FilterByDifficulty
|
||||||
[HttpGet("FilterByDifficulty")]
|
[HttpGet("FilterByDifficulty")]
|
||||||
public async Task<IActionResult> FilterByDifficulty(Difficulty? selectedDifficulty)
|
public async Task<IActionResult> FilterByDifficulty(Difficulty? selectedDifficulty)
|
||||||
@@ -219,11 +259,11 @@
|
|||||||
// POST: /Recipe/{recipeId}/AddInstruction
|
// POST: /Recipe/{recipeId}/AddInstruction
|
||||||
[HttpPost("{recipeId}/AddInstruction")]
|
[HttpPost("{recipeId}/AddInstruction")]
|
||||||
[ValidateAntiForgeryToken]
|
[ValidateAntiForgeryToken]
|
||||||
public async Task<IActionResult> AddInstruction(Guid recipeId, string description, int number)
|
public async Task<IActionResult> AddInstruction(Guid recipeId, string description)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _instructionRepository.CreateInstructionToRecipeAsync(recipeId, description, number);
|
await _instructionRepository.CreateInstructionToRecipeAsync(recipeId, description);
|
||||||
return RedirectToAction("AddInstruction", new { recipeId });
|
return RedirectToAction("AddInstruction", new { recipeId });
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
namespace Francesco.Recipes.World.Models
|
||||||
|
{
|
||||||
|
using Francesco.Recipes.World.Models.BackendModels.Instruction;
|
||||||
|
|
||||||
|
public class InstructionViewModel
|
||||||
|
{
|
||||||
|
public Guid RecipeId { get; set; }
|
||||||
|
|
||||||
|
public List<Instruction> Instructions { get; set; } = new List<Instruction>();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ using Francesco.Recipes.World.Repositories.MediaFile;
|
|||||||
using Francesco.Recipes.World.Repositories.Recipe;
|
using Francesco.Recipes.World.Repositories.Recipe;
|
||||||
using Francesco.Recipes.World.Repositories.ShoppingList;
|
using Francesco.Recipes.World.Repositories.ShoppingList;
|
||||||
using Francesco.Recipes.World.Repositories.Unit;
|
using Francesco.Recipes.World.Repositories.Unit;
|
||||||
|
using Francesco.Recipes.World.Services.Instruction;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
@@ -40,6 +41,8 @@ builder.Services.AddScoped<IInstructionRepository, InstructionRepository>();
|
|||||||
|
|
||||||
builder.Services.AddScoped<IFavoriteRepository, FavoritRepository>();
|
builder.Services.AddScoped<IFavoriteRepository, FavoritRepository>();
|
||||||
|
|
||||||
|
builder.Services.AddScoped<IInstructionService, InstructionService>();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
|
|||||||
@@ -6,10 +6,14 @@
|
|||||||
{
|
{
|
||||||
Task<Instruction> GetInstructionAsync(Guid instructionId);
|
Task<Instruction> GetInstructionAsync(Guid instructionId);
|
||||||
|
|
||||||
Task<Instruction> CreateInstructionToRecipeAsync(Guid recipeId, string description, int number);
|
Task<Instruction> CreateInstructionToRecipeAsync(Guid recipeId, string description);
|
||||||
|
|
||||||
Task<List<Instruction>> GetInstructionsByRecipeIdAsync(Guid recipeId);
|
|
||||||
|
|
||||||
Task RemoveInstructionFromRecipeAsync(Guid recipeId, Guid instructionId);
|
Task RemoveInstructionFromRecipeAsync(Guid recipeId, Guid instructionId);
|
||||||
|
|
||||||
|
Task SwapInstructionNumbersAsync(Instruction a, Instruction b);
|
||||||
|
|
||||||
|
Task<List<Instruction>> GetInstructionsOfRecipeAsync(Guid recipeId);
|
||||||
|
|
||||||
|
Task RenumberInstructionsAsync(Guid recipeId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,25 +22,30 @@
|
|||||||
return instruction ?? throw new InvalidDataException($"Instruction {instructionId} not found.");
|
return instruction ?? throw new InvalidDataException($"Instruction {instructionId} not found.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Instruction> CreateInstructionToRecipeAsync(Guid recipeId, string description, int number)
|
public async Task<Instruction> CreateInstructionToRecipeAsync(Guid recipeId, string description)
|
||||||
{
|
{
|
||||||
var recipe = await _recipeRepository.GetRecipeAsync(recipeId);
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(description))
|
if (string.IsNullOrWhiteSpace(description))
|
||||||
{
|
{
|
||||||
throw new ArgumentException("Description cannot be empty", nameof(description));
|
throw new ArgumentException("Description cannot be empty", nameof(description));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (number <= 0)
|
var recipe = await _context.Recipes
|
||||||
|
.Include(r => r.Instructions)
|
||||||
|
.FirstOrDefaultAsync(r => r.Id == recipeId);
|
||||||
|
|
||||||
|
if (recipe == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentOutOfRangeException(nameof(number), "Number must be greater than 0.");
|
throw new ArgumentException("Recipe not found.", nameof(recipeId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var nextNumber = recipe.Instructions?.Max(i => i.Number) ?? 0;
|
||||||
|
nextNumber++;
|
||||||
|
|
||||||
var newInstruction = new Instruction
|
var newInstruction = new Instruction
|
||||||
{
|
{
|
||||||
Id = Guid.NewGuid(),
|
Id = Guid.NewGuid(),
|
||||||
Description = description,
|
Description = description,
|
||||||
Number = number,
|
Number = nextNumber,
|
||||||
Recipe = recipe,
|
Recipe = recipe,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -65,17 +70,68 @@
|
|||||||
|
|
||||||
if (instructionToRemove != null)
|
if (instructionToRemove != null)
|
||||||
{
|
{
|
||||||
|
await _context.Entry(instructionToRemove)
|
||||||
|
.Collection(i => i.MediaFiles)
|
||||||
|
.LoadAsync();
|
||||||
|
|
||||||
|
if (instructionToRemove.MediaFiles != null && instructionToRemove.MediaFiles.Any())
|
||||||
|
{
|
||||||
|
_context.MediaFiles.RemoveRange(instructionToRemove.MediaFiles);
|
||||||
|
}
|
||||||
|
|
||||||
recipe.Instructions?.Remove(instructionToRemove);
|
recipe.Instructions?.Remove(instructionToRemove);
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
|
await RenumberInstructionsAsync(recipeId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<Instruction>> GetInstructionsByRecipeIdAsync(Guid recipeId)
|
public async Task<List<Instruction>> GetInstructionsOfRecipeAsync(Guid recipeId)
|
||||||
{
|
{
|
||||||
return await _context.Instructions
|
var instructions = await _context.Instructions
|
||||||
.Include(i => i.Recipe)
|
|
||||||
.Where(i => i.Recipe.Id == recipeId)
|
.Where(i => i.Recipe.Id == recipeId)
|
||||||
|
.OrderBy(i => i.Number)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
|
if (!instructions.Any())
|
||||||
|
{
|
||||||
|
throw new InvalidDataException($"No instructions found for Recipe ID {recipeId}.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return instructions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task RenumberInstructionsAsync(Guid recipeId)
|
||||||
|
{
|
||||||
|
var instructions = await _context.Instructions
|
||||||
|
.Where(i => i.Recipe.Id == recipeId)
|
||||||
|
.OrderBy(i => i.Number)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
for (var i = 0; i < instructions.Count; i++)
|
||||||
|
{
|
||||||
|
instructions[i].Number = i + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task SwapInstructionNumbersAsync(Instruction a, Instruction b)
|
||||||
|
{
|
||||||
|
if (a == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(a), "Instruction 'a' cannot be null.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (b == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(b), "Instruction 'b' cannot be null.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var temp = a.Number;
|
||||||
|
a.Number = b.Number;
|
||||||
|
b.Number = temp;
|
||||||
|
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
namespace Francesco.Recipes.World.Services.Instruction
|
||||||
|
{
|
||||||
|
using Francesco.Recipes.World.Models.BackendModels.Instruction;
|
||||||
|
|
||||||
|
public interface IInstructionService
|
||||||
|
{
|
||||||
|
Task MoveInstructionUpAsync(Guid recipeId, Guid instructionId);
|
||||||
|
|
||||||
|
Task MoveInstructionDownAsync(Guid recipeId, Guid instructionId);
|
||||||
|
|
||||||
|
Task<List<Instruction>> GetSortedInstructionsAsync(Guid recipeId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
namespace Francesco.Recipes.World.Services.Instruction
|
||||||
|
{
|
||||||
|
using Francesco.Recipes.World.Models.BackendModels.Instruction;
|
||||||
|
using Francesco.Recipes.World.Repositories.Instruction;
|
||||||
|
|
||||||
|
public class InstructionService : IInstructionService
|
||||||
|
{
|
||||||
|
private readonly IInstructionRepository _instructionRepository;
|
||||||
|
|
||||||
|
public InstructionService(IInstructionRepository instructionRepository)
|
||||||
|
{
|
||||||
|
_instructionRepository = instructionRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task MoveInstructionUpAsync(Guid recipeId, Guid instructionId)
|
||||||
|
=> MoveInstructionAsync(recipeId, instructionId, moveUp: true);
|
||||||
|
|
||||||
|
public Task MoveInstructionDownAsync(Guid recipeId, Guid instructionId)
|
||||||
|
=> MoveInstructionAsync(recipeId, instructionId, moveUp: false);
|
||||||
|
|
||||||
|
public async Task<List<Instruction>> GetSortedInstructionsAsync(Guid recipeId)
|
||||||
|
{
|
||||||
|
var instructions = await _instructionRepository.GetInstructionsOfRecipeAsync(recipeId);
|
||||||
|
return instructions.OrderBy(i => i.Number).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task MoveInstructionAsync(Guid recipeId, Guid instructionId, bool moveUp)
|
||||||
|
{
|
||||||
|
var instructions = await _instructionRepository.GetInstructionsOfRecipeAsync(recipeId);
|
||||||
|
|
||||||
|
var instruction = instructions.FirstOrDefault(i => i.Id == instructionId);
|
||||||
|
|
||||||
|
if (instruction == null)
|
||||||
|
{
|
||||||
|
throw new InvalidDataException($"Instruction with ID {instructionId} not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var minStep = 1;
|
||||||
|
var maxStep = instructions.Max(i => i.Number);
|
||||||
|
|
||||||
|
if ((moveUp && instruction.Number == minStep) || (!moveUp && instruction.Number >= maxStep))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Instructions are ordered by ascending numbers (1, 2, 3, ...).
|
||||||
|
// Moving up means swapping with the instruction that has one number less (Number - 1).
|
||||||
|
// Moving down means swapping with the instruction that has one number more (Number + 1).
|
||||||
|
var targetNumber = moveUp ? instruction.Number - 1 : instruction.Number + 1;
|
||||||
|
var neighbor = instructions.FirstOrDefault(i => i.Number == targetNumber);
|
||||||
|
|
||||||
|
if (neighbor != null)
|
||||||
|
{
|
||||||
|
await _instructionRepository.SwapInstructionNumbersAsync(instruction, neighbor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,28 +24,28 @@
|
|||||||
<label for="description">Description</label>
|
<label for="description">Description</label>
|
||||||
<input type="text" class="form-control" id="description" name="description" required />
|
<input type="text" class="form-control" id="description" name="description" required />
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<button type="button" class="btn btn-primary" onclick="addInstructionToRecipe()">Add Instruction</button>
|
||||||
<label for="number">Number</label>
|
|
||||||
<input type="number" class="form-control" id="number" name="number" required min="1" />
|
|
||||||
</div>
|
|
||||||
<button type="submit" class="btn btn-primary" onclick="addInstructionToRecipe()">Add Instruction</button>
|
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@section Scripts {
|
@section Scripts {
|
||||||
<script>
|
<script>
|
||||||
async function addInstructionToRecipe() {
|
async function addInstructionToRecipe() {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
var form = document.getElementById('instruction-form');
|
var form = document.getElementById('instruction-form');
|
||||||
var recipeId = form.querySelector('input[name="recipeId"]').value;
|
var recipeId = form.querySelector('input[name="recipeId"]').value;
|
||||||
var description = form.querySelector('input[name="description"]').value;
|
var description = form.querySelector('input[name="description"]').value;
|
||||||
var number = form.querySelector('input[name="number"]').value;
|
var token = form.querySelector('input[name="__RequestVerificationToken"]').value;
|
||||||
|
|
||||||
var response = await fetch('/Recipe/' + recipeId + '/AddInstruction', {
|
var response = await fetch('/' + recipeId + '/AddInstruction', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
|
'RequestVerificationToken': token
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ recipeId: recipeId, description: description, number: number })
|
body: `recipeId=${encodeURIComponent(recipeId)}&description=${encodeURIComponent(description)}`
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
@@ -54,6 +54,8 @@
|
|||||||
alert('Failed to add instruction.');
|
alert('Failed to add instruction.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,118 @@
|
|||||||
|
@model Francesco.Recipes.World.Models.InstructionViewModel
|
||||||
|
@Html.AntiForgeryToken()
|
||||||
|
<div id="instructions-container">
|
||||||
|
@for (int i = 0; i < Model.Instructions.Count; i++)
|
||||||
|
{
|
||||||
|
<div class="instruction-item" id="instruction-@Model.Instructions[i].Id">
|
||||||
|
<div class="instruction-controls">
|
||||||
|
<input type="file" />
|
||||||
|
<textarea placeholder="Beschreibung" class="form-control">@Model.Instructions[i].Description</textarea>
|
||||||
|
<button type="button" class="btn-delete" onclick="removeInstruction('@Model.Instructions[i].Id')">🗑️</button>
|
||||||
|
</div>
|
||||||
|
<div class="instruction-actions">
|
||||||
|
<button type="button" class="btn-move-up" onclick="moveInstructionUp('@Model.Instructions[i].Id')">⬆️</button>
|
||||||
|
<button type="button" class="btn-move-down" onclick="moveInstructionDown('@Model.Instructions[i].Id')">⬇️</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button type="button" class="btn-add" onclick="addInstruction()">Schritte hinzufügen</button>
|
||||||
|
|
||||||
|
|
||||||
|
@section Scripts {
|
||||||
|
<script>
|
||||||
|
htmx.on('htmx:afterSwap', (event) => {
|
||||||
|
if (event.target.id === 'instructions-container') {
|
||||||
|
console.log('Instructions reloaded.');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const recipeId = '@Model.RecipeId';
|
||||||
|
|
||||||
|
|
||||||
|
async function moveInstructionUp(instructionId) {
|
||||||
|
try {
|
||||||
|
const response = await fetch(`/Recipe/${recipeId}/Instruction/${instructionId}/move-up`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'RequestVerificationToken': document.querySelector('input[name="__RequestVerificationToken"]').value
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
htmx.ajax('GET', `/Recipe/${recipeId}/Instructions`, '#instructions-container');
|
||||||
|
} else {
|
||||||
|
const error = await response.json();
|
||||||
|
alert(error.Error || 'Failed to move instruction up.');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error moving instruction up:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function moveInstructionDown(instructionId) {
|
||||||
|
try {
|
||||||
|
const response = await fetch(`/Recipe/${recipeId}/Instruction/${instructionId}/move-down`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'RequestVerificationToken': document.querySelector('input[name="__RequestVerificationToken"]').value
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
htmx.ajax('GET', `/Recipe/${recipeId}/Instructions`, '#instructions-container');
|
||||||
|
} else {
|
||||||
|
const error = await response.json();
|
||||||
|
alert(error.Error || 'Failed to move instruction down.');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error moving instruction down:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function removeInstruction(instructionId) {
|
||||||
|
if (!confirm('Möchtest du diesen Schritt wirklich löschen?')) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`/${recipeId}/RemoveInstruction/${instructionId}`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'RequestVerificationToken': document.querySelector('input[name="__RequestVerificationToken"]').value
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
const element = document.getElementById(`instruction-${instructionId}`);
|
||||||
|
if (element) {
|
||||||
|
element.remove();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const error = await response.json();
|
||||||
|
alert(error.Error || 'Fehler beim Löschen der Anweisung.');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Fehler beim Löschen:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function addInstruction() {
|
||||||
|
const container = document.getElementById('instructions-container');
|
||||||
|
const newInstructionHtml = `
|
||||||
|
<div class="instruction-item">
|
||||||
|
<div class="instruction-controls">
|
||||||
|
<input type="file" />
|
||||||
|
<textarea placeholder="Beschreibung" class="form-control"></textarea>
|
||||||
|
<button type="button" class="btn-delete" onclick="deleteInstruction()">🗑️</button>
|
||||||
|
</div>
|
||||||
|
<div class="instruction-actions">
|
||||||
|
<button type="button" class="btn-move-up" onclick="moveInstructionUp()">⬆️</button>
|
||||||
|
<button type="button" class="btn-move-down" onclick="moveInstructionDown()">⬇️</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
container.insertAdjacentHTML('beforeend', newInstructionHtml);
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user