Created
April 24, 2019 13:30
-
-
Save Martin-Alexander/b8274b03cc99212d0cf8526284b5e31d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "csv" | |
require_relative "router" | |
require_relative "app/models/meal" | |
require_relative "app/controllers/meals_controller" | |
require_relative "app/repositories/meal_repository" | |
require_relative "app/views/meal_view" | |
require_relative "app/models/customer" | |
require_relative "app/controllers/customers_controller" | |
require_relative "app/repositories/customer_repository" | |
require_relative "app/views/customer_view" | |
require_relative "app/models/employee" | |
require_relative "app/controllers/sessions_controller" | |
require_relative "app/repositories/employee_repository" | |
require_relative "app/views/session_view" | |
meals_csv_path = "data/meals.csv" | |
meals_repo = MealRepository.new(meals_csv_path) | |
meals_controller = MealsController.new(meals_repo) | |
customers_csv_path = "data/customers.csv" | |
customers_repo = CustomerRepository.new(customers_csv_path) | |
customers_controller = CustomersController.new(customers_repo) | |
employee_csv_path = "data/employees.csv" | |
employee_repo = EmployeeRepository.new(employee_csv_path) | |
sessions_controller = SessionsController.new(employee_repo) | |
router = Router.new( | |
meals_controller, | |
customers_controller, | |
sessions_controller | |
) | |
router.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Customer | |
attr_reader :name, :address | |
attr_accessor :id | |
def initialize(attributes = {}) | |
@id = attributes[:id] | |
@name = attributes[:name] | |
@address = attributes[:address] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CustomerRepository | |
def initialize(csv_file_path) | |
@csv_file_path = csv_file_path | |
@customers = [] # An array of customer objects | |
csv_options = { headers: true, header_converters: :symbol } | |
if File.exist?(@csv_file_path) | |
CSV.foreach(@csv_file_path, csv_options) do |row| | |
# row = { id: "1", name: "Paul McCartney", address: "Liverpool" } | |
row[:id] = row[:id].to_i | |
@customers << Customer.new(row) | |
end | |
end | |
end | |
def all | |
@customers | |
end | |
def add(new_customer) | |
# set the id of the new customer | |
if @customers.length == 0 | |
new_id = 1 | |
else | |
new_id = @customers.last.id + 1 | |
end | |
new_customer.id = new_id | |
# push it into the array `@customers` | |
@customers << new_customer | |
# write to the csv | |
CSV.open(@csv_file_path, "w") do |csv_file| | |
csv_file << ["id", "name", "address"] | |
@customers.each do |customer| | |
csv_file << [customer.id, customer.name, customer.address] | |
end | |
end | |
end | |
def find(customer_id) | |
@customers.find do |customer| | |
customer.id == customer_id | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CustomerView | |
def display_customers(customers) | |
customers.each do |customer| | |
puts "#{customer.id} - #{customer.name} #{customer.address}" | |
end | |
end | |
def ask_for(message) | |
puts message | |
print "> " | |
user_input = gets.chomp | |
user_input | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id | name | address | |
---|---|---|---|
1 | Paul McCartney | Liverpool | |
2 | John Bonham | Redditch | |
3 | John Entwistle | Chiswick | |
4 | Martin | 123 Fake St. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative "../views/customer_view" | |
class CustomersController | |
def initialize(repository) | |
@repository = repository | |
@view = CustomerView.new | |
end | |
def list | |
customers = @repository.all | |
@view.display_customers(customers) | |
end | |
def add | |
customer_name = @view.ask_for("Name:") | |
customer_address = @view.ask_for("Address:") | |
customer_hash = { | |
name: customer_name, | |
address: customer_address | |
} | |
new_customer = Customer.new(customer_hash) | |
@repository.add(new_customer) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Employee | |
attr_reader :username, :role, :password | |
attr_accessor :id | |
def initialize(attributes = {}) | |
@id = attributes[:id] | |
@username = attributes[:username] | |
@password = attributes[:password] | |
@role = attributes[:role] | |
end | |
def manager? | |
@role == "manager" | |
end | |
def delivery_guy? | |
@role == "delivery_guy" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EmployeeRepository | |
def initialize(csv_file_path) | |
@employees = [] # Stores employee objects | |
if File.exist?(csv_file_path) | |
options = { headers: true, header_converters: :symbol } | |
CSV.foreach(csv_file_path, options) do |row| | |
row[:id] = row[:id].to_i | |
@employees << Employee.new(row) | |
end | |
end | |
end | |
def all_delivery_guys | |
@employees.select { |employee| employee.delivery_guy? } | |
end | |
def find(id) | |
@employees.find { |employee| employee.id == id } | |
end | |
def find_by_username(username) | |
@employees.find { |employee| employee.username == username } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id | username | password | role | |
---|---|---|---|---|
1 | paul | secret | manager | |
2 | john | secret | delivery_guy | |
3 | ringo | secret | delivery_guy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Meal | |
attr_reader :name, :price | |
attr_accessor :id | |
def initialize(attributes = {}) | |
@id = attributes[:id] | |
@name = attributes[:name] | |
@price = attributes[:price] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MealRepository | |
def initialize(csv_file_path) | |
@csv_file_path = csv_file_path | |
@meals = [] # An array of Meal objects | |
csv_options = { headers: true, header_converters: :symbol } | |
if File.exist?(@csv_file_path) | |
CSV.foreach(@csv_file_path, csv_options) do |row| | |
# row = { id: "1", name: "Margherita", price: "8" } | |
row[:id] = row[:id].to_i | |
row[:price] = row[:price].to_i | |
@meals << Meal.new(row) | |
end | |
end | |
end | |
def all | |
@meals | |
end | |
def add(new_meal) | |
# set the id of the new meal | |
if @meals.length == 0 | |
new_id = 1 | |
else | |
new_id = @meals.last.id + 1 | |
end | |
new_meal.id = new_id | |
# push it into the array `@meals` | |
@meals << new_meal | |
# write to the csv | |
CSV.open(@csv_file_path, "w") do |csv_file| | |
csv_file << ["id", "name", "price"] | |
@meals.each do |meal| | |
csv_file << [meal.id, meal.name, meal.price] | |
end | |
end | |
end | |
def find(meal_id) | |
@meals.find do |meal| | |
meal.id == meal_id | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MealView | |
def display_meals(meals) | |
meals.each do |meal| | |
puts "#{meal.id} - $#{meal.price} #{meal.name}" | |
end | |
end | |
def ask_for(message) | |
puts message | |
print "> " | |
user_input = gets.chomp | |
user_input | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id | name | price | |
---|---|---|---|
1 | Margherita | 8 | |
2 | Capricciosa | 11 | |
3 | Napolitana | 9 | |
4 | Funghi | 12 | |
5 | Calzone | 10 | |
6 | Pizza | 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative "../views/meal_view" | |
class MealsController | |
def initialize(repository) | |
@repository = repository | |
@view = MealView.new | |
end | |
def list | |
meals = @repository.all | |
@view.display_meals(meals) | |
end | |
def add | |
meal_name = @view.ask_for("Name:") | |
meal_price = @view.ask_for("Price:").to_i | |
meal_hash = { | |
name: meal_name, | |
price: meal_price | |
} | |
new_meal = Meal.new(meal_hash) | |
@repository.add(new_meal) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Router | |
def initialize(meals_controller, customers_controller, sessions_controller) | |
@meals_controller = meals_controller | |
@customers_controller = customers_controller | |
@sessions_controller = sessions_controller | |
end | |
def run | |
employee = @sessions_controller.login | |
if employee.manager? | |
start_manager_menu_loop | |
else | |
start_delivery_guy_menu_loop | |
end | |
end | |
private | |
def start_manager_menu_loop | |
loop do | |
puts "=== MAIN MENU ===" | |
puts "1 - All Meals" | |
puts "2 - New Meal" | |
puts "3 - All Customers" | |
puts "4 - New Customer" | |
puts "5 - Quit" | |
puts "Input" | |
print "> " | |
user_choice = gets.chomp.to_i | |
case user_choice | |
when 1 then @meals_controller.list | |
when 2 then @meals_controller.add | |
when 3 then @customers_controller.list | |
when 4 then @customers_controller.add | |
when 5 then break | |
end | |
end | |
end | |
def start_delivery_guy_menu_loop | |
loop do | |
puts "=== MAIN MENU ===" | |
puts "1 - My Undelivered Orders" | |
puts "2 - Mark Order as Delivered" | |
puts "3 - Quit" | |
puts "Input" | |
print "> " | |
user_choice = gets.chomp.to_i | |
case user_choice | |
when 1 then puts "TODO: undelivered orders" | |
when 2 then puts "TODO: mark as delivered" | |
when 3 then break | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SessionView | |
def ask_for(message) | |
puts message | |
user_input = gets.chomp | |
user_input | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SessionsController | |
def initialize(employee_repository) | |
@employee_repository = employee_repository | |
@view = SessionView.new | |
end | |
def login | |
username = @view.ask_for("Enter username:") | |
password = @view.ask_for("Enter password:") | |
employee = @employee_repository.find_by_username(username) | |
if employee != nil && employee.password == password | |
return employee | |
else | |
login | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment