public IQueryable<Album> GetAll() { using (var context = new StoreContext()) { return context.Albums.ToList().AsQueryable(); } }
public AlbumRepository() // Duoc khoi tao o StoreController { using (var context = new StoreContext()) { /* Album album = new Album() {Name = "I need some sleep", Genre = "Rap", Artist = "Eminem", Price = 1.99f }; context.Albums.Add(album); album = new Album() {Name = "When i'm gone ", Genre = "Rock", Artist = "Rocker 1", Price = 2.99f }; context.Albums.Add(album); album = new Album() {Name = "Mockingbird", Genre = "Jazz", Artist = "Jazz 1", Price = 3.99f }; context.Albums.Add(album); album = new Album() {Name = "Hello", Genre = "Balad", Artist = "Andele", Price = 4.99f }; context.Albums.Add(album); album = new Album() {Name = "We will rock you", Genre = "Rock", Artist = "2PM", Price = 5.99f }; context.Albums.Add(album); album = new Album() {Name = "Happy new year", Genre = "Balad", Artist = "SNSD", Price = 6.99f }; context.Albums.Add(album); album = new Album() { Name = "Buon", Genre = "Rap", Artist = "Blackbi", Price = 7.99f }; context.Albums.Add(album); album = new Album() {Name = "Trust ", Genre = "Jazz", Artist = "Akon", Price = 8.99f }; context.Albums.Add(album); album = new Album() {Name = "Kissing on my tatoo", Genre = "Disco", Artist = "Justin", Price = 9.99f }; context.Albums.Add(album); album = new Album() {Name = "The night ", Genre = "Disco", Artist = "Binz", Price = 10.99f }; context.Albums.Add(album);*/ context.SaveChanges(); } }
public Album Get(int id) { using (var context = new StoreContext()) { return context.Albums.ToList().Find(a => a.Id == id); } }
public IQueryable<Album> GetAlbumByGenre(string genre) { using (var context = new StoreContext()) { IQueryable<Album> result = context.Albums.ToList().AsQueryable().Where(a => a.Genre.Equals(genre)); return result; } }
public Album Add(Album album) { using (var context = new StoreContext()) { //album.Id = nextId++; context.Albums.Add(album); context.SaveChanges(); return album; } }
public bool Remove(int id) { using (var context = new StoreContext()) { /* if (id <= context.Albums.Count()) { Album album = context.Albums.ToList().Find(a => a.Id == id); context.Albums.Remove(album); context.SaveChanges(); return true; } return false; * */ var album = context.Albums.Where(s => s.Id == id).FirstOrDefault<Album>(); if (album != null) { context.Albums.Remove(album); context.SaveChanges(); return true; } return false; } }
public bool Update(Album newAlbum) { using (var context = new StoreContext()) { /* int index = context.Albums.ToList().FindIndex(a => a.Id == album.Id); if (index == -1) { return false; } context.Albums.ToList().RemoveAt(index); context.Albums.Add(album); context.SaveChanges(); return true; * */ var album = context.Albums.Where(a => a.Id == newAlbum.Id).FirstOrDefault<Album>(); if (album != null) { album.Id = newAlbum.Id; album.Name = newAlbum.Name; album.Genre = newAlbum.Genre; album.Artist = newAlbum.Artist; album.Price = newAlbum.Price; context.Entry(album).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); return true; } return false; } }