Example #1
0
        public void deleteAllBookingLineForBooking(int bookingid)
        {
            using (ElectricCarEntities context = new ElectricCarEntities())
            {
                try
                {
                    BookingLine[] blsToDelete;
                    var items = from item in context.BookingLines where item.bId == bookingid select item;
                    blsToDelete = items.ToArray<BookingLine>();

                    if (blsToDelete != null)
                    {
                        foreach (BookingLine item in blsToDelete)
                        {
                            context.Entry(item).State = EntityState.Deleted;
                        }

                    }
                    context.SaveChanges();
                }
                catch (Exception)
                {

                    throw new System.NullReferenceException("Can not find booking line");
                }
            }
        }
Example #2
0
 public int addNewRecord(string name, string producer, decimal capacity, decimal exchangeCost, int storageNumber)
 {
     using (ElectricCarEntities context = new ElectricCarEntities())
     {
         try
         {
             int newid = context.BatteryType.Count() + 1;
             context.BatteryType.Add(new BatteryType()
             {
                 Id = newid,
                 name = name,
                 producer = producer,
                 capacity = capacity,
                 exchangeCost = exchangeCost,
                 storageNumber = storageNumber
             });
             context.SaveChanges();
             return newid;
         }
         catch (Exception)
         {
             throw new SystemException("Can not add battery type");
         }
      }
 }
Example #3
0
 public int addNewRecord(string name, string producer, decimal capacity, decimal exchangeCost)
 {
     using (TransactionScope transaction = new TransactionScope((TransactionScopeOption.Required)))
     {
         try
         {
             int newid = -1;
             using (ElectricCarEntities context = new ElectricCarEntities())
             {
                 bool failed = true;
                 do
                 {
                     try
                     {
                         try
                         {
                             var max = context.BatteryTypes.Max(bt => bt.Id);
                             newid = (int)max + 1;
                         }
                         catch (Exception)
                         {
                             newid = 1;
                         }
                         context.BatteryTypes.Add(new BatteryType()
                     {
                         Id = newid,
                         name = name,
                         producer = producer,
                         capacity = capacity,
                         exchangeCost = exchangeCost,
                     });
                      context.SaveChanges();
                         failed = false;
                     }
                     // needs to be done:
                     // set 'Concurrency Mode = Fixed' in the property panel for the timestamp in the designer
                     // DbUpdateConcurrencyException or OptimisticConcurrencyException
                     catch (DbUpdateConcurrencyException e)
                     {
                         // just in case, shouldn't be needed to set explicitly failed
                         failed = true;
                         e.Entries.Single().Reload();
                         Console.WriteLine("DbUpdateConcurrencyException with message: " +
                             e.Message + "\n\n was handled and trying again");
                     }
                 } while (failed);
             }
             transaction.Complete();
             return newid;
             }
             catch (TransactionAbortedException e)
             {
                 throw new SystemException("Cannot finish transaction for adding Battery Type " +
                    " with an error " + e.Message);
             }
         }
 }
Example #4
0
 // delete LoginInfoes from arguments
 public int addNewRecord(string fName, string lName, string address, string country, string phone, string email, MDiscountGroup discountGroup, string payStatus)
 {
     using (TransactionScope transaction = new TransactionScope((TransactionScopeOption.Required)))
     {
         try
         {
             int newId = -1;
             using (ElectricCarEntities context = new ElectricCarEntities())
             {
                 try
                 {
                     int max;
                     try {
                         max = context.People.Max(cust => cust.Id);
                     } catch {
                         max = 0;
                     }
                     newId = max + 1;
                     context.People.Add(new Customer()
                     {
                         Id = newId,
                         fName = fName,
                         lname = lName,
                         address = address,
                         country = country,
                         phone = phone,
                         email = email,
                         // together and after customer creation add logInfo and include returned
                         // person Id in there
                         // LoginInfoes = DLogInfo.buildLogInfos(logInfos),
                         // TODO: change to enum
                         pType = PType.Customer.ToString(),
                         payStatus = null,
                         // TODO: not considering putting the ICollection<Booking> Bookings on Customer record creation
                         // TODO: dgId or DiscountGroup, using dgId
                         dgId = discountGroup.ID,
                         // DiscoutGroup = DDiscountGroup.buildDiscountGroup(discountGroup)
                     });
                     context.SaveChanges();
                 }
                 catch (Exception e)
                 {
                     throw new SystemException("Cannot add new Customer record " +
                         " with an error " + e.Message);
                 }
             }
             transaction.Complete();
             return newId;
         }
         catch (TransactionAbortedException e)
         {
             throw new SystemException("Cannot finish transaction for adding Customer " +
                " with an error " + e.Message);
         }
     }
 }
