internal async Task <Cadet> UpdateCadetAsync(Guid id, Cadet cadetDTO) { var cadets = await _context.Cadet.Where(c => c.id == id) .Include(c => c.GroupDB) .Include(c => c.GroupDB.ProfessionDB) .Include(c => c.GroupDB.SpecializationDB) .FirstOrDefaultAsync(); if (cadets == null || cadets.id != id) { return(null); } cadets = UpdateCadetInDB(cadetDTO, cadets); _context.Entry(cadets).State = EntityState.Modified; try { await _context.SaveChangesAsync(); return(cadetDTO); } catch (DbUpdateConcurrencyException) { if (!CadetDBExists(id)) { return(null); } else { throw; } } }
internal async Task <Cadet> AddCadetAsync(Cadet cadet) { var gr = await _context.Group.Where(c => c.numberOfGroup == cadet.groupNumber).FirstOrDefaultAsync(); var militeryRank = await _context.MilitaryRank.Where(c => c.name == cadet.militaryRank).FirstOrDefaultAsync(); var position = await _context.Position.Where(c => c.name == cadet.Position).FirstOrDefaultAsync(); if (gr == null || militeryRank == null || position == null) { return(null); } CadetDB cadetDB = new CadetDB(gr, cadet); try { _context.Cadet.Add(cadetDB); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CadetDBExists(cadet.id)) { return(null); } else { throw; } } return(cadet); }
private CadetDB UpdateCadetInDB(Cadet cadet, CadetDB cadetDB) { /* * cadetDB.id = cadet.id; * cadetDB.info = cadet.info; * cadetDB.isMarried = cadet.isMarried; * cadetDB.lastName = cadet.lastName; * cadetDB.middleName = cadet.middleName; * cadetDB.firstName = cadet.firstName; * cadetDB.militaryRank = cadet.militaryRank; * cadetDB.Position = cadet.Position.; * cadetDB.pathPhotoBig = cadet.pathPhotoBig; * cadetDB.pathPhotoSmall = cadet.pathPhotoSmall; * cadetDB.birthDay = cadet.birthDay; * cadetDB.dateOfStartService = cadet.dateOfStartService; * * * if (cadetDB.GroupDB.numberOfGroup != cadet.groupNumber) * { * GroupDB groupDB = _context.Group.Where(c => c.numberOfGroup == cadet.groupNumber).FirstOrDefault(); * cadetDB.GroupDB = groupDB; * cadetDB.GroupDBid = groupDB.id; * } */ return(cadetDB); }
private static void SeedData(CadetContext context) { System.Console.WriteLine("Appling Migrations..."); context.Database.Migrate(); var rnd = new Random(); if (!context.Cadets.Any()) { for (int i = 1; i < 200; i++) { var cdt = new Cadet() { FirstName = _firstNames[rnd.Next(0, _firstNames.Length)], LastName = _lastNames[rnd.Next(0, _lastNames.Length)], Element = rnd.Next(1, 3), Program = rnd.Next(1, 5), CurrentRank = rnd.Next(1, 5), BirthDate = DateTime.Today.AddYears(-12 + rnd.Next(0, 4)), }; context.Add(cdt); } context.SaveChanges(); } }
internal async Task <Cadet> GetCadetByIdAsync(Guid id) { var grups = await _context.Cadet.Where(c => c.id == id).Include(c => c.GroupDB) .Include(s => s.GroupDB.ProfessionDB) .Include(s => s.GroupDB.SpecializationDB) .FirstOrDefaultAsync(); Cadet cadetDTO = new Cadet(grups); return(cadetDTO); }
internal async Task <Cadet> DeletecadetAsync(Guid id) { var cadetDB = await _context.Cadet.FindAsync(id); if (cadetDB == null) { return(null); } _context.Cadet.Remove(cadetDB); await _context.SaveChangesAsync(); Cadet cadet = new Cadet(cadetDB); return(cadet); }
public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } Cadet = await _context.Cadet.FirstOrDefaultAsync(m => m.ID == id); if (Cadet == null) { return(NotFound()); } return(Page()); }
public async Task <IActionResult> PutCadetDB(Guid id, Cadet cadet) { if (id != cadet.id) { return(BadRequest()); } var cadetNew = await _cadetServies.UpdateCadetAsync(id, cadet); if (cadetNew != null) { return(CreatedAtAction("GetCadetDB", new { id = cadetNew.id }, cadetNew)); } return(NoContent()); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } Cadet = await _context.Cadet.FindAsync(id); if (Cadet != null) { _context.Cadet.Remove(Cadet); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
public CCFTracker.ViewModel.CadetView CadetToViewModel(Cadet source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } return(new ViewModel.CadetView { ID = source.ID, Rank = $"{source.Rank}", FullName = $"{source.Surname} ', ' {source.KnownAs}", Year = source.Year, Gender = $"{source.Gender}", Form = $"{source.Form}", GroupLocation = $"{source.Platoon} ', ' {source.Section}" }); }
public async Task <ActionResult <Cadet> > PostCadetDB(Cadet cadet) { var cadetNew = await _cadetServies.AddCadetAsync(cadet); return(CreatedAtAction("GetCadetDB", new { id = cadetNew.id }, cadetNew)); }