public async Task <IActionResult> Delete(string Username) { try { Pengguna pengguna = new Pengguna(); pengguna = await _Pengguna.getpenggunausername(Username); if (pengguna != null) { if (pengguna.rolename == "Pasien") { var cekpasien = await _detailPasien.getusername(Username); if (cekpasien != null) { await _detailPasien.DeleteByuser(Username); } } else { await _prosentase.Deletebyusername(Username); var TenantUser = await _tenantPengguna.getusertenantlist(Username); foreach (var item in TenantUser) { await _tenantPengguna.Delete(item.TenantPenggunaID); } //detail gaji to //pengajian var cekpegawai = await _detailPegawai.getusername(Username, pengguna.TenantID); if (cekpegawai != null) { await _detailPegawai.DeleteByuser(Username); } } } var tenant = await _tenantPengguna.getusertenantlist(Username); foreach (var data in tenant) { await _tenantPengguna.Delete(data.TenantPenggunaID); } await _Pengguna.DeletebyUser(pengguna.Username); //await _userService.Delete(Username); //delete data pengguna sekalian return(Ok("Data berhasil didelete")); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task UpdateAsync(Pengguna obj) { try { //harus dapetin role dari user var getdatapengguna = await getpenggunausername(obj.Username); if (getdatapengguna.rolename == null) { await _UserRole.AddUserToRoleAsync(obj.Username, obj.rolename); } else { await _UserRole.RemoveuserRole(getdatapengguna.Username, getdatapengguna.rolename); await _UserRole.AddUserToRoleAsync(obj.Username, obj.rolename); } if (getdatapengguna.rolename == null) { await _UserRole.AddUserToRoleAsync(obj.Username, obj.rolename); } if (obj.rolename != "Pasien") { if (getdatapengguna.IdPasien != null) { obj.IdPasien = null; } var detailpasien = await _detailPasien.getusername(obj.Username); if (detailpasien != null) { await _detailPasien.DeleteByuser(obj.Username); } DetailPegawai pegawai = new DetailPegawai(); var pegawaii = await _detailPegawai.getusername(obj.Username); if (pegawaii == null) { pegawai.Username = obj.Username; pegawai.Jabatan = obj.rolename; pegawai.TanggalJoin = DateTime.Today; await _detailPegawai.CreateAsync(pegawai); } // create data pegawai } else if (obj.rolename == "Pasien") { //kasi contraint klo ada data pasien nya g usah di delete tapi di update // klo g ada dia create DetailPasien detpasien = new DetailPasien(); var detailpas = await _detailPasien.getusername(obj.Username); if (detailpas == null) { obj.IdPasien = "pasien" + obj.Username; detpasien.IdPasien = obj.IdPasien; detpasien.Username = obj.Username; detpasien.Registrasi = DateTime.Today; await _detailPasien.CreateAsync(detpasien); } } Users user = new Users(); user.Username = obj.Username; user.Email = obj.Email; await _userService.Updateuser(user); } catch (Exception ex) { throw new Exception($"{ex.Message} {ex.InnerException.Message}"); } var data = await getusername(obj.Username); if (data != null) { try { data.IdPasien = obj.IdPasien; data.Alamat = obj.Alamat; data.Nama = obj.Nama; data.Gender = obj.Gender; data.Umur = obj.Umur; data.Kota = obj.Kota; data.NoTelpon = obj.NoTelpon; data.NoHP = obj.NoHP; data.Email = obj.Email; data.rolename = obj.rolename; await _context.SaveChangesAsync(); } catch (Exception ex) { throw new Exception($"{ex.Message} {ex.InnerException.Message}"); } } else { throw new Exception("Data tidak ditemukan"); } }
//[Authorize(Roles = "Admin")] public async Task <DetailPasien> Getusername(string Username) { var model = await _DetailPasien.getusername(Username); return(model); }
public async Task UpdateAsync(Pengguna obj) { try { //harus dapetin role dari user var getdatapengguna = await getpenggunausername(obj.Username); if (getdatapengguna.rolename == null) { await _UserRole.AddUserToRoleAsync(obj.Username, obj.rolename); } else { await _UserRole.RemoveuserRole(getdatapengguna.Username, getdatapengguna.rolename); await _UserRole.AddUserToRoleAsync(obj.Username, obj.rolename); } if (getdatapengguna.rolename == null) { await _UserRole.AddUserToRoleAsync(obj.Username, obj.rolename); } if (obj.rolename == "Perawat") { if (getdatapengguna.IdPasien != null) { obj.IdPasien = null; } if (await _prosentase.getbyusername(obj.Username) == null) { await _prosentase.Deletebyusername(obj.Username); } var detailpasien = await _detailPasien.getusername(obj.Username); DetailPegawai pegawai = new DetailPegawai(); var pegawaii = await _detailPegawai.getusername(obj.Username, obj.TenantID); if (pegawaii == null) { pegawai.Username = obj.Username; pegawai.Jabatan = "Perawat"; pegawai.TanggalJoin = DateTime.Today; await _detailPegawai.CreateAsync(pegawai); } else { pegawai.Username = obj.Username; pegawai.Jabatan = "Perawat"; pegawai.TanggalJoin = DateTime.Today; await _detailPegawai.UpdateAsync(pegawai); } // create data pegawai } if (obj.rolename == "Dokter") { if (getdatapengguna.IdPasien != null) { obj.IdPasien = null; } if (getdatapengguna.Prosentase == 0) { obj.Prosentase = 30; } var detailpasien = await _detailPasien.getusername(obj.Username); //if (detailpasien != null) //{ // await _detailPasien.DeleteByuser(obj.Username); //} //tidak akan di delete karena bergantung pada detailidpasien pada bagian transaksi DetailPegawai pegawai = new DetailPegawai(); var pegawaii = await _detailPegawai.getusername(obj.Username, getdatapengguna.TenantID); if (pegawaii == null) { pegawai.Username = obj.Username; pegawai.Jabatan = "Dokter"; pegawai.TanggalJoin = DateTime.Today; await _detailPegawai.CreateAsync(pegawai); } else { pegawai.Username = obj.Username; pegawai.Jabatan = "Dokter"; pegawai.TanggalJoin = DateTime.Today; await _detailPegawai.UpdateAsync(pegawai); } var dt_prosen = await _context.Prosentase.Where(r => r.Username == getdatapengguna.Username && r.TenantID == getdatapengguna.TenantID).ToListAsync(); if (dt_prosen == null) { var dt_jenisTindakan = await _context.JenisTindakan.Where(r => r.TenantID == getdatapengguna.TenantID).Include(r => r.Prosentase).ToListAsync(); foreach (var dt_jenis in dt_jenisTindakan) { // create data pegawai Prosentase prosen = new Prosentase(); prosen.Username = obj.Username; prosen.IdJenisTindakan = dt_jenis.IdJenisTindakan; prosen.Prosen = obj.Prosentase; prosen.TenantID = getdatapengguna.TenantID; prosen.DetailPegawaiID = pegawaii.DetailPegawaiID; await _prosentase.CreateAsync(prosen); } } } else if (obj.rolename == "Pasien") { //kasi contraint klo ada data pasien nya g usah di delete tapi di update // klo g ada dia create DetailPasien detpasien = new DetailPasien(); var detailpas = await _detailPasien.getusername(obj.Username); if (detailpas == null) { obj.IdPasien = "pasien" + obj.Username; detpasien.IdPasien = obj.IdPasien; detpasien.Username = obj.Username; detpasien.Registrasi = DateTime.Today; await _detailPasien.CreateAsync(detpasien); } else { obj.IdPasien = "pasien" + obj.Username; detpasien.IdPasien = obj.IdPasien; detpasien.Username = obj.Username; detpasien.Registrasi = DateTime.Today; await _detailPasien.UpdateAsync(detpasien); } } Users user = new Users(); user.Username = obj.Username; user.Email = obj.Email; await _userService.Updateuser(user); } catch (Exception ex) { throw new Exception($"{ex.Message} {ex.InnerException.Message}"); } var data = await getpenggunausername(obj.Username); if (data != null) { try { data.IdPasien = obj.IdPasien; data.Alamat = obj.Alamat; data.Nama = obj.Nama; data.Gender = obj.Gender; data.Umur = obj.Umur; data.Kota = obj.Kota; data.NoTelpon = obj.NoTelpon; data.NoHP = obj.NoHP; data.Email = obj.Email; data.rolename = obj.rolename; data.Prosentase = obj.Prosentase; await _context.SaveChangesAsync(); } catch (Exception ex) { throw new Exception($"{ex.Message} {ex.InnerException.Message}"); } } else { throw new Exception("Data tidak ditemukan"); } }