From a268b638514c592879d42aca6c8f07e14d56f03d Mon Sep 17 00:00:00 2001 From: Garrett Bowman Date: Thu, 12 Sep 2024 13:17:30 -0500 Subject: [PATCH 1/6] Reorganized DB --- app/controllers/api/v1/events_controller.rb | 15 ++++++++++ .../20240912180217_add_user_id_to_events.rb | 5 ++++ ...20240912181519_recreate_attendees_table.rb | 18 ++++++++++++ db/schema.rb | 28 ++++++++++--------- 4 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 db/migrate/20240912180217_add_user_id_to_events.rb create mode 100644 db/migrate/20240912181519_recreate_attendees_table.rb diff --git a/app/controllers/api/v1/events_controller.rb b/app/controllers/api/v1/events_controller.rb index 09e8622..0e289f4 100644 --- a/app/controllers/api/v1/events_controller.rb +++ b/app/controllers/api/v1/events_controller.rb @@ -9,6 +9,21 @@ def create end end + def index + begin + if params[:user_id] + user = User.find(params[:user_id]) + events = user.events + render json: EventSerializer.new(events) + else + events = Event.all + render json: EventSerializer.new(events) + end + rescue ActiveRecord::RecordNotFound => e + render json: ErrorSerializer.new(e).serialize_json, status: :not_found + end + end + private def event_params diff --git a/db/migrate/20240912180217_add_user_id_to_events.rb b/db/migrate/20240912180217_add_user_id_to_events.rb new file mode 100644 index 0000000..2368314 --- /dev/null +++ b/db/migrate/20240912180217_add_user_id_to_events.rb @@ -0,0 +1,5 @@ +class AddUserIdToEvents < ActiveRecord::Migration[7.1] + def change + add_reference :events, :user, foreign_key: true + end +end diff --git a/db/migrate/20240912181519_recreate_attendees_table.rb b/db/migrate/20240912181519_recreate_attendees_table.rb new file mode 100644 index 0000000..822f6ba --- /dev/null +++ b/db/migrate/20240912181519_recreate_attendees_table.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 0b813c5..31350b2 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.1].define(version: 2024_09_12_070736) do +ActiveRecord::Schema[7.1].define(version: 2024_09_12_181519) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -22,6 +22,15 @@ t.index ["musicbrainz_id"], name: "index_artists_on_musicbrainz_id", unique: true end + create_table "attendees", force: :cascade 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"], name: "index_attendees_on_event_id" + t.index ["user_id"], name: "index_attendees_on_user_id" + end + create_table "events", force: :cascade do |t| t.string "venue_name" t.string "event_name" @@ -30,6 +39,8 @@ t.string "location" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.bigint "user_id" + t.index ["user_id"], name: "index_events_on_user_id" end create_table "user_artists", force: :cascade do |t| @@ -41,16 +52,6 @@ t.index ["user_id"], name: "index_user_artists_on_user_id" end - create_table "user_events", force: :cascade do |t| - t.boolean "host" - 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"], name: "index_user_events_on_event_id" - t.index ["user_id"], name: "index_user_events_on_user_id" - end - create_table "users", force: :cascade do |t| t.string "name" t.string "email" @@ -58,8 +59,9 @@ t.datetime "updated_at", null: false end + add_foreign_key "attendees", "events" + add_foreign_key "attendees", "users" + add_foreign_key "events", "users" add_foreign_key "user_artists", "artists" add_foreign_key "user_artists", "users" - add_foreign_key "user_events", "events" - add_foreign_key "user_events", "users" end From 7657bbb0fd88e2b5fff6456f041777492e550369 Mon Sep 17 00:00:00 2001 From: Garrett Bowman Date: Thu, 12 Sep 2024 13:20:02 -0500 Subject: [PATCH 2/6] Added routes --- config/routes.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index a3367e6..b55cc60 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,10 +9,13 @@ # root "posts#index" namespace :api do namespace :v1 do - resources :events, only: [:show, :create, :update, :destroy] - resources :users, only: [:show, :create, :update, :destroy] + resources :events, only: [:show, :create, :update, :destroy] # 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 :user_events, only: [:show, :create, :update, :destroy] resources :artists, only: [:index, :create, :destroy] + resources :attendees, only: [:index, :create, :destroy] end end end From d3d9199b9ddb44452c605ea417da0e9bc3aa3c17 Mon Sep 17 00:00:00 2001 From: Garrett Bowman Date: Thu, 12 Sep 2024 13:28:23 -0500 Subject: [PATCH 3/6] Feat: Added Attendees table and controller --- .../api/v1/attendees_controller.rb | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 app/controllers/api/v1/attendees_controller.rb diff --git a/app/controllers/api/v1/attendees_controller.rb b/app/controllers/api/v1/attendees_controller.rb new file mode 100644 index 0000000..7bd53e3 --- /dev/null +++ b/app/controllers/api/v1/attendees_controller.rb @@ -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 \ No newline at end of file From ab8345786b0e0a9e79fd9bbcbe87c4530d58a22c Mon Sep 17 00:00:00 2001 From: Garrett Bowman Date: Thu, 12 Sep 2024 13:32:34 -0500 Subject: [PATCH 4/6] WIP: Added notes to controllers, moved old controller logic to attendees --- app/controllers/api/v1/events_controller.rb | 4 +- .../api/v1/user_events_controller.rb | 43 ++----------------- 2 files changed, 7 insertions(+), 40 deletions(-) diff --git a/app/controllers/api/v1/events_controller.rb b/app/controllers/api/v1/events_controller.rb index 0e289f4..eeaddeb 100644 --- a/app/controllers/api/v1/events_controller.rb +++ b/app/controllers/api/v1/events_controller.rb @@ -1,4 +1,6 @@ -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 + # THAT WILL HAPPEN IN USER_EVENTS CONTROLLER def create event = Event.new(event_params) diff --git a/app/controllers/api/v1/user_events_controller.rb b/app/controllers/api/v1/user_events_controller.rb index 532f094..67888bb 100644 --- a/app/controllers/api/v1/user_events_controller.rb +++ b/app/controllers/api/v1/user_events_controller.rb @@ -1,41 +1,6 @@ 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]) - - if user_event.destroy - render json: { message: "UserEvent has been deleted" }, status: :no_content - 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 - end - end - - private - - def user_event_params - params.require(:user_event).permit(:user_id, :event_id, :host) - end + # 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 end \ No newline at end of file From edbffa6cdfaf3becc37771eb590d73a4bc4c9a68 Mon Sep 17 00:00:00 2001 From: Garrett Bowman Date: Thu, 12 Sep 2024 13:39:30 -0500 Subject: [PATCH 5/6] WIP: Added Notes for Controllers --- app/controllers/api/v1/events_controller.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/events_controller.rb b/app/controllers/api/v1/events_controller.rb index eeaddeb..e77ee3a 100644 --- a/app/controllers/api/v1/events_controller.rb +++ b/app/controllers/api/v1/events_controller.rb @@ -1,6 +1,8 @@ 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 + # 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) From ede61db23b3f448338e575329056cabd4389a29f Mon Sep 17 00:00:00 2001 From: Garrett Bowman Date: Thu, 12 Sep 2024 13:42:39 -0500 Subject: [PATCH 6/6] Feat: Added Setup to UserEvents --- app/controllers/api/v1/user_events_controller.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/controllers/api/v1/user_events_controller.rb b/app/controllers/api/v1/user_events_controller.rb index 67888bb..5776277 100644 --- a/app/controllers/api/v1/user_events_controller.rb +++ b/app/controllers/api/v1/user_events_controller.rb @@ -3,4 +3,17 @@ class Api::V1::UserEventsController < ApplicationController # 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 event.save + render json: EventSerializer.new(event), status: :created + else + render json: { errors: event.errors.full_messages }, status: :unprocessable_entity + end + end + + private + params.require(:event).permit(:venue_name, :event_name, :date_time, :artist, :location, :user_id) end \ No newline at end of file