public petugas SaveChange(petugas pet) { using (var db = new OcphDbContext()) { try { if (pet.Id <= 0) { pet.Id = db.Petugas.InsertAndGetLastID(pet); if (pet.Id <= 0) { throw new SystemException("Data tidak tersimpan"); } } else { if (db.Petugas.Update(O => new { O.Alamat, O.Jabatan, O.Nama, O.NIP }, pet, O => O.Id == pet.Id)) { throw new SystemException("Data Tidak Tersimpan"); } } return(pet); } catch (Exception ex) { throw new SystemException(ex.Message); } } }
public AdminService(petugas admin, IPermohonanService permohonanService, IBidangUOW bidangUow) { this.Petugas = admin; PermohonanService = permohonanService; BidangUnitOfWork = bidangUow; ListBidangTugas = BidangUnitOfWork.GetBidangTugas(); }
private bool ValidateData(petugas data) { if (string.IsNullOrEmpty(data.Role)) { return(false); } return(true); }
public petugas UpdatePetugas(petugas item) { using (var db = new OcphDbContext()) { var trans = db.BeginTransaction(); try { var saved = db.Workers.Update(O => new { O.Address, O.Handphone, O.Name }, item, O => O.Id == item.Id); if (saved) { foreach (var data in item.Roles) { role r = db.Roles.Where(O => O.Name == data.Name).FirstOrDefault(); if (r == null) { var roleid = db.Roles.Insert(new role { Name = data.Name, Id = data.Name + "123" }); r = db.Roles.Where(O => O.Name == data.Name).FirstOrDefault(); } if (db.UserRoles.Where(x => x.UserId == item.UserId && x.RoleId == r.Id).Count() <= 0) { if (!db.UserRoles.Insert(new userrole { RoleId = r.Id, UserId = item.UserId })) { throw new SystemException("Tidak Berhasil Role Petugas"); } } if (r != null & data.Id == "-1") { if (!db.UserRoles.Delete(O => O.UserId == item.UserId && O.RoleId == r.Id)) { throw new SystemException("Tidak Berhasil Tambah Petugas"); } } } item.Roles.RemoveAll(O => O.Id == "-1"); trans.Commit(); return(item); } throw new SystemException("Tidak Berhasil Tambah Petugas"); } catch (Exception ex) { trans.Rollback(); throw new SystemException(ex.Message); } } }
public IActionResult Get(int id) { try { petugas results = service.GetPetugasById(id); return(Ok(results)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public IHttpActionResult Put(int id, petugas item) { UOWUserProfile context = new UOWUserProfile(); try { var result = context.UpdatePetugas(item); return(Ok(result)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
private async Task <petugas> Register(petugas item, string role) { var user = new IdentityUser { Email = item.Email, UserName = item.Email }; try { var result = await _userManager.CreateAsync(user, "Petugas@123"); if (result.Succeeded) { if (!await _roleManager.RoleExistsAsync(role)) { var roleCreated = await _roleManager.CreateAsync(new IdentityRole { Name = role }); if (!roleCreated.Succeeded) { throw new SystemException("Petugas Tidak Berhasil Ditambahkan"); } } var addToRole = await _userManager.AddToRoleAsync(user, role); if (!addToRole.Succeeded) { throw new SystemException("Petugas Tidak Berhasil Ditambahkan"); } item.UserId = user.Id; var petugasResult = service.SaveChange(item); if (petugasResult == null) { throw new SystemException(); } return(petugasResult); } throw new SystemException(); } catch (Exception) { await _userManager.DeleteAsync(user); throw new SystemException("Petugas Tidak Berhasil Ditambahkan"); } }
public async Task <IActionResult> Post([FromBody] petugas value) { try { if (value != null) { var result = await Register(value, "petugas"); return(Ok(result)); } throw new SystemException("Data Tidak Valid"); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public Task <petugas> UpdateProfile(petugas value) { using (var db = new OcphDbContext()) { try { if (!db.Petugas.Update(O => new { O.Alamat, O.Foto, O.Nama, O.NIP, O.Jabatan }, value, O => O.Id == value.Id)) { throw new SystemException("Data Tidak Tersimpan"); } return(Task.FromResult(value)); } catch (Exception ex) { throw new SystemException(ex.Message); } } }
public petugas AddNewUser(petugas item) { using (var db = new OcphDbContext()) { try { item.Id = db.Workers.InsertAndGetLastID(item); if (item.Id > 0) { return(item); } throw new SystemException("Tidak Berhasil Tambah Petugas"); } catch (Exception ex) { throw new SystemException(ex.Message); } } }
public IActionResult Put(int id, [FromBody] petugas value) { petugas results = service.SaveChange(value); return(Ok(results)); }
public BidangUOW(petugas petugas) { SelectedPetugas = petugas; }
public async Task <IHttpActionResult> AddNewUser(petugas item) { UOWUserProfile context = new UOWUserProfile(); IdentityResult result = null; var userModel = new Models.ApplicationUser { Email = item.Email, UserName = item.Email }; try { if (item == null || !ValidateData(item)) { throw new SystemException("Lengkapi Data Anda"); } Random rand = new Random(); var password = Helper.GetRandomAlphanumericString(6) + "3#"; result = await UserManager.CreateAsync(userModel, password); if (result.Succeeded) { string code = await UserManager.GenerateEmailConfirmationTokenAsync(userModel.Id); System.Web.Mvc.UrlHelper urlHelper = new System.Web.Mvc.UrlHelper(HttpContext.Current.Request.RequestContext, RouteTable.Routes); string callbackUrl = urlHelper.Action( "ConfirmEmail", "Account", new { userId = userModel.Id, code = code }, HttpContext.Current.Request.Url.Scheme ); await UserManager.SendEmailAsync(userModel.Id, "Confirm your account", "Your Password : "******" , and Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>"); if (!await RoleManager.RoleExistsAsync(item.Role)) { var roleCreate = RoleManager.Create(new IdentityRole(Guid.NewGuid().ToString(), item.Role)); if (!roleCreate.Succeeded) { throw new SystemException("User Tidak Berhasil Ditambah"); } } var addUserRole = await UserManager.AddToRoleAsync(userModel.Id, item.Role); if (!addUserRole.Succeeded) { throw new SystemException("User Tidak Berhasil Ditambah"); } item.UserId = userModel.Id; var user = context.AddNewUser(item); if (user != null) { return(Ok(user)); } } throw new SystemException("User Tidak Berhasil Ditambah"); } catch (Exception ex) { if (result != null && result.Succeeded) { UserManager.Delete(userModel); } return(BadRequest(ex.Message)); } }