public IActionResult Create([FromForm] CreateFilmModel createFilmModel) { var films = _mongoDB.GetCollection <BsonDocument>("films"); var posters = _mongoDB.GetCollection <BsonDocument>("posters"); var toAdd = new Film() { Director = createFilmModel.Director, Storyline = createFilmModel.Storyline, Title = createFilmModel.Title, Year = createFilmModel.Year, Negative = 0, Positive = 0 }; byte[] posterBytes = new byte[createFilmModel.PosterFile.Length]; createFilmModel.PosterFile.OpenReadStream().Read(posterBytes, 0, (int)createFilmModel.PosterFile.Length); var posterId = ObjectId.GenerateNewId(); var posterToAdd = new PosterModel() { Image = posterBytes, Id = posterId, }; posters.InsertOneAsync(posterToAdd.ToBsonDocument()); toAdd.PosterId = posterId; films.InsertOneAsync(toAdd.ToBsonDocument()); return(Redirect("/")); }
public IActionResult Edit([FromForm] EditFilmModel editFilmModel) { var films = _mongoDB.GetCollection <BsonDocument>("films"); var posters = _mongoDB.GetCollection <BsonDocument>("posters"); GetFilm(editFilmModel.FilmId); Film.Director = editFilmModel.Director; Film.Storyline = editFilmModel.Storyline; Film.Title = editFilmModel.Title; Film.Year = editFilmModel.Year; if (editFilmModel.PosterFile != null) { byte[] posterBytes = new byte[editFilmModel.PosterFile.Length]; editFilmModel.PosterFile.OpenReadStream().Read(posterBytes, 0, (int)editFilmModel.PosterFile.Length); var posterId = ObjectId.GenerateNewId(); var posterToAdd = new PosterModel() { Image = posterBytes, Id = posterId, }; posters.InsertOneAsync(posterToAdd.ToBsonDocument()); posters.DeleteOne($"{{_id: ObjectId('{Film.PosterId}')}}"); Film.PosterId = posterId; } films.ReplaceOne($"{{_id: ObjectId('{Film.Id}')}}", Film.ToBsonDocument()); return(Redirect("/film/detail?id=" + Film.Id.ToString())); }