private async Task <int> Create(Director director) { director.CreatedDate = DateTime.Now; await _context.Directors.AddAsync(director); await _context.SaveChangesAsync(); if (director.Id == 0) { throw new Exception("Невозможно создать объект"); } return(director.Id); }
private async Task <int> CreateFilm(Film film) { film.CreatedDate = DateTime.Now; await _context.Films.AddAsync(film); await _context.SaveChangesAsync(); if (film.Id == 0) { throw new Exception("Невозможно создать объект"); } return(film.Id); }
public async Task <FilmViewModel> EditFilmAsync(FilmViewModel film) { if (film == null) { throw new NullReferenceException($"Параметр film не должен быть null!"); } FilmDataModel model; using (var ctx = new FilmsCatalogDbContext()) { model = await ctx.Films .Include("User") .FirstOrDefaultAsync(f => f.Id == film.Id); if (model == null) { throw new Exception($"Фильм с идентификатором {film.Id} не найден в базе данных!"); } model.Name = film.Name; model.Producer = film.Producer; model.Description = film.Description; model.CreateYear = film.CreateYear; model.MimeType = film.MimeType; model.FileName = film.FileName; await ctx.SaveChangesAsync(); } var filmMapper = new FilmModelMapper().CreateMapper(); return(filmMapper.Map <FilmViewModel>(model)); }
public async Task <bool> RemoveFilmAsync(int id) { using (var ctx = new FilmsCatalogDbContext()) { var model = await ctx.Films.FirstOrDefaultAsync(f => f.Id == id); if (model == null) { throw new Exception($"Фильм с идентификатором {id} не найден в базе данных!"); } ctx.Films.Remove(model); await ctx.SaveChangesAsync(); } return(true); }
public async Task <FilmViewModel> SaveFilmAsync(FilmViewModel film) { if (film == null) { throw new NullReferenceException($"Параметр film не должен быть null!"); } var filmMapper = new FilmModelMapper().CreateMapper(); var model = filmMapper.Map <FilmDataModel>(film); using (var ctx = new FilmsCatalogDbContext()) { model = ctx.Films.Add(model); await ctx.SaveChangesAsync(); } return(filmMapper.Map <FilmViewModel>(model)); }