Skip to content

Commit

Permalink
Merge pull request #19 from ab-noori/feature/add-new-course-form
Browse files Browse the repository at this point in the history
Creating the form for adding a new course 🚀
  • Loading branch information
ab-noori authored Oct 11, 2023
2 parents fb7a019 + 84bb0ca commit 6f15e73
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '3.1.4'
ruby '3.2.2'

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem 'rails', '~> 7.0.8'
Expand Down
35 changes: 29 additions & 6 deletions app/controllers/api/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,35 @@ class Api::CoursesController < ApplicationController

# GET /courses
def index
@courses = Course.all
@courses_with_images = Course.includes(:image_attachment).map do |course|
image_url = course.image.attached? ? url_for(course.image) : nil
{
id: course.id,
name: course.name,
description: course.description,
fee: course.fee,
startDate: course.startDate,
image_url:
}
end

render json: @courses
render json: @courses_with_images, location: api_courses_url
end

# GET /courses/1
def show
render json: @course
@course_with_image = course_with_image
render json: @course_with_image
end

# POST /courses
def create
@course = Course.new(course_params)

if @course.save
render json: @course, status: :created, location: @course
render json: @course, status: :created, location: api_course_url(@course)
else
render json: @course.errors, status: :unprocessable_entity
render json: { errors: @course.errors.full_messages }, status: :unprocessable_entity
end
end

Expand All @@ -42,7 +53,19 @@ def destroy

# Use callbacks to share common setup or constraints between actions.
def set_course
@course = Course.find(params[:id])
@course = Course.includes(:image_attachment).find(params[:id])
end

def course_with_image
image_url = @course.image.attached? ? url_for(@course.image) : nil
{
id: @course.id,
name: @course.name,
description: @course.description,
fee: @course.fee,
startDate: @course.startDate,
image_url:
}
end

# Only allow a list of trusted parameters through.
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/reservations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Api::ReservationsController < ApplicationController
# GET /reservations
def index
user_id = params[:user_id]
@reservations = Reservation.includes(:course).where(user_id: user_id)
@reservations = Reservation.includes(:course).where(user_id:)

render json: @reservations, include: :course
end
Expand Down
3 changes: 2 additions & 1 deletion app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ class Course < ApplicationRecord
has_many :reservations
has_many :users, through: :reservations

has_one_attached :image

validates :name, presence: true
validates :description, presence: true
validates :image, presence: true
validates :fee, presence: true, numericality: { greater_than: 0 }
validates :startDate, presence: true
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
def change
# Use Active Record's configured type for primary and foreign keys
primary_key_type, foreign_key_type = primary_and_foreign_key_types

create_table :active_storage_blobs, id: primary_key_type do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.string :service_name, null: false
t.bigint :byte_size, null: false
t.string :checksum

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

t.index [ :key ], unique: true
end

create_table :active_storage_attachments, id: primary_key_type do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type
t.references :blob, null: false, type: foreign_key_type

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end

create_table :active_storage_variant_records, id: primary_key_type do |t|
t.belongs_to :blob, null: false, index: false, type: foreign_key_type
t.string :variation_digest, null: false

t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end

private
def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end
32 changes: 31 additions & 1 deletion db/schema.rb

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

41 changes: 18 additions & 23 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: "Star Wars" }, { name: "Lord of the Rings" }])
# Character.create(name: "Luke", movie: movies.first)
# db/seeds.rb

# Create sample users
user1 = User.create(name: "John Doe")
user2 = User.create(name: "Jane Smith")

# Create an array of image URLs
# image_urls = ['/course/database.png', '/course/web.png']

# Create sample courses
course1 = Course.create(
name: "Course 1",
description: "Description for Course 1",
image: "course1.jpg",
fee: 100.0,
startDate: Date.new(2023, 10, 10)
)
course2 = Course.create(
name: "Course 2",
description: "Description for Course 2",
image: "course2.jpg",
fee: 150.0,
startDate: Date.new(2023, 11, 5)
)
# courses = []
# 6.times do |i|
# courses << Course.create(
# name: "Course #{i + 1}",
# description: "Description for Course #{i + 1}",
# image: image_urls[i % 2], # Alternating between the two images
# fee: 100.0 + (i * 10), # Incrementing fee
# startDate: Date.new(2023, 10, 10) + i.days # Incrementing start date
# )
# end
Course.create(name: 'course_1', description:'this is our first course', fee: 1200, startDate: "2023-04-04")

course = Course.first

# Create sample reservations associated with users and courses
Reservation.create(user_id: user1.id, course_id: course1.id, city: "New York", date: Date.new(2023, 10, 12))
Reservation.create(user_id: user2.id, course_id: course2.id, city: "Los Angeles", date: Date.new(2023, 11, 8))
Reservation.create(user_id: user1.id, course_id: course.id, city: "New York", date: Date.new(2023, 10, 12))
Reservation.create(user_id: user2.id, course_id: course.id, city: "Los Angeles", date: Date.new(2023, 11, 8))

0 comments on commit 6f15e73

Please sign in to comment.