Beispiel #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");
                }
            }
        }
Beispiel #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");
         }
      }
 }
Beispiel #3
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);
         }
     }
 }
Beispiel #4
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);
             }
         }
 }
Beispiel #5
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);
         }
     }
 }
Beispiel #6
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);
                }

            }
        }
 public List<string> getAllInfo()
 {
     List<string> info = new List<string>();
     using (ElectricCarEntities context = new ElectricCarEntities())
     {
         foreach (BatteryStorage bs in context.BatteryStorage)
         {
             info.Add(bs.ToString());
         }
     }
     return info;
 }
Beispiel #8
0
 public List<string> getAllInfo()
 {
     List<string> info = new List<string>();
     using (ElectricCarEntities context = new ElectricCarEntities())
     {
         foreach (Period p in context.Period)
         {
             info.Add(p.ToString());
         }
     }
     return info;
 }
Beispiel #9
0
 public List<string> getAllInfo()
 {
     List<string> info = new List<string>();
     using (ElectricCarEntities context = new ElectricCarEntities())
     {
         foreach (Connection c in context.Connections)
         {
             info.Add(c.ToString());
         }
     }
     return info;
 }
Beispiel #10
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);
         }
     }
 }
Beispiel #11
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");
            }
        }
Beispiel #12
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");
         }
     }
 }
Beispiel #13
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);
                }

            }
        }
Beispiel #14
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");
         }
     }
 }
Beispiel #15
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");
         }
     }
 }
Beispiel #16
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();
            }
        }
Beispiel #17
0
 public List<MConnection> getAllRecord(bool getAssociation)
 {
     List<MConnection> connections = new List<MConnection>();
     using (ElectricCarEntities context = new ElectricCarEntities())
     {
         foreach (Connection c in context.Connections)
         {
             MConnection connection = buildConnection(c);
             if (getAssociation)
             {
                 connection.Station1 = dbStation.getRecord(connection.Station1.Id, false);
                 connection.Station2 = dbStation.getRecord(connection.Station2.Id, false);
             }
             connections.Add(connection);
         }
     }
     return connections;
 }
Beispiel #18
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);
         }
     }
 }
Beispiel #19
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);
         }
     }
 }
Beispiel #20
0
        public void deleteRecord(int id)
        {
            using (ElectricCarEntities context = new ElectricCarEntities())
            {
                try
                {
                    Station staToDelete = context.Stations.Find(id);
                    if (staToDelete != null)
                    {
                        context.Entry(staToDelete).State = EntityState.Deleted;
                        context.SaveChanges();
                    }
                }
                catch (Exception)
                {
                    throw new System.NullReferenceException("Can not find nabor station");
                }

            }
        }
Beispiel #21
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");
                }

            }
        }
Beispiel #22
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");
                }

            }
        }
Beispiel #23
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");
            }
        }
Beispiel #24
0
        public void deleteRecord(int bsID, DateTime time)
        {
            using (ElectricCarEntities context = new ElectricCarEntities())
            {
                try
                {
                bool success = false;
                using(TransactionScope scope = new TransactionScope())
                {
                    try
                    {
                    Object[] key = { bsID, time };
                    Period perToDelete = context.Periods.Find(key);
                        context.Entry(perToDelete).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);
                }
            }
        }
Beispiel #25
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");
                }

            }
        }
Beispiel #26
0
 public List<MPeriod> getAllRecord(bool getAssociation)
 {
     List<MPeriod> periods = new List<MPeriod>();
     using (ElectricCarEntities context = new ElectricCarEntities())
     {
         foreach (Period p in context.Period)
         {
             MPeriod period = buildPeriod(p);
             if (getAssociation)
             {
                 //TODO
             }
             periods.Add(period);
         }
     }
     return periods;
 }
Beispiel #27
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");
         }
     }
 }
Beispiel #28
0
 public List<MPeriod> getStoragePeriods(int bsID)
 {
     List<MPeriod> periods = new List<MPeriod>();
     using (ElectricCarEntities context = new ElectricCarEntities())
     {
         foreach (Period p in context.Period)
         {
             if (p.bsId == bsID)
             {
                 MPeriod period = buildPeriod(p);
                 periods.Add(period);
             }
         }
     }
     return periods;
 }
Beispiel #29
0
        public MPeriod getRecord(int bsID, bool getAssociation)
        {
            using (ElectricCarEntities context = new ElectricCarEntities())
            {
                try
                {
                    Period p = context.Period.Find(bsID);
                    MPeriod period = buildPeriod(p);
                    if (getAssociation)
                    {
                        //TODO get stationCtr to retreive station info
                    }

                    return period;
                }
                catch (Exception e)
                {
                    throw new System.NullReferenceException("Can not find period", e);
                    //throw new SystemException("Can not find period");
                }
            }
        }
Beispiel #30
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);
         }
     }
 }