public IndiagramForDevice CreateIndiagram(long userId, long deviceId, IndiagramRequest request)
        {
            Indiagram indiagram = _context.Indiagrams.Add(new Indiagram
            {
                UserId = userId
            });

            IndiagramInfo info = _context.Set<IndiagramInfo>().Add(new IndiagramInfo
            {
                IndiagramId = indiagram.Id,
                Version = request.Version,
                ParentId = request.ParentId,
                Position = request.Position,
                Text = request.Text,
                IsCategory = request.IsCategory
            });
            _context.SaveChanges();

            indiagram.LastIndiagramInfoId = info.Id;

            IndiagramState state = _context.Set<IndiagramState>().Add(new IndiagramState
            {
                DeviceId = deviceId,
                IndiagramInfoId = info.Id,
                IsEnabled = request.IsEnabled
            });

            _context.SaveChanges();
            return ToIndiagramForDevice(indiagram, info, state);
        }
        public IndiagramForDevice UpdateIndiagram(long userId, long deviceId, IndiagramRequest request)
        {
            Indiagram indiagram = GetIndiagramUser(userId, request.Id);

            if (indiagram == null)
            {
                return null;
            }

            IndiagramInfo info = CreateIndiagramInfo(indiagram, request.Version);

            info.ParentId = request.ParentId;
            info.Position = request.Position;
            info.Text = request.Text;
            info.IsCategory = request.IsCategory;

            IndiagramState state = info.States.FirstOrDefault(x => x.DeviceId == deviceId);
            if (state != null)
            {
                state.IsEnabled = request.IsEnabled;
            }
            else
            {
                _context.Set<IndiagramState>().Add(new IndiagramState
                {
                    DeviceId = deviceId,
                    IndiagramInfoId = info.Id,
                    IsEnabled = request.IsEnabled
                });
            }
            _context.SaveChanges();
            return ToIndiagramForDevice(indiagram, info, state);
        }