Skip to content
Open

Db #2

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"autoload": {
"psr-4": {
"my\\namespace\\" : "src"
"Davian\\api\\" : "src"
}
}
}
54 changes: 54 additions & 0 deletions apidb.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
-- MySQL dump 10.13 Distrib 5.7.18, for osx10.12 (x86_64)
--
-- Host: localhost Database: apidb
-- ------------------------------------------------------
-- Server version 5.7.18

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `people`
--
CREATE DATABASE apidb;
USE apidb;
DROP TABLE IF EXISTS `people`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `people` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`age` int(11) NOT NULL,
`occupation` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `people`
--

LOCK TABLES `people` WRITE;
/*!40000 ALTER TABLE `people` DISABLE KEYS */;
INSERT INTO `people` VALUES (1,'Steve Martin',73,'comedian'),(2,'Johnny Depp',55,'actor'),(3,'Sammy Hagar',70,'musician'),(4,'Elvis Presley',42,'musician'),(5,'Joe Johnson',25,'driver'),(6,'Sarah Andrews',31,'consultant'),(7,'Samuel L Jackson',69,'actor'),(8,'Steve Wozniak',68,'programmer'),(9,'Ada Lovelace',36,'programmer'),(10,'Grace Hopper',85,'programmer'),(11,'Alan Turing',41,'programmer');
/*!40000 ALTER TABLE `people` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2018-08-28 13:28:52
36 changes: 36 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response; //This allows us to use the Request and Response objects in our code

require './vendor/autoload.php'; //It allows you to automatically pull in scripts and classes without having to do it manually

$config['db']['host'] = 'localhost';
$config['db']['user'] = 'root';
$config['db']['pass'] = 'root';
$config['db']['dbname'] = 'apidb';
//SQL database

$app = (new Davian\api\App($db))->get(); //$app is our instance of slim $app = (new davian\firstSlim\App($db))->get();
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
//{name} in the url indicates what the argument will be called
$name = $args['name'];
$response->getBody()->write("Hello, $name"); //Line 15; adds the text "Hello, $name" to the response
return $response;
});
$app->get('/people', function (Request $request, Response $response) {
$this->logger->addInfo("GET /people");
$people = $this->db->query('SELECT * from people')->fetchAll();
$jsonResponse = $response->withJson($people);
return $jsonResponse;
}); //First Endpoint (Get people)

$container = $app->getContainer();
$container['logger'] = function($c) {
$logger = new \Monolog\Logger('my_logger');
$file_handler = new \Monolog\Handler\StreamHandler('../logs/app.log');
$logger->pushHandler($file_handler);
return $logger;
}; //This creates a new directory 'logs' in the api-lab-template directory
//Creates and writes to app.log file in that directory.

$app->run(); //This tells PHP to run the slim app