private  void CreateProgramsForAgency(SpaceAgencies newAgency, IXLRow row)
        {
            int indexOfFirstProgramField = 6;
            try
            { 
                SpacePrograms program;
                var existingPrograms = (from programs in _context.SpacePrograms
                                        where programs.Title.Contains(row.Cell(indexOfFirstProgramField).Value.ToString())
                                        select programs).ToList();
                if (existingPrograms.Count > 0)
                {
                    program = existingPrograms[0];
                }
                else
                {
                    program = CreateProgram(row, indexOfFirstProgramField);
                    States state =  _context.States.FirstOrDefault(s => s.StateName == row.Cell(indexOfFirstProgramField + 3)
                                    .Value.ToString());
                    _context.SpacePrograms.Add(program);
                    AddNewProgramAndStatePairToContext(program, state);
                    AddNewAgencyAndProgramPairToContext(newAgency, program);
                }
            }
            catch (Exception ex)
            {

            }
        }
        public async Task<IActionResult> Edit(int id, [Bind("Id,Name,DateOfEstablishment,Budget,HeadquarterCountryId")] SpaceAgencies spaceAgencies)
        {
            if (id != spaceAgencies.Id)
            {
                return NotFound();
            }
             if (ModelState.IsValid)
            {
                try
                {
                    
                    _context.Update(spaceAgencies);
                    if (await findAgencyInTheSameCountry(spaceAgencies) != null) ModelState.AddModelError(String.Empty, "This country already has an agency");
                    if (await findAgencyWithTheSameName(spaceAgencies) != null) ModelState.AddModelError(String.Empty, "Agency with this name already exists");
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {


                    if (!SpaceAgenciesExists(spaceAgencies.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            ViewData["HeadquarterCountry"] = new SelectList(_context.Countires, "Id", "CountryName", spaceAgencies.HeadquarterCountryId);
            return View(spaceAgencies);
        }
 private SpaceAgencies CreateAgency(IXLWorksheet worksheet)
 {
     SpaceAgencies agency = new SpaceAgencies();
     agency.Name = worksheet.Name;
     var row = RowWithIndexInWorksheet(worksheet, 0, 1);
     agency.Budget = Double.Parse(row.Cell(1).Value.ToString());
     agency.DateOfEstablishment = DateTime.Parse(row.Cell(2).Value.ToString());
     return agency;
 }
        private void AddNewAgencyAndProgramPairToContext(SpaceAgencies agency, SpacePrograms program)
        {
            AgenciesPrograms newAgencyAndProgramRecord = new AgenciesPrograms();
            newAgencyAndProgramRecord.SpaceAgencyId = agency.Id;
            newAgencyAndProgramRecord.SpaceProgramId = program.Id;
            newAgencyAndProgramRecord.SpaceProgram = program;
            newAgencyAndProgramRecord.SpaceAgency = agency;
            _context.AgenciesPrograms.Add(newAgencyAndProgramRecord);

        }
        public async Task<IActionResult> Create([Bind("Id,Name,DateOfEstablishment,Budget,HeadquarterCountryId")] SpaceAgencies spaceAgencies)
        {
            
            if (await findAgencyInTheSameCountry(spaceAgencies) != null) ModelState.AddModelError(String.Empty, "This country already has an agency");
            if (await findAgencyWithTheSameName(spaceAgencies) != null) ModelState.AddModelError(String.Empty, "Agency with this name already exists");
            if (ModelState.IsValid)
            {
                _context.Add(spaceAgencies);
                 await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));   
            }
            ViewData["HeadquarterCountry"] = new SelectList(_context.Countires, "Id", "CountryName", spaceAgencies.HeadquarterCountryId);
            return View(spaceAgencies);

        }
 private void AttachExistingCountryToAgency(SpaceAgencies agency, Countires country) 
 {
     agency.HeadquarterCountry = country;
     agency.HeadquarterCountryId = country.Id;
 }
 private void WriteAgencyToWorksheet(SpaceAgencies agency, IXLWorksheet worksheet)
 {
     int indexOfFirstAgencyCell = 1;
     worksheet.Cell(2, indexOfFirstAgencyCell).Value = agency.Budget;
     worksheet.Cell(2, indexOfFirstAgencyCell + 1).Value = agency.DateOfEstablishment;
 }
 public async Task<SpaceAgencies> findAgencyWithTheSameName(SpaceAgencies spaceAgencies)
 {
    return await _context.SpaceAgencies.FirstOrDefaultAsync(a => a.Name == spaceAgencies.Name);
 }
 // GET: SpaceAgencies/Delete/5
 public async Task<SpaceAgencies> findAgencyInTheSameCountry(SpaceAgencies spaceAgencies)
 {
     return await _context.SpaceAgencies.FirstOrDefaultAsync(a => a.HeadquarterCountryId == spaceAgencies.HeadquarterCountryId);
 }