public async Task <IActionResult> SignUp(SignUpEmployeeViewModel model)
        {
            Employee                  newEmployee                  = null;
            Country                   newCountry                   = null;
            LivingPlace               newLivingPlace               = null;
            DestrictedArea            newDestrictedArea            = null;
            Street                    newStreet                    = null;
            Address                   newAddress                   = null;
            CountryLivingPlace        newCountryLivingPlace        = null;
            LivingPlaceDestrictedArea newLivingPlaceDestrictedArea = null;
            DestrictedAreaStreet      newDestrictedAreaStreet      = null;

            if (ModelState.IsValid)
            {
                using (var _context = new FlightsManagerContext())
                {
                    int id = _context.Countries.Max(c => c.Id);
                    newCountry = new Country()
                    {
                        Id          = id + 1,
                        Name        = model.Country,
                        ContinentId = model.ContinentId
                    };
                    _context.Countries.Add(newCountry);
                    _context.SaveChanges();
                }
                using (var _context = new FlightsManagerContext())
                {
                    int id = _context.LivingPlaces.Max(l => l.Id);
                    newLivingPlace = new LivingPlace()
                    {
                        Id   = id + 1,
                        Name = model.LivingArea
                    };
                    _context.LivingPlaces.Add(newLivingPlace);
                    _context.SaveChanges();
                }
                using (var _context = new FlightsManagerContext())
                {
                    int id = _context.DestrictedAreas.Max(d => d.Id);
                    newDestrictedArea = new DestrictedArea()
                    {
                        Id   = id + 1,
                        Name = model.DestrictedArea
                    };
                    _context.DestrictedAreas.Add(newDestrictedArea);
                    _context.SaveChanges();
                }
                using (var _context = new FlightsManagerContext())
                {
                    int id = _context.Streets.Max(s => s.Id);
                    newStreet = new Street()
                    {
                        Id   = id + 1,
                        Name = model.Street
                    };
                    _context.Streets.Add(newStreet);
                    _context.SaveChanges();
                }
                using (var _context = new FlightsManagerContext())
                {
                    int id = _context.Addresses.Max(a => a.Id);
                    newAddress = new Address()
                    {
                        Id               = id + 1,
                        ContitnetId      = model.ContinentId,
                        CountryId        = newCountry.Id,
                        LivingPlaceId    = newLivingPlace.Id,
                        DestrictedAreaId = newDestrictedArea.Id,
                        StreetId         = newStreet.Id,
                        Number           = model.Number,
                        Floor            = model.Floor,
                        Apartament       = model.Apartament,
                        AddressTypeId    = 1
                    };
                    _context.Addresses.Add(newAddress);
                    _context.SaveChanges();
                }
                using (var _context = new FlightsManagerContext())
                {
                    int id = _context.CountryLivingPlaces.Max(e => e.Id);
                    newCountryLivingPlace = new CountryLivingPlace()
                    {
                        Id            = id + 1,
                        CountryId     = newCountry.Id,
                        LivingPlaceId = newLivingPlace.Id
                    };
                    _context.CountryLivingPlaces.Add(newCountryLivingPlace);
                    _context.SaveChanges();
                }
                using (var _context = new FlightsManagerContext())
                {
                    int id = _context.LivingPlaceDestrictedAreas.Max(e => e.Id);
                    newLivingPlaceDestrictedArea = new LivingPlaceDestrictedArea()
                    {
                        Id               = id + 1,
                        LivingPlaceId    = newLivingPlace.Id,
                        DestrictedAreaId = newDestrictedArea.Id
                    };
                    _context.LivingPlaceDestrictedAreas.Add(newLivingPlaceDestrictedArea);
                    _context.SaveChanges();
                }
                using (var _context = new FlightsManagerContext())
                {
                    int id = _context.DestrictedAreaStreets.Max(e => e.Id);
                    newDestrictedAreaStreet = new DestrictedAreaStreet()
                    {
                        Id = id + 1,
                        DestrictedAreaId = newDestrictedArea.Id,
                        StreetId         = newStreet.Id
                    };
                    _context.DestrictedAreaStreets.Add(newDestrictedAreaStreet);
                    _context.SaveChanges();
                }
                using (var _context = new FlightsManagerContext())
                {
                    int id = _context.Employees.Max(e => e.Id);
                    newEmployee = new Employee()
                    {
                        Id          = id + 1,
                        Username    = model.Username,
                        Password    = HashPassword.GenerateSHA512(model.Password),
                        Email       = model.Email,
                        Name        = model.Name,
                        Family      = model.Family,
                        Egn         = model.Egn,
                        AddressId   = newAddress.Id,
                        PhoneNumber = model.PhoneNumber,
                        RoleId      = 2
                    };
                    _context.Employees.Add(newEmployee);
                    await _context.SaveChangesAsync();
                }

                return(RedirectToAction(nameof(LoginForm)));
            }
            else
            {
                return(View(model));
            }
        }
