/// <summary> /// Saves a single movie back to the repository. /// </summary> /// <param name="movie"></param> internal Movie Save(Movie movie) { // Read in the existing movies var movies = this.Retrieve(); // Locate and replace the item var itemIndex = movies.FindIndex(p => p.MovieId == movie.MovieId); if (itemIndex > 0) { movies[itemIndex] = movie; } else { // Assign a new Id var maxId = movies.Max(m => m.MovieId); movie.MovieId = maxId + 1; movies.Add(movie); } // Write out the Json var filePath = HostingEnvironment.MapPath(@"~/App_Data/movies.json"); var json = JsonConvert.SerializeObject(movies, Formatting.Indented); System.IO.File.WriteAllText(filePath, json); return movie; }
public async Task<string> PutMovie (Movie movie) { var collection = database.GetCollection<Movie>("movies"); var filter = Builders<Movie>.Filter.Eq(s => s.movieId, movie.movieId); var result = await collection.ReplaceOneAsync (filter, movie); return result.ToString(); }
/// <summary> /// Creates a new product with default values /// </summary> /// <returns></returns> internal Movie Create() { Movie movie = new Movie { ReleaseDate = DateTime.Now }; return movie; }
public async Task<string> PostMovie (Movie movie) { movie.movieId = ObjectId.GenerateNewId().ToString(); var collection = database.GetCollection<Movie>("movies"); await collection.InsertOneAsync(movie); return movie.movieId; }
/// <summary> /// Saves a single movie back to the repository. /// </summary> /// <param name="value"></param> internal void Save(Movie value) { // Read in the existing movies var movies = this.Retrieve(); // Locate and replace the item var itemIndex = movies.FindIndex(p => p.movieId == value.movieId); if (itemIndex > 0) { movies[itemIndex] = value; } else { movies.Add(value); } // Write out the Json var filePath = HttpContext.Current.Server.MapPath(@"~/App_Data/movies.json"); var json = JsonConvert.SerializeObject(movies, Formatting.Indented); System.IO.File.WriteAllText(filePath, json); }