Example #5
0
        public int addNewRecord(int btID, int sID, int storageNumber)
        {
            // althought 'Required' is supposed to be default
            using (TransactionScope transaction = new TransactionScope((TransactionScopeOption.Required)))
            {
                try
                {
                    int newid = -1;
                    using (ElectricCarEntities context = new ElectricCarEntities())
                    {
                        bool failed = true;
                        do
                        {
                        try
                        {
                            try
                            {
                                var max = context.BatteryStorages.Max(bs => bs.Id);
                                newid = (int)max + 1;
                            }
                            catch (Exception)
                            {
                                newid = 1;
                            }
                            context.BatteryStorages.Add(new BatteryStorage()
                            {
                                Id = newid,
                                sId = sID,
                                btId = btID,
                                storageNumber = storageNumber
                            });
                            context.SaveChanges();
                            failed = false;
                        }
                       catch (DbUpdateConcurrencyException e)
                            {
                                // just in case, shouldn't be needed to set explicitly failed
                                failed = true;
                                e.Entries.Single().Reload();
                                Console.WriteLine("DbUpdateConcurrencyException with message: " +
                                    e.Message + "\n\n was handled and trying again");
                            }
                        } while (failed);
                    }
                    transaction.Complete();
                    return newid;
                    }
                catch (TransactionAbortedException e)
                {
                    throw new SystemException("Cannot finish transaction for adding Battery Storage " +
                       " with an error " + e.Message);
                }

            }
        }
Example #6
0
 public int addNewRecord(string fName, string lName, string address, string country, string phone, string email, int sId, EmployeePosition position)
 {
     using (TransactionScope transaction = new TransactionScope((TransactionScopeOption.Required)))
     {
         try
         {
             int newId = -1;
             using (ElectricCarEntities context = new ElectricCarEntities())
             {
                 try
                 {
                     int max;
                     try {
                         max = context.People.Max(emp => emp.Id);
                     } catch {
                         max = 0;
                     }
                     newId = max + 1;
                     context.People.Add(new Employee()
                     {
                         Id = newId,
                         fName = fName,
                         lname = lName,
                         address = address,
                         country = country,
                         phone = phone,
                         email = email,
                         // together and after employee creation add logInfo and include returned
                         // person Id in there
                         // LoginInfoes = DLogInfo.buildLogInfos(logInfos),
                         pType = PType.Employee.ToString(),
                         sId = sId,
                         position = position.ToString(),
                     });
                     context.SaveChanges();
                 }
                 catch (Exception e)
                 {
                     throw new SystemException("Cannot add new Employee record " +
                          " with an error " + e.Message);
                 }
             }
             transaction.Complete();
             return newId;
         }
         catch (TransactionAbortedException e)
         {
             throw new SystemException("Cannot finish transaction for adding an Employee " +
                 " with an error " + e.Message);
         }
     }
 }
Example #7
0
        public int addRecord(int CId, decimal TotalPrice, DateTime CreateDate, DateTime TripStart, string CreditCard)
        {
            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    int newid = -1;

                    using (ElectricCarEntities context = new ElectricCarEntities())
                    {
                        if (context.Bookings.Count() == 0)
                        {
                            newid = 1;
                        }
                        else
                        {
                            newid = context.Bookings.Max(x => x.Id) + 1;
                        }

                        Booking b = new Booking();
                        b.Id = newid;
                        b.cId = CId;
                        b.totalPrice = TotalPrice;
                        b.createDate = CreateDate;
                        b.tripStart = TripStart;
                        b.creaditCard = CreditCard;
                        context.Bookings.Add(b);
                        //context.Bookings.Add(new Booking()
                        //{
                        //Id = newid,
                        //cId = CId,
                        //totalPrice = TotalPrice,
                        //createDate = CreateDate,
                        //tripStart = TripStart,
                        //creaditCard = CreditCard
                        //});
                    context.SaveChanges();
                    }
                    scope.Complete();
                    return newid;
                }
            }
            catch (TransactionAbortedException)
            {

                throw new SystemException("Can not add new booking");
            }
        }
