public Task <KandidatHmps> Post(KandidatHmps model) { try { var periode = _db.Periode.Where(x => x.Aktif).FirstOrDefault(); if (periode == null) { throw new SystemException("Periode Aktif Tidak Ditemukan !"); } model.Periode = periode; _db.Entry(model.Periode).State = EntityState.Unchanged; _db.Entry(model.Ketua).State = EntityState.Unchanged; _db.Entry(model.Wakil).State = EntityState.Unchanged; if (model.ProgramStudi != null) { _db.Entry(model.ProgramStudi).State = EntityState.Unchanged; } _db.KandidatHmps.Add(model); _db.SaveChanges(); return(Task.FromResult(model)); } catch (Exception ex) { throw new SystemException(ex.Message); } }
public async Task <IActionResult> Put(int id, KandidatHmps model) { try { var response = await _hmpsService.Put(id, model); return(Ok(response)); } catch (System.Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <IActionResult> VoteHmps(KandidatHmps model) { try { var userid = User.UserId().Value; var mahasiswa = await _mahasiswaService.GetById(userid); var response = await _voteService.VoteHmps(mahasiswa, model); return(Ok(response)); } catch (System.Exception ex) { return(BadRequest(ex.Message)); } }
public Task <bool> Put(int id, KandidatHmps model) { try { var oldData = _db.KandidatHmps.Where(x => x.Id == id).FirstOrDefault(); if (oldData == null) { throw new SystemException("Data tidak ditemukan !"); } _db.Entry(oldData).CurrentValues.SetValues(model); _db.SaveChanges(); return(Task.FromResult(true)); } catch (Exception ex) { throw new SystemException(ex.Message); } }
public async Task <bool> VoteHmps(Mahasiswa pemilih, KandidatHmps kandidat) { try { Periode periode = await _periodeService.GetActived(); if (periode == null) { throw new SystemException("Belum Ada Periode Aktif !"); } if (kandidat.ProgramStudi.Id != pemilih.ProgramStudi.Id) { throw new SystemException("Anda Tidak Berhak untuk memilih kandidat !"); } kandidat.Periode = periode; var model = new Models.VoteHmps { Kandidat = kandidat, Periode = periode, Mahasiswa = pemilih, ProgramStudi = pemilih.ProgramStudi, Waktu = DateTime.Now }; _dbContext.Entry(model.Kandidat).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; _dbContext.Entry(model.Periode).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; _dbContext.Entry(model.Mahasiswa).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; _dbContext.Entry(model.ProgramStudi).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; _dbContext.VoteHmps.Add(model); _dbContext.SaveChanges(); //Send To SignalR await _hubContext.Clients.All.SendAsync("onVoteHmps", kandidat.ProgramStudi.Id, kandidat.Id); return(true); } catch (Exception ex) { throw new SystemException(ex.Message); } }