diff --git a/.vscode/.ltici_apitoken.yml b/.vscode/.ltici_apitoken.yml new file mode 100644 index 0000000..b9316dd --- /dev/null +++ b/.vscode/.ltici_apitoken.yml @@ -0,0 +1,3 @@ +--- +submission_url: https://grades.firstdraft.com +personal_access_token: qDWCypEgSWcA3qJK6gPmFgre diff --git a/Gemfile b/Gemfile index 3d7ed82..ce03a34 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,8 @@ ruby '3.0.3' gem "grade_runner", github: "firstdraft/grade_runner" +gem "draft_generators", github: "firstdraft/draft_generators" + # Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' gem 'rails', '~> 6.1.3', '>= 6.1.3.1' # Use postgresql as the database for Active Record diff --git a/Gemfile.lock b/Gemfile.lock index fe0ec3b..a484f54 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,11 @@ +GIT + remote: https://github.com/firstdraft/draft_generators.git + revision: 23069b562615e42e04c3343e330846594ce51fdb + specs: + draft_generators (0.0.3) + devise + indefinite_article + GIT remote: https://github.com/firstdraft/grade_runner.git revision: e19ab1764a5a4e0a89731fcab76e352d60240ddf @@ -90,6 +98,7 @@ GEM ast (2.4.2) awesome_print (1.9.2) backport (1.2.0) + bcrypt (3.1.18) benchmark (0.2.0) better_errors (2.9.1) coderay (>= 1.0.0) @@ -130,6 +139,12 @@ GEM rexml crass (1.0.6) debug_inspector (1.1.0) + devise (4.9.2) + bcrypt (~> 3.0) + orm_adapter (~> 0.1) + railties (>= 4.1.0) + responders + warden (~> 1.2.3) diff-lcs (1.5.0) diffy (3.4.2) e2mmap (0.1.0) @@ -157,6 +172,8 @@ GEM image_processing (1.12.2) mini_magick (>= 4.9.5, < 5) ruby-vips (>= 2.0.17, < 3) + indefinite_article (0.2.5) + activesupport jaro_winkler (1.5.4) jbuilder (2.11.5) actionview (>= 5.0.0) @@ -193,6 +210,7 @@ GEM faraday (>= 1, < 3) sawyer (~> 0.9) oj (3.10.18) + orm_adapter (0.5.0) parallel (1.22.1) parser (3.1.1.0) ast (~> 2.4.1) @@ -256,6 +274,9 @@ GEM ffi (~> 1.0) rchardet (1.8.0) regexp_parser (2.2.1) + responders (3.1.0) + actionpack (>= 5.2) + railties (>= 5.2) reverse_markdown (2.1.1) nokogiri rexml (3.2.5) @@ -375,6 +396,8 @@ GEM tzinfo (>= 1.0.0) unicode-display_width (2.1.0) uniform_notifier (1.16.0) + warden (1.2.9) + rack (>= 2.0.9) web-console (4.2.0) actionview (>= 6.0.0) activemodel (>= 6.0.0) @@ -417,6 +440,7 @@ DEPENDENCIES byebug capybara (>= 3.26) carrierwave + draft_generators! factory_bot_rails grade_runner! htmlbeautifier diff --git a/app/assets/stylesheets/books.scss b/app/assets/stylesheets/books.scss new file mode 100644 index 0000000..e0f44b0 --- /dev/null +++ b/app/assets/stylesheets/books.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the books controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: https://sass-lang.com/ diff --git a/app/assets/stylesheets/scaffolds.scss b/app/assets/stylesheets/scaffolds.scss new file mode 100644 index 0000000..bb2597f --- /dev/null +++ b/app/assets/stylesheets/scaffolds.scss @@ -0,0 +1,65 @@ +body { + background-color: #fff; + color: #333; + margin: 33px; } + +body, p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; } + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; } + +a { + color: #000; } + +a:visited { + color: #666; } + +a:hover { + color: #fff; + background-color: #000; } + +th { + padding-bottom: 5px; } + +td { + padding: 0 5px 7px; } + +div.field, +div.actions { + margin-bottom: 10px; } + +#notice { + color: green; } + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; } + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px 7px 0; + margin-bottom: 20px; + background-color: #f0f0f0; } + +#error_explanation h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px -7px 0; + background-color: #c00; + color: #fff; } + +#error_explanation ul li { + font-size: 12px; + list-style: square; } + +label { + display: block; } diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb new file mode 100644 index 0000000..5b0ba65 --- /dev/null +++ b/app/controllers/books_controller.rb @@ -0,0 +1,70 @@ +class BooksController < ApplicationController + before_action :set_book, only: %i[ show edit update destroy ] + + # GET /books or /books.json + def index + @books = Book.all + end + + # GET /books/1 or /books/1.json + def show + end + + # GET /books/new + def new + @book = Book.new + end + + # GET /books/1/edit + def edit + end + + # POST /books or /books.json + def create + @book = Book.new(book_params) + + respond_to do |format| + if @book.save + format.html { redirect_to book_url(@book), notice: "Book was successfully created." } + format.json { render :show, status: :created, location: @book } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @book.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /books/1 or /books/1.json + def update + respond_to do |format| + if @book.update(book_params) + format.html { redirect_to book_url(@book), notice: "Book was successfully updated." } + format.json { render :show, status: :ok, location: @book } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @book.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /books/1 or /books/1.json + def destroy + @book.destroy + + respond_to do |format| + format.html { redirect_to books_url, notice: "Book was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_book + @book = Book.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def book_params + params.require(:book).permit(:title, :description, :released) + end +end diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb new file mode 100644 index 0000000..d8fd184 --- /dev/null +++ b/app/controllers/movies_controller.rb @@ -0,0 +1,81 @@ +class MoviesController < ApplicationController + + def new + + @the_movie = Movie.new + + render template: "movies/new.html.erb" + end + + def edit + the_id = params.fetch(:id) + @the_movie = Movie.where(id: the_id).first + + render template: "movies/edit.html.erb" + end + + def index + matching_movies = Movie.all + + @list_of_movies = matching_movies.order({ :created_at => :desc }) + + render({ :template => "movies/index.html.erb" }) + end + + def show + the_id = params.fetch("id") + + matching_movies = Movie.where({ :id => the_id }) + + # @the_movie = matching_movies.at(0) + + @the_movie = matching_movies[0] + @the_movie = matching_movies.first + + render({ :template => "movies/show.html.erb" }) + end + + def create + @the_movie = Movie.new + @the_movie.title = params.fetch("query_title") + @the_movie.description = params.fetch("query_description") + @the_movie.released = params.fetch("query_released", false) + + if @the_movie.valid? + @the_movie.save + redirect_to("/movies", { :notice => "Movie created successfully." }) + else + # cookies[:title] = params.fetch("query_title") + # cookies[:description] = params.fetch("query_description") + + # redirect_to("/movies/new", { :alert => the_movie.errors.full_messages.to_sentence }) + + render template: "movies/new" + end + end + + def update + the_id = params.fetch("id") + the_movie = Movie.where({ :id => the_id }).first + + the_movie.title = params.fetch("query_title") + the_movie.description = params.fetch("query_description") + the_movie.released = params.fetch("query_released", false) + + if the_movie.valid? + the_movie.save + redirect_to("/movies/#{the_movie.id}", { :notice => "Movie updated successfully." }) + else + redirect_to("/movies/#{the_movie.id}", { :alert => the_movie.errors.full_messages.to_sentence }) + end + end + + def destroy + the_id = params.fetch("id") + the_movie = Movie.where({ :id => the_id }).first + + the_movie.destroy + + redirect_to("/movies", { :notice => "Movie deleted successfully." }) + end +end diff --git a/app/helpers/books_helper.rb b/app/helpers/books_helper.rb new file mode 100644 index 0000000..4b9311e --- /dev/null +++ b/app/helpers/books_helper.rb @@ -0,0 +1,2 @@ +module BooksHelper +end diff --git a/app/models/book.rb b/app/models/book.rb new file mode 100644 index 0000000..0bf915a --- /dev/null +++ b/app/models/book.rb @@ -0,0 +1,4 @@ +class Book < ApplicationRecord + validates :title, presence: true + validates :description, presence: true +end diff --git a/app/models/movie.rb b/app/models/movie.rb new file mode 100644 index 0000000..c9b623a --- /dev/null +++ b/app/models/movie.rb @@ -0,0 +1,4 @@ +class Movie < ApplicationRecord + validates :title, presence: true + validates :description, presence: true +end diff --git a/app/views/books/_book.json.jbuilder b/app/views/books/_book.json.jbuilder new file mode 100644 index 0000000..996e96f --- /dev/null +++ b/app/views/books/_book.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! book, :id, :title, :description, :released, :created_at, :updated_at +json.url book_url(book, format: :json) diff --git a/app/views/books/_form.html.erb b/app/views/books/_form.html.erb new file mode 100644 index 0000000..42c97c0 --- /dev/null +++ b/app/views/books/_form.html.erb @@ -0,0 +1,32 @@ +<%= form_with(model: book) do |form| %> + <% if book.errors.any? %> +
<%= notice %>
+ +Title | +Description | +Released | ++ | ||
---|---|---|---|---|---|
<%= book.title %> | +<%= book.description %> | +<%= book.released %> | +<%= link_to 'Show', book %> | +<%= link_to 'Edit', edit_book_path(book) %> | +<%= link_to 'Destroy', book, method: :delete, data: { confirm: 'Are you sure?' } %> | +
<%= notice %>
+ ++ Title: + <%= @book.title %> +
+ ++ Description: + <%= @book.description %> +
+ ++ Released: + <%= @book.released %> +
+ +<%= link_to 'Edit', edit_book_path(@book) %> | +<%= link_to 'Back', books_path %> diff --git a/app/views/books/show.json.jbuilder b/app/views/books/show.json.jbuilder new file mode 100644 index 0000000..c1e5174 --- /dev/null +++ b/app/views/books/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "books/book", book: @book diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 9917f83..2140133 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,6 +11,9 @@ + +<%= notice %>
+<%= alert %>
<%= yield %>