Example #8
0
 public void deleteRecord(int id1, int id2)
 {
     using (ElectricCarEntities context = new ElectricCarEntities())
     {
         Connection conToDelete = context.Connections.Find(id1, id2);
         if (conToDelete != null)
         {
             context.Entry(conToDelete).State = EntityState.Deleted;
             context.SaveChanges();
         }
         else
         {
             throw new System.NullReferenceException("Can not find nabor station");
         }
     }
 }
Example #9
0
        public int addNewRecord(int bsID, DateTime time, int init, int cust)
        {
            using (TransactionScope transaction = new TransactionScope((TransactionScopeOption.Required)))
            {
                try
                {
                    int newid = -1;
                    using (ElectricCarEntities context = new ElectricCarEntities())
                    {
                        bool failed = true;
                        do
                        {
                            try

                        {
                            context.Periods.Add(new Period()
                            {
                                bsId = bsID,
                                time = time,
                                avaiNumber = init,
                                custBookNumber = cust
                            });
                            newid = bsID;
                            context.SaveChanges();
                            failed = false;
                        }
                            catch (DbUpdateConcurrencyException e)
                            {
                                // just in case, shouldn't be needed to set explicitly failed
                                failed = true;
                                e.Entries.Single().Reload();
                                Console.WriteLine("DbUpdateConcurrencyException with message: " +
                                    e.Message + "\n\n was handled and trying again");
                            }
                        } while (failed);
                    }
                    transaction.Complete();
                    return newid;
                }
                catch (TransactionAbortedException e)
                {
                    throw new SystemException("Cannot finish transaction for adding Period " +
                       " with an error " + e.Message);
                }

            }
        }
Example #10
0
        public void addRecord(int BId, int BtId, int SId, int Quantity, decimal Price, DateTime Time)
        {
            using (ElectricCarEntities context = new ElectricCarEntities())
            {

                context.BookingLines.Add(new BookingLine()
                {
                    bId = BId,
                    btId = BtId,
                    sId = SId,
                    quantity = Quantity,
                    price = Price,
                    time = Time
                });
                context.SaveChanges();
            }
        }
Example #11
0
 public void deleteRecord(int id)
 {
     using (ElectricCarEntities context = new ElectricCarEntities())
     {
         BatteryStorage storToDelete = context.BatteryStorage.Find(id);
         if (storToDelete != null)
         {
             context.Entry(storToDelete).State = EntityState.Deleted;
             context.SaveChanges();
         }
         else
         {
             throw new System.NullReferenceException("Can not find battery storage");
             //throw new SystemException("Can not find battery storage");
         }
     }
 }
Example #12
0
 public void deleteRecord(int bsID)
 {
     using (ElectricCarEntities context = new ElectricCarEntities())
     {
         Period perToDelete = context.Period.Find(bsID);
         if (perToDelete != null)
         {
             context.Entry(perToDelete).State = EntityState.Deleted;
             context.SaveChanges();
         }
         else
         {
             throw new System.NullReferenceException("Can not find period");
             //throw new SystemException("Can not find period");
         }
     }
 }
Example #13
0
 public int addNewRecord(string loginName, string password, int personId)
 {
     // althought 'Required' is supposed to be default
     using (TransactionScope transaction = new TransactionScope((TransactionScopeOption.Required)))
     {
         try
         {
             int newId = -1;
             using (ElectricCarEntities context = new ElectricCarEntities())
             {
                     try
                     {
                         int max;
                         try {
                             max = context.LoginInfoes.Max(li => li.Id);
                         } catch {
                             max = 0;
                         }
                         newId = max + 1;
                         context.LoginInfoes.Add(new LoginInfo()
                         {
                             Id = newId,
                             name = loginName,
                             password = password,
                             pId = personId
                         });
                         context.SaveChanges();
                     }
                     catch (Exception e)
                     {
                         throw new SystemException("Cannot add new Log Info with an error: " + e.Message);
                     }
             }
             transaction.Complete();
             return newId;
         }
         catch (TransactionAbortedException e)
         {
             throw new SystemException("Cannot finish transaction for adding Login Info " +
                " with an error " + e.Message);
         }
     }
 }
