diff --git a/app/controllers/ratings_controller.rb b/app/controllers/ratings_controller.rb index 79798cb..986ff39 100644 --- a/app/controllers/ratings_controller.rb +++ b/app/controllers/ratings_controller.rb @@ -10,6 +10,8 @@ class RatingsController < ApplicationController def create Rating.create params.require(:rating).permit(:score, :beer_id) + session[:last_rating] = "#{rating.beer.name} #{rating.score} points" + redirect_to ratings_path end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..13c6038 --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,15 @@ +class SessionsController < ApplicationController + def new + end + + def create + user = User.find_by username: params[:username] + session[:user_id] = user.id if user + redirect_to user + end + + def destroy + session[:user_id] = nil + redirect_to :root + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..8970d8b --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,70 @@ +class UsersController < ApplicationController + before_action :set_user, only: %i[ show edit update destroy ] + + # GET /users or /users.json + def index + @users = User.all + end + + # GET /users/1 or /users/1.json + def show + end + + # GET /users/new + def new + @user = User.new + end + + # GET /users/1/edit + def edit + end + + # POST /users or /users.json + def create + @user = User.new(user_params) + + respond_to do |format| + if @user.save + format.html { redirect_to user_url(@user), notice: "User was successfully created." } + format.json { render :show, status: :created, location: @user } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @user.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /users/1 or /users/1.json + def update + respond_to do |format| + if @user.update(user_params) + format.html { redirect_to user_url(@user), notice: "User was successfully updated." } + format.json { render :show, status: :ok, location: @user } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @user.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /users/1 or /users/1.json + def destroy + @user.destroy + + respond_to do |format| + format.html { redirect_to users_url, notice: "User was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_user + @user = User.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def user_params + params.require(:user).permit(:username) + end +end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb new file mode 100644 index 0000000..2310a24 --- /dev/null +++ b/app/helpers/users_helper.rb @@ -0,0 +1,2 @@ +module UsersHelper +end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..379658a --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,2 @@ +class User < ApplicationRecord +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index e879b32..d5c21eb 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -12,5 +12,13 @@ <%= yield %> + <% if not session[:user_id].nil? %> +

<%= User.find(session[:user_id]).username %> signed in

+ <% end %> + <% if session[:last_rating].nil? %> +

no ratings given

+ <% else %> +

previous rating: <%= session[:last_rating] %>

+ <% end %> diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb new file mode 100644 index 0000000..63b2cf8 --- /dev/null +++ b/app/views/sessions/new.html.erb @@ -0,0 +1,6 @@ +

Sign in

+ +<%= form_with url: session_path, method: :post do |form| %> + <%= form.text_field :username %> + <%= form.submit "Log in" %> +<% end %> diff --git a/app/views/users/_form.html.erb b/app/views/users/_form.html.erb new file mode 100644 index 0000000..eb063d9 --- /dev/null +++ b/app/views/users/_form.html.erb @@ -0,0 +1,22 @@ +<%= form_with(model: user) do |form| %> + <% if user.errors.any? %> +
+

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

+ + +
+ <% end %> + +
+ <%= form.label :username, style: "display: block" %> + <%= form.text_field :username %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/users/_user.html.erb b/app/views/users/_user.html.erb new file mode 100644 index 0000000..a67816c --- /dev/null +++ b/app/views/users/_user.html.erb @@ -0,0 +1,7 @@ +
+

+ Username: + <%= user.username %> +

+ +
diff --git a/app/views/users/_user.json.jbuilder b/app/views/users/_user.json.jbuilder new file mode 100644 index 0000000..c88f34b --- /dev/null +++ b/app/views/users/_user.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! user, :id, :username, :created_at, :updated_at +json.url user_url(user, format: :json) diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb new file mode 100644 index 0000000..9dda632 --- /dev/null +++ b/app/views/users/edit.html.erb @@ -0,0 +1,10 @@ +

Editing user

+ +<%= render "form", user: @user %> + +
+ +
+ <%= link_to "Show this user", @user %> | + <%= link_to "Back to users", users_path %> +
diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb new file mode 100644 index 0000000..7f15d6b --- /dev/null +++ b/app/views/users/index.html.erb @@ -0,0 +1,15 @@ +

<%= notice %>

+ +

Users

+ +
+ <% @users.each do |user| %> + <%= render user %> +

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

