Esempio n. 1
0
 public static Food LoadById(int fdcid)
 {
     try
     {
         using (AmbrosiaEntities dc = new AmbrosiaEntities())
         {
             tblFoodItem row = dc.tblFoodItems.FirstOrDefault(g => g.FDCId == fdcid);
             if (row != null)
             {
                 FoodItem foodItem = new FoodItem
                 {
                     Id       = row.Id,
                     FDCId    = row.FDCId,
                     MealId   = row.MealId,
                     Quantity = row.Quantity
                 };
                 return(fdcFoodManager.Search(foodItem));
             }
             else
             {
                 throw new Exception("Row was not found!");
             }
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Esempio n. 2
0
        public void LoadTest()
        {
            using (AmbrosiaEntities dc = new AmbrosiaEntities())
            {
                DbContextTransaction transaction = null;
                tblFoodItem          row         = dc.tblFoodItems.FirstOrDefault(fi => fi.FDCId == 1);

                Assert.IsTrue(row != null);
                transaction.Rollback();
            }
        }
Esempio n. 3
0
        public void DeleteTest()
        {
            using (AmbrosiaEntities dc = new AmbrosiaEntities())
            {
                DbContextTransaction transaction = null;
                tblFoodItem          row         = dc.tblFoodItems.FirstOrDefault(fi => fi.FDCId == 1);

                dc.tblFoodItems.Remove(row);
                Assert.IsTrue(dc.SaveChanges() == 1);
                transaction.Rollback();
            }
        }
Esempio n. 4
0
        public void InsertTest()
        {
            using (AmbrosiaEntities dc = new AmbrosiaEntities())
            {
                DbContextTransaction transaction = null;
                tblFoodItem          row         = new tblFoodItem
                {
                    Id       = Guid.NewGuid(),
                    FDCId    = 1,
                    MealId   = dc.tblMeals.FirstOrDefault(m => m.Description == "Ate 4 eggs").Id,
                    Quantity = 1
                };
                dc.tblFoodItems.Add(row);

                Assert.IsTrue(dc.SaveChanges() == 1);
                transaction.Rollback();
            }
        }
Esempio n. 5
0
        public static void InsertList(List <Food> foodItems, Guid mealid, bool rollback = false)
        {
            try
            {
                using (AmbrosiaEntities dc = new AmbrosiaEntities())
                {
                    DbContextTransaction transaction = null;
                    if (rollback)
                    {
                        transaction = dc.Database.BeginTransaction();
                    }
                    // Make a new row
                    foreach (Food foodItem in foodItems)
                    {
                        tblFoodItem row = new tblFoodItem();

                        // Set the properties
                        row.Id       = Guid.NewGuid();
                        row.FDCId    = foodItem.FDCId;
                        row.Quantity = foodItem.Quantity;
                        row.MealId   = mealid;


                        // Back fill the Id on the  object (parameter)
                        foodItem.Id = row.Id;

                        // Insert the row
                        dc.tblFoodItems.Add(row);

                        if (rollback)
                        {
                            transaction.Rollback();
                        }
                    }
                    dc.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 6
0
        public static int Update(FoodItem foodItem, bool rollback = false)
        {
            //Update Row
            try
            {
                int results;
                using (AmbrosiaEntities dc = new AmbrosiaEntities())
                {
                    DbContextTransaction transaction = null;
                    if (rollback)
                    {
                        transaction = dc.Database.BeginTransaction();
                    }

                    //New Table Object for row
                    tblFoodItem row = dc.tblFoodItems.FirstOrDefault(g => g.Id == foodItem.Id);

                    if (row != null)
                    {
                        //Set properties
                        row.FDCId    = foodItem.FDCId;
                        row.MealId   = foodItem.MealId;
                        row.Quantity = foodItem.Quantity;

                        results = dc.SaveChanges();
                        if (rollback)
                        {
                            transaction.Rollback();
                        }
                    }
                    else
                    {
                        throw new Exception("Row was not found!!");
                    }
                    return(results);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 7
0
        public static int Insert(FoodItem foodItem, bool rollback = false)
        {
            try
            {
                int results;

                using (AmbrosiaEntities dc = new AmbrosiaEntities())
                {
                    DbContextTransaction transaction = null;
                    if (rollback)
                    {
                        transaction = dc.Database.BeginTransaction();
                    }
                    // Make a new row
                    tblFoodItem row = new tblFoodItem();

                    // Set the properties
                    row.Id       = Guid.NewGuid();
                    row.FDCId    = foodItem.FDCId;
                    row.Quantity = foodItem.Quantity;
                    row.MealId   = foodItem.MealId;


                    // Back fill the Id on the  object (parameter)
                    foodItem.Id = row.Id;

                    // Insert the row
                    dc.tblFoodItems.Add(row);

                    results = dc.SaveChanges();
                    if (rollback)
                    {
                        transaction.Rollback();
                    }
                }
                return(results);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 8
0
        public static int Delete(FoodItem foodItem, bool rollback = false)
        {
            try
            {
                int results;
                using (AmbrosiaEntities dc = new AmbrosiaEntities())
                {
                    DbContextTransaction transaction = null;
                    if (rollback)
                    {
                        transaction = dc.Database.BeginTransaction();
                    }

                    //New Table Object for row
                    tblFoodItem row = dc.tblFoodItems.FirstOrDefault(g => g.Id == foodItem.Id);

                    if (row != null)
                    {
                        //Delete object
                        dc.tblFoodItems.Remove(row);
                        results = dc.SaveChanges();
                        if (rollback)
                        {
                            transaction.Rollback();
                        }
                    }
                    else
                    {
                        throw new Exception("Row was not found!!");
                    }
                    return(results);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }