I am currently developing a project for space bookings. Its an API project. I have done till Booking API. But want to check availability check and validations for booking. How to write logic code for checking availability of the space and to do validations on weather the space is available or not. The spaces I am using in this project is meeting rooms, private space, and desks
This is my space creation JSON
{
"space": {
"name": "Star Bucks",
"email": "stabucks@mail.com",
"website" : "www.starbucks.com",
"phone" : "7907653366",
"amenities_attributes": [
{
"name": "wifi",
"available": true
},
{
"name": "ac",
"available": true
}
],
"meeting_rooms_attributes": [
{
"count": 5,
"capacity": 6
}
],
"private_offices_attributes": [
{
"count": 2,
"capacity": 6
}
],
"desks_attributes": [
{
"count": 12,
"desk_type": "open_desk"
}
],
"operating_hours_attributes": [
{
"day": "Mon-Fri",
"slot": "10:00-5:00"
}
]
}
}
This is my Space booking request JSON
{
"booking": {
"space_id": 17,
"booking_items_attributes": [
{
"entity_type": "PrivateOffice",
"entity_id": 20,
"count": 1
},
{
"entity_type": "Desk",
"entity_id": 20,
"count": 2
}
],
"booking_dates_attributes": [
{
"from_date": "2020-03-18",
"to_date": "2020-03-18",
"from_time": "2020-03-18 10:00:00 ",
"to_time": "2020-03-18 14:00:00 "
}]
}
}
Booking Controller
class Api::V1::BookingsController < ApiController
before_action :fetch_space, only: [:show]
def index
bookings = Booking.all
render_collection(bookings, { name: 'bookings' }, each_serializer: BookingItemSerializer)
end
def create
if booking = current_user.bookings.create(booking_params)
render_object(booking, { name: 'booking' }, { serializer: BookingSerializer })
else
render_error(booking.errors.full_messages)
end
end
private
def booking_params
params.require(:booking).permit(:space_id,
booking_items_attributes: [:id, :entity_type, :entity_id, :count],
booking_dates_attributes: [:id, :from_date, :to_date, :from_time, :to_time])
end
end
Booking Serializer
class BookingSerializer < ActiveModel::Serializer
attributes :id, :user_id, :space_id, :status, :booked
# has_many :booking_items, serializer: BookingItemSerializer
def booked
object.booking_items.entity_type.sum(:count)
end
end
Booking Item Serializer
class BookingItemSerializer < ActiveModel::Serializer
attributes :id, :availabitity
belongs_to :booking, serializer: BookingItemSerializer
# def availabitity
# availabitity = object.meeting.sum(:count) - booked
# end
end
Aucun commentaire:
Enregistrer un commentaire