diff --git a/README.md b/README.md new file mode 100644 index 0000000..b720ee5 --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# Ad Lister Project + +* FYI Credentials are saved in a gitignored .env.php file. * + +The Ad Lister project is a modified craigslist clone that will help test and solidify your understanding of the HTML, CSS, JavaScript, and PHP concepts you have learned so far in this course. You will be working on this project in teams of two to three. Teams of two are preferred unless there are an odd number of students, forcing one team of three. + +This project is meant to be a challenge. Working through tough challenges that require lots of coding will help take your skills and confidence to the next level. + +Here are the primary goals of the project: + +Gain experience in building a well-designed in PHP site without the use of a framework. +Learn how to use Git in a team environment. +Test your resourcefulness in solving problems. + +# Getting Started +1. Clone this repo into `~/vagrant-lamp/sites/` so that it creates adlister.dev/ there. +2. Setup a host for adlister.dev +3. Create your `.env.php` file +4. Create a new database +5. Run `/database/migrations.php` in order to create tables. +6. Run `/database/seeder.php` in order to seed the tables with test data. + +# $_ENV setup +1. Open env-template.php to see the fields you should use to create your .env.php file. +2. Specify your environmental variables and credentials within your .env.php file. +3. So that you DO NOT commit passwords, double check that .env.php is added to .gitignore. diff --git a/adlister_login.php b/adlister_login.php deleted file mode 100644 index 973af47..0000000 --- a/adlister_login.php +++ /dev/null @@ -1,6 +0,0 @@ - '50cc Hyamadahizuki Motorized Cycle', + 'price' => '$500', + 'description' => 'Tear up the road with this great road-hog!', + 'location' => 'San Antonio, TX', + 'date_posted' => '2015-01-01', + 'user_id' => '1', + ], + [ + 'item' => '', + 'price' => '', + 'description' => '', + 'location' => '', + 'date_posted' => '', + 'user_id' => '', + ], + [ + 'item' => '', + 'price' => '', + 'description' => '', + 'location' => '', + 'date_posted' => '', + 'user_id' => '', + ], + [ + 'item' => '', + 'price' => '', + 'description' => '', + 'location' => '', + 'date_posted' => '', + 'user_id' => '', + ], + [ + 'item' => '', + 'price' => '', + 'description' => '', + 'location' => '', + 'date_posted' => '', + 'user_id' => '', + ], + [ + 'item' => '', + 'price' => '', + 'description' => '', + 'location' => '', + 'date_posted' => '', + 'user_id' => '', + ], + [ + 'item' => '', + 'price' => '', + 'description' => '', + 'location' => '', + 'date_posted' => '', + 'user_id' => '', + ], + [ + 'item' => '', + 'price' => '', + 'description' => '', + 'location' => '', + 'date_posted' => '', + 'user_id' => '', + ], + [ + 'item' => '', + 'price' => '', + 'description' => '', + 'location' => '', + 'date_posted' => '', + 'user_id' => '', + ], + [ + 'item' => '', + 'price' => '', + 'description' => '', + 'location' => '', + 'date_posted' => '', + 'user_id' => '', + ], + [ + 'item' => '', + 'price' => '', + 'description' => '', + 'location' => '', + 'date_posted' => '', + 'user_id' => '', + ], + + + ] diff --git a/database/create_ads_table_migration.php b/database/create_ads_table_migration.php new file mode 100644 index 0000000..a601a04 --- /dev/null +++ b/database/create_ads_table_migration.php @@ -0,0 +1,16 @@ +exec('DROP TABLE IF EXISTS ads'); + +$query = 'CREATE TABLE ads ( + id INT UNSIGNED NOT NULL AUTO_INCREMENT, + item VARCHAR(512) NOT NULL, + price INT UNSIGNED NOT NULL, + description TEXT, + location VARCHAR(256), + date_posted DATE, + user_id INT UNSIGNED, + PRIMARY KEY (id) +)'; + +$dbc->exec($query); diff --git a/database/create_users_table_migration.php b/database/create_users_table_migration.php new file mode 100644 index 0000000..e3409d0 --- /dev/null +++ b/database/create_users_table_migration.php @@ -0,0 +1,13 @@ +exec('DROP TABLE IF EXISTS users'); + +$query = 'CREATE TABLE users ( + id INT UNSIGNED NOT NULL AUTO_INCREMENT, + email VARCHAR(128) NOT NULL, + password VARCHAR(256) NOT NULL, + PRIMARY KEY (id) +)'; + +$dbc->exec($query); diff --git a/database/migration.php b/database/migration.php index e69de29..bdeb175 100644 --- a/database/migration.php +++ b/database/migration.php @@ -0,0 +1,6 @@ + + diff --git a/database/users_seeder.php b/database/users_seeder.php new file mode 100644 index 0000000..72b0a04 --- /dev/null +++ b/database/users_seeder.php @@ -0,0 +1,15 @@ + $_ENV['USER_EMAIL'], + 'password' => $_ENV['USER_PASS'], + ]; + +$query = 'INSERT INTO users (email, password) VALUES (:email, :password)'; +$stmt = $dbc->prepare($query); +$stmt->bindValue(':email', $user['email'], PDO::PARAM_STR); +$stmt->bindValue(':password', $user['password'], PDO::PARAM_STR); +$stmt->execute(); + diff --git a/env-template.php b/env-template.php new file mode 100644 index 0000000..146f0f4 --- /dev/null +++ b/env-template.php @@ -0,0 +1,10 @@ + '127.0.0.1', + 'DB_NAME' => 'your-db-name', + 'DB_USER' => 'your-db-user', + 'DB_PASS' => 'your-db-password', + 'USER_EMAIL' => '', + 'USER_PASS' => '' +); diff --git a/models/Ad.php b/models/Ad.php index e69de29..57cfd88 100644 --- a/models/Ad.php +++ b/models/Ad.php @@ -0,0 +1,27 @@ +prepare($query); + $stmt->bindValue(':item', $this->item, PDO::PARAM_STR); + $stmt->bindValue(':price', $this->price, PDO::PARAM_STR); + $stmt->bindValue(':description', $this->description, PDO::PARAM_STR); + $stmt->bindValue(':location', $this->location, PDO::PARAM_STR); + $stmt->exectute(); + + + } + + protected function update() + { + + } +} diff --git a/models/BaseModel.php b/models/BaseModel.php index cd30b75..2627e52 100644 --- a/models/BaseModel.php +++ b/models/BaseModel.php @@ -1,8 +1,8 @@ prepare($query); - $stmt->bindValue(':first_name', $this->first_name, PDO::PARAM_STR); - $stmt->bindValue(':last_name', $this->last_name, PDO::PARAM_STR); - $stmt->bindValue(':username', $this->username, PDO::PARAM_STR); - $stmt->bindValue(':password', $this->password, PDO::PARAM_STR); - $stmt->execute(); - - // @TODO: After insert, add the id back to the attributes array so the object can properly reflect the id - - } - - protected function update() - { - $table = static::$table; - - // @TODO: Ensure that update is properly handled with the id key - $query = "UPDATE $table SET - first_name = :first_name, - last_name = :last_name, - email = :email, - username = :username, - password = :password - WHERE id = :id"; - - // @TODO: Use prepared statements to ensure data security - $stmt = self::$dbc->prepare($query); - $stmt->bindValue(':first_name', $this->first_name, PDO::PARAM_STR); - $stmt->bindValue(':last_name', $this->last_name, PDO::PARAM_STR); - $stmt->bindValue(':username', $this->username, PDO::PARAM_STR); - $stmt->bindValue(':email', $this->email, PDO::PARAM_STR); - $stmt->bindValue(':password', $this->password, PDO::PARAM_STR); - $stmt->bindValue(':id', $this->id, PDO::PARAM_INT); - $stmt->execute(); - } - + protected function insert() {} + protected function update() {} /* * Find a record based on an id @@ -155,7 +114,9 @@ public static function all() { self::dbConnect(); - $result = self::$dbc->query('SELECT * FROM users')->fetchAll(PDO::FETCH_ASSOC); + $table = static::$table; + + $result = self::$dbc->query("SELECT * FROM $table")->fetchAll(PDO::FETCH_ASSOC); $instance = null; if ($result) @@ -179,3 +140,4 @@ public function delete() } } + diff --git a/models/User.php b/models/User.php index e69de29..9d5d63d 100644 --- a/models/User.php +++ b/models/User.php @@ -0,0 +1,54 @@ +prepare($query); + $stmt = self::$dbc->bindValue(':username', $username, PDO::PARAM_STR); + $stmt->execute(); + $result = $stmt->fetch(PDO::FETCH_ASSOC); + + // The following code will set the attributes on the calling object based on the result variable's contents + + $instance = null; + if ($result) + { + $instance = new static; + $instance->attributes = $result; + } + return $instance; + + } + + protected function insert() + {} + + protected function update() + { + + $hashed_pass = password_hash($this->password, PASSWORD_DEFAULT); + + $query = "UPDATE users...."; + + $stmt->bindValue(':password', $hashed_pass, PDO::PARAM_STR); + + } +} + + +$userToFind = User::findUserByUsername('Bob'); +var_dump($userToFind); + +$userToFind == ['username' => 'Bob']; + +$newUser = new User(); +$newUser->username = $userToFind[''] diff --git a/public/ad.create.php b/public/ads.create.php similarity index 83% rename from public/ad.create.php rename to public/ads.create.php index 1fb8860..2a72556 100644 --- a/public/ad.create.php +++ b/public/ads.create.php @@ -10,8 +10,7 @@
-diff --git a/public/ad.index.php b/public/ads.index.php similarity index 100% rename from public/ad.index.php rename to public/ads.index.php diff --git a/public/index.php b/public/index.php index ecdd5e0..98a03a9 100644 --- a/public/index.php +++ b/public/index.php @@ -1,14 +1,54 @@
-
+
+