Exemplo n.º 1
0
        public void CreateSharedLock(int contentId, string @lock)
        {
            var timeLimit = DateTime.UtcNow.AddTicks(-SharedLockTimeout.Ticks);
            var row       = SharedLocks.FirstOrDefault(x => x.ContentId == contentId);

            if (row != null && row.CreationDate < timeLimit)
            {
                SharedLocks.Remove(row);
                row = null;
            }

            if (row == null)
            {
                var newSharedLockId = SharedLocks.Count == 0 ? 1 : SharedLocks.Max(t => t.SharedLockId) + 1;
                SharedLocks.Add(new SharedLockRow
                {
                    SharedLockId = newSharedLockId,
                    ContentId    = contentId,
                    Lock         = @lock,
                    CreationDate = DateTime.UtcNow
                });
                return;
            }

            if (row.Lock != @lock)
            {
                throw new LockedNodeException(null, $"The node (#{contentId}) is locked by another shared lock.");
            }

            row.CreationDate = DateTime.UtcNow;
        }