Add also here recipeId
This commit is contained in:
@@ -1,6 +1,65 @@
|
||||
namespace Francesco.Recipes.World.Controller.Instruction
|
||||
{
|
||||
public class InstructionController
|
||||
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 instructions = await _instructionRepository.GetInstructionsOfRecipeAsync(recipeId);
|
||||
var sortedInstructions = instructions.OrderBy(i => i.Number).ToList();
|
||||
ViewData["RecipeId"] = recipeId;
|
||||
|
||||
return View("~/Views/Shared/_GetInstructions.cshtml", sortedInstructions);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return BadRequest(new { Error = ex.Message });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace Francesco.Recipes.World.Controller.MediaFile
|
||||
{
|
||||
using Francesco.Recipes.World.Data;
|
||||
|
||||
using Francesco.Recipes.World.Repositories.MediaFile;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
{
|
||||
private readonly IMediaFileRepository _mediaFileRepository;
|
||||
|
||||
public MediaFileController(IMediaFileRepository mediaFileRepository, FrancescosRecipesWorldDbContext context)
|
||||
public MediaFileController(IMediaFileRepository mediaFileRepository)
|
||||
{
|
||||
_mediaFileRepository = mediaFileRepository;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
|
||||
|
||||
public class RecipeController : Controller
|
||||
{
|
||||
private readonly IRecipeRepository _recipeRepository;
|
||||
|
||||
@@ -12,6 +12,6 @@
|
||||
|
||||
Task SwapInstructionNumbersAsync(Instruction a, Instruction b);
|
||||
|
||||
Task<List<Instruction>> GetInstructionsOfRecipeAsync(Guid instructionId);
|
||||
Task<List<Instruction>> GetInstructionsOfRecipeAsync(Guid recipeId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,19 +70,19 @@
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<Instruction>> GetInstructionsOfRecipeAsync(Guid instructionId)
|
||||
public async Task<List<Instruction>> GetInstructionsOfRecipeAsync(Guid recipeId)
|
||||
{
|
||||
var instruction = await _context.Instructions
|
||||
.Include(i => i.Recipe)
|
||||
.ThenInclude(r => r.Instructions)
|
||||
.FirstOrDefaultAsync(i => i.Id == instructionId);
|
||||
var instructions = await _context.Instructions
|
||||
.Where(i => i.Recipe.Id == recipeId)
|
||||
.OrderBy(i => i.Number)
|
||||
.ToListAsync();
|
||||
|
||||
if (instruction?.Recipe == null)
|
||||
if (!instructions.Any())
|
||||
{
|
||||
throw new InvalidDataException($"Instruction with ID {instructionId} or its Recipe not found.");
|
||||
throw new InvalidDataException($"No instructions found for Recipe ID {recipeId}.");
|
||||
}
|
||||
|
||||
return instruction.Recipe.Instructions.ToList();
|
||||
return instructions;
|
||||
}
|
||||
|
||||
public async Task SwapInstructionNumbersAsync(Instruction a, Instruction b)
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
@Html.AntiForgeryToken()
|
||||
<div id="instructions-container">
|
||||
@for (int i = 0; i < Model.Count; i++)
|
||||
{
|
||||
<div class="instruction-item" id="instruction-@Model[i].Id">
|
||||
<div class="instruction-controls">
|
||||
<input type="file" />
|
||||
<textarea placeholder="Beschreibung" class="form-control">@Model[i].Description</textarea>
|
||||
<button type="button" class="btn-delete">🗑️</button>
|
||||
</div>
|
||||
<div class="instruction-actions">
|
||||
<button type="button" class="btn-move-up" onclick="moveInstructionUp('@Model[i].Id')">⬆️</button>
|
||||
<button type="button" class="btn-move-down" onclick="moveInstructionDown('@Model[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 = '@ViewData["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);
|
||||
}
|
||||
}
|
||||
|
||||
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