-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from WWC-Hackathon-2023/1refactor
BE | Refactor `PuzzlesController#index` & Cleanup `UserPuzzlesController`
- Loading branch information
Showing
10 changed files
with
248 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,37 @@ | ||
class Api::V1::Users::PuzzlesController < ApplicationController | ||
before_action :find_user | ||
before_action :find_puzzle, only: [:update] | ||
|
||
def index | ||
render json: PuzzleSerializer.new(@user.puzzles) | ||
end | ||
|
||
def show | ||
puzzle = @user.puzzles.find(params[:puzzle_id]) | ||
render json: PuzzleSerializer.new(puzzle) | ||
show_puzzle = @user.puzzles.find(params[:puzzle_id]) | ||
render json: PuzzleSerializer.new(show_puzzle) | ||
end | ||
|
||
def create | ||
puzzle = @user.puzzles.new(puzzle_params) | ||
render json: PuzzleSerializer.new(puzzle), status: 201 if puzzle.save | ||
new_puzzle = @user.puzzles.new(puzzle_params) | ||
render json: PuzzleSerializer.new(new_puzzle), status: 201 if new_puzzle.save | ||
end | ||
|
||
def update | ||
puzzle = Puzzle.find(params[:puzzle_id]) | ||
|
||
puzzle.update(puzzle_params) | ||
render json: PuzzleSerializer.new(puzzle) | ||
@puzzle.update(puzzle_params) | ||
render json: PuzzleSerializer.new(@puzzle) | ||
end | ||
|
||
private | ||
|
||
def puzzle_params | ||
params.permit(:status, :title, :description, :total_pieces, :notes, :puzzle_image_url) # did not include user_id | ||
params.permit(:status, :title, :description, :total_pieces, :notes, :puzzle_image_url) # did not include user_id since this will not be updated & is available when created already | ||
end | ||
|
||
def find_user | ||
@user = User.find(params[:user_id]) | ||
end | ||
|
||
def find_puzzle | ||
@puzzle = Puzzle.find(params[:puzzle_id]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,31 @@ | ||
class Api::V1::UsersController < ApplicationController | ||
before_action :find_user, only: [:show, :dashboard] | ||
|
||
def show | ||
render json: UserSerializer.new(User.find(params[:user_id])) | ||
render json: UserSerializer.new(@user) | ||
end | ||
|
||
def dashboard | ||
dashboard = User.find(params[:user_id]).find_dashboard_info | ||
dashboard = @user.find_dashboard_info | ||
render json: DashboardSerializer.new(dashboard) | ||
end | ||
|
||
def create | ||
new_user = User.new(user_params) | ||
new_user.email.downcase | ||
new_user.format_phone_number | ||
return unless new_user.save | ||
|
||
session[:user_id] = new_user.id | ||
render json: UserSerializer.new(new_user), status: :created | ||
new_user.format_attributes | ||
if new_user.save | ||
session[:user_id] = new_user.id | ||
render json: UserSerializer.new(new_user), status: :created | ||
end | ||
end | ||
|
||
private | ||
|
||
def user_params | ||
params.permit(:full_name, :password, :password_confirmation, :email, :zip_code, :phone_number) | ||
end | ||
|
||
def find_user | ||
@user = User.find(params[:user_id]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class NoPuzzlesException < StandardError | ||
end | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.