Example #14
0
 public int addNewRecord(string name, Nullable<decimal> discount)
 {
     using (TransactionScope transaction = new TransactionScope((TransactionScopeOption.Required)))
     {
         try
         {
             int newId = -1;
             using (ElectricCarEntities context = new ElectricCarEntities())
             {
                 try
                 {
                     int max;
                     try {
                         max = context.DiscoutGroups.Max(dg => dg.Id);
                     } catch {
                         max = 0;
                     }
                     newId = max + 1;
                     context.DiscoutGroups.Add(new DiscoutGroup()
                     {
                         Id = newId,
                         name = name,
                         dgRate = discount
                     });
                     context.SaveChanges();
                 }
                 catch (Exception e)
                 {
                     throw new SystemException("Cannot add new Discount Group record " +
                         " with an error " + e.Message);
                 }
             }
             transaction.Complete();
             return newId;
         }
         catch (TransactionAbortedException e)
         {
             throw new SystemException("Cannot finish transaction for adding Discount Group " +
                " with an error " + e.Message);
         }
     }
 }
Example #15
0
        public void deleteRecord(int id)
        {
            using (ElectricCarEntities context = new ElectricCarEntities())
            {
                try
                {
                    Booking booToDelete = context.Bookings.Find(id);
                    if (booToDelete != null)
                    {
                        context.Entry(booToDelete).State = EntityState.Deleted;
                        context.SaveChanges();
                    }
                }
                catch (Exception)
                {
                    throw new System.NullReferenceException("Can not find booking");
                }

            }
        }
Example #16
0
        public void addNewRecord(int id1, int id2, decimal dist, decimal time)
        {
            using (ElectricCarEntities context = new ElectricCarEntities())
            {
                try
                {
                    context.Connections.Add(new Connection()
                    {
                        sId1 = id1,
                        sId2 = id2,
                        distance = dist,
                        driveHour = time
                    });
                    context.SaveChanges();
                }
                catch (Exception)
                {
                    throw new SystemException("Can not add association between two stations");
                }

            }
        }
Example #17
0
        public int addNewRecord(int btID, int sID)
        {
            using (ElectricCarEntities context = new ElectricCarEntities())
            {
                try
                {
                    int newid = context.Battery.Count() + 1;
                    context.BatteryStorage.Add(new BatteryStorage()
                    {
                        Id = newid,
                        sId = sID,
                        btId = btID
                    });
                    context.SaveChanges();
                    return newid;
                }
                catch (Exception)
                {
                    throw new SystemException("Can not add battery storage");
                }

            }
        }
Example #18
0
        public int addNewRecord(string Name, string Address, string Country, string State)
        {
            try
            {
                using (TransactionScope scope = new TransactionScope()) //open transaction
                {
                    int newid = -1;
                    using (ElectricCarEntities context = new ElectricCarEntities())
                    {
                        if (context.Stations.Count() == 0)
                        {
                            newid = 1;
                        }
                        else
                        {
                            newid = context.Stations.Max(x => x.Id) + 1;
                        }
                        context.Stations.Add(new Station()
                        {
                            Id = newid,
                            name = Name,
                            address = Address,
                            country = Country,
                            state = State
                        });
                        context.SaveChanges();  //update station table
                    }
                    scope.Complete(); //close transaction
                    return newid;
                }

            }
            catch (TransactionAbortedException)
            {
                throw new SystemException("Can not add new station");
            }
        }
Example #19
0
        public int addNewRecord(int bsID, DateTime time, int init, int cust, int future)
        {
            using (ElectricCarEntities context = new ElectricCarEntities())
            {
                try
                {
                    context.Period.Add(new Period()
                    {
                        bsId = bsID,
                        time = time,
                        avaiNumber = init,
                        custBookNumber = cust,
                        futureBookNumber = future
                    });
                    context.SaveChanges();
                    return bsID;
                }
                catch (Exception)
                {
                    throw new SystemException("Can not add period");
                }

            }
        }
