public async Task <int> AddItemAsync(Album album)
        {
            try
            {
                using (var ctx = new PickPixDbContext(this.filePath))
                {
                    album.CreatedAt = DateTime.Now;
                    album.UpdatedAt = DateTime.Now;
                    album.Active    = true;
                    album.UserId    = this.UserId;
                    var findExistingAlbum = await ctx.Albums.Where(s => s.Name.ToLower() == album.Name.ToLower()).SingleOrDefaultAsync().ConfigureAwait(false);

                    if (findExistingAlbum != null)
                    {
                        return(-1);
                    }
                    var tracker = await ctx.Albums.AddAsync(album).ConfigureAwait(false);

                    await ctx.SaveChangesAsync().ConfigureAwait(false);
                }
                return(album.Id);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return(0);
        }
        public async Task <bool> UpdateItemAsync(Album item)
        {
            bool result = false;

            try
            {
                using (var ctx = new PickPixDbContext(this.filePath))
                {
                    Album album = ctx.Albums.SingleOrDefault(A => A.Id == item.Id);
                    if (album != null)
                    {
                        album.Name        = item.Name;
                        album.Description = item.Description;
                        album.Privacy     = item.Privacy;
                        album.Active      = item.Active;
                        album.UpdatedAt   = item.UpdatedAt;
                        await ctx.SaveChangesAsync().ConfigureAwait(false);

                        result = true;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }

            return(result);
        }
        public Task <bool> DeleteItemAsync(int id)
        {
            bool result = false;

            try
            {
                using (var dbContext = new PickPixDbContext(filePath))
                {
                    var pictureTags = dbContext.PictureTags.Where(p => p.PictureId == id).ToList();
                    if (pictureTags != null && pictureTags.Count > 0)
                    {
                        dbContext.PictureTags.RemoveRange(pictureTags);
                    }
                    var pictureAlbums = dbContext.PictureAlbums.Where(p => p.PictureId == id).ToList();
                    if (pictureAlbums != null && pictureAlbums.Count > 0)
                    {
                        dbContext.PictureTags.RemoveRange(pictureTags);
                    }
                    Picture picture = new Picture()
                    {
                        Id = id
                    };
                    dbContext.Pictures.Remove(picture);

                    dbContext.SaveChanges();
                    result = true;
                }
            }
            catch (Exception ex)
            {
                // Implement logging here
            }
            return(Task.FromResult(result));
        }
示例#4
0
        public async Task <IEnumerable <Picture> > GetTaggedPictures(int tagId)
        {
            IEnumerable <Picture> pictures = new List <Picture>();

            try
            {
                using (var ctx = new PickPixDbContext(this.filePath)) {
                    PictureTag[] picTagsResult = ctx.PictureTags.Where(a => (a.TagId == tagId && (a.Picture.UserId == this.UserId || a.Picture.Privacy.ToLower() == "public"))).ToArray();
                    foreach (PictureTag picTag in picTagsResult)
                    {
                        Picture pic = await ctx.Pictures.Where(p => p.Id == picTag.PictureId).FirstOrDefaultAsync().ConfigureAwait(false);

                        if (!pictures.Contains(pic))
                        {
                            IEnumerable <Picture> pic1 = await ctx.Pictures.Where(p => p.Id == pic.Id).Distinct().ToArrayAsync().ConfigureAwait(false);

                            pictures = pictures.Union(pic1);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //To-do: Implement logging
            }
            return(pictures);
        }
示例#5
0
        public async Task <bool> UpdateItemAsync(Tag item)
        {
            bool result = false;

            try
            {
                using (var ctx = new PickPixDbContext(this.filePath))
                {
                    Tag tag = ctx.Tags.SingleOrDefault(A => A.TagId == item.TagId);
                    if (tag != null)
                    {
                        tag.Name    = item.Name;
                        tag.TagType = item.TagType;
                        tag.Updated = DateTime.Now;
                        await ctx.SaveChangesAsync().ConfigureAwait(false);

                        result = true;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }

            return(result);
        }
 public async Task <IEnumerable <Models.Tag> > FindTagByPictureIdAsync(int PictureId)
 {
     using (var dbContext = new PickPixDbContext(filePath))
     {
         return(await dbContext.PictureTags
                .Where(s => s.PictureId == PictureId)
                .Select(c => c.Tag)
                .ToListAsync().ConfigureAwait(false));
     }
 }
        public async Task <Models.Tag> FindTagAsync(int ID)
        {
            Models.Tag tag;
            using (var dbContext = new PickPixDbContext(filePath))
            {
                tag = await dbContext.Tags.
                      Where(s => s.TagId == ID).SingleOrDefaultAsync().ConfigureAwait(false);

                return(tag);
            }
        }
        public async Task <int> HandleImageCommit(int userId, Dictionary <Stream, string> streams, string[][] megaTags, int albumId, string privacy, string notes)
        {
            Models.Tag[] applyTags = Array.Empty <Models.Tag>();
            // Album[] applyAlbums = Array.Empty<Album>();

            if (megaTags.Length > 0)
            {
                applyTags = await HandleTags(userId, megaTags).ConfigureAwait(false);
            }
            //if (albums.Length > 0)
            //    applyAlbums = await HandleAlbums(userId, albums).ConfigureAwait(false);

            using (var ctx = new PickPixDbContext(filePath))
            {
                foreach (KeyValuePair <Stream, string> curPic in streams)
                {
                    Picture pic = getPictureModel(curPic.Key, curPic.Value);
                    pic.Notes   = notes;
                    pic.Privacy = privacy;
                    pic.UserId  = userId;
                    int curPicId = await this.AddItemAsync(pic).ConfigureAwait(false);

                    if (curPicId == 0)
                    {
                        System.Diagnostics.Debug.WriteLine("========================================= Failed to save picture");
                        return(0);
                    }

                    //var tracker = await ctx.Pictures.UpdateItemAsync(curPic){
                    //}
                    foreach (Models.Tag curTag in applyTags)
                    {
                        var tracker = await ctx.PictureTags.AddAsync(new PictureTag
                        {
                            PictureId = curPicId,
                            TagId     = curTag.TagId
                        }).ConfigureAwait(false);

                        await ctx.SaveChangesAsync().ConfigureAwait(false);
                    }
                    if (albumId > 0)
                    {
                        await ctx.PictureAlbums.AddAsync(new PictureAlbum
                        {
                            PictureId = curPicId,
                            AlbumId   = albumId
                        }).ConfigureAwait(false);
                    }
                }
                await ctx.SaveChangesAsync().ConfigureAwait(false);
            }
            return(0);
        }
        public async Task <Album> FindItemAsync(int ID)
        {
            Album album;

            using (var dbContext = new PickPixDbContext(filePath))
            {
                album = await dbContext.Albums.
                        Where(s => s.Id == ID).SingleOrDefaultAsync().ConfigureAwait(false);

                return(album);
            }
        }
示例#10
0
        public async Task <User> GetUser(string email)
        {
            User user;

            using (var dbContext = new PickPixDbContext(filePath))
            {
                user = await dbContext.Users.
                       Where(s => s.Email.ToLower() == email.ToLower())
                       .SingleOrDefaultAsync().ConfigureAwait(false);
            }

            return(user);
        }
        public async Task <Picture> FindItemAsync(int ID)
        {
            Picture pic;

            using (var dbContext = new PickPixDbContext(filePath))
            {
                pic = await dbContext.Pictures

                      .Where(s => s.Id == ID).SingleOrDefaultAsync().ConfigureAwait(false);

                return(pic);
            }
        }
        public async Task <IEnumerable <Picture> > GetItemsAsync(string searchTerm)
        {
            if (string.IsNullOrWhiteSpace(searchTerm))
            {
                return(await GetItemsAsync().ConfigureAwait(false));
            }
            IEnumerable <Picture> pictures = new List <Picture>();

            // IEnumerable<Picture> union = new List<Picture>();
            try
            {
                using (var ctx = new PickPixDbContext(this.filePath))
                {
                    var        albumPics  = ctx.Albums.Where(a => (a.Name.ToLower().Contains(searchTerm.ToLower()) && (a.UserId == this.UserId || a.Privacy.ToLower() == "public"))).Select(p => p.PictureAlbums).ToList();
                    List <int> pictureIds = new List <int>();
                    foreach (var albumPic in albumPics)
                    {
                        var pics = albumPic.Select(p => p.PictureId).ToList();
                        if (pics != null && pics.Count > 0)
                        {
                            pictureIds.AddRange(pics);
                        }
                    }
                    var tagPics = ctx.Tags.Where(a => a.Name.ToLower().Contains(searchTerm.ToLower())).Select(p => p.PictureTags).ToList();
                    foreach (var tagPic in tagPics)
                    {
                        var pics = tagPic.Select(p => p.PictureId).ToList();
                        if (pics != null && pics.Count > 0)
                        {
                            pictureIds.AddRange(pics);
                        }
                    }

                    if (pictureIds.Count > 0)
                    {
                        pictureIds = pictureIds.Distinct().ToList();
                    }
                    if (pictureIds != null && pictureIds.Count > 0)
                    {
                        pictures = await ctx.Pictures.
                                   Where(p => (pictureIds.Contains(p.Id) && (p.UserId == this.UserId || p.Privacy.ToLower() == "public"))).
                                   ToListAsync().ConfigureAwait(false);
                    }
                }
            }
            catch (Exception ex)
            {
                //To-do: Implement logging
            }
            return(pictures);
        }
示例#13
0
        public async Task <int> RegisterUser(User user)
        {
            try
            {
                using (var dbContext = new PickPixDbContext(filePath))
                {
                    var tracker = await dbContext.Users.AddAsync(user).ConfigureAwait(false);

                    await dbContext.SaveChangesAsync().ConfigureAwait(false);
                }
                return(user.Id);
            }
            catch (Exception)
            {
                return(0);
            }
        }
        public async Task <IEnumerable <Picture> > GetItemsAsync()
        {
            IEnumerable <Picture> pictures = new List <Picture>();

            try
            {
                using (var ctx = new PickPixDbContext(this.filePath))
                {
                    pictures = await Task.FromResult(ctx.Pictures.Where(s => (s.UserId == this.UserId || s.Privacy.ToLower() == "public")).ToList()).ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                //To Do: implement logging
            }
            return(pictures);
        }
示例#15
0
        public async Task <IEnumerable <Tag> > GetItemsAsync()
        {
            IEnumerable <Tag> tags = new List <Tag>();

            try
            {
                using (var ctx = new PickPixDbContext(this.filePath))
                {
                    tags = await Task.FromResult(ctx.Tags.ToList()).ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                //To Do: implement logging
            }
            return(tags);
        }
示例#16
0
        public async Task <IEnumerable <Album> > GetItemsAsync(string searchTerm)
        {
            IEnumerable <Album> albums = new List <Album>();

            try
            {
                using (var ctx = new PickPixDbContext(this.filePath))
                {
                    albums = await Task.FromResult(ctx.Albums.Where(S => S.Name.ToLower().Contains(searchTerm.ToLower())).ToList()).ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return(albums);
        }
示例#17
0
        public async Task <IEnumerable <Album> > GetItemsAsync()
        {
            IEnumerable <Album> albums = new List <Album>();

            try
            {
                using (var ctx = new PickPixDbContext(this.filePath))
                {
                    albums = await Task.FromResult(ctx.Albums.Where(s => (s.UserId == this.UserId || s.Privacy.ToLower() == "public")).ToList()).ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return(albums);
        }
示例#18
0
        public async Task <IEnumerable <Picture> > GetAlbumPictures(int albumId)
        {
            IEnumerable <Picture> pictures = new List <Picture>();

            try
            {
                using (var ctx = new PickPixDbContext(this.filePath))
                {
                    /*var res = ctx.Albums.Where(a => a.Id == albumId).Select(s => new
                     * {
                     *  Pictures = s.PictureAlbums.Select(p => p.Picture)
                     * }).ToList();
                     * if (res != null && res.Count > 0)
                     * {
                     *  pictures = res[0].Pictures;
                     * }*/
                    PictureAlbum[] picAlbumsResult = await ctx.PictureAlbums.
                                                     Where(a => (a.AlbumId == albumId &&
                                                                 (a.Picture.UserId == this.UserId || a.Picture.Privacy.ToLower() == "public"))).ToArrayAsync().ConfigureAwait(false);

                    foreach (PictureAlbum picAlbum in picAlbumsResult)
                    {
                        Picture pic = await ctx.Pictures.Where(p => p.Id == picAlbum.PictureId).FirstOrDefaultAsync().ConfigureAwait(false);

                        if (!pictures.Contains(pic))
                        {
                            IEnumerable <Picture> pic1 = await ctx.Pictures.Where(p => p.Id == pic.Id).Distinct().ToArrayAsync().ConfigureAwait(false);

                            pictures = pictures.Union(pic1);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //To-do: Implement logging
            }
            return(pictures);
        }
        public async Task <int> AddItemAsync(Picture picture)
        {
            bool result = false;

            try
            {
                using (var cxt = new PickPixDbContext(this.filePath))
                {
                    picture.Created = DateTime.Now;
                    picture.Updated = DateTime.Now;
                    cxt.Pictures.Add(picture);
                    await cxt.SaveChangesAsync().ConfigureAwait(false);

                    return(picture.Id);
                }
            }
            catch (Exception ex)
            {
                //To-Do Implement logging
            }
            return(0);
        }
        public async Task <int> AddTagAsync(Models.Tag tag)
        {
            try
            {
                using (var dbContext = new PickPixDbContext(filePath))
                {
                    Tag findExistingTag = await dbContext.Tags.Where(s => (s.Name.ToLower() == tag.Name.ToLower()) && (s.TagType == tag.TagType)).FirstOrDefaultAsync().ConfigureAwait(false);

                    if (findExistingTag != null)
                    {
                        return(findExistingTag.TagId);
                    }
                    var tracker = await dbContext.Tags.AddAsync(tag).ConfigureAwait(false);

                    await dbContext.SaveChangesAsync().ConfigureAwait(false);
                }
                return(tag.TagId);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(0);
        }