Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using CookifyAPI.Services;
using Microsoft.AspNetCore.Mvc;

namespace CookifyAPI.Controllers.Imports;

[ApiController]
[Route("api/[controller]")]
public class ImagesMigrationController : ControllerBase
{
private readonly ImageMigrationService _migrationService;

public ImagesMigrationController(ImageMigrationService migrationService)
{
_migrationService = migrationService;
}

[HttpPost("process-json")]
public async Task<IActionResult> ProcessJson(IFormFile file)
{
if (file == null || file.Length == 0)
return BadRequest("File is empty");

string json;

using (var reader = new StreamReader(file.OpenReadStream()))
{
json = await reader.ReadToEndAsync();
}

var processed = await _migrationService.ProcessAllAsync(json);

var resultJson = JsonSerializer.Serialize(processed, new JsonSerializerOptions
{
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
});

return Content(resultJson, "application/json");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using CookifyAPI.Services;
using Microsoft.AspNetCore.Mvc;

namespace CookifyAPI.Controllers.Imports;

[ApiController]
[Route("api/[controller]")]
public class IngredientImportController : ControllerBase
{
private readonly IngredientImportService _importService;

public IngredientImportController(IngredientImportService importService)
{
_importService = importService;
}

[HttpPost("import")]
public async Task<IActionResult> Import(IFormFile file)
{
if (file == null || file.Length == 0)
return BadRequest("Файл отсутствует");

if (!file.FileName.EndsWith(".json"))
return BadRequest("Требуется JSON файл");

using var reader = new StreamReader(file.OpenReadStream());
var json = await reader.ReadToEndAsync();

await _importService.ImportAsync(json);

return Ok(new { message = "Импорт завершён" });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using CookifyAPI.Services;
using Microsoft.AspNetCore.Mvc;

namespace CookifyAPI.Controllers.Imports;

[ApiController]
[Route("api/[controller]")]
public class TagImportController : ControllerBase
{
private readonly TagImportService _service;

public TagImportController(TagImportService service)
{
_service = service;
}

[HttpPost("import")]
public async Task<IActionResult> Import(IFormFile file)
{
if (file == null || file.Length == 0)
return BadRequest("Файл отсутствует");

if (!file.FileName.EndsWith(".json"))
return BadRequest("Требуется JSON файл");

using var reader = new StreamReader(file.OpenReadStream());
var json = await reader.ReadToEndAsync();

await _service.ImportAsync(json);

return Ok(new { message = "Импорт завершён" });
}
}
18 changes: 17 additions & 1 deletion backend/CookifyAPI/CookifyAPI/Controllers/RecipesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ namespace CookifyAPI.Controllers;
public class RecipesController : ControllerBase
{
private readonly IRecipeService _service;
private readonly RecipeImportService _importService;

public RecipesController(IRecipeService service)
public RecipesController(IRecipeService service, RecipeImportService importService)
{
_service = service;
_importService = importService;
}

// GET: api/recipes
Expand All @@ -37,6 +39,20 @@ public async Task<ActionResult<RecipeDetailDto>> GetRecipe(int id)
return Ok(recipe);
}

[HttpPost("import")]
[RequestSizeLimit(50_000_000)]
public async Task<IActionResult> Import(IFormFile file)
{
if (file == null || file.Length == 0)
return BadRequest("Файл пуст");

using var stream = file.OpenReadStream();

await _importService.ImportAsync(stream);

return Ok();
}


// [HttpPost]
// public async Task<IActionResult> Create(Recipe recipe)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CookifyAPI.DTOs;
namespace CookifyAPI.DTOs.Ingredients;

public class IngredientDto
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text.Json.Serialization;

namespace CookifyAPI.DTOs.Ingredients;

public class JsonIngredientDto
{
[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("calories_100g")]
public float? Calories100g { get; set; }

[JsonPropertyName("protein_100g")]
public float? Protein100g { get; set; }

[JsonPropertyName("fat_100g")]
public float? Fat100g { get; set; }

[JsonPropertyName("carb_100g")]
public float? Carb100g { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Text.Json.Serialization;

namespace CookifyAPI.DTOs.Recipes;

public class JsonIngredientInRecipeDto
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("amount")]
public float Amount { get; set; }
[JsonPropertyName("unit")]
public string Unit { get; set; }
}
46 changes: 46 additions & 0 deletions backend/CookifyAPI/CookifyAPI/DTOs/Recipes/JsonRecipeDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Text.Json.Serialization;
using CookifyAPI.DTOs.Steps;

namespace CookifyAPI.DTOs.Recipes;

public class JsonRecipeDto
{
[JsonPropertyName("title")]
public string Title { get; set; }

[JsonPropertyName("cooking_time_min")]
public int CookingTimeMin { get; set; }

[JsonPropertyName("servings")]
public int Servings { get; set; }

[JsonPropertyName("difficulty_text")]
public string DifficultyText { get; set; }

[JsonPropertyName("description")]
public string Description { get; set; }

[JsonPropertyName("calories")]
public float Calories { get; set; }

[JsonPropertyName("protein")]
public float Protein { get; set; }

[JsonPropertyName("fat")]
public float Fat { get; set; }

[JsonPropertyName("carb")]
public float Carb { get; set; }

[JsonPropertyName("ingredients")]
public List<JsonIngredientInRecipeDto> Ingredients { get; set; }

[JsonPropertyName("tags")]
public List<string> Tags { get; set; }

[JsonPropertyName("steps")]
public List<JsonStepDto> Steps { get; set; }

[JsonPropertyName("images")]
public List<string> Images { get; set; }
}
6 changes: 4 additions & 2 deletions backend/CookifyAPI/CookifyAPI/DTOs/Recipes/RecipeDetailDto.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using CookifyAPI.DTOs;
using CookifyAPI.DTOs.Ingredients;
using CookifyAPI.DTOs.Steps;

namespace CookifyAPI.DTOs.Recipes;

public class RecipeDetailDto
Expand All @@ -19,7 +21,7 @@ public class RecipeDetailDto

// Связанные данные
public List<RecipeImageDto> Images { get; set; } = new();
public List<RecipeStepDto> Steps { get; set; } = new();
public List<StepDto> Steps { get; set; } = new();
public List<string> Tags { get; set; } = new();
public List<IngredientDto> Ingredients { get; set; }
}
20 changes: 20 additions & 0 deletions backend/CookifyAPI/CookifyAPI/DTOs/Steps/JsonStepDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@


using System.Text.Json.Serialization;

namespace CookifyAPI.DTOs.Steps;

public class JsonStepDto
{
[JsonPropertyName("title")]
public string Title { get; set; }

[JsonPropertyName("step_number")]
public int StepNumber { get; set; }

[JsonPropertyName("description")]
public string Description { get; set; }

[JsonPropertyName("image_url")]
public string ImageUrl { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CookifyAPI.DTOs.Recipes;
namespace CookifyAPI.DTOs.Steps;

public class RecipeStepDto
public class StepDto
{
public int Id { get; set; }
public string Title {get; set;}
Expand Down
9 changes: 9 additions & 0 deletions backend/CookifyAPI/CookifyAPI/DTOs/Tags/JsonTagDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text.Json.Serialization;

namespace CookifyAPI.DTOs.Tags;

public class JsonTagDto
{
[JsonPropertyName("name")]
public string Name { get; set; } = null!;
}
Loading