Beispiel #1
0
        public string registerAirport(string name)
        {
            DB Db = new DB();

            try
            {
                var airport = new Airport {
                    Name = name
                };

                Db.Airport.Add(airport);
                Db.SaveChanges();
                int id = airport.Id;

                WriteLogEvent("Added new airport: " + name + " with id: " + id);

                return("ok");
            }
            catch (Exception e)
            {
                WriteLogError("Adding new airport failed: " + e.Message);
                return("Adding to DB failed");
            }
        }
Beispiel #2
0
        public string editFlight(int id, int fromAirportId, int toAirportId, DateTime departure, DateTime arrival, int price)
        {
            DB Db = new DB();

            try
            {
                var fromAirport  = Db.Airport.Find(fromAirportId);
                var toAirport    = Db.Airport.Find(toAirportId);
                var flightToEdit = Db.Flight.Find(id);

                var From      = flightToEdit.FromAirport.Name;
                var To        = flightToEdit.ToAirport.Name;
                var Departure = flightToEdit.Departure;
                var Arrival   = flightToEdit.Arrival;
                var Price     = flightToEdit.Price;

                flightToEdit.FromAirport = fromAirport;
                flightToEdit.ToAirport   = toAirport;
                flightToEdit.Departure   = departure;
                flightToEdit.Arrival     = arrival;
                flightToEdit.Price       = price;
                Db.SaveChanges();

                WriteLogEvent("Edited flight id " + id + ": From airport was " + From + " " + Departure +
                              ", now " + flightToEdit.FromAirport.Name + " " + flightToEdit.Departure + ". Destination was " + To +
                              " " + Arrival + ", now " + flightToEdit.ToAirport.Name + " " + flightToEdit.Arrival +
                              ". Price was " + Price + ", now " + flightToEdit.Price);

                return("ok");
            }
            catch (Exception e)
            {
                WriteLogError("Could not edit flight id " + id + ". Error: " + e.Message);
                return("Editing in DB failed");
            }
        }
Beispiel #3
0
        public string registerUser(string fornavn, string etternavn, string adresse, string postnummer, string poststed, string epost, byte[] passord)
        {
            try
            {
                var Db = new DB();
                var poststedToInsert = Db.Poststed.Find(postnummer);
                if (poststedToInsert == null)
                {
                    poststedToInsert          = new PostSted();
                    poststedToInsert.Postnr   = postnummer;
                    poststedToInsert.Poststed = poststed;
                    Db.Poststed.Add(poststedToInsert);
                }

                var user = new User();
                user.Fornavn     = fornavn;
                user.Etternavn   = etternavn;
                user.Adresse     = adresse;
                user.Poststed    = poststedToInsert;
                user.Epost       = epost;
                user.PassordHash = passord;

                Db.Users.Add(user);
                Db.SaveChanges();

                WriteLogEvent("Registered new user: "******", " + user.Fornavn + ". " + user.Adresse + " " + user.Poststed.Postnr + " " +
                              user.Poststed.Poststed + ". " + user.Epost + " (id " + user.Id + ")");

                return("ok");
            }
            catch (Exception e)
            {
                WriteLogError("Could not register new user. Error: " + e.Message);
                return("Adding to DB failed");
            }
        }
Beispiel #4
0
        public User getUser(int id)
        {
            DB Db = new DB();

            return(Db.Users.Find(id));
        }
Beispiel #5
0
        public Airport getAirport(int id)
        {
            DB Db = new DB();

            return(Db.Airport.Find(id));
        }
Beispiel #6
0
        public Booking getBooking(int id)
        {
            DB Db = new DB();

            return(Db.Booking.Find(id));
        }
Beispiel #7
0
        public Flight getFlight(int id)
        {
            DB Db = new DB();

            return(Db.Flight.Find(id));
        }
Beispiel #8
0
        public List <Flight> getAllFlights(string from, string to, string departure)
        {
            DB  Db           = new DB();
            var fromSet      = int.TryParse(from, out int fromId);
            var toSet        = int.TryParse(to, out int toId);
            var departureSet = DateTime.TryParse(departure, out DateTime departureFilter);

            if (fromSet && !toSet && !departureSet)
            {
                return(Db.Flight.Where(f => f.FromAirport.Id == fromId).
                       OrderBy(f => f.Departure).ThenBy(f => f.ToAirport.Name).ToList());
            }
            else if (!fromSet && toSet && !departureSet)
            {
                return(Db.Flight.Where(f => f.ToAirport.Id == toId).
                       OrderBy(f => f.Departure).ThenBy(f => f.FromAirport.Name).ToList());
            }
            else if ((!fromSet && !toSet && departureSet) && (String.IsNullOrEmpty(from) && String.IsNullOrEmpty(to)))
            {
                return(Db.Flight.Where(f => DbFunctions.TruncateTime(f.Departure) == departureFilter.Date).
                       OrderBy(f => f.Departure).ThenBy(f => f.FromAirport.Name).ThenBy(f => f.ToAirport.Name).ToList());
            }
            else if (fromSet && toSet && !departureSet)
            {
                return(Db.Flight.Where(f => f.FromAirport.Id == fromId && f.ToAirport.Id == toId).
                       OrderBy(f => f.Departure).ToList());
            }
            else if (fromSet && !toSet && departureSet)
            {
                return(Db.Flight.Where(f => f.FromAirport.Id == fromId && DbFunctions.TruncateTime(f.Departure) == departureFilter.Date).
                       OrderBy(f => f.Departure).ThenBy(f => f.ToAirport.Name).ToList());
            }
            else if (!fromSet && toSet && departureSet)
            {
                return(Db.Flight.Where(f => f.ToAirport.Id == toId && DbFunctions.TruncateTime(f.Departure) == departureFilter.Date).
                       OrderBy(f => f.Departure).ThenBy(f => f.FromAirport.Name).ToList());
            }
            else if (fromSet && toSet && departureSet)
            {
                return(Db.Flight.Where(f => f.FromAirport.Id == fromId && f.ToAirport.Id == toId && DbFunctions.TruncateTime(f.Departure) == departureFilter.Date).
                       OrderBy(f => f.Departure).ToList());
            }
            else
            {
                if (!String.IsNullOrEmpty(from) && String.IsNullOrEmpty(to))
                {
                    return(Db.Flight.Where(f => f.FromAirport.Name == from).ToList());
                }
                if (!String.IsNullOrEmpty(to) && String.IsNullOrEmpty(from))
                {
                    return(Db.Flight.Where(f => f.ToAirport.Name == to).ToList());
                }
                if (!String.IsNullOrEmpty(from) && !String.IsNullOrEmpty(to))
                {
                    return(Db.Flight.Where(f => f.FromAirport.Name == from && f.ToAirport.Name == to).ToList());
                }


                return(Db.Flight.OrderBy(f => f.Departure).ThenBy(f => f.FromAirport.Name).ThenBy(f => f.ToAirport.Name).ToList());
            }
        }