Example #20
0
        public void deleteRecordByType(int btID)
        {
            using (ElectricCarEntities context = new ElectricCarEntities())
            {
                try
                {
                    bool success = false;
                    using (TransactionScope scope = new TransactionScope())
                    {
                        try
                        {
                            BatteryStorage storToDelete = (BatteryStorage) context.BatteryStorages.Where(bt=>btID==bt.btId).FirstOrDefault();
                            context.Entry(storToDelete).State = EntityState.Deleted;
                            context.SaveChanges();
                            success = true;
                        }
                        catch (Exception)
                        {
                            throw new System.NullReferenceException("Can not delete battery storage. Delete battery type first.");
                            //throw new SystemException("Can not find battery type");
                        }
                        if (success)
                        {
                            scope.Complete();
                        }

                    }
                }
                catch (TransactionAbortedException e)
                {
                    throw new SystemException("Cannot finish transaction for deleting BatteryType " +
                       " with an error " + e.Message);
                }
            }
        }
Example #21
0
 public void updateRecord(int bsID, DateTime time, int init, int cust, int future)
 {
     using (ElectricCarEntities context = new ElectricCarEntities())
     {
         Period perToUpdate = context.Period.Find(bsID);
         if (perToUpdate != null)
         {
             perToUpdate.time = time;
             perToUpdate.avaiNumber = init;
             perToUpdate.custBookNumber = cust;
             perToUpdate.futureBookNumber = future;
             context.SaveChanges();
         }
         else
         {
             throw new System.NullReferenceException("Can not find period");
             //throw new SystemException("Can not find period");
         }
     }
 }
Example #22
0
 public void deleteRecord(int id)
 {
     using (TransactionScope transaction = new TransactionScope((TransactionScopeOption.Required)))
     {
         try
         {
             using (ElectricCarEntities context = new ElectricCarEntities())
             {
                 try
                 {
                     DiscoutGroup dg = context.DiscoutGroups.Find(id);
                     if (dg != null)
                     {
                         context.Entry(dg).State = EntityState.Deleted;
                         context.SaveChanges();
                     }
                 }
                 catch (Exception e)
                 {
                     throw new SystemException("Cannot delete Discount Group " + id + " record " +
                         " with an error " + e.Message);
                 }
             }
             transaction.Complete();
         }
         catch (TransactionAbortedException e)
         {
             throw new SystemException("Cannot finish transaction for deleting Discount Group " +
                " with an error " + e.Message);
         }
     }
 }
Example #23
0
 public void updateRecord(int id, string loginName, string password)
 {
     using (TransactionScope transaction = new TransactionScope((TransactionScopeOption.Required)))
     {
         try
         {
             using (ElectricCarEntities context = new ElectricCarEntities())
             {
                 try
                 {
                     LoginInfo li = context.LoginInfoes.Find(id);
                     li.name = loginName;
                     li.password = password;
                     context.SaveChanges();
                 }
                 catch (Exception e)
                 {
                     throw new SystemException("Cannot update Login Info " + id + " record " +
                         " with an error " + e.Message);
                 }
             }
             transaction.Complete();
         }
         catch (TransactionAbortedException e)
         {
             throw new SystemException("Cannot finish transaction for updating Login Info " +
                " with an error " + e.Message);
         }
     }
 }
Example #24
0
 public void updateRecord(int id, string name, Nullable<decimal> discount)
 {
     using (TransactionScope transaction = new TransactionScope((TransactionScopeOption.Required)))
     {
         try
         {
             using (ElectricCarEntities context = new ElectricCarEntities())
             {
                 try
                 {
                     DiscoutGroup dg = context.DiscoutGroups.Find(id);
                     dg.name = name;
                     dg.dgRate = discount;
                     context.SaveChanges();
                 }
                 catch (Exception e)
                 {
                     throw new SystemException("Cannot update Discount Group " + id + " record " +
                         " with an error " + e.Message);
                 }
             }
             transaction.Complete();
         }
         catch (TransactionAbortedException e)
         {
             throw new SystemException("Cannot finish transaction for updating Discount Group " +
                " with an error " + e.Message);
         }
     }
 }
