Beispiel #1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="pictureId"></param>
 public void UnLinkPicture(int pictureId)
 {
     try
     {
         int index = PicturesInAlbums.ToList().FindIndex(o => o.PictureId == pictureId);
         PicturesInAlbums.RemoveAt(index);
     }
     catch { }
 }
Beispiel #2
0
        public AlbumEntity RemovePictureDependency(int albumId, int pictureId, bool save = true)
        {
            AlbumOptionsSelect options = new AlbumOptionsSelect {
                PrimaryKey = albumId
            };

            options.Dependencies.Add(EnumEntitiesDependencies.PicturesInAlbums);

            AlbumEntity      album      = Select(options);
            PicturesInAlbums dependency = album.PicturesInAlbums.SingleOrDefault(c => c.PictureId == pictureId);

            album.PicturesInAlbums.Remove(dependency);

            return(Update(album, save));
        }
Beispiel #3
0
        /// <summary>
        /// Method add pictures to albums.
        /// </summary>
        public static AlbumEntity AssociateDefaultPicture(int albumPK)
        {
            AlbumEntity entity = Db.Albums.SingleOrNull(
                new AlbumOptionsSelect
            {
                PrimaryKey   = albumPK,
                Dependencies = { EnumEntitiesDependencies.All }
            });

            PicturesInAlbums p = entity?.PicturesInAlbums?[0];

            if (p != null)
            {
                entity.PreviewPictureId    = p.PictureId;
                entity.ThumbnailPictureId  = p.PictureId;
                entity.BackgroundPictureId = p.PictureId;
            }

            return(DbUpdate(entity));
        }
Beispiel #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="pictureId"></param>
        /// <param name="isNew"></param>
        public void LinkPicture(int pictureId, bool isNew = true)
        {
            try
            {
                int index = PicturesInAlbums.ToList().FindIndex(o => o.PictureId == pictureId);

                if (index < 0)
                {
                    if (isNew)
                    {
                        PicturesInAlbums.Add(new PicturesInAlbums {
                            PictureId = pictureId
                        });
                    }
                    else
                    {
                        PicturesInAlbums.Add(new PicturesInAlbums {
                            PictureId = pictureId, AlbumId = PrimaryKey
                        });
                    }
                }
            }
            catch { }
        }
Beispiel #5
0
        /// <summary>
        /// Method to insert a list of Picture entities into the database.
        /// </summary>
        /// <param name="newItems">The list of items to add.</param>
        /// <param name="albums">The list of Albums, past on reference, to associate the items and update their informations.</param>
        /// <returns>The list of new items inserted in the database.</returns>
        public static IList <PictureEntity> DbInsert(IEnumerable <PictureEntity> newItems, ref IEnumerable <AlbumEntity> albums)
        {
            log.Debug($"{typeof(PictureEntityCollection).Name}.{MethodBase.GetCurrentMethod().Name} 3 : {newItems?.Count()} newItems to insert into database.");

            if (newItems == null)
            {
                log.Error(Exceptions.GetArgumentNull(nameof(newItems), typeof(IEnumerable <PictureEntity>)).Output());
                return(null);
            }

            if (newItems.Count() == 0)
            {
                log.Debug($"{typeof(AlbumEntityCollection).Name}.{MethodBase.GetCurrentMethod().Name} : the list of {typeof(IEnumerable<PictureEntity>).Name} to insert is empty.");
                return(null);
            }

            if (albums == null)
            {
                log.Error(Exceptions.GetArgumentNull(nameof(albums), typeof(IEnumerable <AlbumEntity>)).Output());
                return(null);
            }

            // Create new Picture list to return.
            IList <PictureEntity> itemsAdded = new List <PictureEntity>();

            try
            {
                log.Debug("----------------------------------------------------------------------------------------------------------");
                log.Info($"Adding {newItems.Count()} picture{(newItems.Count() > 1 ? "s" : "")} : Please wait...");

                foreach (PictureEntity entity in newItems)
                {
                    if (entity == null)
                    {
                        log.Error($"{typeof(PictureEntityCollection).Name}.{MethodBase.GetCurrentMethod().Name} : Picture null can't be inserted in database.");
                        continue;
                    }

                    // Process association to each AlbumEntity.
                    if (albums != null && albums.Count() > 0)
                    {
                        foreach (AlbumEntity a in albums)
                        {
                            if (a == null)
                            {
                                log.Error(new NullReferenceException($"Album null can't be associated to a Picture.").Output());
                                continue;
                            }

                            // Try to find Picture and Album dependency
                            PicturesInAlbums dependency = (new List <PicturesInAlbums>(entity.PicturesInAlbums)).Find(x => x.AlbumId == a.AlbumId);

                            // Associate Picture to the Album if not already set.
                            if (dependency == null)
                            {
                                entity.PicturesInAlbums.Add(
                                    new PicturesInAlbums
                                {
                                    AlbumId  = a.AlbumId,
                                    Ordering = entity.PicturesInAlbums.Count + 1
                                }
                                    );

                                log.Info($"Picture [{entity.PrimaryKey}:{entity.Name}] associated to Album [{a.PrimaryKey}:{a.Name}].");
                            }
                        }
                    }

                    // Add picture strored in database to the return list.
                    var pictAdded = Db.Pictures.Add(entity);
                    itemsAdded.Add(pictAdded);

                    foreach (AlbumEntity a in albums)
                    {
                        if (a == null)
                        {
                            log.Error(new NullReferenceException($"Picture null can't be associated to a Album.").Output());
                            continue;
                        }

                        a.LinkPicture(pictAdded.PrimaryKey, false);

                        log.Info($"Album [{a.PrimaryKey}:{a.Name}] associated to Picture [{entity.PrimaryKey}:{entity.Name}].");
                    }

                    log.Info($"Picture [{entity.PrimaryKey}:{entity.Name}] added to database.");
                }

                AppNavigatorBase.Clear();
                log.Debug("Application navigator cleared.");
                log.Info($"Adding {itemsAdded?.Count() ?? 0} picture{(itemsAdded?.Count() > 1 ? "s" : "")} : Done.");
            }

            catch (Exception ex)
            {
                log.Error(ex.Output(), ex);
                MessageBoxs.Fatal(ex, $"Adding {newItems.Count()}/{newItems.Count()} picture{(newItems.Count() > 1 ? "s" : "")} : Failed.");
            }

            log.Debug("----------------------------------------------------------------------------------------------------------");
            return(itemsAdded);
        }
