using MyMediaCollection.Enums; using MyMediaCollection.Interfaces; using MyMediaCollection.Model; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace MyMediaCollection.Services { public class DataService : IDataService { private IList _items; private IList _itemTypes; private IList _mediums; private IList _locationTypes; public async Task InitializeDataAsync() { PopulateItemTypes(); PopulateMediums(); PopulateLocationTypes(); PopulateItems(); await Task.Delay(1); } private void PopulateItems() { var cd = new MediaItem { Id = 1, Name = "Classical Favorites", MediaType = ItemType.Music, MediumInfo = _mediums.FirstOrDefault(m => m.Name == "CD"), Location = LocationType.InCollection }; var book = new MediaItem { Id = 2, Name = "Classic Fairy Tales", MediaType = ItemType.Book, MediumInfo = _mediums.FirstOrDefault(m => m.Name == "Hardcover"), Location = LocationType.InCollection }; var bluRay = new MediaItem { Id = 3, Name = "The Mummy", MediaType = ItemType.Video, MediumInfo = _mediums.FirstOrDefault(m => m.Name == "Blu Ray"), Location = LocationType.InCollection }; _items = new List { cd, book, bluRay }; } private void PopulateMediums() { var cd = new Medium { Id = 1, MediaType = ItemType.Music, Name = "CD" }; var vinyl = new Medium { Id = 2, MediaType = ItemType.Music, Name = "Vinyl" }; var hardcover = new Medium { Id = 3, MediaType = ItemType.Book, Name = "Hardcover" }; var paperback = new Medium { Id = 4, MediaType = ItemType.Book, Name = "Paperback" }; var dvd = new Medium { Id = 5, MediaType = ItemType.Video, Name = "DVD" }; var bluRay = new Medium { Id = 6, MediaType = ItemType.Video, Name = "Blu Ray" }; _mediums = new List { cd, vinyl, hardcover, paperback, dvd, bluRay }; } private void PopulateItemTypes() { _itemTypes = new List { ItemType.Book, ItemType.Music, ItemType.Video }; } private void PopulateLocationTypes() { _locationTypes = new List { LocationType.InCollection, LocationType.Loaned }; } public async Task AddItemAsync(MediaItem item) { item.Id = _items.Max(i => i.Id) + 1; _items.Add(item); await Task.Delay(1); return item.Id; } public async Task GetItemAsync(int id) { await Task.Delay(1); return _items.FirstOrDefault(i => i.Id == id); } public async Task> GetItemsAsync() { await Task.Delay(1); return _items; } public IList GetItemTypes() { return _itemTypes; } public IList GetMediums() { return _mediums; } public IList GetMediums(ItemType itemType) { return _mediums .Where(m => m.MediaType == itemType) .ToList(); } public IList GetLocationTypes() { return _locationTypes; } public async Task UpdateItemAsync(MediaItem item) { var idx = -1; var matchedItem = (from x in _items let ind = idx++ where x.Id == item.Id select ind).FirstOrDefault(); if (idx == -1) { throw new Exception("Unable to update item. Item not found in collection."); } _items[idx] = item; await Task.Delay(1); } public async Task DeleteItemAsync(MediaItem item) { await Task.Delay(1); throw new NotImplementedException(); } public Medium GetMedium(string name) { return _mediums.FirstOrDefault(m => m.Name == name); } } }