public async Task <IHttpActionResult> DeleteIPAddressRange(int id)
        {
            WhitelistIP whitelistIP = await db.WhitelistIPs.FindAsync(id);

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

            db.WhitelistIPs.Remove(whitelistIP);

            await db.SaveChangesAsync();

            IPAddressRange iPAddressRange = new IPAddressRange
            {
                Id           = whitelistIP.Id,
                StartAddress = whitelistIP.StartIP.IPAddressToString(),
                EndAddress   = whitelistIP.EndIP.IPAddressToString(),
                IsActive     = whitelistIP.IsActive,
                RuleName     = whitelistIP.RuleName
            };

            MemoryCacher.DeleteIfContains(iPAddressRange);

            return(Ok(iPAddressRange));
        }
        public async Task <IHttpActionResult> PostIPAddressRange(IPAddressRange iPAddressRange)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!iPAddressRange.ValidAddresses())
            {
                return(BadRequest());
            }

            WhitelistIP whitelistIP = new WhitelistIP()
            {
                RuleName = iPAddressRange.RuleName,
                StartIP  = iPAddressRange.StartAddress.GetBytes(),
                EndIP    = iPAddressRange.EndAddress.GetBytes(),
                IsActive = iPAddressRange.IsActive
            };

            db.WhitelistIPs.Add(whitelistIP);
            await db.SaveChangesAsync();

            return(CreatedAtRoute("DefaultApi", new { id = iPAddressRange.Id }, iPAddressRange));
        }
        public async Task <IHttpActionResult> PutIPAddressRange(int id, IPAddressRange iPAddressRange)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != iPAddressRange.Id)
            {
                return(BadRequest());
            }

            if (!iPAddressRange.ValidAddresses())
            {
                return(BadRequest());
            }
            WhitelistIP whitelistIP = new WhitelistIP()
            {
                Id       = iPAddressRange.Id,
                RuleName = iPAddressRange.RuleName,
                StartIP  = iPAddressRange.StartAddress.GetBytes(),
                EndIP    = iPAddressRange.EndAddress.GetBytes(),
                IsActive = iPAddressRange.IsActive
            };

            db.Entry(whitelistIP).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();

                MemoryCacher.UpdateOrDelete(iPAddressRange);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!IPAddressRangeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public async Task <IHttpActionResult> GetIPAddressRange(int id)
        {
            WhitelistIP whitelistIP = await db.WhitelistIPs.FindAsync(id);

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

            return(Ok(new IPAddressRange
            {
                Id = whitelistIP.Id,
                StartAddress = whitelistIP.StartIP.IPAddressToString(),
                EndAddress = whitelistIP.EndIP.IPAddressToString(),
                IsActive = whitelistIP.IsActive,
                RuleName = whitelistIP.RuleName
            }));
        }