Example #25
0
        public void deleteRecord(int id)
        {
            using (ElectricCarEntities context = new ElectricCarEntities())
            {
                try
                {
                bool success = false;
                using(TransactionScope scope = new TransactionScope())
                {
                    try
                    {
                         BatteryType btToDelete = context.BatteryTypes.Find(id);

                                context.Entry(btToDelete).State = EntityState.Deleted;
                                context.SaveChanges();
                                success = true;
                     }
                        catch(Exception)
                        {
                            throw new System.NullReferenceException("Can not find battery type");
                            //throw new SystemException("Can not find battery type");
                        }
                        if (success)
                        {
                            scope.Complete();
                        }

                    }
                }
                    catch (TransactionAbortedException e)
                    {
                        throw new SystemException("Cannot finish transaction for deleting BatteryType " +
                           " with an error " + e.Message);
                    }
                }
        }
Example #26
0
 public void updateRecord(int id, int cId, decimal totalPrice, DateTime createDate, DateTime tripStart, string creditCard)
 {
     using (ElectricCarEntities context = new ElectricCarEntities())
     {
         Booking booUpToDate = context.Bookings.Find(id);
         if (booUpToDate != null)
         {
             booUpToDate.cId = cId;
             booUpToDate.totalPrice = totalPrice;
             booUpToDate.createDate = createDate;
             booUpToDate.tripStart = tripStart;
             booUpToDate.creaditCard = creditCard;
             context.SaveChanges();
         }
         else
         {
             throw new System.NullReferenceException("Can not find booking");
         }
     }
 }
Example #27
0
 public void updateRecord(int id, string name, string producer, decimal capacity, decimal exchangeCost)
 {
     using (ElectricCarEntities context = new ElectricCarEntities())
     {
         try
         {
             bool success = false;
             using (TransactionScope scope = new TransactionScope())
             {
                 try
                 {
                     BatteryType btToUpdate = context.BatteryTypes.Find(id);
                     btToUpdate.name = name;
                     btToUpdate.producer = producer;
                     btToUpdate.capacity = capacity;
                     btToUpdate.exchangeCost = exchangeCost;
                     context.SaveChanges();
                     success = true;
                 }
                 catch (Exception)
                 {
                     throw new System.NullReferenceException("Can not find battery type");
                     //throw new SystemException("Can not find battery type");
                 }
                 if (success)
                 {
                     scope.Complete();
                 }
             }
         }
         catch (Exception)
         {
             throw new SystemException("Can not open transaction scope");
         }
     }
 }
Example #28
0
 public void updateRecord(int id, string Name, string Address, string Country, string State)
 {
     using (ElectricCarEntities context = new ElectricCarEntities())
     {
         Station staUpToDate = context.Stations.Find(id);
         if (staUpToDate != null)
         {
             staUpToDate.name = Name;
             staUpToDate.address = Address;
             staUpToDate.country = Country;
             staUpToDate.state = State;
             context.SaveChanges();
         }
         else
         {
             throw new System.NullReferenceException("Can not find nabor station");
         }
     }
 }
Example #29
0
        public void updateRecord(int id, string state, int btid)
        {
            using (ElectricCarEntities context = new ElectricCarEntities())
            {
                try
                {
                    bool success = false;
                    using (TransactionScope scope = new TransactionScope())
                    {
                        try
                        {
                        Battery batToUpdate = context.Batteries.Find(id);

                            batToUpdate.state = state;
                            batToUpdate.btId = btid;
                             context.SaveChanges();
                            success = true;
                    }
                        catch (Exception)
                        {
                            throw new System.NullReferenceException("Can not find battery type");
                            //throw new SystemException("Can not find battery type");
                        }
                        if (success)
                        {
                            scope.Complete();
                        }
                    }
                }
                catch (Exception)
                {
                    throw new SystemException("Can not open transaction scope");
                }
            }
        }
Example #30
0
 public void updateRecord(int bId, int btId, int sId, int quantity, decimal price, DateTime time)
 {
     using (ElectricCarEntities context = new ElectricCarEntities())
     {
         BookingLine blToDate = context.BookingLines.Find(bId, btId, sId);
         if (blToDate != null)
         {
             blToDate.quantity = quantity;
             blToDate.price = price;
             blToDate.time = time;
             context.SaveChanges();
         }
         else
         {
             throw new System.NullReferenceException("Can not find booking line");
         }
     }
 }