Пример #1
0
        public void InsertMediaItem(IMediaItem item, int index)
        {
            // make sure the input is within bounds and is not null
            if (ReferenceEquals(item, null) || index > PlaylistCount || index < 0 || ReferenceEquals(PlaylistId, null))
            {
                return;
            }

            ISQLiteConnection conn = null;
            try
            {
                int? id = Injection.Kernel.Get<IItemRepository>().GenerateItemId(ItemType.PlaylistItem);

                if (!ReferenceEquals(id, null))
                {
                    // to do - better way of knowing whether or not a query has been successfully completed.
                    conn = Injection.Kernel.Get<IDatabase>().GetSqliteConnection();
                    conn.BeginTransaction();
                    for (int position = (int)PlaylistCount - 1; position >= index; position--)
                    {
                        logger.IfInfo("Updating position " + position + " to " + (position + 1));
                        conn.ExecuteLogged("UPDATE PlaylistItem SET ItemPosition = ItemPosition + 1 WHERE PlaylistId = ? AND ItemPosition = ?", PlaylistId, position);
                    }

                    // conditional rollback here

                    // Insert the new item
                    var playlistItem = new PlaylistItem();
                    playlistItem.PlaylistItemId = id;
                    playlistItem.PlaylistId = PlaylistId;
                    playlistItem.ItemType = item.ItemType;
                    playlistItem.ItemId = item.ItemId;
                    playlistItem.ItemPosition = index;
                    int affected = conn.Insert(playlistItem);

                    // conditional rollback here

                    if (affected > 0)
                    {
                        PlaylistCount++;
                        PlaylistDuration += (int)item.Duration;
                        LastUpdateTime = DateTime.UtcNow.ToUnixTime();
                        Md5Hash = CalculateHash();
                        conn.ExecuteLogged("UPDATE Playlist SET PlaylistName = ?, PlaylistCount = ?, PlaylistDuration = ?, Md5Hash = ?, LastUpdateTime = ? " +
                                           "WHERE PlaylistId = ?", PlaylistName == null ? "" : PlaylistName, PlaylistCount, PlaylistDuration, Md5Hash, LastUpdateTime, PlaylistId);

                        conn.Commit();
                    }
                    else
                    {
                        conn.Rollback();
                    }
                }
            }
            catch (Exception e)
            {
                if (!ReferenceEquals(conn, null))
                {
                    conn.Rollback();
                }
                logger.Error(e);
            }
            finally
            {
                Injection.Kernel.Get<IDatabase>().CloseSqliteConnection(conn);
            }
        }
Пример #2
0
        public void InsertMediaItem(IMediaItem item, int index)
        {
            // make sure the input is within bounds and is not null
            if (ReferenceEquals(item, null) || index > PlaylistCount || index < 0 || ReferenceEquals(PlaylistId, null))
            {
                return;
            }

            ISQLiteConnection conn = null;

            try
            {
                int?id = Injection.Kernel.Get <IItemRepository>().GenerateItemId(ItemType.PlaylistItem);

                if (!ReferenceEquals(id, null))
                {
                    // to do - better way of knowing whether or not a query has been successfully completed.
                    conn = Injection.Kernel.Get <IDatabase>().GetSqliteConnection();
                    conn.BeginTransaction();
                    for (int position = (int)PlaylistCount - 1; position >= index; position--)
                    {
                        logger.IfInfo("Updating position " + position + " to " + (position + 1));
                        conn.ExecuteLogged("UPDATE PlaylistItem SET ItemPosition = ItemPosition + 1 WHERE PlaylistId = ? AND ItemPosition = ?", PlaylistId, position);
                    }

                    // conditional rollback here

                    // Insert the new item
                    var playlistItem = new PlaylistItem();
                    playlistItem.PlaylistItemId = id;
                    playlistItem.PlaylistId     = PlaylistId;
                    playlistItem.ItemType       = item.ItemType;
                    playlistItem.ItemId         = item.ItemId;
                    playlistItem.ItemPosition   = index;
                    int affected = conn.Insert(playlistItem);

                    // conditional rollback here

                    if (affected > 0)
                    {
                        PlaylistCount++;
                        PlaylistDuration += (int)item.Duration;
                        LastUpdateTime    = DateTime.UtcNow.ToUnixTime();
                        Md5Hash           = CalculateHash();
                        conn.ExecuteLogged("UPDATE Playlist SET PlaylistName = ?, PlaylistCount = ?, PlaylistDuration = ?, Md5Hash = ?, LastUpdateTime = ? " +
                                           "WHERE PlaylistId = ?", PlaylistName == null ? "" : PlaylistName, PlaylistCount, PlaylistDuration, Md5Hash, LastUpdateTime, PlaylistId);

                        conn.Commit();
                    }
                    else
                    {
                        conn.Rollback();
                    }
                }
            }
            catch (Exception e)
            {
                if (!ReferenceEquals(conn, null))
                {
                    conn.Rollback();
                }
                logger.Error(e);
            }
            finally
            {
                Injection.Kernel.Get <IDatabase>().CloseSqliteConnection(conn);
            }
        }
Пример #3
0
        public void AddMediaItem(IMediaItem item, bool updateDatabase = true)
        {
            ISQLiteConnection conn = null;
            try
            {
                int? id = Injection.Kernel.Get<IItemRepository>().GenerateItemId(ItemType.PlaylistItem);
                // to do - better way of knowing whether or not a query has been successfully completed.
                conn = Injection.Kernel.Get<IDatabase>().GetSqliteConnection();
                var playlistItem = new PlaylistItem();
                playlistItem.PlaylistItemId = id;
                playlistItem.PlaylistId = PlaylistId;
                playlistItem.ItemType = item.ItemType;
                playlistItem.ItemId = item.ItemId;
                playlistItem.ItemPosition = PlaylistCount == null ? 0 : PlaylistCount;
                int affected = conn.Insert(playlistItem);

                if (affected > 0)
                {
                    PlaylistCount++;
                    PlaylistDuration += (int)item.Duration;
                }

                if (updateDatabase)
                {
                    UpdateDatabase();
                }
            }
            catch (Exception e)
            {
                logger.Error(e);
            }
            finally
            {
                Injection.Kernel.Get<IDatabase>().CloseSqliteConnection(conn);
            }
        }