// Methods #region public static DTOStatus UpdateCategory(int id, CategoryNode categoryNode, string ConnectionString) public static DTOStatus UpdateCategory(int id, CategoryNode categoryNode, string ConnectionString) { // Status to return DTOStatus objDTOStatus = new DTOStatus(); objDTOStatus.Success = true; var optionsBuilder = new DbContextOptionsBuilder <ADefHelpDeskContext>(); optionsBuilder.UseSqlServer(ConnectionString); using (var context = new ADefHelpDeskContext(optionsBuilder.Options)) { var existingCategoryNode = context.AdefHelpDeskCategories.SingleOrDefault(x => x.CategoryId == id); if (existingCategoryNode == null) { objDTOStatus.StatusMessage = $"id #{id} Not Found"; objDTOStatus.Success = false; return(objDTOStatus); } // Update the Node existingCategoryNode.CategoryName = categoryNode.NodeName; if (categoryNode.ParentId > 0) { existingCategoryNode.ParentCategoryId = categoryNode.ParentId; } else { existingCategoryNode.ParentCategoryId = null; } existingCategoryNode.Selectable = categoryNode.Selectable; existingCategoryNode.RequestorVisible = categoryNode.RequestorVisible; context.Entry(existingCategoryNode).State = EntityState.Modified; try { context.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { objDTOStatus.StatusMessage = ex.GetBaseException().Message; objDTOStatus.Success = false; return(objDTOStatus); } } return(objDTOStatus); }
public static DTOStatus UpdateRole(int id, RoleDTO RoleDTO, string ConnectionString) { // Status to return DTOStatus objDTOStatus = new DTOStatus(); objDTOStatus.Success = true; var optionsBuilder = new DbContextOptionsBuilder <ADefHelpDeskContext>(); optionsBuilder.UseSqlServer(ConnectionString); using (var context = new ADefHelpDeskContext(optionsBuilder.Options)) { var existingRole = context.AdefHelpDeskRoles.SingleOrDefaultAsync(x => x.Id == id).Result; if (existingRole == null) { objDTOStatus.StatusMessage = $"id #{id} Not Found"; objDTOStatus.Success = false; return(objDTOStatus); } // Update the Role existingRole.RoleName = RoleDTO.roleName; context.Entry(existingRole).State = EntityState.Modified; try { context.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { objDTOStatus.StatusMessage = ex.GetBaseException().Message; objDTOStatus.Success = false; } } return(objDTOStatus); }
public async Task <IActionResult> Put([FromRoute] int id, [FromBody] ApiSecurityDTO ApiSecurityDTO) { // Must be a Super Administrator to call this Method if (!UtilitySecurity.IsSuperUser(this.User.Identity.Name, GetConnectionString())) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != ApiSecurityDTO.id) { return(BadRequest()); } // Status to return DTOStatus objDTOStatus = new DTOStatus(); objDTOStatus.StatusMessage = "Failure"; objDTOStatus.Success = false; #region Validate if (ApiSecurityDTO.password == null || ApiSecurityDTO.password == "") { objDTOStatus.StatusMessage = $"Error: A Password is required."; objDTOStatus.Success = false; return(Ok(objDTOStatus)); } if (ApiSecurityDTO.password.Trim().Length < 5) { objDTOStatus.StatusMessage = $"Error: A password longer than 5 characters is required."; objDTOStatus.Success = false; return(Ok(objDTOStatus)); } #endregion var optionsBuilder = new DbContextOptionsBuilder <ADefHelpDeskContext>(); optionsBuilder.UseSqlServer(GetConnectionString()); using (var context = new ADefHelpDeskContext(optionsBuilder.Options)) { var existingApiSecurity = await context.AdefHelpDeskApiSecurity.SingleOrDefaultAsync(x => x.Id == id); if (existingApiSecurity == null) { return(NotFound()); } // Update the ApiSecurity existingApiSecurity.ContactName = ApiSecurityDTO.contactName; existingApiSecurity.ContactCompany = ApiSecurityDTO.contactCompany; existingApiSecurity.ContactWebsite = ApiSecurityDTO.contactWebsite; existingApiSecurity.ContactEmail = ApiSecurityDTO.contactEmail; existingApiSecurity.ContactPhone = ApiSecurityDTO.contactPhone; existingApiSecurity.IsActive = ApiSecurityDTO.isActive; if (ApiSecurityDTO.password != null) { if (ApiSecurityDTO.password.Trim().Length > 1) { existingApiSecurity.Password = ApiSecurityDTO.password.Trim(); } } context.Entry(existingApiSecurity).State = EntityState.Modified; try { await context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException ex) { objDTOStatus.StatusMessage = ex.GetBaseException().Message; objDTOStatus.Success = false; return(Ok(objDTOStatus)); } catch (Exception ex) { objDTOStatus.StatusMessage = ex.GetBaseException().Message; objDTOStatus.Success = false; return(Ok(objDTOStatus)); } // Log to the System Log Log.InsertSystemLog( GetConnectionString(), Constants.WebAPIAccountUpdated, this.User.Identity.Name, $"({this.User.Identity.Name}) Updated Username: {ApiSecurityDTO.username}"); } objDTOStatus.StatusMessage = ""; objDTOStatus.Success = true; return(Ok(objDTOStatus)); }