+ <% end %> +
+ +<%= link_to "New user", new_user_path %> +<%= link_to "List of breweries", breweries_path %> diff --git a/app/views/users/index.json.jbuilder b/app/views/users/index.json.jbuilder new file mode 100644 index 0000000..98788da --- /dev/null +++ b/app/views/users/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @users, partial: "users/user", as: :user diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb new file mode 100644 index 0000000..eedbd83 --- /dev/null +++ b/app/views/users/new.html.erb @@ -0,0 +1,9 @@ +

New user

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

<%= notice %>

+ +<%= render @user %> + +
+ <%= link_to "Edit this user", edit_user_path(@user) %> | + <%= link_to "Back to users", users_path %> + + <%= button_to "Destroy this user", @user, method: :delete %> +
diff --git a/app/views/users/show.json.jbuilder b/app/views/users/show.json.jbuilder new file mode 100644 index 0000000..ff40bb9 --- /dev/null +++ b/app/views/users/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "users/user", user: @user diff --git a/config/routes.rb b/config/routes.rb index f3fc887..e191a75 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :users resources :beers resources :breweries # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html @@ -6,5 +7,7 @@ Rails.application.routes.draw do # Defines the root path route ("/") # root "articles#index" get '/', to: 'breweries#index' + get 'signup', to: 'users#new' resources :ratings, only: [:index, :new, :create, :destroy] + resource :session, only: [:new, :create, :destroy] end diff --git a/db/migrate/20230808143510_create_users.rb b/db/migrate/20230808143510_create_users.rb new file mode 100644 index 0000000..b29c33f --- /dev/null +++ b/db/migrate/20230808143510_create_users.rb @@ -0,0 +1,9 @@ +class CreateUsers < ActiveRecord::Migration[7.0] + def change + create_table :users do |t| + t.string :username + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 84c2e26..87be6c1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_06_04_140719) do +ActiveRecord::Schema[7.0].define(version: 2023_08_08_143510) do create_table "beers", force: :cascade do |t| t.string "name" t.string "style" @@ -33,4 +33,10 @@ ActiveRecord::Schema[7.0].define(version: 2023_06_04_140719) do t.datetime "updated_at", null: false end + create_table "users", force: :cascade do |t| + t.string "username" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + end diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb new file mode 100644 index 0000000..c2d855d --- /dev/null +++ b/test/controllers/users_controller_test.rb @@ -0,0 +1,48 @@ +require "test_helper" + +class UsersControllerTest < ActionDispatch::IntegrationTest + setup do + @user = users(:one) + end + + test "should get index" do + get users_url + assert_response :success + end + + test "should get new" do + get new_user_url + assert_response :success + end + + test "should create user" do + assert_difference("User.count") do + post users_url, params: { user: { username: @user.username } } + end + + assert_redirected_to user_url(User.last) + end + + test "should show user" do + get user_url(@user) + assert_response :success + end + + test "should get edit" do + get edit_user_url(@user) + assert_response :success + end + + test "should update user" do + patch user_url(@user), params: { user: { username: @user.username } } + assert_redirected_to user_url(@user) + end + + test "should destroy user" do + assert_difference("User.count", -1) do + delete user_url(@user) + end + + assert_redirected_to users_url + end +end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..eb9d7d6 --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,7 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + username: MyString + +two: + username: MyString diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 0000000..5c07f49 --- /dev/null +++ b/test/models/user_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class UserTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/system/users_test.rb b/test/system/users_test.rb new file mode 100644 index 0000000..ea8907d --- /dev/null +++ b/test/system/users_test.rb @@ -0,0 +1,41 @@ +require "application_system_test_case" + +class UsersTest < ApplicationSystemTestCase + setup do + @user = users(:one) + end + + test "visiting the index" do + visit users_url + assert_selector "h1", text: "Users" + end + + test "should create user" do + visit users_url + click_on "New user" + + fill_in "Username", with: @user.username + click_on "Create User" + + assert_text "User was successfully created" + click_on "Back" + end + + test "should update User" do + visit user_url(@user) + click_on "Edit this user", match: :first + + fill_in "Username", with: @user.username + click_on "Update User" + + assert_text "User was successfully updated" + click_on "Back" + end + + test "should destroy User" do + visit user_url(@user) + click_on "Destroy this user", match: :first + + assert_text "User was successfully destroyed" + end +end