public async static Task SaveItems(List <Models.CollectionControlModel> list) { var data = list.First().Data; if (data is PictureAlbum) { foreach (var album in list) { var pictureAlbum = album.Data as PictureAlbum; if (await CreatePhotoAlbumAsync(pictureAlbum.Name, pictureAlbum.Pictures.First().GetThumbnail())) { List <PPhoto> photos = new List <PPhoto>(); foreach (var picture in pictureAlbum.Pictures) { PPhoto photo = new PPhoto(); photo.AlbumName = pictureAlbum.Name; photo.Name = picture.Name; photo.Path = Path.Combine(Directories.AlbumRoot, photo.AlbumName, photo.Name); await Task.Run(async() => { using (var source = picture.GetImage()) { byte[] arr = new byte[source.Length]; await source.ReadAsync(arr, 0, arr.Length); photo.Data = new MemoryStream(); await photo.Data.WriteAsync(arr, 0, arr.Length); } }); photos.Add(photo); } await FileManager.AddPhotosAsync(photos, CancellationToken.None); } else { ShellToast toast = new ShellToast(); toast.Content = "Album is already imported"; } } } else { if (data is Picture) { string albumName = (data as Picture).Album.Name; if (!PhotoAlbumExcists(albumName)) { await CreatePhotoAlbumAsync(albumName, (data as Picture).GetThumbnail()); } List <PPhoto> photos = new List <PPhoto>(); foreach (var p in list) { Picture picture = p.Data as Picture; PPhoto photo = new PPhoto(); photo.AlbumName = albumName; photo.Name = picture.Name; photo.Path = Path.Combine(Directories.AlbumRoot, photo.AlbumName, photo.Name); await Task.Run(async() => { using (var source = picture.GetImage()) { byte[] arr = new byte[source.Length]; await source.ReadAsync(arr, 0, arr.Length); photo.Data = new MemoryStream(); await photo.Data.WriteAsync(arr, 0, arr.Length); } }); photos.Add(photo); } await FileManager.AddPhotosAsync(photos, CancellationToken.None); } else { if (data is StoredContact) { List <PContact> contacts = new List <PContact>(); foreach (var c in list) { var contact = c.Data as StoredContact; PContact pc = new PContact(); pc.Name = contact.DisplayName; pc.Path = Path.Combine(Directories.Contacts, pc.Name); pc.Contact = contact; contacts.Add(pc); } FileManager.AddContactsAsync(contacts, CancellationToken.None); } } } }
public List <Post> GetPosts(List <int> ids) { //initialize the list for all the posts List <Post> Posts = new List <Post>(); foreach (int id in ids) { using (SqlConnection connection = new SqlConnection(connectionString)) using (SqlCommand command = new SqlCommand("dbo.GetAllPosts", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@UserId", id); try { connection.Open(); SqlDataReader dataReader = command.ExecuteReader(); while (dataReader.Read()) { Post Userpost = new Post { PostID = (int)dataReader["PostID"], UserId = (int)dataReader["UserID"], Title = (string)dataReader["Title"], Description = (string)dataReader["Description"], DateTime = (DateTime)dataReader["DateTime"], Location = (string)dataReader["Location"], PublicPost = Convert.ToBoolean((int)dataReader["Public"]), PostLike = (int)dataReader["TotalLikes"], UserProfileImage = (string)dataReader["User_photo_Path"] ?? string.Empty, UserName = (string)dataReader["Firstname"] + (string)dataReader["Lastname"] }; if ((int)dataReader["FishInfo_FishInfoID"] != 0) { FishInfo fishInfo = new FishInfo { FishInfoID = (int)dataReader["FishInfo_FishInfoID"], Name = (string)dataReader["Name"] ?? string.Empty, Lenght = Convert.ToDouble((decimal)dataReader["Lenght"] == null ? 0 : (decimal)dataReader["Lenght"]), Weight = Convert.ToDouble((decimal)dataReader["Weight"] == null ? 0 : (decimal)dataReader["Weight"]), Bait = (string)dataReader["Bait"] ?? string.Empty, }; Userpost.FishInfo = fishInfo; } Posts.Add(Userpost); } List <PPhoto> postPictureList = new List <PPhoto>(); //for the next table if (dataReader.NextResult()) { while (dataReader.Read()) { PPhoto postPhoto = new PPhoto { Post = (int)dataReader["Post_PostID"], Path = (string)dataReader["Path"] ?? string.Empty }; postPictureList.Add(postPhoto); } } foreach (PPhoto Photo in postPictureList) { Post post = Posts.Find(P => P.PostID == Photo.Post); if (post.Photos == null) { post.Photos = new List <PPhoto>(); } if (post != null) { PPhoto _tempList = new PPhoto { Post = Photo.Post, Path = Photo.Path }; post.Photos.Add(_tempList); } } } catch (Exception errorException) { throw errorException; } } } return(Posts); }