public async Task <ActionResult> BeniYokla(string Key) { var email = User.Claims.First().Value; AppUser user = await _userManager.FindByEmailAsync(email); List <int> ogrenciDersleri = (await _context.AlinanDersler.Where(x => x.OgrenciId == user.Id) .Include(x => x.Ders) .ToListAsync()).Select(x => x.Ders).Select(x => x.Id).ToList(); DateTime now = DateTime.Now; AcilanDers ders = await _context.AcilanDersler.Where(x => x.Key == Key && x.SonGecerlilik >= now && ogrenciDersleri.Contains(x.DersId)).FirstOrDefaultAsync(); if (ders == null) { return(BadRequest()); } if (await _context.YoklananOgrenciler.FirstOrDefaultAsync(x => x.OgrenciId == user.Id && x.Key == Key) != null) { return(Ok()); } YoklananOgrenci yoklananOgrenci = new YoklananOgrenci() { DersId = ders.DersId, OgrenciId = user.Id, Key = Key }; _context.YoklananOgrenciler.Add(yoklananOgrenci); await _context.SaveChangesAsync(); return(Ok()); }
public async Task <IActionResult> OgrenciEkleCikar(int yoklama, int ogr_id, int ekle) { if (yoklama <= 0 || ogr_id <= 0 || ekle < 0 || ekle > 1) { return(RedirectToAction("Index", "Home")); } AcilanDers ders = await _context.AcilanDersler.Include(x => x.Ders).FirstOrDefaultAsync(x => x.Id == yoklama); if (ders == null || ders.Ders.OgretmenId != ActiveUserId) { return(RedirectToAction("Index")); } AppUser ogrenci = await _context.Users.Include(x => x.AlinanDersler).Where(x => x.Id == ogr_id).FirstOrDefaultAsync(); List <int> dersIds = ogrenci.AlinanDersler.Select(x => x.DersId).ToList(); if (ogrenci == null || !dersIds.Contains(ders.DersId)) { return(RedirectToAction("Index")); } YoklananOgrenci yoklanma = await _context.YoklananOgrenciler.FirstOrDefaultAsync(x => x.OgrenciId == ogr_id && x.Id == yoklama); if (yoklanma == null && ekle == 1) { // Öğrenci yoklamaya eklenecek YoklananOgrenci yeni = new YoklananOgrenci() { DersId = ders.DersId, Key = ders.Key, OgrenciId = ogrenci.Id }; _context.YoklananOgrenciler.Add(yeni); await _context.SaveChangesAsync(); } else if (yoklanma != null && ekle == 0) { // Öğrenci yoklamadan çıkarılacak _context.YoklananOgrenciler.Remove(yoklanma); await _context.SaveChangesAsync(); } return(RedirectToAction(nameof(Yoklama), new { id = yoklama })); }