Beispiel #6
0
        /// <summary>
        /// Method to add a <see cref="PictureEntity"/>.
        /// </summary>
        /// <param name="albumId">The <see cref="AlbumEntity"/> primary key for association filter.</param>
        /// <param name="picture">The <see cref="PictureEntity"/> to add.</param>
        /// <param name="insertId"></param>
        /// <returns>The added <see cref="PictureEntity"/>.</returns>
        public PictureEntity Add(int albumId, PictureEntity picture, int insertId = 0)
        {
            // Search if item is already in database.
            PictureEntity item = Connector.Pictures.Where(p => p.PicturePath == picture.PicturePath).SingleOrDefault();

            // Get the max order of pictures associated to the album.
            int maxorder = MaxOrder(albumId);

            // Initialize return entity.
            EntityEntry et = null;

            if (insertId == 0)
            {
                insertId = InsertId(Connector.Pictures, x => x.PictureId);
            }

            if (picture == null || picture.PictureId == 0)
            {
                picture.PictureId = insertId;
                //picture.PicturesInAlbums =
                //    new ObservablePicturesInAlbums<PictureEntity, AlbumEntity>
                //    {
                //        new PicturesInAlbums
                //        {
                //            AlbumId = albumId,
                //            Ordering = ++maxorder
                //        }
                //    };
                picture.PicturesInAlbums.Add(
                    new PicturesInAlbums
                {
                    AlbumId  = albumId,
                    Ordering = ++maxorder
                });

                et = Connector.Pictures.Add(picture);
            }
            else
            {
                PicturesInAlbums dependency = (new List <PicturesInAlbums>(picture.PicturesInAlbums)).Find(p => p.AlbumId == albumId);
                if (dependency == null)
                {
                    picture.PicturesInAlbums.Add(
                        new PicturesInAlbums
                    {
                        AlbumId  = albumId,
                        Ordering = ++maxorder
                    }
                        );

                    et = Connector.Pictures.Update(picture);
                }
                else
                {
                    (new List <PicturesInAlbums>(picture.PicturesInAlbums)).Find(p => p.AlbumId == albumId).Ordering = ++maxorder;
                }
            }

            Save();

            if (et != null)
            {
                return((PictureEntity)et.Entity);
            }
            else
            {
                return(picture);
            }
        }