diff --git a/Gemfile.lock b/Gemfile.lock index 9589226d..511c4a10 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,50 +1,50 @@ GEM remote: https://rubygems.org/ specs: - activemodel (6.0.3.1) - activesupport (= 6.0.3.1) - activerecord (6.0.3.1) - activemodel (= 6.0.3.1) - activesupport (= 6.0.3.1) - activesupport (6.0.3.1) + activemodel (6.1.3) + activesupport (= 6.1.3) + activerecord (6.1.3) + activemodel (= 6.1.3) + activesupport (= 6.1.3) + activesupport (6.1.3) concurrent-ruby (~> 1.0, >= 1.0.2) - i18n (>= 0.7, < 2) - minitest (~> 5.1) - tzinfo (~> 1.1) - zeitwerk (~> 2.2, >= 2.2.2) - coderay (1.1.1) - concurrent-ruby (1.1.6) - i18n (1.8.2) + i18n (>= 1.6, < 2) + minitest (>= 5.1) + tzinfo (~> 2.0) + zeitwerk (~> 2.3) + coderay (1.1.3) + concurrent-ruby (1.1.8) + i18n (1.8.9) concurrent-ruby (~> 1.0) - method_source (0.8.2) - minitest (5.14.1) - mustermann (1.0.3) - pry (0.10.4) - coderay (~> 1.1.0) - method_source (~> 0.8.1) - slop (~> 3.4) + method_source (1.0.0) + minitest (5.14.4) + mustermann (1.1.1) + ruby2_keywords (~> 0.0.1) + pry (0.14.0) + coderay (~> 1.1) + method_source (~> 1.0) rack (2.2.3) - rack-protection (2.0.7) + rack-protection (2.1.0) rack - require_all (1.3.3) - sinatra (2.0.7) + require_all (3.0.0) + ruby2_keywords (0.0.4) + sinatra (2.1.0) mustermann (~> 1.0) - rack (~> 2.0) - rack-protection (= 2.0.7) + rack (~> 2.2) + rack-protection (= 2.1.0) tilt (~> 2.0) - sinatra-activerecord (2.0.12) - activerecord (>= 3.2) + sinatra-activerecord (2.0.22) + activerecord (>= 4.1) sinatra (>= 1.0) - slop (3.6.0) - sqlite3 (1.3.13) - thread_safe (0.3.6) + sqlite3 (1.4.2) tilt (2.0.10) - tzinfo (1.2.7) - thread_safe (~> 0.1) - zeitwerk (2.3.0) + tzinfo (2.0.4) + concurrent-ruby (~> 1.0) + zeitwerk (2.4.2) PLATFORMS - ruby + x86_64-darwin-20 + x86_64-linux DEPENDENCIES pry @@ -53,4 +53,4 @@ DEPENDENCIES sqlite3 BUNDLED WITH - 1.14.6 + 2.2.11 diff --git a/Rakefile b/Rakefile index 508ef20e..89cb92bc 100644 --- a/Rakefile +++ b/Rakefile @@ -3,6 +3,14 @@ require 'sinatra/activerecord/rake' desc 'starts a console' task :console do - ActiveRecord::Base.logger = Logger.new(STDOUT) + #Enables logging in Pry console whenever ActiveRecord writes SQL for us + #ActiveRecord::Base.logger = Logger.new(STDOUT) + #Above outputs SQL + #Open Pry console, similar to binding.pry. Pry.start end + + +task :start do +Interface.welcome +end diff --git a/lib/.keep b/app/models/.keep similarity index 100% rename from lib/.keep rename to app/models/.keep diff --git a/app/models/interface.rb b/app/models/interface.rb new file mode 100644 index 00000000..39c93ba4 --- /dev/null +++ b/app/models/interface.rb @@ -0,0 +1,29 @@ +class Interface + + # attr_accessor :student + + def self.welcome + puts "Welcome to Full Stack Tutoring!" + login_or_register + end + + def self.login_or_register + puts "Login or Register" + answer = STDIN.gets.chomp + if answer == "login" + @student = Student.login + elsif answer == "register" + @student = Student.register + else + puts "We make all choices...that was a bad one." + end + if @student + Lesson.start(@student) + elsif + Interface.welcome + end + end + +end + + diff --git a/app/models/lesson.rb b/app/models/lesson.rb new file mode 100644 index 00000000..ba4e4a1e --- /dev/null +++ b/app/models/lesson.rb @@ -0,0 +1,99 @@ +class Lesson < ActiveRecord::Base + belongs_to :student + belongs_to :tutor + + def self.start(student) + puts "Main Menu" + puts "1. Schedule lesson" + puts "2. View past lessons" + puts "3. View scheduled lessons" + answer = STDIN.gets.chomp + if answer == "1" + schedule_lesson(student) + elsif answer == "2" + past_lessons(student) + elsif answer == "3" + scheduled_lessons(student) + else + puts "Try again" + end + end + + def self.schedule_lesson(student) + puts "What would you like to learn?" + #Puts giving list of subject options + answer = STDIN.gets.chomp + puts "When would you like to have your lesson?" + date = STDIN.gets.chomp + puts "Who would you like to learn with?" + tutors = Tutor.where(subject: answer) + + if tutors.exists? + tutors.each do |tutor| + puts "#{tutor.id}. #{tutor.name}" + end + end + + tutor_number = STDIN.gets.chomp + tutor = Tutor.find_by(id: tutor_number) + Lesson.create(topic: answer, date: date, student: student, tutor: tutor) + puts "Your lesson has been scheduled." + + Interface.welcome + end + + def self.past_lessons(student) + lessons = student.lessons.where("date < ?", Time.current) + if lessons.exists? + lessons.each do |lesson| + puts "#{student.username} had a #{lesson.topic} lesson on #{lesson.date}." + end + end + Interface.welcome + end + + def self.scheduled_lessons(student) + puts "Pick a lesson to reschedule or cancel." + lessons = student.lessons.where("date > ?", Time.current) + if lessons.exists? + lessons.each do |lesson| + puts "#{lesson.id}. #{student.username} has a #{lesson.topic} lesson on #{lesson.date}." # #{lesson.id}. with #{lesson.tutor.name} + end + lesson_id = STDIN.gets.chomp + lesson = Lesson.find_by(id: lesson_id) + puts "1. Reschedule" + puts "2. Cancel" + student_choice = STDIN.gets.chomp + if student_choice == "1" + puts "Pick a lesson to reschedule." + reschedule_lesson(lesson) + elsif student_choice == "2" + puts "Pick a lesson to cancel." + cancel_lesson(lesson) + end + end + end + + def self.reschedule_lesson(lesson) + puts "When would you like to reschedule your lesson?" + lesson_reschedule = STDIN.gets.chomp + lesson.update(date: lesson_reschedule) + puts "Your lesson has been rescheduled!" + Interface.welcome + end + + def self.cancel_lesson(lesson) + lesson.destroy + puts "Your lesson has been cancelled." + Interface.welcome + end + + def display_nicely + puts "#You had a #{topic} lesson on #{date} with #{tutor.name}" + end +end + + +# user_option = STDIN.gets.chomp +# if user_option == "yes" +# Lesson.start(@student) \ No newline at end of file diff --git a/app/models/student.rb b/app/models/student.rb new file mode 100644 index 00000000..a0d3bb79 --- /dev/null +++ b/app/models/student.rb @@ -0,0 +1,36 @@ + +class Student < ActiveRecord::Base + has_many :lessons + has_many :tutors, through: :lessons + + + def self.login + puts "Enter Username" + username = STDIN.gets.chomp + puts "Enter Password" + password = STDIN.gets.chomp + + Student.find_by(username: username, password: password) + #Search db for entered username and password + #find a way to check validity of username and password + end + + + + + def self.register + puts "Create Username" + username = STDIN.gets.chomp + puts "Create Password" + password = STDIN.gets.chomp + + Student.create(username: username, password: password) + # Allow new user to create username and password. + # Return to login method once user info is established. + end + + + +end + + diff --git a/app/models/tutor.rb b/app/models/tutor.rb new file mode 100644 index 00000000..d63a30e1 --- /dev/null +++ b/app/models/tutor.rb @@ -0,0 +1,14 @@ +class Tutor < ActiveRecord::Base + + has_many :lessons + has_many :students, through: :lessons + + + + # def initialize (name, subject) + # @name = name + # @subject = subject + # end + + +end diff --git a/config/environment.rb b/config/environment.rb index 4dbe13e5..e1cb24cc 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -1,5 +1,7 @@ require 'bundler' Bundler.require + ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'db/development.db') -require_all 'lib' +require_all 'app' + diff --git a/db/migrate/20210309224650_create_students.rb b/db/migrate/20210309224650_create_students.rb new file mode 100644 index 00000000..7d3a533b --- /dev/null +++ b/db/migrate/20210309224650_create_students.rb @@ -0,0 +1,11 @@ +class CreateStudents < ActiveRecord::Migration[6.1] + def change + create_table :students do |t| + t.string :name + t.string :subject + t.string :username + t.string :password + t.timestamps + end + end +end diff --git a/db/migrate/20210309224919_create_tutors.rb b/db/migrate/20210309224919_create_tutors.rb new file mode 100644 index 00000000..9961039b --- /dev/null +++ b/db/migrate/20210309224919_create_tutors.rb @@ -0,0 +1,9 @@ +class CreateTutors < ActiveRecord::Migration[6.1] + def change + create_table :tutors do |t| + t.string :name + t.string :subject + t.timestamps + end + end +end diff --git a/db/migrate/20210309225016_create_lessons.rb b/db/migrate/20210309225016_create_lessons.rb new file mode 100644 index 00000000..bcb8e345 --- /dev/null +++ b/db/migrate/20210309225016_create_lessons.rb @@ -0,0 +1,11 @@ +class CreateLessons < ActiveRecord::Migration[6.1] + def change + create_table :lessons do |t| + t.string :topic + t.string :date + t.references :student + t.references :tutor + t.timestamps + end + end +end diff --git a/db/migrate/20210310225540_change_date_on_lessons.rb b/db/migrate/20210310225540_change_date_on_lessons.rb new file mode 100644 index 00000000..d217d43c --- /dev/null +++ b/db/migrate/20210310225540_change_date_on_lessons.rb @@ -0,0 +1,5 @@ +class ChangeDateOnLessons < ActiveRecord::Migration[6.1] + def change + change_column :lessons, :date, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 00000000..a308bf4e --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,42 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 2021_03_10_225540) do + + create_table "lessons", force: :cascade do |t| + t.string "topic" + t.datetime "date" + t.integer "student_id" + t.integer "tutor_id" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["student_id"], name: "index_lessons_on_student_id" + t.index ["tutor_id"], name: "index_lessons_on_tutor_id" + end + + create_table "students", force: :cascade do |t| + t.string "name" + t.string "subject" + t.string "username" + t.string "password" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + end + + create_table "tutors", force: :cascade do |t| + t.string "name" + t.string "subject" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + end + +end diff --git a/db/seeds.rb b/db/seeds.rb new file mode 100644 index 00000000..4245f6cc --- /dev/null +++ b/db/seeds.rb @@ -0,0 +1,27 @@ +require 'pry' + +require_all + +Student.destroy_all +Tutor.destroy_all +Lesson.destroy_all + + +barak = Student.create(username: "Barak", password: "123") +nyasha = Student.create(username: "Nyasha", password: "1234") + +eric = Tutor.create(name: "Eric", subject: "Math") +sean = Tutor.create(name: "Sean", subject: "English") +toni = Tutor.create(name: "Toni", subject: "Math") +justin = Tutor.create(name: "Justin", subject: "Science") + +math = Lesson.create(topic: "Math", date: "08/08/2021 9:00am", student: barak, tutor: eric ) +english = Lesson.create(topic: "English", date: "08/08/2021 10:00am", student: nyasha, tutor: sean ) +science = Lesson.create(topic: "Science", date: "22/02/2021 11:00am", student: barak, tutor: toni ) +math = Lesson.create(topic: "Math", date: "22/02/2021 8:00am", student: nyasha, tutor: justin ) + +# nyasha = Student.new(name = "Nyasha", subject = "Math") +# barak = Tutor.new(name = "Barak", subject = "Math") +# lesson = Lesson.new(topic = "math", date = "Monday", student_id = 1, tutor_id = 1) + +