From 2b03239968b22244e4e6df88d62f8831cd32517e Mon Sep 17 00:00:00 2001 From: YashPhatak Date: Sat, 7 Sep 2024 17:12:43 -0500 Subject: [PATCH] part-3-changes --- app/controllers/movies_controller.rb | 70 ++++++++++++++++++++++ app/helpers/movies_helper.rb | 2 + app/views/movies/_form.html.erb | 37 ++++++++++++ app/views/movies/_movie.html.erb | 22 +++++++ app/views/movies/_movie.json.jbuilder | 2 + app/views/movies/edit.html.erb | 12 ++++ app/views/movies/index.html.erb | 16 +++++ app/views/movies/index.json.jbuilder | 1 + app/views/movies/new.html.erb | 11 ++++ app/views/movies/show.html.erb | 10 ++++ app/views/movies/show.json.jbuilder | 1 + config/routes.rb | 14 +---- test/controllers/movies_controller_test.rb | 48 +++++++++++++++ test/system/movies_test.rb | 47 +++++++++++++++ 14 files changed, 281 insertions(+), 12 deletions(-) create mode 100644 app/controllers/movies_controller.rb create mode 100644 app/helpers/movies_helper.rb create mode 100644 app/views/movies/_form.html.erb create mode 100644 app/views/movies/_movie.html.erb create mode 100644 app/views/movies/_movie.json.jbuilder create mode 100644 app/views/movies/edit.html.erb create mode 100644 app/views/movies/index.html.erb create mode 100644 app/views/movies/index.json.jbuilder create mode 100644 app/views/movies/new.html.erb create mode 100644 app/views/movies/show.html.erb create mode 100644 app/views/movies/show.json.jbuilder create mode 100644 test/controllers/movies_controller_test.rb create mode 100644 test/system/movies_test.rb diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb new file mode 100644 index 0000000..2a235e2 --- /dev/null +++ b/app/controllers/movies_controller.rb @@ -0,0 +1,70 @@ +class MoviesController < ApplicationController + before_action :set_movie, only: %i[ show edit update destroy ] + + # GET /movies or /movies.json + def index + @movies = Movie.all + end + + # GET /movies/1 or /movies/1.json + def show + end + + # GET /movies/new + def new + @movie = Movie.new + end + + # GET /movies/1/edit + def edit + end + + # POST /movies or /movies.json + def create + @movie = Movie.new(movie_params) + + respond_to do |format| + if @movie.save + format.html { redirect_to movie_url(@movie), notice: "Movie was successfully created." } + format.json { render :show, status: :created, location: @movie } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @movie.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /movies/1 or /movies/1.json + def update + respond_to do |format| + if @movie.update(movie_params) + format.html { redirect_to movie_url(@movie), notice: "Movie was successfully updated." } + format.json { render :show, status: :ok, location: @movie } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @movie.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /movies/1 or /movies/1.json + def destroy + @movie.destroy! + + respond_to do |format| + format.html { redirect_to movies_url, notice: "Movie was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_movie + @movie = Movie.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def movie_params + params.require(:movie).permit(:title, :rating, :description, :release_date) + end +end diff --git a/app/helpers/movies_helper.rb b/app/helpers/movies_helper.rb new file mode 100644 index 0000000..493eee5 --- /dev/null +++ b/app/helpers/movies_helper.rb @@ -0,0 +1,2 @@ +module MoviesHelper +end diff --git a/app/views/movies/_form.html.erb b/app/views/movies/_form.html.erb new file mode 100644 index 0000000..52c7b00 --- /dev/null +++ b/app/views/movies/_form.html.erb @@ -0,0 +1,37 @@ +<%= form_with(model: movie) do |form| %> + <% if movie.errors.any? %> +
+

<%= pluralize(movie.errors.count, "error") %> prohibited this movie from being saved:

+ + +
+ <% end %> + +
+ <%= form.label :title, style: "display: block" %> + <%= form.text_field :title %> +
+ +
+ <%= form.label :rating, style: "display: block" %> + <%= form.text_field :rating %> +
+ +
+ <%= form.label :description, style: "display: block" %> + <%= form.text_field :description %> +
+ +
+ <%= form.label :release_date, style: "display: block" %> + <%= form.text_field :release_date %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/movies/_movie.html.erb b/app/views/movies/_movie.html.erb new file mode 100644 index 0000000..551c782 --- /dev/null +++ b/app/views/movies/_movie.html.erb @@ -0,0 +1,22 @@ +
+

+ Title: + <%= movie.title %> +

+ +

+ Rating: + <%= movie.rating %> +

+ +

+ Description: + <%= movie.description %> +

+ +

+ Release date: + <%= movie.release_date %> +

+ +
diff --git a/app/views/movies/_movie.json.jbuilder b/app/views/movies/_movie.json.jbuilder new file mode 100644 index 0000000..ddc9af3 --- /dev/null +++ b/app/views/movies/_movie.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! movie, :id, :title, :rating, :description, :release_date, :created_at, :updated_at +json.url movie_url(movie, format: :json) diff --git a/app/views/movies/edit.html.erb b/app/views/movies/edit.html.erb new file mode 100644 index 0000000..166e6cc --- /dev/null +++ b/app/views/movies/edit.html.erb @@ -0,0 +1,12 @@ +<% content_for :title, "Editing movie" %> + +

