public XmlDocument GetMovieXml(long movieId) { XmlDocument xml = new XmlDocument(); string xmlString; try { xmlString = MovieProxy.DetailMovie(movieId); } catch (Exception ex) { throw new NetworkException(ex); } try { XDocument xmlDocument = XDocument.Parse(xmlString, LoadOptions.None); XElement categoryElement = xmlDocument.Element("xml").Element("product").Element("category").Element("id"); if (categoryElement.Value.Trim() != Properties.Settings.Default.WebShop_moviesCategoryId) { xml.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xml><error><text>Products not found</text></error></xml>"); return(xml); } } catch (NullReferenceException) { } xml.LoadXml(xmlString); return(xml); }
public XmlDocument SearchMoviesXml(string keywords, int startIndex, int count) { string xmlString; try { xmlString = MovieProxy.FindMovies(keywords, startIndex, count); } catch (Exception ex) { throw new NetworkException(ex); } XmlDocument xml = new XmlDocument(); xml.LoadXml(xmlString); return(xml); }
public Movie CreateMovie(MovieRequest movieRequest) { MovieResponse response = MovieProxy.GetMovieByIMDB(movieRequest.Imdb); var movie = new Movie() { EndAt = DateTime.Parse(movieRequest.EndAt), }; Coppier <MovieResponse, Movie> .Copy(response, movie); Coppier <MovieRequest, Movie> .Copy(movieRequest, movie); var rate = dbContext.Rates.Where(r => r.Name == response.RateName).FirstOrDefault(); if (rate == null) { rate = new Rate() { Name = response.RateName, MinAge = 0, }; dbContext.Add(rate); } movie.Rate = rate; var screenTypes = dbContext.ScreenTypes.Where(s => movieRequest.ScreenTypeIds.Contains(s.Id)).ToList(); List <string> actorNames = new List <string>(); foreach (var actor in response.Actors) { actorNames.Add(actor.Name); } var actors = dbContext.Actors.Where(a => actorNames.Contains(a.Name)).ToList(); var isNotExistActors = response.Actors.Where(a => !actors.Any(a2 => a2.Name == a.Name)).ToList(); if (isNotExistActors != null) { foreach (var actor in isNotExistActors) { dbContext.Add(actor); } } //genre List <string> genreNames = new List <string>(); foreach (var genre in response.Genres) { genreNames.Add(genre.Name); } var genres = dbContext.Genres.Where(a => genreNames.Contains(a.Name)).ToList(); var isNotExistGenres = response.Genres.Where(a => !genres.Any(a2 => a2.Name == a.Name)).ToList(); if (isNotExistGenres != null) { foreach (var genre in isNotExistGenres) { dbContext.Add(genre); } } foreach (var screenType in screenTypes) { var movieScreenType = new MovieScreenType() { Movie = movie, ScreenType = screenType, }; dbContext.Add(movieScreenType); } foreach (var actor in actors.Concat(isNotExistActors)) { var movieActor = new MovieActor() { Movie = movie, Actor = actor, }; dbContext.Add(movieActor); } foreach (var genre in genres.Concat(isNotExistGenres)) { var movieGenre = new MovieGenre() { Movie = movie, Genre = genre, }; dbContext.Add(movieGenre); } dbContext.Add(movie); var isSuccess = Save(); if (!isSuccess) { return(null); } return(movie); }
public MovieDetails GetMovie(long movieId) { string xmlString; try { xmlString = MovieProxy.DetailMovie(movieId); } catch (Exception ex) { throw new NetworkException(ex); } XDocument xmlDocument = XDocument.Parse(xmlString, LoadOptions.None); XElement errorElement = xmlDocument.Element("xml").Element("error"); if (errorElement != null) { XElement textElement = errorElement.Element("text"); if (textElement != null) { string textValue = textElement.Value.Trim(); if (textValue == PRODUCT_NOT_FOUND_ERROR) { throw new InstanceNotFoundException <MovieDetails>("movieId", movieId); } else { throw new XmlErrorException("Unknown error message"); } } throw new XmlErrorException("No <text> tag found for <error>."); } XElement categoryElement = xmlDocument.Element("xml").Element("product").Element("category").Element("id"); if (categoryElement.Value.Trim() != Properties.Settings.Default.WebShop_moviesCategoryId) { throw new InstanceNotFoundException <MovieDetails>("movieId", movieId); } CultureInfo culture = new CultureInfo("en-US"); var result = (from ev in xmlDocument.Element("xml").Elements("product") select new MovieDetails { MovieId = Int64.Parse(ev.Element("id").Value.Trim(), culture), Title = ev.Element("name").Value.Trim(), Description = ev.Element("description").Value.Trim(), Price = Double.Parse(ev.Element("price").Value.Trim(), culture), LinkCount = LinkDao.CountForMovie(Int64.Parse(ev.Element("id").Value.Trim(), culture)) }).ToList(); if (result.Count != 1) { throw new XmlErrorException("More than one <product> tags were found."); } return(result.First()); }
public ListBlock <MovieDetails> SearchMovies(string keywords, int startIndex, int count) { string xmlString; try { xmlString = MovieProxy.FindMovies(keywords, startIndex, count); } catch (Exception ex) { throw new NetworkException(ex); } XDocument xmlDocument = XDocument.Parse(xmlString, LoadOptions.None); XElement errorElement = xmlDocument.Element("xml").Element("error"); if (errorElement != null) { XElement textElement = errorElement.Element("text"); if (textElement != null) { string textValue = textElement.Value.Trim(); if (textValue == PRODUCTS_NOT_FOUND_ERROR) { return(new ListBlock <MovieDetails>(startIndex, false)); } else if (textValue == INCORRECT_PARAMETERS_ERROR) { throw new InternalErrorException("Failure building web service request."); } else { throw new XmlErrorException("Unknown error message"); } } throw new XmlErrorException("No <text> tag found for <error>."); } bool hasMore; XElement moreElement = xmlDocument.Element("xml").Element("more"); if (moreElement != null) { string moreValue = moreElement.Value.Trim(); if (moreValue == "true") { hasMore = true; } else if (moreValue == "false") { hasMore = false; } else { throw new InternalErrorException("Tag <more> has neither \"true\" nor \"false\" values."); } } else { throw new InternalErrorException("No tag <more> was found."); } CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US"); var result = (from ev in xmlDocument.Element("xml").Element("allproducts").Elements("product") select new MovieDetails { MovieId = Int64.Parse(ev.Element("id").Value.Trim(), culture), Title = ev.Element("name").Value.Trim(), Description = null, Price = Double.Parse(ev.Element("price").Value.Trim(), culture), LinkCount = LinkDao.CountForMovie(Int64.Parse(ev.Element("id").Value.Trim(), culture)) }).ToList(); return(new ListBlock <MovieDetails>(result, startIndex, hasMore)); }