public BankSoal GetNewSoal(int sesiId) { BankSoal obj = null; try { var random = new Random(); var sql = @"select bank_soal_id from bank_soal where bank_soal_id not in (select bank_soal_id from histori_tes where sesi_id = @sesiId)"; var soalId = _context.Db.Query <int>(sql, new { sesiId }) .OrderBy(f => random.Next()).Take(1) .FirstOrDefault(); obj = GetById(soalId); if (obj != null) { IHistoriTesRepository repo = new HistoriTesRepository(_context); obj.nomor_soal = repo.GetSoalNumber(sesiId); } } catch (Exception ex) { //_log.Error("Error:", ex); } return(obj); }
public HistoriTes GetLastSoal(int sesiId, bool cekJawaban = true, int incrementSoalNumber = 1) { HistoriTes obj = null; try { var sql = @"select histori_tes.histori_tes_id, histori_tes.user_id, histori_tes.sesi_id, histori_tes.tanggal, histori_tes.jawaban, histori_tes.hasil_jawaban, histori_tes.batal, bank_soal.bank_soal_id, bank_soal.soal, bank_soal.jawaban, bank_soal.analisa from histori_tes inner join bank_soal on bank_soal.bank_soal_id = histori_tes.bank_soal_id {WHERE} order by histori_tes.histori_tes_id desc limit 1"; if (cekJawaban) { sql = sql.Replace("{WHERE}", "where histori_tes.sesi_id = @sesiId and histori_tes.batal = 0 and histori_tes.jawaban is null"); } else { sql = sql.Replace("{WHERE}", "where histori_tes.sesi_id = @sesiId and histori_tes.batal = 0"); } obj = _context.Db.Query <HistoriTes, BankSoal, HistoriTes>(sql, (histori, soal) => { histori.bank_soal_id = soal.bank_soal_id; histori.BankSoal = soal; return(histori); }, new { sesiId }, splitOn: "bank_soal_id").SingleOrDefault(); if (obj != null) { IHistoriTesRepository repo = new HistoriTesRepository(_context); obj.BankSoal.nomor_soal = repo.GetSoalNumber(sesiId, incrementSoalNumber); } } catch (Exception ex) { //_log.Error("Error:", ex); } return(obj); }