Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

controllers implemented with basic CRUD features #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions movie-base/app/controllers/film_casts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class FilmCastsController < ApplicationController
def index
@film = Film.find(params[:film_id])
@actors = @film.actors
render json: @actors
end
end
47 changes: 47 additions & 0 deletions movie-base/app/controllers/films_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class FilmsController < ApplicationController
# disable token for external testing
protect_from_forgery with: :null_session

before_action :set_film, only: %i[show update destroy]

def index
@films = Film.all
render json: @films
end

def show
render json: @film
end

def create
@film = Film.new(film_params)

if @film.save
render json: @film, status: :created
else
render json: @film.errors, status: :unprocessable_entity
end
end

def update
if @film.update(film_params)
render json: @film
else
render json: @film.errors, status: :unprocessable_entity
end
end

def destroy
@film.destroy
end

private

def set_film
@film = Film.find(params[:id])
end

def film_params
params.require(:film).permit(:title, :description, :release_date, :rating)
end
end
47 changes: 47 additions & 0 deletions movie-base/app/controllers/sources_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class SourcesController < ApplicationController
# disable token for external testing
protect_from_forgery with: :null_session

before_action :set_source, only: %i[show update destroy]

def index
@sources = Source.all
render json: @sources
end

def show
render json: @source
end

def create
@source = Source.new(source_params)

if @source.save
render json: @source, status: :created
else
render json: @source.errors, status: :unprocessable_entity
end
end

def update
if @source.update(source_params)
render json: @source
else
render json: @source.errors, status: :unprocessable_entity
end
end

def destroy
@source.destroy
end

private

def set_source
@source = Source.find(params[:id])
end

def source_params
params.require(:source).permit(:name, :description, :url)
end
end
47 changes: 47 additions & 0 deletions movie-base/app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class UsersController < ApplicationController
# disable token for external testing
protect_from_forgery with: :null_session

before_action :set_user, only: %i[show update destroy]

def index
@users = User.all
render json: @users
end

def show
render json: @user
end

def create
@user = User.new(user_params)

if @user.save
render json: @user, status: :created
else
render json: @user.errors, status: :unprocessable_entity
end
end

def update
if @user.update(user_params)
render json: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end

def destroy
@user.destroy
end

private

def set_user
@user = User.find(params[:id])
end

def user_params
params.require(:user).permit(:name, :birth_date, :document_type, :document)
end
end
2 changes: 2 additions & 0 deletions movie-base/app/helpers/film_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module FilmHelper
end
2 changes: 2 additions & 0 deletions movie-base/app/helpers/film_users_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module FilmUsersHelper
end
2 changes: 2 additions & 0 deletions movie-base/app/helpers/source_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module SourceHelper
end
2 changes: 2 additions & 0 deletions movie-base/app/helpers/user_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module UserHelper
end
4 changes: 2 additions & 2 deletions movie-base/app/models/film.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class Film < ApplicationRecord
validates :title, :genre, :release_date, :score, presence: true
validates :score, numericality: { only_integer: false, greater_than_or_equal_to: 1, less_than_or_equal_to: 10 }

scope :by_genre, ->(genre) { where("genre LIKE ?", "%#{genre.to_s}%") }
scope :by_genre, ->(genre) { where('genre LIKE ?', "%#{genre}%") }

class << self
[:action, :drama, :comedy, :science_fiction, :historical, :horror, :suspense, :fantasy].each do |genre|
%i[action drama comedy science_fiction historical horror suspense fantasy].each do |genre|
define_method(genre) { by_genre(genre) }
end
end
Expand Down
12 changes: 11 additions & 1 deletion movie-base/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
Rails.application.routes.draw do
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

Rails.application.routes.draw do
resources :users
end
Rails.application.routes.draw do
resources :films do
resources :cast, only: [:index], controller: 'film_casts' # film/id/cast will give the movie cast
end
end
Rails.application.routes.draw do
resources :sources
end
# 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
Expand Down
76 changes: 38 additions & 38 deletions movie-base/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,52 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_02_16_204428) do
create_table "film_casts", force: :cascade do |t|
t.integer "film_id", null: false
t.integer "actor_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["actor_id"], name: "index_film_casts_on_actor_id"
t.index ["film_id"], name: "index_film_casts_on_film_id"
ActiveRecord::Schema[7.1].define(version: 20_240_216_204_428) do
create_table 'film_casts', force: :cascade do |t|
t.integer 'film_id', null: false
t.integer 'actor_id', null: false
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
t.index ['actor_id'], name: 'index_film_casts_on_actor_id'
t.index ['film_id'], name: 'index_film_casts_on_film_id'
end

