private IEnumerable <CategoryEntity> GetCategories(int[] ids, CinemaStoreContext context) { context = context ?? new CinemaStoreContext(); IEnumerable <CategoryEntity> result = context.Category.Where(x => ids.Any(y => y == x.Id)); return(result); }
public List <FilmModel> GetFilms(PaginationModel model, Func <FilmEntity, bool> predicate = null) { List <FilmModel> result = new List <FilmModel>(); using (context = new CinemaStoreContext()) { model.TotalItems = context.FilmEntity.Count(); var films = context.FilmEntity .Include(x => x.Posters) .Include(x => x.Categories) .Include(x => x.FilmCrew) .AsNoTracking(); if (predicate != null) { films = films.Where(predicate).AsQueryable(); } var tempfilms = films.OrderBy(x => x.DateCreate) .Skip(() => (model.Page - 1) * model.CountOnPage).Take(() => model.CountOnPage) .ToList(); result = MapTo(tempfilms); } return(result); }
public FilmCrewEntity Edit(FilmCrewEntity entity) { FilmCrewEntity entry = null; using (context = new CinemaStoreContext()) { if (entity.Id > 0) { entry = context.FilmCrew.FirstOrDefault(x => x.Id == entity.Id); } else { entry = new FilmCrewEntity(); context.FilmCrew.Add(entry); } entry.FamilyName = entity.FamilyName; entry.GivenName = entity.GivenName; entry.Patronymic = entity.Patronymic; entry.Group = entity.Group; try { context.SaveChanges(); } catch (Exception ex) { throw ex; } } return(entry); }
public List <FilmModel> GetByCategory(PaginationModel model, int categoryId) { List <FilmModel> result = new List <FilmModel>(); using (context = new CinemaStoreContext()) { //model.TotalItems = context.FilmEntity.Count(); var films = context.FilmEntity .Include(x => x.Categories); if (categoryId > 0) { films = films .Where(f => f.Categories.Any(c => c.Id == categoryId)); model.TotalItems = films.Count(); } else { return(result); } var tempfilms = films .OrderBy(x => x.DateCreate) .Skip((model.Page - 1) * model.CountOnPage) .Take(model.CountOnPage) .Include(x => x.Posters) .Include(x => x.FilmCrew) .ToList(); result = MapTo(tempfilms); } return(result); }
private IEnumerable <FilmCrewEntity> GetFilmCrew(int[] ids, CinemaStoreContext context = null) { context = context ?? new CinemaStoreContext(); IEnumerable <FilmCrewEntity> result = context.FilmCrew.Where(x => ids.Any(y => y == x.Id)); return(result); }
public CategoryEntity Edit(CategoryEntity entity) { CategoryEntity category = null; using (context = new CinemaStoreContext()) { if (entity.Id > 0) { category = context.Category.FirstOrDefault(x => x.Id == entity.Id); } else { category = new CategoryEntity(); context.Category.Add(category); } category.Name = entity.Name; category.Alias = entity.Alias; category.Description = entity.Description; try { context.SaveChanges(); } catch (Exception ex)//sql exceptions, object disposed, invalid operation etc... { throw ex; } } return(category); }
private FilmCrewEntity GetBy(int id) { FilmCrewEntity entity = null; using (context = new CinemaStoreContext()) { entity = context.FilmCrew.FirstOrDefault(x => x.Id == id); } return(entity); }
public List <FilmCrewEntity> Get(int group) { List <FilmCrewEntity> filmCrew = null; using (context = new CinemaStoreContext()) { filmCrew = context.FilmCrew.ToList(); } return(filmCrew); }
private List <FilmCrewEntity> Get() { List <FilmCrewEntity> result = null; using (context = new CinemaStoreContext()) { result = context.FilmCrew.ToList(); } return(result); }
private CategoryEntity Get(int id) { CategoryEntity entity = new CategoryEntity(); using (context = new CinemaStoreContext()) { entity = context .Category.FirstOrDefault(x => x.Id == id); } return(entity); }
private List <CategoryEntity> Get() { List <CategoryEntity> categories = null; using (context = new CinemaStoreContext()) { categories = context .Category.ToList(); } return(categories); }
public void Delete(FilmCrewEntity entity) { using (context = new CinemaStoreContext()) { var entry = context.FilmCrew.FirstOrDefault(x => x.Id == entity.Id); context.FilmCrew.Remove(entry); try { context.SaveChanges(); } catch (Exception ex) { throw ex; } } }
public List <FilmEntity> Get(params Expression <Func <FilmEntity, object> >[] includeExpressions) { List <FilmEntity> res = null; using (context = new CinemaStoreContext()) { IQueryable <FilmEntity> set = context .FilmEntity .AsNoTracking(); foreach (var includeItem in includeExpressions) { set = set.Include(includeItem); } } return(res); }
private FilmEntity Get(int id) { FilmEntity entity = null; using (context = new CinemaStoreContext()) { entity = context .FilmEntity .Include(x => x.FilmCrew) .Include(x => x.Categories) .Include(x => x.Posters) .Include(x => x.User) .FirstOrDefault(x => x.Id == id); } return(entity); }
public void Delete(PosterImageEntity image) { using (context = new CinemaStoreContext()) { try { context.Entry <PosterImageEntity>(image).State = System.Data.Entity.EntityState.Deleted; } catch (Exception ex) { throw ex; } context.SaveChanges(); } }
public FilmEntity Edit(FilmEditModel model) { FilmEntity entry = null; using (context = new CinemaStoreContext()) { if (model.Id > 0) { entry = context.FilmEntity .Include(x => x.FilmCrew) .Include(x => x.Categories) .FirstOrDefault(x => x.Id == model.Id); } else { entry = new FilmEntity(); context.FilmEntity.Add(entry); } entry.Description = model.Description; entry.Name = model.Name; entry.Year = model.Year; entry.UserId = model.UserId; List <int> selectedFilmCrewIds = AgregateSelectedFilmCrewIds(model); var selectedFilmCrew = GetFilmCrew(selectedFilmCrewIds.ToArray(), context); var selectedCategories = GetCategories(model.CategoryIds, context); try { UpdateFilmCrew(selectedFilmCrew, entry, context); UpdateFilmCategories(selectedCategories, entry, context); context.SaveChanges(); } catch (Exception ex) { throw ex; } } return(entry); }
public List <PosterImageEntity> this[int filmId] { get { List <PosterImageEntity> images = null; if (filmId == 0) { return(images); } using (context = new CinemaStoreContext()) { images = context .PosterImage .Where(x => x.FilmId == filmId) .ToList(); } return(images); } }
public void Add(PosterImageEntity image) { if (image == null) { return; } using (context = new CinemaStoreContext()) { context.PosterImage.Add(image); try { context.SaveChanges(); } catch (Exception ex) { throw ex; } } }
public void Delete(CategoryEntity entity) { using (context = new CinemaStoreContext()) { var category = context .Category.FirstOrDefault(x => x.Id == entity.Id); if (category != null) { context.Category.Remove(category); } try { context.SaveChanges(); } catch (Exception ex) { throw ex; } } }
public List <FilmCrewEntity> GetEntries(PaginationModel model, Func <FilmCrewEntity, bool> expr = null) { List <FilmCrewEntity> filmCrew = null; using (context = new CinemaStoreContext()) { var query = context.FilmCrew.AsQueryable(); model.TotalItems = query.Count(); if (expr != null) { query = query.Where(expr).AsQueryable(); } filmCrew = query .OrderBy(x => x.DateCreate) .Skip((model.Page - 1) * model.CountOnPage).Take(model.CountOnPage) .ToList(); } return(filmCrew); }
private void UpdateFilmCrew(IEnumerable <FilmCrewEntity> selectedCrew, FilmEntity entity, CinemaStoreContext context) { var exist = entity.FilmCrew.ToList(); foreach (var item in exist ?? new List <FilmCrewEntity>()) { entity.FilmCrew.Remove(item); } foreach (var item in selectedCrew) { entity.FilmCrew.Add(item); } }
private void UpdateFilmCategories(IEnumerable <CategoryEntity> selectedCategory, FilmEntity entity, CinemaStoreContext context) { var exist = entity.Categories.ToList(); foreach (var item in exist ?? new List <CategoryEntity>()) { entity.Categories.Remove(item); } foreach (var item in selectedCategory) { entity.Categories.Add(item); } }