//récupération d'une commande par ID avec les produits commandés public Commande GetCommandeByID(int ID) { using (var context = new BBFRContext()) { return(context.Commandes.Where(x => x.ID == ID).Include(x => x.ProduitsCommandes).Include("ProduitsCommandes.Produit").FirstOrDefault()); } }
//Permet de récupérer le dernier produit enregistré (Pour le widget de Nouveaux produits sur le Home) public List <Produit> GetDernierProduit(int nombreProduits) { using (var context = new BBFRContext()) { return(context.Produits.OrderByDescending(x => x.ProduitID).Take(nombreProduits).Include(x => x.Type_Produit).ToList()); } }
//Permet de récupérer UNE catégorie par son ID public Type_Produit GetType_Produit(int ID) { using (var context = new BBFRContext()) { return(context.Type_Produits.Find(ID)); } }
//Methode permettant de récupérer une Liste de toutes les catégories (Pour la création d'un produit) public List <Type_Produit> GetType_Produits() { using (var context = new BBFRContext()) { return(context.Type_Produits.ToList()); } }
//Methode récupérant PLUSIEURS produits, grace a une liste, mais depuis une liste d'IDs (par exemple utiliser les IDs présent dans une catégorie) public List <Produit> GetProduits(List <int> IDs) { using (var context = new BBFRContext()) { return(context.Produits.Where(produit => IDs.Contains(produit.ProduitID)).ToList()); } }
//Methode pour récupérer des produits grace a l'ID d'une catégorie public List <Produit> GetProduitParCategorie(int type_produitID) { using (var context = new BBFRContext()) { return(context.Produits.Where(x => x.Type_Produit.Type_ProduitID == type_produitID).OrderByDescending(x => x.Type_ProduitID).Include(x => x.Type_Produit).ToList()); } }
//Methode récupérant PLUSIEURS produits, grace a une liste public List <Produit> GetProduits() { using (var context = new BBFRContext()) { return(context.Produits.Include(x => x.Type_Produit).ToList()); } }
//Permet de mettre a jour une catégorie dans la BDD public void UpdateType_Produit(Type_Produit type_produit) { using (var context = new BBFRContext()) { context.Entry(type_produit).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } }
//Permet de sauvegarder une catégorie dans la BDD public void SaveType_Produit(Type_Produit type_produit) { using (var context = new BBFRContext()) { context.Type_Produits.Add(type_produit); context.SaveChanges(); } }
//Methode de sauvegarde commande public int SaveCommande(Commande commande) { using (var context = new BBFRContext()) { context.Commandes.Add(commande); return(context.SaveChanges()); } }
//Permet de supprimer une catégorie de la BDD public void DeleteType_Produit(int ID) { using (var context = new BBFRContext()) { var type_produit = context.Type_Produits.Find(ID); context.Type_Produits.Remove(type_produit); context.SaveChanges(); } }
//Methode pour sauvegarder un produit public void SaveProduit(Produit produit) { using (var context = new BBFRContext()) { context.Entry(produit.Type_Produit).State = EntityState.Unchanged; context.Produits.Add(produit); context.SaveChanges(); } }
//methode pour mettre a jour le statut de commande depuis le bouton de validation des commande/Détails public bool UpdateCommandeStatut(int ID, string statut) { using (var context = new BBFRContext()) { var commande = context.Commandes.Find(ID); commande.Statut = statut; context.Entry(commande).State = EntityState.Modified; return(context.SaveChanges() > 0); } }
//Non utilisé public int GetType_ProduitCount(string search) { using (var context = new BBFRContext()) { if (!string.IsNullOrEmpty(search)) { return(context.Type_Produits.Where(type_produit => type_produit.Type != null && type_produit.Type.ToLower().Contains(search.ToLower())).Count()); } else { return(context.Type_Produits.Count()); } } }
//Service de recherche de commandes pour l'affichage, avec l'ID de l'user et le statut en paramétre public List <Commande> SearchCommandes(string userID, string statut) { using (var context = new BBFRContext()) { var commandes = context.Commandes.ToList(); //recherche de l'ID en mettant les paramétres et la recherche en minuscule if (!string.IsNullOrEmpty(userID)) { commandes = commandes.Where(x => x.UserID.ToLower().Contains(userID.ToLower())).ToList(); } //recherche du statut en mettant les paramétres et la recherche en minuscule if (!string.IsNullOrEmpty(statut)) { commandes = commandes.Where(x => x.Statut.ToLower().Contains(statut.ToLower())).ToList(); } return(commandes.ToList()); } }
//Methode pour récupérer une liste de produits depuis une recherche en String public List <Produit> GetProduits(string search) { using (var context = new BBFRContext()) { if (!string.IsNullOrEmpty(search)) { return(context.Produits.Where(produit => produit.Nom != null && produit.Nom.ToLower().Contains(search.ToLower())) .OrderBy(x => x.ProduitID) .Include(x => x.Type_Produit) .ToList()); } else { return(context.Produits .OrderBy(x => x.ProduitID) .Include(x => x.Type_Produit) .ToList()); } } }
//Methode pour récupérer PLUSIEURS produit dans une liste, grace a une recherche de type string (Non utilisé) public List <Produit> SearchProduits(string search, int?type_produitID, int?sortBy) { using (var context = new BBFRContext()) { var produits = context.Produits.ToList(); if (type_produitID.HasValue) { produits = produits.Where(x => x.Type_Produit.Type_ProduitID == type_produitID.Value).ToList(); } if (!string.IsNullOrEmpty(search)) { produits = produits.Where(x => x.Nom.ToLower().Contains(search.ToLower())).ToList(); } if (sortBy.HasValue) { switch (sortBy.Value) { case 2: produits = produits.OrderByDescending(x => x.ProduitID).ToList(); break; case 3: produits = produits.OrderBy(x => x.Prix).ToList(); break; default: produits = produits.OrderByDescending(x => x.Prix).ToList(); break; } } return(produits.ToList()); } }