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

Db reconfig #10

Merged
merged 7 commits into from
Sep 12, 2024
Merged
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
33 changes: 33 additions & 0 deletions app/controllers/api/v1/attendees_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Api::V1::AttendeesController < ApplicationController
def create
attendee = Attendee.new(attendee_params)

if attendee.save
render json: attendee, status: :created
else
render json: { errors: attendee.errors.full_messages }, status: :unprocessable_entity
end
end

def destroy
attendee = Attendee.find_by(user_id: params[:user_id], event_id: params[:event_id])

if attendee
attendee.destroy
render json: { message: 'Attendee removed successfully' }, status: :ok
else
render json: { errors: 'Attendee not found' }, status: :not_found
end
end

def index
attendees = Attendee.where(event_id: params[:event_id])
render json: attendees, status: :ok
end
##### NEED TO SQUARE AWAY JSON RENDERING WITH SERIALIZERS FOR ALL ACTIONS #####
private

def attendee_params
params.require(:attendee).permit(:user_id, :event_id)
end
end
9 changes: 7 additions & 2 deletions app/controllers/api/v1/events_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class Api::V1::EventsController < ApplicationController
class Api::V1::EventsController < ApplicationController
# THIS CONTROLLER NOW RENDERS JSON FOR ALL DB EVENTS, WE DO NOT NEED A CREATE AND DESTROY ACTION FOR EVENTS IN THIS CONTROLLER
# THAT WILL HAPPEN IN USER_EVENTS CONTROLLER
# WE JUST NEED INDEX ACTION TO SHOW ALL EVENTS EVENT.ALL
# AND SHOW ACTION TO SHOW A SINGLE EVENT EVENT.FIND(BY ID)
def create
event = Event.new(event_params)

Expand All @@ -9,11 +13,12 @@ def create
end
end


def destroy
event = Event.find(params[:id])
event.destroy
end

def index
begin
if params[:user_id]
Expand Down
44 changes: 11 additions & 33 deletions app/controllers/api/v1/user_events_controller.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,19 @@
class Api::V1::UserEventsController < ApplicationController
def show
user_event = UserEvent.find(params[:id])
render json: UserEventSerializer.new(user_event)
end

def create
user_event = UserEvent.new(user_event_params)
if user_event.save
render json: UserEventSerializer.new(user_event), status: :created
else
render json: { errors: user_event.errors.full_messages }, status: :unprocessable_entity
end
end

def destroy
user_event = UserEvent.find(params[:id])
# this controller will handle creating an event associated with a user
# deleting an event associated with a user
# showing an event associated with a user
# showing all events associated with a user
def create
user = User.find(params[:user_id])
event = user.events.create(event_params)

if user_event.destroy
render json: { message: "UserEvent has been deleted" }, status: :no_content
if event.save
render json: EventSerializer.new(event), status: :created
else
render json: { errors: user_event.errors.full_messages }, status: :unprocessable_entity
end
end

def update
user_event = UserEvent.find(params[:id])

if user_event.update(user_event_params)
render json: UserEventSerializer.new(user_event), status: :ok
else
render json: { errors: user_event.errors.full_messages }, status: :unprocessable_entity
render json: { errors: event.errors.full_messages }, status: :unprocessable_entity
end
end

private

def user_event_params
params.require(:user_event).permit(:user_id, :event_id, :host)
end
params.require(:event).permit(:venue_name, :event_name, :date_time, :artist, :location, :user_id)
end
8 changes: 5 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
# root "posts#index"
namespace :api do
namespace :v1 do
resources :events, only: [:index, :show, :create, :destroy]
resources :users, only: [:show, :create, :update, :destroy]
resources :user_events, only: [:show, :create, :update, :destroy]
resources :events, only: [:show, :index] # this will be all events in db no matter the user
resources :users, only: [:show, :create, :update, :destroy] do
resources :user_events, only: [:create, :show, :index, :destroy] #user_events controller to pull events for a user (and create and delete)
end
resources :artists, only: [:index, :create, :destroy]
resources :attendees, only: [:index, :create, :destroy]
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20240912180217_add_user_id_to_events.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUserIdToEvents < ActiveRecord::Migration[7.1]
def change
add_reference :events, :user, foreign_key: true
end
end
18 changes: 18 additions & 0 deletions db/migrate/20240912181519_recreate_attendees_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class RecreateAttendeesTable < ActiveRecord::Migration[7.1]
def change
drop_table :user_events, if_exists: true

create_table :attendees do |t|
t.bigint :user_id, null: false
t.bigint :event_id, null: false
t.datetime :created_at, null: false
t.datetime :updated_at, null: false

t.index :event_id
t.index :user_id
end

add_foreign_key :attendees, :users
add_foreign_key :attendees, :events
end
end
28 changes: 15 additions & 13 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading