Correction PDF
This commit is contained in:
@@ -50,14 +50,20 @@ public class HelloFreshController : AppController
|
||||
{
|
||||
try
|
||||
{
|
||||
if (page < 1 || count < 1)
|
||||
return BadRequest("Les paramètres 'page' et 'count' doivent être supérieurs à 0.");
|
||||
if (page < 1 || count < 1) return BadRequest("Les paramètres 'page' et 'count' doivent être supérieurs à 0.");
|
||||
|
||||
// ✅ Base query : uniquement les recettes avec un PDF non vide
|
||||
var q = _context.Recettes
|
||||
.AsNoTracking()
|
||||
.Where(r => r.Pdf != null && r.Pdf != "") // éviter IsNullOrWhiteSpace pour une traduction SQL sûre
|
||||
.AsQueryable();
|
||||
|
||||
var q = _context.Recettes.AsQueryable();
|
||||
if (!string.IsNullOrWhiteSpace(search))
|
||||
{
|
||||
var s = search.Trim().ToLowerInvariant();
|
||||
q = q.Where(r => r.Name.ToLower().Contains(s));
|
||||
var s = search.Trim();
|
||||
q = q.Where(r => EF.Functions.Like(r.Name, $"%{s}%"));
|
||||
// Optionnel : chercher aussi dans les tags
|
||||
// q = q.Where(r => EF.Functions.Like(r.Name, $"%{s}%") || (r.Tags != null && EF.Functions.Like(r.Tags, $"%{s}%")));
|
||||
}
|
||||
|
||||
var totalItems = q.Count();
|
||||
@@ -84,6 +90,8 @@ public class HelloFreshController : AppController
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ❌ Supprime la limite de 3 portions
|
||||
[HttpPost("SaveRecipe")]
|
||||
public IActionResult SaveRecipe([FromBody] string idSavingRecette)
|
||||
@@ -393,12 +401,16 @@ public class HelloFreshController : AppController
|
||||
var userId = HttpContext.Session.GetInt32("UserId");
|
||||
if (userId == null) return Unauthorized("Utilisateur non connecté.");
|
||||
|
||||
var scopeIdsOwned = PantryScopeUserIds();
|
||||
var ownedNames = _context.Ingredients
|
||||
.AsNoTracking()
|
||||
.Where(i => scopeIdsOwned.Contains(i.UserId)) // 👈 union Mae+Byakuya
|
||||
.Select(i => i.NameOwnedIngredients)
|
||||
.ToList()
|
||||
.Select(s => (s ?? "").Trim().ToLowerInvariant())
|
||||
.ToHashSet();
|
||||
|
||||
|
||||
var perRecipe = _context.SavingRecettes
|
||||
.GroupBy(s => s.IdSavingRecette)
|
||||
.Select(g => new { RecetteId = g.Key, Portions = g.Count() })
|
||||
@@ -494,11 +506,16 @@ public class HelloFreshController : AppController
|
||||
var userId = HttpContext.Session.GetInt32("UserId");
|
||||
if (userId == null) return Unauthorized("Utilisateur non connecté.");
|
||||
|
||||
var ownedNames = _context.Ingredients.Select(i => i.NameOwnedIngredients)
|
||||
var scopeIdsOwned = PantryScopeUserIds();
|
||||
var ownedNames = _context.Ingredients
|
||||
.AsNoTracking()
|
||||
.Where(i => scopeIdsOwned.Contains(i.UserId)) // 👈 union Mae+Byakuya
|
||||
.Select(i => i.NameOwnedIngredients)
|
||||
.ToList()
|
||||
.Select(s => (s ?? "").Trim().ToLowerInvariant())
|
||||
.ToHashSet();
|
||||
|
||||
|
||||
var perRecipe = _context.SavingRecettes
|
||||
.Where(s => s.UserId == userId)
|
||||
.GroupBy(s => s.IdSavingRecette)
|
||||
@@ -640,7 +657,7 @@ public class HelloFreshController : AppController
|
||||
var idList = ids.Split(',').Select(s => s.Trim()).Where(s => s.Length > 0).ToList();
|
||||
|
||||
var recipes = _context.Recettes
|
||||
.Where(r => idList.Contains(r.Id))
|
||||
.Where(r => idList.Contains(r.Id) && !string.IsNullOrEmpty(r.Pdf)) // 👈 filtre: PDF non vide
|
||||
.Select(r => new
|
||||
{
|
||||
id = r.Id,
|
||||
@@ -654,14 +671,19 @@ public class HelloFreshController : AppController
|
||||
|
||||
return Ok(recipes);
|
||||
}
|
||||
|
||||
[HttpPost("ToggleOwnedIngredient")]
|
||||
public IActionResult ToggleOwnedIngredient([FromBody] string ingredientName)
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = HttpContext.Session.GetInt32("UserId");
|
||||
if (userId == null) return Unauthorized("Utilisateur non connecté.");
|
||||
|
||||
var name = (ingredientName ?? "").Trim();
|
||||
if (string.IsNullOrWhiteSpace(name)) return BadRequest("Nom d'ingrédient invalide.");
|
||||
|
||||
var existing = _context.Ingredients
|
||||
.FirstOrDefault(i => i.NameOwnedIngredients == ingredientName);
|
||||
.FirstOrDefault(i => i.UserId == userId.Value && i.NameOwnedIngredients == name);
|
||||
|
||||
if (existing != null)
|
||||
{
|
||||
@@ -671,7 +693,11 @@ public class HelloFreshController : AppController
|
||||
}
|
||||
else
|
||||
{
|
||||
_context.Ingredients.Add(new Ingredient { NameOwnedIngredients = ingredientName });
|
||||
_context.Ingredients.Add(new Ingredient
|
||||
{
|
||||
UserId = userId.Value, // 👈 lie l’ingrédient au user courant
|
||||
NameOwnedIngredients = name
|
||||
});
|
||||
_context.SaveChanges();
|
||||
return Ok(new { status = "added" });
|
||||
}
|
||||
@@ -682,12 +708,18 @@ public class HelloFreshController : AppController
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[HttpGet("GetOwnedIngredients")]
|
||||
public IActionResult GetOwnedIngredients()
|
||||
{
|
||||
try
|
||||
{
|
||||
var scopeIds = PantryScopeUserIds();
|
||||
if (scopeIds.Count == 0) return Unauthorized("Utilisateur non connecté.");
|
||||
|
||||
var names = _context.Ingredients
|
||||
.AsNoTracking()
|
||||
.Where(i => scopeIds.Contains(i.UserId)) // 👈 union Mae+Byakuya, sinon user seul
|
||||
.Select(i => i.NameOwnedIngredients)
|
||||
.Distinct()
|
||||
.ToList();
|
||||
@@ -700,6 +732,7 @@ public class HelloFreshController : AppController
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[HttpGet("GetAllRecipes")]
|
||||
public IActionResult GetAllRecipes()
|
||||
{
|
||||
@@ -714,8 +747,10 @@ public class HelloFreshController : AppController
|
||||
})
|
||||
.ToList();
|
||||
|
||||
// 👇 Ne garder que les recettes avec un PDF non vide
|
||||
var recettes = _context.Recettes
|
||||
.AsNoTracking()
|
||||
.Where(r => r.Pdf != null && r.Pdf != "") // équiv. à !string.IsNullOrEmpty(r.Pdf)
|
||||
.Select(r => new
|
||||
{
|
||||
r.Id,
|
||||
@@ -723,7 +758,8 @@ public class HelloFreshController : AppController
|
||||
r.Image,
|
||||
r.TempsDePreparation,
|
||||
r.Tags,
|
||||
IngredientsToHad = r.IngredientsToHad
|
||||
IngredientsToHad = r.IngredientsToHad,
|
||||
r.Pdf // utilisé pour le filtre (pas renvoyé ensuite)
|
||||
})
|
||||
.ToList();
|
||||
|
||||
@@ -773,6 +809,7 @@ public class HelloFreshController : AppController
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
|
||||
[HttpGet("/HelloFresh/GetAllRecipesWithUsers")]
|
||||
public IActionResult GetAllRecipesWithUsers()
|
||||
{
|
||||
@@ -1095,4 +1132,100 @@ public class HelloFreshController : AppController
|
||||
|
||||
return Ok(distinct);
|
||||
}
|
||||
|
||||
// GET /HelloFresh/GetRecipesOwnedByUsers?names=Mae,Byakuya
|
||||
[HttpGet("GetRecipesOwnedByUsers")]
|
||||
public IActionResult GetRecipesOwnedByUsers([FromQuery] string names)
|
||||
{
|
||||
var raw = (names ?? "").Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||
.Select(s => s.Trim())
|
||||
.Where(s => s.Length > 0)
|
||||
.ToList();
|
||||
if (raw.Count == 0) return Ok(new List<object>());
|
||||
|
||||
// usernames -> ids
|
||||
var users = _layout.Users.AsNoTracking()
|
||||
.Where(u => raw.Contains(u.Username))
|
||||
.Select(u => new { u.Id, u.Username })
|
||||
.ToList();
|
||||
|
||||
if (users.Count == 0) return Ok(new List<object>());
|
||||
|
||||
var userIds = users.Select(u => u.Id).ToList();
|
||||
var idToName = users.ToDictionary(u => u.Id, u => u.Username);
|
||||
|
||||
// portions par (Recette, User)
|
||||
var counts = _context.SavingRecettes.AsNoTracking()
|
||||
.Where(s => userIds.Contains(s.UserId))
|
||||
.GroupBy(s => new { s.IdSavingRecette, s.UserId })
|
||||
.Select(g => new { RecetteId = g.Key.IdSavingRecette, UserId = g.Key.UserId, Portions = g.Count() })
|
||||
.ToList();
|
||||
|
||||
if (counts.Count == 0) return Ok(new List<object>());
|
||||
|
||||
var recetteIds = counts.Select(c => c.RecetteId).Distinct().ToList();
|
||||
var recs = _context.Recettes.AsNoTracking()
|
||||
.Where(r => recetteIds.Contains(r.Id))
|
||||
.Select(r => new
|
||||
{
|
||||
r.Id,
|
||||
r.Name,
|
||||
r.Image,
|
||||
r.TempsDePreparation,
|
||||
r.Tags,
|
||||
r.IngredientsToHad
|
||||
})
|
||||
.ToList();
|
||||
var recDict = recs.ToDictionary(r => r.Id, r => r, StringComparer.Ordinal);
|
||||
|
||||
// Agrégation par recette limitée aux users demandés
|
||||
var result = counts
|
||||
.GroupBy(c => c.RecetteId)
|
||||
.Select(g =>
|
||||
{
|
||||
recDict.TryGetValue(g.Key, out var r);
|
||||
return new
|
||||
{
|
||||
id = g.Key,
|
||||
name = r?.Name ?? g.Key,
|
||||
image = r?.Image,
|
||||
tempsDePreparation = r?.TempsDePreparation ?? 0,
|
||||
portions = g.Sum(x => x.Portions),
|
||||
tags = r?.Tags,
|
||||
users = g.Select(x => idToName.TryGetValue(x.UserId, out var un) ? un : null)
|
||||
.Where(un => !string.IsNullOrWhiteSpace(un))
|
||||
.Distinct()
|
||||
.ToList(),
|
||||
ingredientsToHad = r?.IngredientsToHad
|
||||
};
|
||||
})
|
||||
.OrderBy(x => x.name, StringComparer.Create(new System.Globalization.CultureInfo("fr-FR"), true))
|
||||
.ToList();
|
||||
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
// --- Scope "pantry" (ingrédients possédés) ---
|
||||
// Mae & Byakuya partagent leur garde-manger ; les autres sont en solo.
|
||||
private List<int> PantryScopeUserIds()
|
||||
{
|
||||
var sessionUserId = HttpContext.Session.GetInt32("UserId");
|
||||
if (sessionUserId == null) return new List<int>();
|
||||
|
||||
var me = _layout.Users.AsNoTracking().FirstOrDefault(u => u.Id == sessionUserId.Value);
|
||||
if (me == null) return new List<int> { sessionUserId.Value };
|
||||
|
||||
// Liste blanche des prénoms qui partagent entre eux
|
||||
var core = new[] { "Mae", "Byakuya" };
|
||||
var isCore = core.Any(n => string.Equals(n, me.Username, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (!isCore) return new List<int> { me.Id };
|
||||
|
||||
// Récupère les IDs de Mae & Byakuya
|
||||
return _layout.Users.AsNoTracking()
|
||||
.Where(u => core.Contains(u.Username))
|
||||
.Select(u => u.Id)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user