Sample Code/MovieHunter.API/Models/MovieRepository.cs
/// <summary>
/// Saves a single movie back to the repository.
/// </summary>
/// <param name="movie"></param>
Saves a single movie back to the repository.
Saves a single movie back to the repository .
internal Movie Save(Movie movie)
        {
            var movies = this.Retrieve();
            var itemIndex = movies.FindIndex(p => p.MovieId == movie.MovieId);
            if (itemIndex > 0)
            {
                movies[itemIndex] = movie;
            }
            else
            {
                var maxId = movies.Max(m => m.MovieId);
                movie.MovieId = maxId + 1;
                movies.Add(movie);
            }
            var filePath = HostingEnvironment.MapPath(@"~/App_Data/movies.json");
            var json = JsonConvert.SerializeObject(movies, Formatting.Indented);
            System.IO.File.WriteAllText(filePath, json);
            return movie;
        }
internal Movie Save ( Movie movie ) { var movies = this . Retrieve ( ) ; var itemIndex = movies . FindIndex ( p => p . MovieId == movie . MovieId ) ; if ( itemIndex > 0 ) { movies [ itemIndex ] = movie ; } else { var maxId = movies . Max ( m => m . MovieId ) ; movie . MovieId = maxId + 1 ; movies . Add ( movie ) ; } var filePath = HostingEnvironment . MapPath ( @"~/App_Data/movies.json" ) ; var json = JsonConvert . SerializeObject ( movies , Formatting . Indented ) ; System . IO . File . WriteAllText ( filePath , json ) ; return movie ; }
Saves a single movie back to the repository.
Saves a single movie back to the repository .
// Read in the existing movies // Locate and replace the item // Assign a new Id // Write out the Json
movie
Movie
None