Editing movie

+ +<%= render "form", movie: @movie %> + +
+ +
+ <%= link_to "Show this movie", @movie %> | + <%= link_to "Back to movies", movies_path %> +
diff --git a/app/views/movies/index.html.erb b/app/views/movies/index.html.erb new file mode 100644 index 0000000..fdce12c --- /dev/null +++ b/app/views/movies/index.html.erb @@ -0,0 +1,16 @@ +

<%= notice %>

+ +<% content_for :title, "Movies" %> + +

Movies

+ +
+ <% @movies.each do |movie| %> + <%= render movie %> +

+ <%= link_to "Show this movie", movie %> +

+ <% end %> +
+ +<%= link_to "New movie", new_movie_path %> diff --git a/app/views/movies/index.json.jbuilder b/app/views/movies/index.json.jbuilder new file mode 100644 index 0000000..4367cae --- /dev/null +++ b/app/views/movies/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @movies, partial: "movies/movie", as: :movie diff --git a/app/views/movies/new.html.erb b/app/views/movies/new.html.erb new file mode 100644 index 0000000..9115e20 --- /dev/null +++ b/app/views/movies/new.html.erb @@ -0,0 +1,11 @@ +<% content_for :title, "New movie" %> + +

New movie

+ +<%= render "form", movie: @movie %> + +
+ +
+ <%= link_to "Back to movies", movies_path %> +
diff --git a/app/views/movies/show.html.erb b/app/views/movies/show.html.erb new file mode 100644 index 0000000..b475ad0 --- /dev/null +++ b/app/views/movies/show.html.erb @@ -0,0 +1,10 @@ +

<%= notice %>

+ +<%= render @movie %> + +
+ <%= link_to "Edit this movie", edit_movie_path(@movie) %> | + <%= link_to "Back to movies", movies_path %> + + <%= button_to "Destroy this movie", @movie, method: :delete %> +
diff --git a/app/views/movies/show.json.jbuilder b/app/views/movies/show.json.jbuilder new file mode 100644 index 0000000..cec988f --- /dev/null +++ b/app/views/movies/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "movies/movie", movie: @movie diff --git a/config/routes.rb b/config/routes.rb index 33c9639..d3e33c8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,14 +1,4 @@ Rails.application.routes.draw do - # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html - - # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. - # Can be used by load balancers and uptime monitors to verify that the app is live. - get "up" => "rails/health#show", as: :rails_health_check - - # Render dynamic PWA files from app/views/pwa/* - get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker - get "manifest" => "rails/pwa#manifest", as: :pwa_manifest - - # Defines the root path route ("/") - # root "posts#index" + resources :movies + root :to => redirect('/movies') end diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb new file mode 100644 index 0000000..9889494 --- /dev/null +++ b/test/controllers/movies_controller_test.rb @@ -0,0 +1,48 @@ +require "test_helper" + +class MoviesControllerTest < ActionDispatch::IntegrationTest + setup do + @movie = movies(:one) + end + + test "should get index" do + get movies_url + assert_response :success + end + + test "should get new" do + get new_movie_url + assert_response :success + end + + test "should create movie" do + assert_difference("Movie.count") do + post movies_url, params: { movie: { description: @movie.description, rating: @movie.rating, release_date: @movie.release_date, title: @movie.title } } + end + + assert_redirected_to movie_url(Movie.last) + end + + test "should show movie" do + get movie_url(@movie) + assert_response :success + end + + test "should get edit" do + get edit_movie_url(@movie) + assert_response :success + end + + test "should update movie" do + patch movie_url(@movie), params: { movie: { description: @movie.description, rating: @movie.rating, release_date: @movie.release_date, title: @movie.title } } + assert_redirected_to movie_url(@movie) + end + + test "should destroy movie" do + assert_difference("Movie.count", -1) do + delete movie_url(@movie) + end + + assert_redirected_to movies_url + end +end diff --git a/test/system/movies_test.rb b/test/system/movies_test.rb new file mode 100644 index 0000000..2fb9061 --- /dev/null +++ b/test/system/movies_test.rb @@ -0,0 +1,47 @@ +require "application_system_test_case" + +class MoviesTest < ApplicationSystemTestCase + setup do + @movie = movies(:one) + end + + test "visiting the index" do + visit movies_url + assert_selector "h1", text: "Movies" + end + + test "should create movie" do + visit movies_url + click_on "New movie" + + fill_in "Description", with: @movie.description + fill_in "Rating", with: @movie.rating + fill_in "Release date", with: @movie.release_date + fill_in "Title", with: @movie.title + click_on "Create Movie" + + assert_text "Movie was successfully created" + click_on "Back" + end + + test "should update Movie" do + visit movie_url(@movie) + click_on "Edit this movie", match: :first + + fill_in "Description", with: @movie.description + fill_in "Rating", with: @movie.rating + fill_in "Release date", with: @movie.release_date + fill_in "Title", with: @movie.title + click_on "Update Movie" + + assert_text "Movie was successfully updated" + click_on "Back" + end + + test "should destroy Movie" do + visit movie_url(@movie) + click_on "Destroy this movie", match: :first + + assert_text "Movie was successfully destroyed" + end +end