public async Task <IEnumerable <DirectorDto> > Handle(CreateDirectorCollectionCommand request, CancellationToken cancellationToken) { var directorEntities = _mapper.Map <IEnumerable <Director> >(request.Directors); foreach (var director in directorEntities) { await _movieDatabaseRepository.AddDirector(director); } _movieDatabaseRepository.Save(); return(_mapper.Map <IEnumerable <DirectorDto> >(directorEntities)); }
public async Task <DirectorDto> Handle(CreateDirectorCommand request, CancellationToken cancellationToken) { var directorEntity = _mapper.Map <Director>(request); //At this moment, the entity hasnt been added to database. //It's been added on the DbContext, which represents a session with database. await _movieDatabaseRepository.AddDirector(directorEntity); _movieDatabaseRepository.Save(); return(_mapper.Map <DirectorDto>(directorEntity)); }
public IActionResult CreateMovie([FromBody] MovieDto movie) { if (movie == null) { return(BadRequest()); } var newMovie = Mapper.Map <Data.Movie>(movie); _movieDatabaseRepository.AddMovie(newMovie); if (_movieDatabaseRepository.Save()) { var createdMovie = Mapper.Map <Models.MovieDto>(newMovie); return(CreatedAtRoute("GetMovie", new { id = createdMovie.Id }, createdMovie)); } else { return(StatusCode(500, errorMessage)); } }
public async Task <MovieDto> Handle(CreateMovieForDirectorCommand request, CancellationToken cancellationToken) { if (!_movieDatabaseRepository.DirectorExists(request.DirectorId)) { return(null); } var movieEntity = _mapper.Map <Movie>(request.Movie); await _movieDatabaseRepository.AddMovie(request.DirectorId, movieEntity); _movieDatabaseRepository.Save(); var movieToReturn = _mapper.Map <MovieDto>(movieEntity); return(movieToReturn); }
public async Task <bool> Handle(UpdateMovieForDirectorCommand request, CancellationToken cancellationToken) { if (!_movieDatabaseRepository.DirectorExists(request.DirectorId)) { return(false); } var movieForDirectorFromRepo = await _movieDatabaseRepository.GetMovie(request.DirectorId, request.MovieId); if (movieForDirectorFromRepo == null) { return(false); } _mapper.Map(request.Movie, movieForDirectorFromRepo); _movieDatabaseRepository.UpdateMovie(movieForDirectorFromRepo); _movieDatabaseRepository.Save(); return(true); }