My Booking App

Source: (local) http://localhost/works/laravel/mybooking/public/
(from) https://github.com/prehire/php-23093023909ad09a09

TASK:
NOTE: I do this without using because it’s heavier for my laptop. I can use vagrant later but need to set some configuration. Please read Laravel Homestead VirtualBox Vagrant.

  1. Copy the available local ‘php-23093023909ad09a09’ directory then paste to be ‘mybooking’. If you dont have the source code of ‘php-23093023909ad09a09’, then clone from https://github.com/prehire/php-23093023909ad09a09 then rename it to ‘mybooking’
  2. Go to the directory ‘mybooking’ then use composer to install the dependency packages.
  3. Because I want to use SQLite, so modify .env to use SQLite

    Also modify /config/database.php to make sure these settings:

    Also make sure the sqlite database ‘homework_database.sqlite’ is in /database/ directory!
  4. Set the files permission for these directories:

     
  5. Test the app for the first time http://localhost/works/laravel/mybooking/public/. Here is how it looksmybooking-front-firstIt only shows Homework (http://localhost/works/laravel/mybooking/public/), Booking (http://localhost/works/laravel/mybooking/public/booking), Customer (http://localhost/works/laravel/mybooking/public/customer) and Cleaner (http://localhost/works/laravel/mybooking/public/cleaner).
  6. OK. It’s time for the task description!
    Summary
    We are a local home cleaning company called Homework. Today we are keeping track of all our bookings using a spreadsheet and we want to move it into a web application. Our main goal with this project is to make it possible for customers to schedule a booking online. I’ve already created the scaffolding of the app for you and now I need you to add some functionality to it. This should take you about 1-2 hours, but you have up to 4 hours to complete it.
    Deliverables
    1. All models and columns should have validation as described in the model spec below, plus any common-sense validation you’ll put on new models.
    2. We need to split the site in to Admin-only and Customer-only. Right now all the admin functionality is exposed to the world.
    3. Currently we operate in 10 cities, but plan to expand quickly. We need a way to admin the list of cities we operate in and the ability to add to the list. You should create a new table to do this.
    4. On the admin cleaner form we need a way to select the cities a cleaner works in. This should be a checkbox list populated by the list of the cities we operate in. You may need to create a new table to store this data.
    5. We need a way for customers to signup and schedule a booking all on one form. To accomplish this you will need to do the following:
    – Make the site root a customer-facing form designed for customers to sign up and book a cleaner.
    – On this form, capture all the data needed to create a customer in the database (first name, last name, phone number).
    – If the customer already exists in the database (use phone number to determine this) use the existing record instead of creating a new one. You should probably add a validation to enforce this.
    – Let the customer select what city they are in from the cities table created earlier.
    – Let the customer specify a date and time when they would like their house cleaned.
    – When the user submits the form, look for cleaners that work in the specified city that do not have any bookings that conflict with the time specified.
    – If you can find an available cleaner, create the booking and display the name of the cleaner assigned to the booking.
    – If you can’t find an available cleaner, tell the user that we could not fulfill their request.
    6. Write tests for the parts of the application you feel need it the most.
    7. When you are done, please zip up the entire root directory including your SQLite database and vendor files — do not cherry-pick folders.
    Restrictions
    1. Do NOT switch the database from SQLite to MySQL.
    2. If you create a password-protected account, use credentials “admin@admin.com” and password “admin”.Existing Models
    1. customer
    – first_name (required)
    – last_name (required)
    – phone_number (optional)
    2. booking
    – customer (required, enforce referential integrity)
    – cleaner (required, enforce referential integrity)
    – date (required)
    3. cleaner
    – first_name (required)
    – last_name (required)
    – quality_score (required, must be a number between 0.0 and 5.0)
    Setup
    https://github.com/prehire/php-23093023909ad09a09Summary
    The following deliverables are not required but are nice-to-haves. If you choose to implement them, it should take around 1 hour but you must complete this section within 3 hours. You can complete any number of the bonus deliverables: you don’t need to complete all of them.Bonus Deliverables
    1. Use a free theme to make the application look a little better, especially the pages seen by our customers.
    2. Add security to the admin pages on our site. Customers and cleaners do not need to be able to login at the moment.
    3. The cleaner form should have one additional field: ’email’
    o Email should be required
    0 Check that it is a valid email address
    4. When a new booking is created, email the cleaner with information on the booking and customer.
    5. The Customer Show view should show all associated bookings below the form and link to them.
    6. The Cleaner Show view should show all associated bookings below the form and link to them.
    7. When you are done, please zip up the whole app directory with dependencies and upload it below.
  7. Step by step the task completion!
    “2. We need to split the site in to Admin-only and Customer-only. Right now all the admin functionality is exposed to the world.”. For the Admin-only page, It’d need a login. But it not needed for Customer-only page! So we’re going to show the homepage for customer and also the login link for the admin!
    Use ‘Auth’ to create the login/register scaffolding

    NOTE: This will modify ‘app.blade.php’ in /resources/views/layouts/ directory and would mess the web looks. But no worry. We’d fix this later!
    Then modify ‘web.php’ in /routes/ like this:

    Then create ‘HomeController’ in ‘Home’ directory:

    You’ll see ‘HomeController.php’ in /app/Http/Controllers/Home/ directory!
    NOTE: You can remove ‘HomeController.php’ file in /app/Http/Controllers/ directory created by Auth scaffoldiing!
    Then add a new function ‘index’ in the new controller. Here is the controller content look

    Then create a new directory ‘home’ in /resources/views/ and a new blade template ‘index.blade.php’ in the new directory. Here is the temporary content of the file

    I also need to modify ‘app.blade.php’ in /layouts/ directory so we can fix the web layout and the login (and register) link can be showed up. We also need to add some bootstrap links for the CSS and Javascript/JQuery. Here is the modification in app.blade.php

    OK. Here is the homepage looksmybooking-front-2Then try to add a new user (customer) by clicking the register link (http://localhost/works/laravel/mybooking/public/register). Then add a new user with Name: ‘Admin’, email: ‘advcha@admin.com’ and password: ‘admin123’. This is a ‘fake admin’ user!. Not actual admin user. We’re going to add the real admin later. (NOTE: I think also I need to differentiate the login/register page for the customer and cleaner. May be add an option if the user want to be a customer or a cleaner!)
    Now we can login with the name. But where is the links to Booking, Customer and Cleaner? Here is how to show them up. Modify ‘app.blade.php’ again, then put the links like this:

    Here is the looksmybooking-front-3Okay. but the links still can be clicked properly. We need to add some routes in /routes/web.app like this:

    So create a new directory ‘Admin’ in /app/Http/Controllers/ then move BookingController.php, CleanerController.php and CustomerController.php into the new directory.
    Then (don’t forget) to modify the namespace in those three files to

    Now we can put the routes for the links in ‘app.blade.php’ like this:

    Now we’ve completed the links! You can see the working urls like Booking (http://localhost/works/laravel/mybooking/public/admin/booking), Customer (http://localhost/works/laravel/mybooking/public/admin/customer) and Cleaner (http://localhost/works/laravel/mybooking/public/admin/cleaner). Here is the example for the admin booking page.mybooking-admin-bookingBut there is another thing we need to pay attention for the above links. If we’re logged out, the above links and the urls should not be displayed and opened. Here we need to employ the middleware auth for each the controllers files (BookingController.php, CleanerController.php and CustomerController.php) in ‘__construct’ function like this:

    Now if we open the url (for example:  http://localhost/works/laravel/mybooking/public/admin/customer) without login first, we’d be redirected to the login page!
    <IF YOU HAVE MORE TIME, PLS DO THIS. BUT IT’D TAKE MANY WORKS AND TIME!!!>
    To make the view structures tidier, we need to create a new directory ‘admin’ in /resources/views/ then move these directories ‘booking’, ‘customer’ and ‘cleaner’ into it. Then don’t forget to modify the ‘index’ function in our three controller files to refer to the new location:

    Also do the same thing for others functions like ‘create’, ‘edit’ and ‘show’. Dont forget to modify also almost all the blade template for them!!! for example: /admin/customer/index.blade.php, modify the urls like url(‘/customer/create’) to url(‘/admin/customer/create’) and the others same urls in the file!!!
    </IF YOU HAVE MORE TIME, PLS DO THIS. BUT IT’D TAKE MANY WORKS AND TIME!!!>
    CRUD
    To make CRUD works for them, I need to modify also some functions in the controller files to redirect to the correct route. For example for CustomerController.php, modify the redirect to ‘admin/customer’ for ‘store’, ‘update’ and ‘destroy’ functions. Do the same thing for Booking and Cleaner controllers! Also modify the blade templates!
    Here is the look for the admin customer (http://localhost/works/laravel/mybooking/public/admin/customer)
    mybooking-admin-customerI think I’ve completed the Admin page. NO!! WAIT!! How about the Admin page for Booking??? The Booking form need CustomerId, CleanerId and Date. On the create form, currently the Ids need to be typed manually, I want to change it to a select box to avoid any error like undefined Id. OK. First, modify ‘BookingController.php’ in /app/Http/Controllers/Admin/ to retrieve the customers and cleaners data

    Then modify ‘create.blade.php’ in /resources/views/admin/booking/ to display the selection of the customers and the cleaners

    mybooking-booking-admin-createWe need also change the booking table in /resources/views/admin/booking/index.blade.php to show the names instead of the ids. First, modify the model ‘Booking.php’ in /app/ directory to add relation with customer and cleaner models

    Then modify ‘index’ function in /app/Http/Controllers/Admin/BookingController.php to retrieve also the customer and cleaner data

    Now in /resources/views/admin/booking/index.blade.php, we can show the names instead of the ids

    Here is the looksmybooking-booking-admin-indexFor the booking edit function, we only need to do a small changes. In /app/Http/Controllers/Admin/BookingController.php, modify ‘edit’ function to pass also the customer and the cleaner data

    Then modify /resources/views/admin/booking/edit.blade.php to display the customer and the cleaner options

    NOTE: If any customer or the cleaner is not exists (may be because it’s already deleted) in the selection, the selection SHOWS THE FIRST OPTION NOT NULL. IT CAN BE MISLEADING AS IF THE FIRST OPTION IS SELECTED. HOW TO HANDLE THIS???
    Now I’m going to work on the Customer page (homepage). The current homepage (http://localhost/works/laravel/mybooking/public/home) content still show the ugly text ‘Hello My Customer!’. Please modify the home template ‘app.blade.php’ in /resources/views/layouts/ to make it tidier. especially in the body tag, add class ‘container’ to wrap the content:

    Then add a form and a few text input in /resources/views/home/index.blade.php. So it’ll be like this:

    mybooking-book-frontThe layout looks same with the customer form for adding a new one. But this moment leave it as is. We’re going to change it later with some new features. For the front end booking, it’ll go to ‘/home/book/’ url to save the inputs. We need to add this to our route in /routes/web.php

    Then modify /app/Http/Controllers/Home/HomeController.php to add a new function ‘book’ to store the data into the customer table.

    The ‘book’ function is pretty same with ‘store’ function in CustomerController.php file.
    OK. The Next task: “3. Currently we operate in 10 cities, but plan to expand quickly. We need a way to admin the list of cities we operate in and the ability to add to the list. You should create a new table to do this.”
    First we need to create a new model ‘City’. Use artisan to do this job

    A new file ‘City.php’ would be in /app/ directory. Here is the content

    I’m going to add some variables into the file to represent the table name, the primary key and so on. So it’d be like this:

    Then we need to create a new table ‘cities’ via ‘migration’ command

    NOTE: If you get an error like this

    it means you need to set the file permission of ‘storage’ directory to 777 in order to let the migration command to create a new file

    Here is the migration file created (2017_01_14_044005_create_cities_table.php in /database/migrations/)

    In ‘up’ function, I need to add a new field ‘city_name’ with ‘string’ type. So it’d be like this

    Then do create the new table ‘cities’ via ‘migrate’ command

    NOTE: I got a confusing error the first time when tried to execute ‘migrate’ command. Here is the error

    I JUST DIDN’T REALIZE MY PHP VERSION VIA TERMINAL IS VERSION 7.0! SO I USE ‘php5.6’ INSTEAD AN IT’S WORKS! IT SEEMS I HAVEN’T CONFIGURED THE PDO FOR SQLITE ON PHP 7.0.
    NOTE: ‘migration’ command ONLY RUN ON THE NEW FILE. IT’D NOT RUN THE FILES ALREADY MIGRATED!
    Now create a new controller ‘CityController’ in ‘Admin’ directory

    The new file ‘CityController.php’ would be in /app/Http/Controllers/Admin/ directory!. Here is the content

    Here we want some functions/methods for CRUD the city data. The function much similar within the others controllers like Cleaner. Here it is

    Then modify our routes in /routes/web.php for this ‘city’

    The last step is to create the blades templates for this ‘city’. Create a new directory ‘city’ in /resources/views/admin/. Then create some files ‘index.blade.php’, ‘create.blade.php’, ‘edit.blade.php’ and ‘show.blade.php’. The content of the files are pretty similar with Cleaner! Just modify some details like change ‘cleaner’ to ‘city’ and the others things. Also don’t forget to add ‘City’ menu in /resources/views/layouts/app.blade.php

    http://localhost/works/laravel/mybooking/public/admin/citymybooking-city-admin2The next task: “4. On the admin cleaner form we need a way to select the cities a cleaner works in. This should be a checkbox list populated by the list of the cities we operate in. You may need to create a new table to store this data.”.
    Here I need to create a new table ‘cleaner_cities’. This new table would store the cleaner id and the city id values. Then I also need to modify the form input of the cleaner to include the cities they can work in (they can select more than one city). Then store the selection in the new table ‘cleaner_cities’. Same solution with the above, we need to create a new model ‘CleanerCity’. Use artisan to do this job

    Then modify /app/CleanerCity.php file to add a few variables like cleaner_id and city_id. So it’d be like this:

    Then we need to create a new table ‘cleaner_cities’ via ‘migration’ command

    Then modify ‘up’ function in the migration file created (2017_01_14_070618_create_cleaner_cities_table.php in /database/migrations/)

    Use ‘migrate’ command to create the new table

    Then modify the model ‘Cleaner.php’ to add ‘City’ and ‘CleanerCity’ model

    Then modify ‘CleanerController.php’ in /app/Https/Controllers/Admin/ directory. For example at ‘create’ function, we need to show up the cities checkboxes. So pass the ‘city’ data in the function

    Then modify ‘create.blade.php’ in /resources/views/admin/cleaner/ directory to display the checboxes

    Here I need to add a class ‘chk_city’ to manage the checkboxes display (side by side). I need to create a new css file ‘style.css’ in /public/css/ directory. Here is the css content

    Also I need to add the css link in /resources/views/layouts/app.blade.php file

    OK. Here is the looks (http://localhost/works/laravel/mybooking/public/admin/cleaner/create)mybooking-cleaner-admin-createOK. To make sure the create cleaner function work properly, modify ‘store’ function in /app/Http/Controllers/Admin/CleanerController.php like this:

    So this function also check the input if already exist or not then also insert the cleaner_id and city_id into ‘cleaner_cities’ table!
    We can do the same thing for ‘edit’ and ‘update’ function. Modify ‘edit’ and ‘update’ function in /app/Http/Controllers/Admin/CleanerController.php like this

    The ‘update’ function is more complicated. If there is no city selected, remove all the cities if exist in the cleaner_cities related to the cleaner_id. If some cities selected and same within the table, just leave it. But if different cities are selected, add the new cities and remove the unselected ones.
    Then modify also /resources/views/admin/cleaner/edit.blade.php to show the selected/unselected cities

    Now, we want to display the cities also on the cleaner table. So modify ‘index’ function in /app/Http/Controllers/Admin/CleanerController.php like this:

    Then modify /resources/views/admin/cleaner/index.blade.php to display the cities

    Here is the looks:mybooking-cleaner-admin-indexThe last thing for the ‘show’ function. Modify the function to get the cities then pass it to the view

    Then modify /resources/views/admin/cleaner/show.blade.php, to display the cities

    COMPLETED!
    The next task: “5. We need a way for customers to signup and schedule a booking all on one form. To accomplish this you will need to do the following:
    – Make the site root a customer-facing form designed for customers to sign up and book a cleaner.
    – On this form, capture all the data needed to create a customer in the database (first name, last name, phone number).”
    We already have it. Please look http://localhost/works/laravel/mybooking/public/home. Also look the two routes for this in /routes/web.php

    Look the function ‘index’ and ‘book’ (We’ll get it done at the next task) in /app/Http/Controllers/Home/HomeController.php.
    “- If the customer already exists in the database (use phone number to determine this) use the existing record instead of creating a new one. You should probably add a validation to enforce this.”
    Modify /app/Http/Controllers/Home/HomeController.php at ‘book’ function like this:

    Here we add a check and validation if a phone number entered is already exist in the customer table. If so, please give a message about it. Here is the lookmybooking-front-customer
    “- Let the customer select what city they are in from the cities table created earlier.”
    Modify /app/Http/Controllers/Home/HomeController.php to add the city data and pass it to the view

    Then show the city selection in /resources/views/home/index.blade.php file

    Here is the new looks:mybooking-front-customer-cityWe’ll do the rest after working on the below task!
    “- Let the customer specify a date and time when they would like their house cleaned.”
    Put like this to show the datetime inputs in /resources/views/home/index.blade.php file

    I added two datetime input here: Cleaning Time (From) and Cleaning Time (To). I can’t use ‘Form::date’ here because I need ‘Datetime’ not just ‘Date’. I have to use datetimepicker from bootstrap. To make sure the dropdown of the datetime work properly, I have to add/import ‘bootstrap-datetimepicker’ libraries in /resources/views/layouts/app.blade.php file

    Here is the looksmybooking-front-datetimeThen I need to create a new model ‘CityCleaningTime’ to store the city id and the cleanings time.

    Then modify ‘CityCleaningTime.php’ file

    Then do migration to create the migration file

    Then do migrate to create the new table ‘citycleaningtimes’.

    NOTE: I did some mistakes about the migration file and the migrate. First I forgot to put ‘customer_id’ in the migration file. I realized it after I created the new table. I updated the migration file and did the migrate but I got a message ‘Nothing to migrate.’  Because I was in a hurry, I deleted the migration file so I thought I can create a new one. But when I wanted to create the new migration file, I got this error:

    To solve this problem, I have to type this ‘composer dump-autoload’:

    Then did the migration again. After fixing the file, I wanted to create the table again. But, of course, the same table is already exists!

    I tried to fix this mistake by doing the ‘migrate:rollback’

    But I got another error

    Then I created again (manually) the old migration file (copy and paste the new migration file then rename it!). I did the rollback again. but here is another error:

    After some thought, I have to remove (or move it to another directory) the new migration file and let the old one (the above name!). Then did the rollback again. SUCCESS! Fixed the old migration file by adding ‘customer_id’ then did the migrate again. SUCCESS!
    OK. HERE IS THE IMPORTANT LESSON, IF I FOUND AN ERROR/NOT COMPLETED IN MY LAST MIGRATE/MIGRATION FILE, DO THE ROLLBACK IF THE TABLE IS ALREADY CREATED! DONT REMOVE THE MIGRATION FILE!.
    “- When the user submits the form, look for cleaners that work in the specified city that do not have any bookings that conflict with the time specified.
    – If you can find an available cleaner, create the booking and display the name of the cleaner assigned to the booking.
    – If you can’t find an available cleaner, tell the user that we could not fulfill their request.”
  8. First, add a free theme like bootstrap or materialize css.

Leave a Reply

Your email address will not be published. Required fields are marked *