示例#1
0
        public ReadOnlyCollection <ILegDTO> FindDepartures(string @from, TransportType allowedTransportTypes)
        {
            UtilityClass.CheckNotNull(from);
            UtilityClass.CheckOnlyAlphanumChar(from);
            UtilityClass.CheckNameLength(from);
            UtilityClass.CheckTransportType(allowedTransportTypes);

            try
            {
                using (var travelCompanyDBContext = new TravelCompanyContext(TravelCompanyConnectionString))
                {
                    List <ILegDTO> legsDTO       = new List <ILegDTO>();
                    var            elementsLegDb = from l in travelCompanyDBContext.legs
                                                   where (l.From == @from && allowedTransportTypes.HasFlag(l.TransportT))
                                                   select l;

                    foreach (var leg in elementsLegDb)
                    {
                        legsDTO.Add(new LegDTO(leg.From, leg.To, leg.Distance, leg.Cost, leg.TransportT));
                    }
                    return(legsDTO.AsReadOnly());
                }
            }
            catch (Exception e)
            {
                throw new DbConnectionException(e.Message, e);
            }
        }
示例#2
0
        public ReadOnlyCollection <ILegDTO> FindLegs(Expression <Func <ILegDTO, bool> > predicate)
        {
            if (predicate is null)
            {
                throw new ArgumentNullException();
            }

            try
            {
                using (var travelCompanyDBContext = new TravelCompanyContext(TravelCompanyConnectionString))
                {
                    List <ILegDTO> legsDTO       = new List <ILegDTO>();
                    var            elementsLegDb = from l in travelCompanyDBContext.legs
                                                   select l;
                    var del = predicate.Compile();
                    foreach (var leg in elementsLegDb)
                    {
                        LegDTO l = new LegDTO(leg.From, leg.To, leg.Distance, leg.Cost, leg.TransportT);
                        if (del.Invoke(l))
                        {
                            legsDTO.Add(l);
                        }
                    }
                    return(new ReadOnlyCollection <ILegDTO>(legsDTO));
                }
            }
            catch (Exception e)
            {
                throw new DbConnectionException(e.Message, e);
            }
        }
示例#3
0
        public ITravelCompany CreateNew(string travelCompanyConnectionString, string name)
        {
            UtilityClass.CheckNotNull(travelCompanyConnectionString);
            UtilityClass.CheckNotNull(name);
            UtilityClass.CheckNotEmpty(travelCompanyConnectionString);
            UtilityClass.CheckNotEmpty(name);
            UtilityClass.CheckConnectionStringLength(travelCompanyConnectionString);
            UtilityClass.CheckNameLength(name);
            UtilityClass.CheckOnlyAlphanumChar(name);

            if (dbConnectionString.Equals(travelCompanyConnectionString))
            {
                throw new SameConnectionStringException();
            }

            try
            {
                using (var brokerDBContext = new TravelCompanyBrokerContext(dbConnectionString))
                {
                    var travelCompany = new TravelCompanyDB()
                    {
                        TravelCompanyName             = name,
                        TravelCompanyConnectionString = travelCompanyConnectionString
                    };

                    brokerDBContext.travelCompanies.Add(travelCompany);
                    brokerDBContext.SaveChanges();

                    using (var travelCompanyDBContext = new TravelCompanyContext(travelCompanyConnectionString))
                    {
                        travelCompanyDBContext.Database.Delete();
                        travelCompanyDBContext.Database.Create();
                    }

                    return(new TravelCompany(name, travelCompanyConnectionString));
                }
            }
            catch (DbUpdateException e)
            {
                if (e.ToString().Contains("KEY"))
                {
                    throw new TapDuplicatedObjectException();
                }

                throw new SameConnectionStringException();
            }
            catch (Exception e)
            {
                throw new DbConnectionException(e.Message, e);
            }
        }
        public ILegDTO GetLegDTOFromId(int legId)
        {
            try
            {
                using (var travelCompanyDBContext = new TravelCompanyContext(travelCompanyConnectionString))
                {
                    var elemLegDb = (from l in travelCompanyDBContext.legs
                                     where l.LegID == legId
                                     select l).Single();

                    return(new LegDTO(elemLegDb.From, elemLegDb.To, elemLegDb.Distance, elemLegDb.Cost, elemLegDb.TransportT));
                }
            }
            catch (InvalidOperationException)
            {
                throw new NonexistentObjectException();
            }
            catch (Exception e)
            {
                throw new DbConnectionException(e.Message, e);
            }
        }
        public int CreateLeg(string @from, string to, int cost, int distance, TAP2017_2018_TravelCompanyInterface.TransportType transportType)
        {
            UtilityClass.CheckNotNull(from);
            UtilityClass.CheckNotNull(to);
            UtilityClass.CheckNameLength(from);
            UtilityClass.CheckNameLength(to);
            UtilityClass.CheckOnlyAlphanumChar(from);
            UtilityClass.CheckOnlyAlphanumChar(to);
            UtilityClass.CheckNotEquals(from, to);
            UtilityClass.CheckStrictlyPositive(cost);
            UtilityClass.CheckStrictlyPositive(distance);
            UtilityClass.CheckTransportType(transportType);

            try
            {
                using (var travelCompanyDBContext = new TravelCompanyContext(travelCompanyConnectionString))
                {
                    var leg = new LegDB()
                    {
                        From       = from,
                        To         = to,
                        Cost       = cost,
                        Distance   = distance,
                        TransportT = transportType
                    };
                    var l = travelCompanyDBContext.legs.Add(leg);
                    travelCompanyDBContext.SaveChanges();
                    return(l.LegID);
                }
            }
            catch (DbUpdateException)
            {
                throw new TapDuplicatedObjectException();
            }
            catch (Exception e)
            {
                throw new DbConnectionException(e.Message, e);
            }
        }
        public void DeleteLeg(int legToBeRemovedId)
        {
            try
            {
                using (var travelCompanyDBContext = new TravelCompanyContext(travelCompanyConnectionString))
                {
                    var elementLegDb = (from l in travelCompanyDBContext.legs
                                        where l.LegID == legToBeRemovedId
                                        select l).Single();

                    travelCompanyDBContext.legs.Remove(elementLegDb);
                    travelCompanyDBContext.SaveChanges();
                }
            }
            catch (InvalidOperationException)
            {
                throw new NonexistentObjectException();
            }
            catch (Exception e)
            {
                throw new DbConnectionException(e.Message, e);
            }
        }