private async Task <IspitPolaganjeVM> BuildNoviIspitPolaganjeViewModel(int ispitniTerminId, int?polaganjeId = null)
        {
            IspitPolaganjeVM model = new IspitPolaganjeVM();

            if (polaganjeId == null)
            {
                var ispitniTermin = await _context.IspitniTermini
                                    .Include(x => x.Angazovan)
                                    .FirstOrDefaultAsync(x => x.Id == ispitniTerminId);

                if (ispitniTermin == null)
                {
                    return(null);
                }

                var upisi = await _context.UpisGodine.Include(x => x.Student)
                            .Where(x => x.AkademskaGodinaId == ispitniTermin.Angazovan.AkademskaGodinaId)
                            .ToListAsync();

                var polaganjaVecDodata = _context.PolaganjaIspita
                                         .Where(x => x.IspitniTerminId == ispitniTerminId);

                if (polaganjaVecDodata.Any())
                {
                    var upisiPostojeciIds = polaganjaVecDodata.Select(x => x.UpisGodine.Id);

                    upisi = upisi.Where(x => !upisiPostojeciIds.Contains(x.Id)).ToList();
                }



                model = new IspitPolaganjeVM {
                    IspitniTerminId = _protector.Protect(ispitniTerminId.ToString()),
                    Studenti        = upisi.ToSelectList(x => _protector.Protect(x.Id.ToString()),
                                                         x => x.Student.ImePrezime(), "Odaberite studenta")
                };
            }
            else
            {
                var polaganje = await _context.PolaganjaIspita
                                .Include(x => x.UpisGodine)
                                .FirstOrDefaultAsync(x => x.Id == polaganjeId);

                var student = _context.Student.Find(polaganje.UpisGodine.StudentId);

                if (polaganje == null || student == null)
                {
                    return(model);
                }

                model = new IspitPolaganjeVM {
                    Id = _protector.Protect(polaganje.Id.ToString()),
                    IspitniTerminId = _protector.Protect(ispitniTerminId.ToString()),
                    Student         = student.Ime + " " + student.Prezime
                };
            }

            return(model);
        }
        public async Task <IActionResult> Snimi(IspitPolaganjeVM model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Podaci nisu validni."));
            }

            if (!model.Ocjena.HasValue)
            {
                var ispitTerminDecryptedId = int.Parse(_protector.Unprotect(model.IspitniTerminId));
                var ispitniTermin          = await _context.IspitniTermini.FindAsync(ispitTerminDecryptedId);

                if (ispitniTermin == null)
                {
                    return(NotFound("Ispitni termin nije pronadjne."));
                }

                var novoPolaganje = new IspitPolaganje
                {
                    IspitniTerminId = int.Parse(_protector.Unprotect(model.IspitniTerminId)),
                    Ocjena          = 0,
                    PristupioIspitu = false,
                    UpisGodineId    = int.Parse(_protector.Unprotect(model.UpisGodineId))
                };
                await _context.AddAsync(novoPolaganje);

                await _context.SaveChangesAsync();

                ViewData["evidentiraniRezultati"] = ispitniTermin.EvidentiraniRezultati;
                ViewData["datumIspita"]           = ispitniTermin.DatumIspita;


                return(PartialView("_PolaganjeIspitaRow", await BuildPolaganjeIspitaVM(novoPolaganje)));
            }

            var polaganjeFromDb = await _context.PolaganjaIspita
                                  .Include(x => x.IspitniTermin)
                                  .FirstOrDefaultAsync(x => x.Id == int.Parse(_protector.Unprotect(model.Id)));

            if (polaganjeFromDb == null)
            {
                return(BadRequest("Polaganje ne postoji."));
            }

            polaganjeFromDb.Ocjena          = model.Ocjena;
            polaganjeFromDb.PristupioIspitu = true;

            _context.Update(polaganjeFromDb);
            await _context.SaveChangesAsync();

            ViewData["evidentiraniRezultati"] = polaganjeFromDb.IspitniTermin.EvidentiraniRezultati;
            ViewData["datumIspita"]           = polaganjeFromDb.IspitniTermin.DatumIspita;

            return(PartialView("_PolaganjeIspitaRow", await BuildPolaganjeIspitaVM(polaganjeFromDb)));
        }