Exemple #2
0
        public ViewResult Details(Flight model)
        {
            byte[] empIdBytes = new byte[200];
            if (HttpContext.Session.TryGetValue("empId", out empIdBytes))
            {
                int employeeId = int.Parse(Encoding.UTF8.GetString(empIdBytes));
                if (employeeId == 1)
                {
                    List <Employee> employees = (from emp in _context.Employees
                                                 where
                                                 emp.Id == employeeId
                                                 select emp).ToList();
                    Employee employee = employees[0];
                    ViewData["username"]   = employee.Username;
                    ViewData["isLoggedIn"] = true;
                    ViewData["isAdmin"]    = true;

                    ViewData["Flight"] = model;

                    Address     locFrom    = _context.Addresses.Find(model.LocationFromId);
                    Country     countrFrom = _context.Countries.Find(locFrom.CountryId);
                    LivingPlace lpFrom     = _context.LivingPlaces.Find(locFrom.LivingPlaceId);
                    ViewData["LocationFrom"] = lpFrom.Name + ", " + countrFrom.Name;

                    Address     locTo    = _context.Addresses.Find(model.LocationToId);
                    Country     countrTo = _context.Countries.Find(locTo.CountryId);
                    LivingPlace lpTo     = _context.LivingPlaces.Find(locTo.LivingPlaceId);
                    ViewData["LocationTo"] = lpTo.Name + ", " + countrTo.Name;

                    return(View());
                }
                else if (employeeId != 1 && employeeId > 0)
                {
                    List <Employee> employees = (from emp in _context.Employees
                                                 where emp.Id == employeeId
                                                 select emp).ToList();
                    Employee employee = employees[0];
                    ViewData["username"]   = employee.Username;
                    ViewData["isLoggedIn"] = true;
                    ViewData["isAdmin"]    = false;

                    ViewData["Flight"] = model;

                    Address     locFrom    = _context.Addresses.Find(model.LocationFromId);
                    Country     countrFrom = _context.Countries.Find(locFrom.CountryId);
                    LivingPlace lpFrom     = _context.LivingPlaces.Find(locFrom.LivingPlaceId);
                    ViewData["LocationFrom"] = lpFrom.Name + ", " + countrFrom.Name;

                    Address     locTo    = _context.Addresses.Find(model.LocationToId);
                    Country     countrTo = _context.Countries.Find(locTo.CountryId);
                    LivingPlace lpTo     = _context.LivingPlaces.Find(locTo.LivingPlaceId);
                    ViewData["LocationTo"] = lpTo.Name + ", " + countrTo.Name;

                    return(View());
                }
                else
                {
                    ViewData["isLoggedIn"] = false;
                    ViewData["isAdmin"]    = false;

                    ViewData["Flight"] = model;

                    Address     locFrom    = _context.Addresses.Find(model.LocationFromId);
                    Country     countrFrom = _context.Countries.Find(locFrom.CountryId);
                    LivingPlace lpFrom     = _context.LivingPlaces.Find(locFrom.LivingPlaceId);
                    ViewData["LocationFrom"] = lpFrom.Name + ", " + countrFrom.Name;

                    Address     locTo    = _context.Addresses.Find(model.LocationToId);
                    Country     countrTo = _context.Countries.Find(locTo.CountryId);
                    LivingPlace lpTo     = _context.LivingPlaces.Find(locTo.LivingPlaceId);
                    ViewData["LocationTo"] = lpTo.Name + ", " + countrTo.Name;

                    return(View());
                }
            }
            else
            {
                ViewData["isLoggedIn"] = false;
                ViewData["isAdmin"]    = false;

                ViewData["Flight"] = model;

                Address     locFrom    = _context.Addresses.Find(model.LocationFromId);
                Country     countrFrom = _context.Countries.Find(locFrom.CountryId);
                LivingPlace lpFrom     = _context.LivingPlaces.Find(locFrom.LivingPlaceId);
                ViewData["LocationFrom"] = lpFrom.Name + ", " + countrFrom.Name;

                Address     locTo    = _context.Addresses.Find(model.LocationToId);
                Country     countrTo = _context.Countries.Find(locTo.CountryId);
                LivingPlace lpTo     = _context.LivingPlaces.Find(locTo.LivingPlaceId);
                ViewData["LocationTo"] = lpTo.Name + ", " + countrTo.Name;

                return(View());
            }
        }