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

HER-62-Create-Availability-Controller #48

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

bbcc33
Copy link
Collaborator

@bbcc33 bbcc33 commented Jan 8, 2025

HER-62-Create-Availability-Controller

https://raquelanaroman.atlassian.net/browse/HER-62

Description

To allow speaker users to manage their availability, we need to create an AvailabilitiesController for handling Availability resources. This controller will manage CRUD operations for the Availability model.
Acceptance Criteria

Create an AvailabilitiesController with index, show, create, update, and destroy actions.

Ensure the controller uses strong parameters to permit the necessary attributes: start_time, end_time, speaker_id, recurring_availability_id.

Add routes for the AvailabilitiesController in config/routes.rb.

Ensure the controller enforces the following relationships: An availability belongs to a speaker user; An availability belongs to a RecurringAvailability.

Ensure appropriate error handling.

Test controller for proper functionality.

Implementation
class Api::V1::AvailabilitiesController < ApplicationController
before_action :set_availability, only: [:show, :update, :destroy]

def index
  # This shows the availabilities including recurring availibilities for the month
  # and year sent in the request from the frontend or a default month and year.
  viewing_month = params[:month].to_i || Date.today.month
  viewing_year = params[:year].to_i || Date.today.year
  start_date = DateTime.new(viewing_year, viewing_month, 1).beginning_of_day
  end_date = start_date.end_of_month.end_of_day

  @availabilities = Availability.where(start_time: start_date..end_date)
  render json: @availabilities
end

def show
  render json: @availability
end

def create
  @availability = Availability.new(availability_params)
  if @availability.save
    render json: @availability, status: :created
  else
    render json: @availability.errors, status: :unprocessable_entity
  end
end

def update
  if @availability.update(availability_params)
    render json: @availability
  else
    render json: @availability.errors, status: :unprocessable_entity
  end
end

def destroy
  @availability.destroy
  head :no_content
end

private

def set_availability
  @availability = Availability.find(params[:id])
end

def availability_params
  params.require(:availability).permit(:start_time, :end_time, :speaker_id, :recurring_availability_id)
end

end
end

config/routes.rb
namespace :api do
namespace :v1 do
# Rest of routes
resources :availabilities, only: [:index, :show, :create, :update, :destroy]
end
end

Ensure relationships in models:
class Availability < ApplicationRecord
belongs_to :speaker
belongs_to :recurring_availability
end

class Speaker < ApplicationRecord
has_many :availabilities
end

class RecurringAvailability < ApplicationRecord
has_many :availabilities
end

Her-64, HER-51

Changes

created an availabilities controller
added availabilites to routes
created tests for availabilites

Review Checklist

  • I have documented my code with code comments.

Copy link
Collaborator

@mhope21 mhope21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented out tests and modify code to pass tests.

spec/requests/api/v1/availabilities_spec.rb Outdated Show resolved Hide resolved
@bbcc33 bbcc33 requested a review from mhope21 January 9, 2025 01:04
Copy link
Collaborator

@dsinn dsinn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see some puts calls; are those for debugging?

@bbcc33
Copy link
Collaborator Author

bbcc33 commented Jan 9, 2025

I see some puts calls; are those for debugging?
@dsinn Yes, I was trying to figure out why a test wasn't passing. Should I go back to get rid of it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants