Esempio n. 1
0
        public int CreateUser(string username,
                              string password,
                              string email,
                              string firstName,
                              string lastName,
                              string ucn,
                              string address,
                              string phoneNumber
                              )
        {
            var user = new AppUser()
            {
                UserName    = username,
                Email       = email,
                FirstName   = firstName,
                LastName    = lastName,
                UCN         = ucn,
                Address     = address,
                PhoneNumber = phoneNumber,
            };

            if (userManager.FindByNameAsync(username).Result == null)
            {
                var createdUser = userManager.CreateAsync(user, password);
                if (createdUser.Result.Succeeded)
                {
                    userManager.AddToRoleAsync(user, "employee").Wait();
                    context.SaveChanges();
                    return(1);
                }
            }
            return(-1);
        }
Esempio n. 2
0
        public Reservation ChangeTicketType(Reservation reservation)
        {
            dBContext.Reservations.Where(r => r.ReservationID == reservation.ReservationID).First().TicketType = reservation.TicketType == "Business" ? "Regular" : "Business";
            dBContext.SaveChanges();

            return(reservation);
        }
Esempio n. 3
0
        private static void Main()
        {
            using (var flightManagerDbContext = new FlightManagerDbContext())
            {
                flightManagerDbContext.Database.Migrate();
                for (int i = 0; i < 10; ++i)
                {
                    flightManagerDbContext.Set <Aircraft>().Add(
                        new Aircraft()
                    {
                        Name = string.Concat("Aircraft ", i), Count = 5, InFlyFuelConsumption = (i * 158.99), InTakeOffFuelConsumption = (58.45 + i), PassengerCapacity = 200, MaxSpeed = "Not Defined"
                    }
                        );
                }

                Random random = new Random();
                for (int i = 0; i < 100; ++i)
                {
                    flightManagerDbContext.Set <Airport>().Add(
                        new Airport()
                    {
                        Name = string.Concat("Airport", i), Latitude = random.NextDouble(), Longitude = random.NextDouble()
                    }
                        );
                }

                flightManagerDbContext.SaveChanges();
            }
        }
Esempio n. 4
0
        public void Edit(UserServiceModel user)
        {
            if (!Contains(user.Id))
            {
                throw new ArgumentException("Invalid user id!");
            }

            var userFromDb = context.Users.SingleOrDefault(u => u.Id == user.Id);

            userFromDb.FirstName = user.FirstName;
            userFromDb.LastName  = user.LastName;
            userFromDb.Address   = user.Address;
            userFromDb.EGN       = user.EGN;

            context.Users.Update(userFromDb);
            context.SaveChanges();
        }
        public void Make(ReservationServiceModel input)
        {
            if (input.TicketsCount <= 0)
            {
                return;
            }

            var flight = context.Flights.SingleOrDefault(f => f.Id == input.FlightId);

            if (input.TicketType == TicketType.Business && input.TicketsCount > flight.FreeBusinessSeats)
            {
                return;
            }

            if (input.TicketType == TicketType.Normal && input.TicketsCount > flight.FreePassengersSeats)
            {
                return;
            }

            var reservation = new Reservation()
            {
                FirstName    = input.FirstName,
                SecondName   = input.SecondName,
                LastName     = input.LastName,
                EGN          = input.EGN,
                Email        = input.Email,
                FlightId     = input.FlightId,
                IsConfirmed  = false,
                Nationality  = input.Nationality,
                TicketsCount = input.TicketsCount,
                TicketType   = input.TicketType,
                PhoneNumber  = input.PhoneNumber
            };

            context.Reservations.Add(reservation);
            context.SaveChanges();

            var message = $@"Dear {reservation.FirstName} {reservation.LastName}, do you wish to confirm your reservation for {reservation.TicketsCount} {reservation.TicketType} Tickets from {flight.From} to {flight.To}?
              <br/>
               <a href='{GlobalConstants.AppAddress}Reservation/Confirm?id={reservation.Id}'>Click here</a> to confirm it.
                <br/>
                <a href='{GlobalConstants.AppAddress}Reservation/Delete?id={reservation.Id}'>Click here</a> to delete it.
            ";

            emailSender.SendEmailAsync(reservation.Email, "Confirm Your Reservation", message).GetAwaiter().GetResult();
        }
Esempio n. 6
0
        public FlightUser CreateUser(string username, string password, string firstName, string lastName, string sSN, string address, string phoneNumber)
        {
            FlightUser user = new FlightUser()
            {
                UserName    = username,
                FirstName   = firstName,
                LastName    = lastName,
                SSN         = sSN,
                Address     = address,
                PhoneNumber = phoneNumber
            };

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

            return(user);
        }
Esempio n. 7
0
        public void Create(FlightServiceModel input)
        {
            var flight = new Flight
            {
                From                = input.From,
                To                  = input.To,
                ArrivalTime         = input.ArrivalTime,
                DepartureTime       = input.DepartureTime,
                FreePassengersSeats = input.FreePassengersSeats,
                FreeBusinessSeats   = input.FreeBusinessSeats,
                PlaneNumber         = input.PlaneNumber,
                PlaneType           = input.PlaneType,
                Image               = input.Image,
                PilotName           = input.PilotName
            };

            context.Flights.Add(flight);
            context.SaveChanges();
        }
