public static void Main(string[] args) { var host = CreateHostBuilder(args).Build(); // Add Static data to DbContext to simulate a DB using (var scope = host.Services.CreateScope()) { var context = scope.ServiceProvider.GetRequiredService <MoviesDbContext>(); MoviesData.SeedDb(context); } host.Run(); }
// Insert Movie public bool InsertMovie(MoviesData data) { SqlCommand cmd = DataBaseConnection.OpenConnection().CreateCommand(); cmd.CommandText = "InsertMovie"; cmd.CommandType = CommandType.StoredProcedure; // store procedure cmd.Parameters.AddWithValue("@Title", data.Title); cmd.Parameters.AddWithValue("@Year", data.Year); cmd.Parameters.AddWithValue("@RentCost", data.RentCost); cmd.Parameters.AddWithValue("@Genre", data.Genre); cmd.Parameters.AddWithValue("@Rating", data.Rating); cmd.Parameters.AddWithValue("@StoryLine", data.StoryLine); bool ans = cmd.ExecuteNonQuery() > 0; cmd.Dispose(); return(ans); }
// Update Movie public bool UpdateMovie(MoviesData data) { SqlCommand cmd = DataBaseConnection.OpenConnection().CreateCommand(); cmd.CommandText = "UpdateMovieInform"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@movieId", data.MovieId); cmd.Parameters.AddWithValue("@title", data.Title); cmd.Parameters.AddWithValue("@year", data.Year); cmd.Parameters.AddWithValue("@rating", data.RentCost); cmd.Parameters.AddWithValue("@genre", data.Genre); cmd.Parameters.AddWithValue("@rentcost", data.Rating); cmd.Parameters.AddWithValue("@StoryLine", data.StoryLine); bool ans = cmd.ExecuteNonQuery() > 0; cmd.Dispose(); return(ans); }
// Show Movies Information public MoviesData ShowMovieInfoById(int movieID) { SqlCommand cmd = DataBaseConnection.OpenConnection().CreateCommand(); cmd.CommandText = "ShowMovieByID"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@movieID", movieID); MoviesData data = null; SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { data = new MoviesData(); data.Title = reader[2].ToString(); data.Rating = reader[1].ToString(); data.RentCost = int.Parse(reader[4].ToString()); data.Year = reader[3].ToString(); data.StoryLine = reader[5].ToString(); data.Genre = reader[6].ToString(); } reader.Dispose(); cmd.Dispose(); return(data); }
public MoviesController(MoviesData db) { _db = db; }
//Update movie private void BtnUpdate_Click(object sender, EventArgs e) { try { string MovieId = TextBoxMovieID.Text; string Title, RentCost, Year, Rating, Genre, StoryLine; Title = TextBoxMOvieTitle.Text; RentCost = TextBoxRent.Text; Rating = TextBoxStarRating.Text; Year = TextBoxYear.Text; Genre = TextBoxGenre.Text; StoryLine = textBoxStoryLine.Text; if (Title == "") { MessageBox.Show("Movie title is required!"); TextBoxMOvieTitle.Focus(); } else if (Year == "") { MessageBox.Show("Movie released year is required!"); TextBoxYear.Focus(); } else if (Genre == "") { MessageBox.Show("Movie genre is required!"); TextBoxGenre.Focus(); } else if (RentCost == "") { MessageBox.Show("Movie rent cost is required!"); } else if (StoryLine == "") { MessageBox.Show("Story Line is required!"); textBoxStoryLine.Focus(); } else if (Rating == "") { MessageBox.Show("Movie rating is required!"); TextBoxStarRating.Focus(); } else { MoviesData data = new MoviesData { Title = Title, Year = Year, Rating = Rating, Genre = Genre, RentCost = int.Parse(RentCost), MovieId = int.Parse(MovieId), StoryLine = StoryLine }; if (new Movies().UpdateMovie(data)) { MoviesGridViewDataBind(); BindDropDownMovie(); btnAddMOvie.Enabled = true; // enable Add movie button MessageBox.Show("Movie Updated Successfully!"); TextBoxMovieID.Text = ""; TextBoxMOvieTitle.Text = "Movie Title..."; TextBoxYear.Text = "Release Year..."; TextBoxStarRating.Text = "Star Rating..."; TextBoxGenre.Text = "Genre..."; TextBoxRent.Text = "($)Rent... "; textBoxStoryLine.Text = "Story Line..."; } else { MessageBox.Show("Unable to update this movie!"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); DataBaseConnection.CloseConnection(); // close connection with database when error occured } }
//insert movies private void BtnAddMOvie_Click(object sender, EventArgs e) { string Title, RentCost, Year, Rating, Genre, StoryLine; Title = TextBoxMOvieTitle.Text; RentCost = TextBoxRent.Text; Rating = TextBoxStarRating.Text; Year = TextBoxYear.Text; Genre = TextBoxGenre.Text; StoryLine = textBoxStoryLine.Text; if (Title == "") { MessageBox.Show("Movie title is required!"); TextBoxMOvieTitle.Focus(); } else if (Year == "") { MessageBox.Show("Movie released year is required!"); TextBoxYear.Focus(); } else if (Genre == "") { MessageBox.Show("Movie genre is required!"); TextBoxGenre.Focus(); } else if (Rating == "") { MessageBox.Show("Movie rating is required!"); TextBoxStarRating.Focus(); } else if (StoryLine == "") { MessageBox.Show("Story Line is required!"); textBoxStoryLine.Focus(); } else { MoviesData data = new MoviesData { Title = Title, Year = Year, Rating = Rating, Genre = Genre, RentCost = int.Parse(RentCost), StoryLine = StoryLine }; if (new Movies().InsertMovie(data)) { MoviesGridViewDataBind(); // method calling to Bind Grid Movies BindDropDownMovie(); // method calling to Bind dropdown Movies MessageBox.Show("Movie Inseted Successfully!"); TextBoxMOvieTitle.Text = "Movie Title..."; TextBoxYear.Text = "Release Year..."; TextBoxStarRating.Text = "Star Rating..."; TextBoxGenre.Text = "Genre..."; TextBoxRent.Text = "($)Rent... "; textBoxStoryLine.Text = "Story Line..."; } } }
// POST: api/Movie public void Post([FromBody] MovieDetails value) { var success = new MoviesData().Save(value); }
public SampleDataController(MoviesData db) { _db = db; }