Ejemplo n.º 1
0
        public ActionResult Register([Bind(Include = "Username,Password,PasswordRepeat,Name,LastName")] RegisterViewModel registerViewModel)
        {
            if (ModelState.IsValid)
            {
                if (db.Users.Any(u => u.Username == registerViewModel.Username))
                {
                    ViewBag.ErrorMessage = "Username is not available!";
                    return(View(registerViewModel));
                }
                if (registerViewModel.Password != registerViewModel.PasswordRepeat)
                {
                    ViewBag.ErrorMessage = "Entered passwords aren't same!";
                    return(View(registerViewModel));
                }
                // everything ok, insert user in db
                decimal count = 10000;
                string  role  = "user";
                User    user  = new User
                {
                    Username = registerViewModel.Username,
                    Password = registerViewModel.Password,
                    Count    = count,
                    Name     = registerViewModel.Name,
                    LastName = registerViewModel.LastName,
                    Role     = role
                };
                db.Users.Add(user);
                db.SaveChanges();
                return(RedirectToAction("Login"));
            }

            return(View(registerViewModel));
        }
Ejemplo n.º 2
0
        public ActionResult Create([Bind(Include = "Id,Name,Model,Capacity")] Avion avion)
        {
            if (ModelState.IsValid)
            {
                db.Avions.Add(avion);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(avion));
        }
Ejemplo n.º 3
0
        public ActionResult Create([Bind(Include = "Id,Name")] State state)
        {
            if (ModelState.IsValid)
            {
                db.States.Add(state);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(state));
        }
Ejemplo n.º 4
0
        public ActionResult Create([Bind(Include = "Id,Name,CityId")] Airport airports)
        {
            if (ModelState.IsValid)
            {
                db.Airports.Add(airports);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.CityId = new SelectList(db.Cities, "Id", "Name", airports.CityId);
            return(View(airports));
        }
Ejemplo n.º 5
0
        public ActionResult Create([Bind(Include = "Id,Name,StateId")] City city)
        {
            if (ModelState.IsValid)
            {
                db.Cities.Add(city);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.StateId = new SelectList(db.States, "Id", "Name", city.StateId);
            return(View(city));
        }
Ejemplo n.º 6
0
        public ActionResult Book(BookFlightViewModel bookFlightViewModel, int id)
        {
            string usrUsername  = Session["Username"].ToString();
            User   user         = db.Users.Where(u => u.Username == usrUsername).FirstOrDefault();
            Flight flight       = db.Flights.Find(id);
            bool   error        = false;
            int    numOfTickets = bookFlightViewModel.NumOfTickets;

            if (ModelState.IsValid)
            {
                int ticketsLeft = flight.TicketsLeft;
                if (numOfTickets > ticketsLeft)
                {
                    ViewBag.TicketsErrorMessage = "Not enough tickets for flight!";
                    error = true;
                }

                // if user has already booked that flight
                if (db.Reservations.Any(r => r.UserId == user.Id && r.FlightId == flight.Id))
                {
                    ViewBag.ErrorMessage = "User je vec izvrsio rezervaciju za ovaj let!";
                    error = true;
                }
                if (user.Count < flight.price * numOfTickets)
                {
                    ViewBag.CountErrorMessage = "You don't have enough money on your count!";
                    error = true;
                }

                Reservation reservation = new Reservation
                {
                    Flight          = flight,
                    User            = user,
                    NumberOfTickets = numOfTickets
                };

                if (!error)
                {
                    // update Flight tickets left
                    flight.TicketsLeft -= numOfTickets;
                    // update user's count
                    user.Count      -= flight.price * numOfTickets;
                    Session["Count"] = user.Count;
                    db.Reservations.Add(reservation);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
            }
            //bookFlightViewModel.Flight = flight;
            return(View(bookFlightViewModel));
        }
        public ActionResult Create([Bind(Include = "Id,UserId,FlightId")] Reservation reservation)
        {
            if (ModelState.IsValid)
            {
                if (!db.Reservations.Any(r => r.UserId == reservation.UserId && r.FlightId == reservation.FlightId))
                {
                    db.Reservations.Add(reservation);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                ViewBag.ErrorMessage = "User je vec izvrsio rezervaciju za ovaj let!";
            }

            ViewBag.FlightId = new SelectList(db.Flights, "Id", "Id", reservation.FlightId);
            ViewBag.UserId   = new SelectList(db.Users, "Id", "Username", reservation.UserId);
            return(View(reservation));
        }