public async Task <IActionResult> ChangeStatus(int crime_id, int criminal_id, bool status) { var criminal = _context.Criminal.Include(c => c.Crimes).FirstOrDefault(item => item.Id == criminal_id); if (criminal != null) { if (status) { var crime = new CrimeCriminal() { CrimeId = crime_id, CriminalId = criminal_id }; criminal.Crimes.Add(crime); criminal.CrimesAsString += $"{crime.CrimeId}, "; } else { criminal.Crimes.Remove(criminal.Crimes.FirstOrDefault(item => item.CrimeId == crime_id)); criminal.CrimesAsString = criminal.CrimesAsString.Replace($"{crime_id}, ", ""); } await _context.SaveChangesAsync(); return(RedirectToAction("AddCrime", new { criminal_id = criminal_id })); } return(NotFound()); }
public void SeedDatabase(int records = 10) { for (int i = 0; i < records; i++) { Console.Clear(); Console.WriteLine("Seeding database: {0}/{1}", i + 1, records); using (var unitOfWork = new UnitOfWork(new CriminalContext())) { Crime crime = _crimeSeeder.GetRandomCrimeSeed(); int criminalAmount = _randomGenerator.GetRandomInt(1, 5); for (int ca = 0; ca < criminalAmount; ca++) { Criminal criminal = _personSeeder.GetRandomPersonSeed <Criminal>(); criminal.Description = "Random Description"; //Adding to fields CrimeCriminal crimeCriminal = new CrimeCriminal(); crimeCriminal.Criminal = criminal; crimeCriminal.Crime = crime; criminal.Crimes.Add(crimeCriminal); crime.CrimeCriminals.Add(crimeCriminal); //Adding to databses unitOfWork.Repository <Criminal>().Add(criminal); unitOfWork.Repository <CrimeCriminal>().Add(crimeCriminal); } int victimAmount = _randomGenerator.GetRandomInt(1, 5); for (int va = 0; va < victimAmount; va++) { Victim victim = _personSeeder.GetRandomPersonSeed <Victim>(); //Adding to fields CrimeVictim crimeVictim = new CrimeVictim(); crimeVictim.Victim = victim; crimeVictim.Crime = crime; victim.Crimes.Add(crimeVictim); crime.CrimeVictims.Add(crimeVictim); //Adding to databse unitOfWork.Repository <Victim>().Add(victim); unitOfWork.Repository <CrimeVictim>().Add(crimeVictim); } unitOfWork.Complete(); } } }
public void AddCrime() { using (var unitOfWork = new UnitOfWork(new CriminalContext())) { int amount; Crime crime = new Crime(); List <Criminal> criminals = new List <Criminal>(); List <Victim> victims = new List <Victim>(); List <CrimeCriminal> crimeCriminals = new List <CrimeCriminal>(); List <CrimeVictim> crimeVictims = new List <CrimeVictim>(); crime = CrimeInfo(crime); Console.WriteLine(); //Adding Crime to UoW unitOfWork.Repository <Crime>().Add(crime); amount = _parser.ParseValue <int>(int.TryParse, "How many criminals?"); for (int i = 0; i < amount; i++) { Criminal criminal = new Criminal(); CrimeCriminal crimeCriminal = new CrimeCriminal(); if (QuestionLoop("Is criminal in database? (y/n)")) { int id = _parser.ParseValue <int>(int.TryParse, "Criminal ID: "); criminal = unitOfWork.Repository <Criminal>().Get(id); } else { Console.WriteLine(); Console.WriteLine("Criminal {0}:", i + 1); criminal = Info(criminal); //Adding Criminal to UoW unitOfWork.Repository <Criminal>().Add(criminal); } //Connecting Criminal to Crime. crimeCriminal.Criminal = criminal; crimeCriminal.Crime = crime; criminal.Crimes.Add(crimeCriminal); crime.CrimeCriminals.Add(crimeCriminal); //Adding Criminal and CrimeCriminal to list. criminals.Add(criminal); crimeCriminals.Add(crimeCriminal); //Adding CrimeCriminal to UoW unitOfWork.Repository <CrimeCriminal>().Add(crimeCriminal); } amount = _parser.ParseValue <int>(int.TryParse, "How many victims?"); for (int i = 0; i < amount; i++) { Victim victim = new Victim(); CrimeVictim crimeVictim = new CrimeVictim(); if (QuestionLoop("Is victim in database? (y/n)")) { int id = _parser.ParseValue <int>(int.TryParse, "Victim ID: "); victim = unitOfWork.Repository <Victim>().Get(id); } else { Console.WriteLine(); Console.WriteLine("Victim {0}:", i + 1); victim = Info(victim); //Adding Victim to UoW unitOfWork.Repository <Victim>().Add(victim); } //Connecting Victim to Crime. crimeVictim.Victim = victim; crimeVictim.Crime = crime; victim.Crimes.Add(crimeVictim); crime.CrimeVictims.Add(crimeVictim); //Adding Victim and CrimeVictim to list. victims.Add(victim); crimeVictims.Add(crimeVictim); //Adding CrimeVictim to UoW unitOfWork.Repository <CrimeVictim>().Add(crimeVictim); } //Validation before adding! if (ValidateAll(crime, criminals, victims) == false) { Console.WriteLine("Error, validation failed!"); } else { unitOfWork.Complete(); } } Console.WriteLine("Done."); Console.ReadLine(); }