public async Task <IActionResult> GetEnterprise([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (User.Claims.Single(x => x.Type == ClaimTypes.Role).Value == "User")
            {
                List <Enterprise> enterprises = GetUserElements.GetUserEnterprises(Convert.ToInt32(User.FindFirstValue("Sub")), _context);
                if (enterprises == null || enterprises.Count == 0 || enterprises.Where(x => x.Id == id).ToList().Count == 0)
                {
                    return(NotFound());
                }
            }

            var enterprise = await _context.Enterprises.FindAsync(id);

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

            return(Ok(enterprise));
        }
        public ActionResult <IEnumerable <Room> > GetEnterpriseRooms([FromRoute] int id)
        {
            if (User.Claims.Single(x => x.Type == ClaimTypes.Role).Value == "User")
            {
                List <Enterprise> enterprises = GetUserElements.GetUserEnterprises(Convert.ToInt32(User.FindFirstValue("Sub")), _context);
                if (enterprises == null || enterprises.Count == 0 || enterprises.Where(x => x.Id == id).ToList().Count == 0)
                {
                    return(NotFound());
                }
            }

            return(_context.Rooms.Where(x => x.EnterpriseId == id).ToList());
        }
        public async Task <IActionResult> PutEnterprise([FromRoute] int id, [FromBody] Enterprise enterprise)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

            if (User.Claims.Single(x => x.Type == ClaimTypes.Role).Value == "User")
            {
                List <Enterprise> enterprises = GetUserElements.GetUserEnterprises(Convert.ToInt32(User.FindFirstValue("Sub")), _context);
                if (enterprises == null || enterprises.Count == 0 || enterprises.Where(x => x.Id == id).ToList().Count == 0)
                {
                    return(NotFound());
                }
            }

            _context.Entry(enterprise).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EnterpriseExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> DeleteEnterprise([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (User.Claims.Single(x => x.Type == ClaimTypes.Role).Value == "User")
            {
                List <Enterprise> enterprises = GetUserElements.GetUserEnterprises(Convert.ToInt32(User.FindFirstValue("Sub")), _context);
                if (enterprises == null || enterprises.Count == 0 || enterprises.Where(x => x.Id == id).ToList().Count == 0)
                {
                    return(NotFound());
                }
            }

            List <Room> rooms = _context.Rooms.Where(x => x.EnterpriseId == id).ToList();

            List <T_sensor> t_Sensors = new List <T_sensor>();
            List <H_sensor> h_Sensors = new List <H_sensor>();

            foreach (Room room in rooms)
            {
                t_Sensors.AddRange(GetUserElements.GetUserT_sensorsFromRoom(room.Id, _context));
                h_Sensors.AddRange(GetUserElements.GetUserH_sensorsFromRoom(room.Id, _context));
            }

            if (t_Sensors != null && t_Sensors.Count > 0)
            {
                foreach (var t_s in t_Sensors)
                {
                    _context.T_sensors.Remove(t_s);
                    _context.Entry(t_s).State = EntityState.Deleted;
                    await _context.SaveChangesAsync();
                }
            }

            if (h_Sensors != null && h_Sensors.Count > 0)
            {
                foreach (var h_s in h_Sensors)
                {
                    _context.H_sensors.Remove(h_s);
                    _context.Entry(h_s).State = EntityState.Deleted;
                    await _context.SaveChangesAsync();
                }
            }

            if (rooms != null && rooms.Count > 0)
            {
                foreach (var room in rooms)
                {
                    _context.Rooms.Remove(room);
                    _context.Entry(room).State = EntityState.Deleted;
                    await _context.SaveChangesAsync();
                }
            }

            var enterprise = await _context.Enterprises.FindAsync(id);

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

            _context.Enterprises.Remove(enterprise);
            _context.Entry(enterprise).State = EntityState.Deleted;

            await _context.SaveChangesAsync();

            return(Ok());
        }