private static Cast MapToCast(CastDto castDto) { return(new Cast { Id = castDto.Person.Id, Birthday = castDto.Person.Birthday, Name = castDto.Person.Name }); }
//----------------------------------------------------------------// public static Cast MapCast(CastDto castDto) { Cast cast = new Cast() { CharacterCast = castDto.Character, Gender = castDto.Gender, Order = castDto.Order }; return(cast); }
private async Task <int> AddOrUpdateWithoutSaveAsync(Show show) { int result = UpdatedStatus; var showDto = await _context.Shows.Include(c => c.Cast).FirstOrDefaultAsync(o => o.Id == show.Id); if (showDto == null) { result = CreatedStatus; showDto = new ShowDto(); await _context.Shows.AddAsync(showDto); } _mapper.Map(show, showDto); var toDelete = new List <CastDto>(); //remove deleted cast foreach (var deleted in showDto.Cast ?? Enumerable.Empty <CastDto>()) { if (!show.Cast?.Any(c => c.Id == deleted.Id) ?? true) { toDelete.Add(deleted); } } if (toDelete.Any()) { _context.Cast.RemoveRange(toDelete); } //update or add details show.Cast?.ToList().ForEach(cast => { if (showDto.Cast == null) { showDto.Cast = new List <CastDto>(); } var castDto = showDto.Cast.FirstOrDefault(d => d.Id == cast.Id); if (castDto == null) { castDto = new CastDto(); showDto.Cast.Add(castDto); } _mapper.Map(cast, castDto); }); return(result); }
//----------------------------------------------------------------// public async Task <Cast> SaveMovieCastAsync(CastDto castDto, Movie movie) { Cast cast = CinemaDtoMapper.MapCast(castDto); cast.Movie = movie; cast.MovieId = movie.Id; if (castDto.PeopleId != null) { People people = await SavePeopleAsync(castDto.PeopleId); cast.People = people; cast.PeopleId = people?.Id; } if (await _castCommand.SaveIfNotExist(_castQuery, cast)) { Logger.Log.Info($"Cast {cast.Id} was saved"); } return(cast); }