public async Task <IActionResult> Delete([FromRoute] int id)
        {
            // Must be a Super Administrator to call this Method
            if (!UtilitySecurity.IsSuperUser(this.User.Identity.Name, GetConnectionString()))
            {
                return(BadRequest());
            }

            var optionsBuilder = new DbContextOptionsBuilder <ADefHelpDeskContext>();

            optionsBuilder.UseSqlServer(GetConnectionString());

            using (var context = new ADefHelpDeskContext(optionsBuilder.Options))
            {
                var objApiSecurity = await context.AdefHelpDeskApiSecurity.SingleOrDefaultAsync(x => x.Id == id);

                if (objApiSecurity == null)
                {
                    return(NotFound());
                }

                context.AdefHelpDeskApiSecurity.Remove(objApiSecurity);
                await context.SaveChangesAsync();

                // Log to the System Log
                Log.InsertSystemLog(
                    GetConnectionString(),
                    Constants.WebAPIAccountDeleted,
                    this.User.Identity.Name,
                    $"({this.User.Identity.Name}) Deleted Username: {objApiSecurity.Username}");
            }

            return(NoContent());
        }
        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));
        }
        public async Task <IActionResult> Post([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));
            }

            // Status to return
            DTOStatus objDTOStatus = new DTOStatus();

            objDTOStatus.StatusMessage = "Failure";
            objDTOStatus.Success       = false;

            #region Validate
            if (
                (ApiSecurityDTO.username == null || ApiSecurityDTO.username == "") ||
                (ApiSecurityDTO.password == null || ApiSecurityDTO.password == "")
                )
            {
                objDTOStatus.StatusMessage = $"Error: A Username and Password are 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

            try
            {
                var optionsBuilder = new DbContextOptionsBuilder <ADefHelpDeskContext>();
                optionsBuilder.UseSqlServer(GetConnectionString());

                using (var context = new ADefHelpDeskContext(optionsBuilder.Options))
                {
                    // Check for duplicate Username
                    var existingApiSecurity = await context.AdefHelpDeskApiSecurity.SingleOrDefaultAsync(x => x.Username == ApiSecurityDTO.username);

                    if (existingApiSecurity != null)
                    {
                        objDTOStatus.StatusMessage = $"Error: The username {ApiSecurityDTO.username} is already used";
                        objDTOStatus.Success       = false;
                        return(Ok(objDTOStatus));
                    }

                    var newApiSecurityDTO = new AdefHelpDeskApiSecurity();

                    newApiSecurityDTO.Username       = ApiSecurityDTO.username.Trim();
                    newApiSecurityDTO.Password       = ApiSecurityDTO.password.Trim();
                    newApiSecurityDTO.ContactName    = ApiSecurityDTO.contactName;
                    newApiSecurityDTO.ContactCompany = ApiSecurityDTO.contactCompany;
                    newApiSecurityDTO.ContactWebsite = ApiSecurityDTO.contactWebsite;
                    newApiSecurityDTO.ContactEmail   = ApiSecurityDTO.contactEmail;
                    newApiSecurityDTO.ContactPhone   = ApiSecurityDTO.contactPhone;
                    newApiSecurityDTO.IsActive       = ApiSecurityDTO.isActive;

                    context.AdefHelpDeskApiSecurity.Add(newApiSecurityDTO);
                    await context.SaveChangesAsync();

                    ApiSecurityDTO.id = newApiSecurityDTO.Id;

                    // Log to the System Log
                    Log.InsertSystemLog(
                        GetConnectionString(),
                        Constants.WebAPIAccountCreated,
                        this.User.Identity.Name,
                        $"({this.User.Identity.Name}) Created Username: {newApiSecurityDTO.Username}");
                }

                objDTOStatus.StatusMessage = "";
                objDTOStatus.Success       = true;
            }
            catch (Exception ex)
            {
                objDTOStatus.StatusMessage = ex.GetBaseException().Message;
                objDTOStatus.Success       = false;
                return(Ok(objDTOStatus));
            }

            return(Ok(objDTOStatus));
        }