Esempio n. 8
0
        public int CreateFlight(string departureLocation,
                                string landingLocation,
                                DateTime departureDateTime,
                                DateTime landingDatetime,
                                string planeType,
                                string planeUniqueId,
                                string pilotName,
                                int regularSeats,
                                int businessSeats)
        {
            if (departureLocation == landingLocation)
            {
                return(-1);                                      //both locations cant be the same
            }
            if (DateTime.Compare(departureDateTime, landingDatetime) > 0)
            {
                return(-2);                                                          //the departure cant be ahead of the landing
            }
            var flights = context.Flights.FirstOrDefault(x => x.PlaneUniqueId == planeUniqueId);

            if (flights != null)
            {
                return(-3);                 //the plane id should be unique
            }
            var flight = new Flight
            {
                DepartureLocation = departureLocation,
                LandingLocation   = landingLocation,
                DepartureDateTime = departureDateTime,
                LandingDateTime   = landingDatetime,
                PlaneType         = planeType,
                PlaneUniqueId     = planeUniqueId,
                PilotName         = pilotName,
                RegularSeats      = regularSeats,
                BusinessSeats     = businessSeats
            };

            context.Flights.Add(flight);
            context.SaveChanges();

            return(flight.Id);
        }
        /// <summary>
        /// Seed-ване на базата данни при първоначално стартиране.
        /// </summary>
        //Seed Admin
        public void SeedRoles()
        {
            string[] roles = { "Admin", "Worker" };

            if (!dbContext.Roles.Any(r => r.Name == "admin"))
            {
                foreach (string role in roles)
                {
                    IdentityRole identityRole = new IdentityRole()
                    {
                        Name           = role,
                        NormalizedName = role.ToLower()
                    };

                    if (!dbContext.Roles.Any(r => r.Name == role))
                    {
                        dbContext.Roles.Add(identityRole);
                    }
                }
            }
            dbContext.SaveChanges();
        }
Esempio n. 10
0
        public Flight CreateFlight(FlightCreateViewModel model)
        {
            Flight flight = new Flight()
            {
                DestinationCity       = model.DestinationCity,
                DepartureCity         = model.DepartureCity,
                DepartureTime         = model.DepartureTime,
                ArrivalTime           = model.ArrivalTime,
                PlaneType             = model.PlaneType,
                PlaneID               = model.PlaneID,
                CaptainName           = model.CaptainName,
                PlaneCapacity         = model.PlaneCapacity,
                BusinessClassCapacity = model.BusinessClassCapacity,
                BusinessTicketsLeft   = model.BusinessClassCapacity,
                TicketsLeft           = model.PlaneCapacity
            };

            dBContext.Flights.Add(flight);
            dBContext.SaveChanges();

            return(flight);
        }
Esempio n. 11
0
        public int CreateReservation(int flightId, string feedbackEmail, int regularSeats, int businessSeats)
        {
            var flight = context.Flights.Find(flightId);

            if (flight.RegularSeats < regularSeats)
            {
                //not enough regular seats
                return(-1);
            }
            if (flight.BusinessSeats < businessSeats)
            {
                //not enough business seats
                return(-2);
            }

            var reservation = new Reservation
            {
                FlightId      = flightId,
                FeedBackEmail = feedbackEmail,
                RegularSeats  = regularSeats,
                BusinessSeats = businessSeats
            };

            flight.RegularSeats  -= regularSeats;
            flight.BusinessSeats -= businessSeats;

            string emailBody = $"Reservation about flight {flightId} from {flight.DepartureLocation} to {flight.LandingLocation} (Plane id: {flight.PlaneUniqueId}). " +
                               $"You reserved {regularSeats} regular seats and {businessSeats} business seats." +
                               $"The plane takes off at {flight.DepartureDateTime}. Expected flight duration: {flight.LandingDateTime-flight.DepartureDateTime}";

            //The feedback mail that the user enters must be valid!!!!!
            SendMail(feedbackEmail, emailBody);

            context.Reservations.Add(reservation);
            context.SaveChanges();

            return(reservation.Id);
        }
Esempio n. 12
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider, FlightManagerDbContext context)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });

            var roleManager = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >();

            if (!roleManager.RoleExistsAsync("admin").Result)
            {
                roleManager.CreateAsync(new IdentityRole("admin")).Wait();
            }
            if (!roleManager.RoleExistsAsync("employee").Result)
            {
                roleManager.CreateAsync(new IdentityRole("employee")).Wait();
            }

            var userManager = serviceProvider.GetRequiredService <UserManager <AppUser> >();

            if (!context.Users.Any())
            {
                //Create the one and only admin
                var user = new AppUser()
                {
                    UserName    = Configuration.GetSection("AdminCredentials")["Username"],
                    Email       = Configuration.GetSection("AdminCredentials")["Email"],
                    FirstName   = Configuration.GetSection("AdminCredentials")["FirstName"],
                    LastName    = Configuration.GetSection("AdminCredentials")["LastName"],
                    UCN         = Configuration.GetSection("AdminCredentials")["UCN"],
                    Address     = Configuration.GetSection("AdminCredentials")["Address"],
                    PhoneNumber = Configuration.GetSection("AdminCredentials")["PhoneNumber"],
                };

                var pass        = Configuration.GetSection("AdminCredentials")["Password"];
                var createdUser = userManager.CreateAsync(user, pass);

                if (createdUser.Result.Succeeded)
                {
                    userManager.AddToRoleAsync(user, "admin").Wait();
                    context.SaveChanges();
                }
            }
        }