create_table "film_directors", force: :cascade do |t|
t.integer "film_id", null: false
t.integer "director_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["director_id"], name: "index_film_directors_on_director_id"
t.index ["film_id"], name: "index_film_directors_on_film_id"
create_table 'film_directors', force: :cascade do |t|
t.integer 'film_id', null: false
t.integer 'director_id', null: false
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
t.index ['director_id'], name: 'index_film_directors_on_director_id'
t.index ['film_id'], name: 'index_film_directors_on_film_id'
end

create_table "films", force: :cascade do |t|
t.string "title"
t.string "genre"
t.date "release_date"
t.float "score"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
create_table 'films', force: :cascade do |t|
t.string 'title'
t.string 'genre'
t.date 'release_date'
t.float 'score'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
end

create_table "sources", force: :cascade do |t|
t.string "format"
t.string "path"
t.string "runtime"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "film_id", null: false
t.index ["film_id"], name: "index_sources_on_film_id"
create_table 'sources', force: :cascade do |t|
t.string 'format'
t.string 'path'
t.string 'runtime'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
t.integer 'film_id', null: false
t.index ['film_id'], name: 'index_sources_on_film_id'
end

create_table "users", force: :cascade do |t|
t.string "name"
t.string "document"
t.string "document_type"
t.date "birth_date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
create_table 'users', force: :cascade do |t|
t.string 'name'
t.string 'document'
t.string 'document_type'
t.date 'birth_date'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
end

add_foreign_key "sources", "films"
add_foreign_key 'sources', 'films'
end
90 changes: 45 additions & 45 deletions movie-base/lib/tasks/auto_annotate_models.rake
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,51 @@ if Rails.env.development?
# You can override any of these by setting an environment variable of the
# same name.
Annotate.set_defaults(
'active_admin' => 'false',
'additional_file_patterns' => [],
'routes' => 'false',
'models' => 'true',
'position_in_routes' => 'before',
'position_in_class' => 'before',
'position_in_test' => 'before',
'position_in_fixture' => 'before',
'position_in_factory' => 'before',
'position_in_serializer' => 'before',
'show_foreign_keys' => 'true',
'show_complete_foreign_keys' => 'false',
'show_indexes' => 'true',
'simple_indexes' => 'false',
'model_dir' => 'app/models',
'root_dir' => '',
'include_version' => 'false',
'require' => '',
'exclude_tests' => 'false',
'exclude_fixtures' => 'false',
'exclude_factories' => 'false',
'exclude_serializers' => 'false',
'exclude_scaffolds' => 'true',
'exclude_controllers' => 'true',
'exclude_helpers' => 'true',
'exclude_sti_subclasses' => 'false',
'ignore_model_sub_dir' => 'false',
'ignore_columns' => nil,
'ignore_routes' => nil,
'ignore_unknown_models' => 'false',
'hide_limit_column_types' => 'integer,bigint,boolean',
'hide_default_column_types' => 'json,jsonb,hstore',
'skip_on_db_migrate' => 'false',
'format_bare' => 'true',
'format_rdoc' => 'false',
'format_yard' => 'false',
'format_markdown' => 'false',
'sort' => 'false',
'force' => 'false',
'frozen' => 'false',
'classified_sort' => 'true',
'trace' => 'false',
'wrapper_open' => nil,
'wrapper_close' => nil,
'with_comment' => 'true'
'active_admin' => 'false',
'additional_file_patterns' => [],
'routes' => 'false',
'models' => 'true',
'position_in_routes' => 'before',
'position_in_class' => 'before',
'position_in_test' => 'before',
'position_in_fixture' => 'before',
'position_in_factory' => 'before',
'position_in_serializer' => 'before',
'show_foreign_keys' => 'true',
'show_complete_foreign_keys' => 'false',
'show_indexes' => 'true',
'simple_indexes' => 'false',
'model_dir' => 'app/models',
'root_dir' => '',
'include_version' => 'false',
'require' => '',
'exclude_tests' => 'false',
'exclude_fixtures' => 'false',
'exclude_factories' => 'false',
'exclude_serializers' => 'false',
'exclude_scaffolds' => 'true',
'exclude_controllers' => 'true',
'exclude_helpers' => 'true',
'exclude_sti_subclasses' => 'false',
'ignore_model_sub_dir' => 'false',
'ignore_columns' => nil,
'ignore_routes' => nil,
'ignore_unknown_models' => 'false',
'hide_limit_column_types' => 'integer,bigint,boolean',
'hide_default_column_types' => 'json,jsonb,hstore',
'skip_on_db_migrate' => 'false',
'format_bare' => 'true',
'format_rdoc' => 'false',
'format_yard' => 'false',
'format_markdown' => 'false',
'sort' => 'false',
'force' => 'false',
'frozen' => 'false',
'classified_sort' => 'true',
'trace' => 'false',
'wrapper_open' => nil,
'wrapper_close' => nil,
'with_comment' => 'true'
)
end

Expand Down
Loading