public async Task <IActionResult> Create(int agencyId, [Bind("Id,Title,StartDate,EndDate,Target,StateId")] SpacePrograms spacePrograms, IFormCollection collection) { if (await findProgramWithTheSameName(spacePrograms) != null) { ModelState.AddModelError(String.Empty, "Program with this name already exists"); } if (ModelState.IsValid) { AgenciesPrograms newPair = new AgenciesPrograms(); var agency = _context.SpaceAgencies.Where(a => a.Id == agencyId).FirstOrDefault(); newPair.SpaceAgency = agency; newPair.SpaceProgram = spacePrograms; newPair.SpaceAgencyId = agencyId; newPair.SpaceProgramId = spacePrograms.Id; spacePrograms.AgenciesPrograms.Add(newPair); agency.AgenciesPrograms.Add(newPair); _context.Add(spacePrograms); _context.Add(newPair); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(spacePrograms)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Title,StartDate,EndDate,Target,AgencyId")] SpacePrograms spacePrograms) { if (id != spacePrograms.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(spacePrograms); if (await findProgramWithTheSameName(spacePrograms) != null) { ModelState.AddModelError(String.Empty, "Program with this name already exists"); } await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SpaceProgramsExists(spacePrograms.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(spacePrograms)); }
private void AddNewProgramAndStatePairToContext(SpacePrograms program, States state) { ProgramsStates newProgramAndStateRecord = new ProgramsStates(); newProgramAndStateRecord.ProgramId = program.Id; newProgramAndStateRecord.StateId = state.Id; newProgramAndStateRecord.Program = program; newProgramAndStateRecord.State = state; _context.ProgramsStates.Add(newProgramAndStateRecord); }
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); }
private void DeleteMissions(SpacePrograms program) { var missions = _context.Missions.Where(m => m.ProgramId == program.Id); if (missions != null) { foreach (var mission in missions) { DeleteCrew(mission); _context.Missions.Remove(mission); } } }
private void DeleteProgram(SpacePrograms program) { var agencyAndProgram = _context.AgenciesPrograms.FirstOrDefault(ap => ap.SpaceProgramId == program.Id); _context.AgenciesPrograms.Remove(agencyAndProgram); DeleteMissions(program); var programAndState = _context.ProgramsStates.FirstOrDefault(ps => ps.ProgramId == program.Id); if (programAndState != null) { _context.ProgramsStates.Remove(programAndState); } _context.SpacePrograms.Remove(program); }
private SpacePrograms CreateProgram(IXLRow row, int indexOfFirstProgramField) { SpacePrograms program = new SpacePrograms(); program.Title = row.Cell(indexOfFirstProgramField).Value.ToString(); program.StartDate = DateTime.Parse(row.Cell(indexOfFirstProgramField + 1).Value.ToString()); string endDate = row.Cell(indexOfFirstProgramField + 2).Value.ToString(); if (endDate.Length > 0) { program.EndDate = DateTime.Parse(endDate); } else { program.EndDate = null; } program.Target = row.Cell(indexOfFirstProgramField + 4).Value.ToString(); return program; }
public async Task <SpacePrograms> findProgramWithTheSameName(SpacePrograms spacePrograms) { return(await _context.SpacePrograms.FirstOrDefaultAsync(p => p.Title == spacePrograms.Title)); }