Created
October 21, 2017 08:27
-
-
Save andy51002000/3651e36f2d6394eb6f817a1beddfb780 to your computer and use it in GitHub Desktop.
FakeDatabase that simulate database to do CRUD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using SimpleMVVM.Models; | |
using System; | |
using System.Collections.ObjectModel; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using System.Xml.Linq; | |
using Windows.Storage; | |
namespace SimpleMVVM.Data | |
{ | |
public class FakeDatabase | |
{ | |
private ObservableCollection<Movie> movies; | |
public ObservableCollection<Movie> Movies | |
{ | |
get => movies; | |
} | |
public FakeDatabase() | |
{ | |
this.movies = new ObservableCollection<Movie>(); | |
} | |
private async Task WriteMoviesToFile() | |
{ | |
StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder; | |
var item= await folder.TryGetItemAsync("movies.xml"); | |
StorageFile moviesFile; | |
if( item == null) | |
{ | |
moviesFile = await folder.CreateFileAsync("movies.xml"); | |
} | |
else | |
{ | |
moviesFile = await folder.GetFileAsync("movies.xml"); | |
} | |
var xmlDoc = new XElement("Root"); | |
try | |
{ | |
foreach (var mv in Movies) | |
{ | |
xmlDoc.Add( | |
new XElement("Movie", | |
new XElement("Name", mv.Name), | |
new XElement("Price", mv.Price))); | |
} | |
}catch(Exception ex) | |
{ | |
System.Diagnostics.Debug.WriteLine(ex.ToString()); | |
} | |
await FileIO.WriteTextAsync(moviesFile, xmlDoc.ToString()); | |
} | |
public async Task LoadMoviesFromFile() | |
{ | |
StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder; | |
var item = await folder.TryGetItemAsync("movies.xml"); | |
if(item != null) | |
{ | |
var moviesFile = item as StorageFile; | |
var moviesFileContent = await FileIO.ReadTextAsync(moviesFile); | |
var xmlDoc = XElement.Parse(moviesFileContent); | |
var xmlMovies = xmlDoc.Descendants("Movie"); | |
foreach(var xmlMovie in xmlMovies) | |
{ | |
Movie movie = new Movie(); | |
var name = xmlMovie.Descendants("Name").FirstOrDefault(); | |
if(name != null) | |
{ | |
movie.Name = name.Value; | |
} | |
var price = xmlMovie.Descendants("Price").FirstOrDefault(); | |
if(price != null) | |
{ | |
movie.Price = price.Value; | |
} | |
movies.Add(movie); | |
} | |
} | |
else | |
{ | |
movies.Add(new Movie | |
{ | |
Name = "Iron Man", | |
Price = "100" | |
}); | |
movies.Add(new Movie | |
{ | |
Name = "Thor", | |
Price = "50" | |
}); | |
movies.Add(new Movie | |
{ | |
Name = "Gone Girl", | |
Price = "10" | |
}); | |
await WriteMoviesToFile(); | |
} | |
} | |
public async Task AddMovie(Movie movie) | |
{ | |
if (!Movies.Contains(movie)) | |
{ | |
Movies.Add(movie); | |
} | |
await WriteMoviesToFile(); | |
} | |
public async Task DeleteMovie(Movie movie) | |
{ | |
if (Movies.Contains(movie)) | |
{ | |
Movies.Remove(movie); | |
} | |
await WriteMoviesToFile(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment