Created
July 28, 2021 08:59
-
-
Save arthur-littm/4f880e520ef7d5318145b14ab60bbbbd 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_relative "app/repositories/meal_repository" | |
require_relative "app/controllers/meals_controller" | |
require_relative "app/controllers/sessions_controller" | |
require_relative "app/repositories/customer_repository" | |
require_relative "app/repositories/employee_repository" | |
require_relative "app/controllers/customers_controller" | |
require_relative "router" | |
MEALS_CSV_FILE = File.join(__dir__, "data/meals.csv") | |
CUSTOMERS_CSV_FILE = File.join(__dir__, "data/customers.csv") | |
EMPLOYEES_CSV_FILE = File.join(__dir__, "data/employees.csv") | |
meal_repository = MealRepository.new(MEALS_CSV_FILE) | |
meals_controller = MealsController.new(meal_repository) | |
customer_repository = CustomerRepository.new(CUSTOMERS_CSV_FILE) | |
customers_controller = CustomersController.new(customer_repository) | |
employee_repository = EmployeeRepository.new(EMPLOYEES_CSV_FILE) | |
sessions_controller = SessionsController.new(employee_repository) | |
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_accessor :id | |
attr_reader :name, :address | |
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
require "csv" | |
require_relative "../models/customer" | |
class CustomerRepository | |
def initialize(csv_file) | |
@csv_file = csv_file | |
@customers = [] | |
@next_id = 1 | |
load_csv if File.exist?(@csv_file) | |
end | |
def create(customer) | |
customer.id = @next_id | |
@customers << customer | |
@next_id += 1 | |
save_csv | |
end | |
def all | |
@customers | |
end | |
def find(id) | |
@customers.find { |customer| customer.id == id } | |
end | |
private | |
def save_csv | |
CSV.open(@csv_file, "wb") do |csv| | |
csv << %w[id name address] | |
@customers.each do |customer| | |
csv << [customer.id, customer.name, customer.address] | |
end | |
end | |
end | |
def load_csv | |
csv_options = { headers: :first_row, header_converters: :symbol } | |
CSV.foreach(@csv_file, csv_options) do |row| | |
row[:id] = row[:id].to_i | |
@customers << Customer.new(row) | |
end | |
@next_id = @customers.last.id + 1 unless @customers.empty? | |
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
require_relative "../views/customers_view" | |
require_relative "../models/customer" | |
class CustomersController | |
def initialize(customer_repository) | |
@customer_repository = customer_repository | |
@customers_view = CustomersView.new | |
end | |
def add | |
name = @customers_view.ask_user_for(:name) | |
address = @customers_view.ask_user_for(:address) | |
customer = Customer.new(name: name, address: address) | |
@customer_repository.create(customer) | |
display_customers | |
end | |
def list | |
display_customers | |
end | |
private | |
def display_customers | |
customers = @customer_repository.all | |
@customers_view.display(customers) | |
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 CustomersView | |
def display(customers) | |
customers.each_with_index do |customer, index| | |
puts "#{index + 1}. #{customer.name} : #{customer.address}" | |
end | |
end | |
def ask_user_for(stuff) | |
puts "#{stuff.capitalize}?" | |
print "> " | |
return gets.chomp | |
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 :id, :username, :password, :role | |
# data | |
def initialize(attributes = {}) | |
# - id | |
@id = attributes[:id] | |
# - username | |
@username = attributes[:username] | |
# - password | |
@password = attributes[:password] | |
# - role ('manager' / 'rider') | |
@role = attributes[:role] | |
end | |
def manager? | |
@role == 'manager' | |
end | |
def rider? | |
@role == 'rider' | |
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
require "csv" | |
require_relative "../models/employee" | |
class EmployeeRepository | |
def initialize(csv_file) | |
@csv_file = csv_file | |
@employees = [] | |
@next_id = 1 | |
load_csv if File.exist?(@csv_file) | |
end | |
def all | |
@employees | |
end | |
def find(id) | |
@employees.find { |employee| employee.id == id } | |
end | |
def find_by_username(username) | |
@employees.find { |employee| employee.username == username } | |
end | |
private | |
def load_csv | |
csv_options = { headers: :first_row, header_converters: :symbol } | |
CSV.foreach(@csv_file, csv_options) do |row| | |
row[:id] = row[:id].to_i | |
@employees << Employee.new(row) | |
end | |
@next_id = @employees.last.id + 1 unless @employees.empty? | |
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 EmployeeView | |
# TODO | |
# def ask_for_username | |
# puts "Whats your username?" | |
# return gets.chomp | |
# end | |
# def ask_for_password | |
# puts "Whats your password?" | |
# return gets.chomp | |
# end | |
def ask_for(something) | |
puts "Whats your #{something}?" | |
return gets.chomp | |
end | |
def correct_credentials | |
puts "Correct credentials, good morning!" | |
end | |
def wrong_credentials | |
puts "Wrong credentials, we're being hacked!! 💻" | |
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 Meal | |
attr_accessor :id | |
attr_reader :name, :price | |
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
require "csv" | |
require_relative "../models/meal" | |
class MealRepository | |
def initialize(csv_file) | |
@csv_file = csv_file | |
@meals = [] | |
@next_id = 1 | |
load_csv if File.exist?(@csv_file) | |
end | |
def create(meal) | |
meal.id = @next_id | |
@meals << meal | |
@next_id += 1 | |
save_csv | |
end | |
def all | |
@meals | |
end | |
def find(id) | |
@meals.find { |meal| meal.id == id } | |
end | |
private | |
def save_csv | |
CSV.open(@csv_file, "wb") do |csv| | |
csv << %w[id name price] | |
@meals.each do |meal| | |
csv << [meal.id, meal.name, meal.price] | |
end | |
end | |
end | |
def load_csv | |
csv_options = { headers: :first_row, header_converters: :symbol } | |
CSV.foreach(@csv_file, csv_options) do |row| | |
row[:id] = row[:id].to_i | |
row[:price] = row[:price].to_i | |
@meals << Meal.new(row) | |
end | |
@next_id = @meals.last.id + 1 unless @meals.empty? | |
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
require_relative "../views/meals_view" | |
require_relative "../models/meal" | |
class MealsController | |
def initialize(meal_repository) | |
@meal_repository = meal_repository | |
@meals_view = MealsView.new | |
end | |
def add | |
name = @meals_view.ask_user_for(:name) | |
price = @meals_view.ask_user_for(:price).to_i | |
meal = Meal.new(name: name, price: price) | |
@meal_repository.create(meal) | |
display_meals | |
end | |
def list | |
display_meals | |
end | |
private | |
def display_meals | |
meals = @meal_repository.all | |
@meals_view.display(meals) | |
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 MealsView | |
def display(meals) | |
meals.each_with_index do |meal, index| | |
puts "#{index + 1}. #{meal.name} : #{meal.price}€" | |
end | |
end | |
def ask_user_for(stuff) | |
puts "#{stuff.capitalize}?" | |
print "> " | |
return gets.chomp | |
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 | |
@running = true | |
end | |
def run | |
employee = @sessions_controller.sign_in | |
while @running | |
# login | |
# if rider print rider menu | |
# if manager | |
if employee.manager? | |
print_menu | |
choice = gets.chomp.to_i | |
print `clear` | |
route_action(choice) | |
else | |
print_menu_rider | |
choice = gets.chomp.to_i | |
print `clear` | |
route_action_rider(choice) | |
end | |
end | |
end | |
private | |
def print_menu | |
puts "--------------------" | |
puts "------- MENU -------" | |
puts "--------------------" | |
puts "1. Add new meal" | |
puts "2. List all meals" | |
puts "3. Add new customer" | |
puts "4. List all customers" | |
puts "8. Exit" | |
print "> " | |
end | |
def route_action(choice) | |
case choice | |
when 1 then @meals_controller.add | |
when 2 then @meals_controller.list | |
when 3 then @customers_controller.add | |
when 4 then @customers_controller.list | |
when 8 then stop! | |
else puts "Try again..." | |
end | |
end | |
def print_menu_rider | |
puts "--------------------" | |
puts "--- MENU RIDER -----" | |
puts "--------------------" | |
puts "1. TODO" | |
end | |
def route_action_rider(choice) | |
case choice | |
when 1 then "TODO" | |
when 8 then stop! | |
else puts "Try again..." | |
end | |
end | |
def stop! | |
@running = false | |
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
require_relative '../views/employee_view' | |
class SessionsController | |
def initialize(employee_repository) | |
@employee_repository = employee_repository | |
@employee_view = EmployeeView.new | |
end | |
def sign_in | |
# 1. ask the employee for username | |
username = @employee_view.ask_for('username') | |
# 2. ask the employee for password | |
password = @employee_view.ask_for('password') | |
# 3. check that the employee username exists | |
employee = @employee_repository.find_by_username(username) | |
# employee is an instance of Employee model | |
if employee && employee.password == password | |
# correct credentials | |
@employee_view.correct_credentials | |
return employee | |
else | |
@employee_view.wrong_credentials | |
sign_in | |
# wrong username or password!! | |
end | |
# 4. if exists check that employee password matches | |
# if not show wrong credentials message and ask again | |
# if yes great! | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment