public int ReserveFlight(int userId, int flightId, List <ViewModel.ReseveFlightVM> model)
        {
            using (var tran = _db.Database.BeginTransaction())
            {
                Models.Reserve r = new Models.Reserve()
                {
                    ReserveDate = DateTime.Now,
                    UserId      = userId,
                };
                _db.Reserves.Add(r);
                _db.SaveChanges();
                try
                {
                    foreach (var item in model)
                    {
                        //به ازای هر یک از اطلاعات مسافران اگر آیدی یا کد ملی وجود داشته باشد همان را از دیتابیس می گیریم
                        // در صورتی که اطلاعات در دیتابیس وجود نداشته باشد آنرا میسازیم
                        var passenger = _db.Passengers.FirstOrDefault(p => p.NationalityCode == item.NationalityCode || p.Id == item.Id);


                        if (passenger == null)
                        {
                            passenger = new Models.Passenger()
                            {
                                Age             = item.Age,
                                NationalityCode = item.NationalityCode,
                                CreateDate      = DateTime.Now,
                                FullName        = item.FullName
                            };
                            _db.Passengers.Add(passenger);

                            _db.SaveChanges();
                        }

                        int?          maxCod = _db.Tickets.Any() ? _db.Tickets.Max(p => p.Code) : 0;
                        Models.Ticket t      = new Models.Ticket()
                        {
                            PassengerId = passenger.Id,
                            Code        = maxCod.HasValue ? maxCod.Value + 1 : 1,
                            ReseveId    = r.Id,
                            Price       = 1000,
                            FlightId    = flightId
                        };
                        _db.Tickets.Add(t);
                        _db.SaveChanges();
                    }

                    tran.Commit();

                    return(r.Id);
                }
                catch (System.Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }
        }
Пример #2
0
        public bool Logout(Guid token)
        {
            var find = _db.Tokens.FirstOrDefault(p => p.Token1 == token);

            if (find != null)
            {
                _db.Remove(find);
            }
            _db.SaveChanges();
            return(true);
        }
Пример #3
0
        internal static void InitializeDemoFlights(Models.FlightContext context)
        {
            // StartType
            var start = new StartType()
            {
                Name = "Spilstart", ShortName = "S"
            };

            context.StartTypes.Add(start);
            context.StartTypes.Add(new StartType()
            {
                Name = "Flyslæb", ShortName = "F"
            });
            context.StartTypes.Add(new StartType()
            {
                Name = "Selvstart", ShortName = "M"
            });
            context.SaveChanges();

            // Locations
            var location = new Location {
                Name = "Kongsted", Country = "DK", ICAO = "EKKL"
            };

            context.Locations.Add(location);
            var location2 = new Location {
                Name = "True", Country = "DK"
            };

            context.Locations.Add(location2);
            var location3 = new Location {
                Name = "Slaglille", Country = "DK", ICAO = "EKSL"
            };

            context.Locations.Add(location3);
            var location4 = new Location {
                Name = "Tølløse"
            };

            context.Locations.Add(location4);
            var location5 = new Location {
                Name = "Martin", Country = "SK", ICAO = "LZMA"
            };

            context.Locations.Add(location5);

            context.Locations.Add(new Location()
            {
                Name = "Arnborg", Country = "DK", ICAO = "EK51"
            });
            context.SaveChanges();

            // Clubs
            var club = new Club()
            {
                ClubId = 38, ShortName = "ØSF", Name = "Øst-Sjællands Flyveklub", Location = location, Website = "http://flyveklubben.dk"
            };

            context.Clubs.Add(club);
            var club2 = new Club()
            {
                ClubId = 99, ShortName = "AASVK", Name = "Århus Svæveflyveklub", Location = location2, Website = "http://www.aasvk.dk"
            };

            context.Clubs.Add(club2);
            var club3 = new Club()
            {
                ClubId = 199, ShortName = "MSF", Name = "Midtsjællands Svæveflyveklub", Location = location3, Website = "http://slaglille.dk"
            };

            context.Clubs.Add(club3);
            var club4 = new Club()
            {
                ClubId = 210, ShortName = "TØL", Name = "Tølløse Flyveklub", Location = location4, Website = "http://www.cumulus.dk/"
            };

            context.Clubs.Add(club4);
            context.SaveChanges();

            // Planes
            var pl2 = new Plane
            {
                CompetitionId    = "R2",
                Registration     = "OY-XMO",
                Type             = "ASK21",
                Seats            = 2,
                DefaultStartType = start,
                Engines          = 0
            };

            context.Planes.Add(pl2);
            var pla = new Plane
            {
                CompetitionId    = "RR",
                Registration     = "OY-RRX",
                Class            = "Open",
                Type             = "Duo Discus",
                Seats            = 2,
                DefaultStartType = start,
                Engines          = 1
            };

            context.Planes.Add(pla);
            var pl1 = new Plane
            {
                CompetitionId    = "PU",
                Registration     = "OY-XPU",
                Class            = "15-Meter",
                Type             = "LS6",
                Model            = "LS6a",
                Seats            = 1,
                DefaultStartType = start,
                Engines          = 0
            };

            context.Planes.Add(pl1);
            context.SaveChanges();

            // Pilots
            var pilot = new Pilot {
                Name = "Jan Hebnes", MemberId = "1241", Club = club, Email = "*****@*****.**", MobilNumber = "+4500000000"
            };

            context.Pilots.Add(pilot);
            var pilot1 = new Pilot {
                Name = "Mr Demo Manager", MemberId = "9991", Club = club, MobilNumber = "+4500000001"
            };

            context.Pilots.Add(pilot1);
            var pilot2 = new Pilot {
                Name = "Mr Demo Editor", MemberId = "9992", Club = club, MobilNumber = "+4500000002"
            };

            context.Pilots.Add(pilot2);
            var pilot3 = new Pilot {
                Name = "Mr Demo Pilot", MemberId = "9993", Club = club, MobilNumber = "+4500000003"
            };

            context.Pilots.Add(pilot3);
            var pilot1B = new Pilot {
                Name = "Mr Demo OtherClub Manager", MemberId = "9995", Club = club3, MobilNumber = "+4500000005"
            };

            context.Pilots.Add(pilot1B);
            var pilot2B = new Pilot {
                Name = "Mr Demo OtherClub Editor", MemberId = "9996", Club = club3, MobilNumber = "+4500000006"
            };

            context.Pilots.Add(pilot2B);
            var pilot3B = new Pilot {
                Name = "Mr Demo OtherClub Pilot", MemberId = "9997", Club = club3, MobilNumber = "+4500000007"
            };

            context.Pilots.Add(pilot3B);


            context.SaveChanges();

            GenerateFlights(pl1, pl2, location, pilot, start)
            .ForEach(b => context.Flights.Add(b));

            GenerateFlights(pl1, pl2, location, pilot2, start)
            .ForEach(b => context.Flights.Add(b));

            GenerateFlights(pl1, pl2, location, pilot3, start)
            .ForEach(b => context.Flights.Add(b));


            GenerateFlights(pl1, pl2, location3, pilot2B, start)
            .ForEach(b => context.Flights.Add(b));

            GenerateFlights(pl1, pl2, location3, pilot3B, start)
            .ForEach(b => context.Flights.Add(b));

            GenerateFlights(pl1, pl2, location5, pilot3B, start)
            .ForEach(b => context.Flights.Add(b));

            context.SaveChanges();
        }