8000 Codeplus initial commit by dkrock · dkrockcom/codeplus@1143667 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1143667

Browse files
committed
Codeplus initial commit by dkrock
1 parent 00c7deb commit 1143667

33 files changed

+4982
-1
lines changed

AppCode/LookupType.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
class LookupType
4+
{
5+
6+
}

AppConfig/AppConfig.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
class AppConfig
4+
{
5+
//Database Configuration.
6+
const HOST = "localhost";
7+
const DBUSER = "root";
8+
const DBPASSWORD = "123456";
9+
const DATABASE = "test";
10+
const PORT = null;
11+
const SOCKET = null;
12+
const CHAR_SET = 'utf8';
13+
14+
//Default File Upload Size
15+
const DEFAULT_UPLOAD_SIZE = 5; //Size 5 MB
16+
17+
//Default Date Format.
18+
const DATETIME_FORMAT = 'Y-m-d H:i:s';
19+
20+
//JWT Configuration.
21+
const JWT_SEQURITY_KEY = "YOUR_JWT_SEQURIT_KEY";
22+
const JWT_ENCRYPTION = 'HS512';
23+
const JWT_LEEWAY = 60;
24+
}

AppLoad.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
require_once 'Framework/Constants.php';
3+
require_once 'vendor/autoload.php';

Business/User.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
class User extends BusinessBase
4+
{
5+
public function __construct()
6+
{
7+
parent::__construct();
8+
}
9+
10+
public $Username;
11+
public $Password;
12+
public $Name;
13+
public $DOB;
14+
public $CreatedOn;
15+
public $CreatedBy;
16+
public $ModifiedOn;
17+
public $ModifiedBy;
18+
19+
}

Controllers/Login.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
require_once '../AppLoad.php';
4+
5+
class LoginController extends ControllerBase
6+
{
7+
public function __construct()
8+
{
9+
parent::__construct(null, AUTHORIZATION_DISABLED);
10+
}
11+
12+
public function execute()
13+
{
14+
try {
15+
$username = $this->params("Username", true);
16+
$password = $this->params("Password", true);
17+
$password = Common::passwordHash($password);
18+
$tokenValue = null;
19+
$db = new Database();
20+
$db->where("Username", $username);
21+
$db->where("Password", $password);
22+
$row = $db->getOne('User');
23+
24+
if ($row) {
25+
$tokenValue = Identity::generateToken($row["UserId"], 60 * 24);
26+
Common::serializeObject(array(SUCCESS => true, TOKEN => $tokenValue, USER => array($row)));
27+
} else {
28+
Common::serializeObject(array(SUCCESS => false, ERROR => INVALID_USERNAME_PASSWORD));
29+
}
30+
} catch (Exception $ex) {
31+
Common::serializeObject($ex->getMessage());
32+
}
33+
}
34+
}
35+
Common::executeController("LoginController");

Controllers/User.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
require_once '../AppLoad.php';
4+
5+
class UserController extends ControllerBase
6+
{
7+
public function __construct()
8+
{
9+
parent::__construct(new User(), AUTHORIZATION_DISABLED);
10+
}
11+
12+
public function execute()
13+
{
14+
echo "executed";
15+
}
16+
}
17+
Common::executeController("UserController");

DB.sql

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
CREATE TABLE `User` (
2+
`UserId` INT NOT NULL AUTO_INCREMENT,
3+
`Username` VARCHAR(100) NOT NULL,
4+
`Password` VARCHAR(100) NOT NULL,
5+
`Name` VARCHAR(100) NOT NULL,
6+
`DOB` DATETIME NOT NULL,
7+
`CreatedOn` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
8+
`CreatedBy` INT NOT NULL DEFAULT 0,
9+
`ModifiedOn` DATETIME NULL DEFAULT NULL,
10+
`ModifiedBy` INT NULL DEFAULT NULL,
11+
PRIMARY KEY (`UserId`),
12+
UNIQUE INDEX `Username_UNIQUE` (`Username` ASC);
13+
14+
CREATE VIEW `vwUserList` AS
15+
SELECT
16+
`User`.`UserId` AS `UserId`,
17+
`User`.`Username` AS `Username`,
18+
`User`.`Name` AS `Name`,
19+
`User`.`DOB` AS `DOB`,
20+
`User`.`CreatedOn` AS `CreatedOn`,
21+
`User`.`CreatedBy` AS `CreatedBy`,
22+
`User`.`ModifiedOn` AS `ModifiedOn`,
23+
`User`.`ModifiedBy` AS `ModifiedBy`
24+
FROM
25+
`User`

Framework/BusinessBase.php

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
3+
class BusinessBase
4+
{
5+
public $Id;
6+
7+
public function __construct()
8+
{
9+
//TODO: Bind DB Object by default.
10+
}
11+
12+
/**
13+
* Save method - Perform action record create and update. perform update when id is set.
14+
* @param Nullable int $id - Table key field Id
15+
*
16+
* @return string json string
17+
*/
18+
public function save($id = null)
19+
{
20+
$db = new Database();
21+
$db->startTransaction();
22+
try {
23+
$properties = get_object_vars($this);
24+
array_splice($properties, array_search(ID, array_keys($properties)), 1);
25+
if (is_null($id)) {
26+
$li = $id = $db->insert($this->tableName(), $properties);
27+
} else {
28+
$db->where($this->keyField(), $id);
29+
$db->update($this->tableName(), $properties);
30+
if ($db->count == 0) {
31+
throw new Exception("Invalid record");
32+
}
33+
}
34+
$db->commit();
35+
if ($db->_stmtErrno > 0) {
36+
throw new Exception($db->_stmtError);
37+
}
38+
$this->load(isset($id) ? $id : $li);
39+
} catch (Exception $ex) {
40+
$db->rollback();
41+
$this->error($ex->getMessage());
42+
exit();
43+
}
44+
}
45+
46+
/**
47+
* load method - Load business class object with assiciated reqord id
48+
* @param int $string - * @param Nullable int $id - Table key field Id
49+
*/
50+
public function load($id)
51+
{
52+
try {
53+
$db = new Database();
54+
$tableName = $this->tableName();
55+
$properties = get_object_vars($this);
56+
$db->where($this->keyField(), $id);
57+
$data = $db->getOne($tableName);
58+
if ($data) {
59+
foreach ($properties as $key => $value) {
60+
$this->{$key} = $key == ID ? $data[$this->keyField()] : $data[$key];
61+
}
62+
}
63+
} catch (Exception $ex) {
64+
$this->error($ex->getMessage());
65+
exit();
66+
}
67+
}
68+
69+
/**
70+
* delete method - Perform action delete record.
71+
* @param int $string - * @param Nullable int $id - Table key field Id
72+
*/
73+
public function delete($id)
74+
{
75+
$db = new Database();
76+
$db->startTransaction();
77+
try {
78+
$db->where($this->keyField(), $id);
79+
$db->delete($this->tableName());
80+
$db->commit();
81+
} catch (Exception $ex) {
82+
$db->rollback();
83+
$this->error($ex->getMessage());
84+
exit();
85+
}
86+
}
87+
88+
/**
89+
* getList method - Perform action get the record list with associate controller class
90+
* @param Nullable_int $startIndex -
91+
* @param Nullable_int $limit - Json association
92+
* @param Array $filters - filters
93+
*
94+
* @return Array listData
95+
*/
96+
public function getList($startIndex = null, $limit = null, $filters = array())
97+
{
98+
$records = array();
99+
try {
100+
$db = new Database();
101+
$db->orderBy($this->keyField(), "Desc");
102+
if (sizeof($filters) == 0) {
103+
$records = $db->withTotalCount()->get($this->tableName(), array($startIndex, $limit));
104+
} else {
105+
$flt = new Filter();
106+
$flt->applyFilters($filters, $db);
107+
$records = $db->withTotalCount()->get($this->tableName(), array($startIndex, $limit));
108+
}
109+
} catch (Exception $ex) {
110+
$this->error($ex->getMessage());
111+
exit();
112+
}
113+
return $records;
114+
}
115+
116+
/**
117+
* error method - Generate error output with message
118+
* @param string $message - Error meesgae
119+
*/
120+
public function error($message)
121+
{
122+
$response = array(SUCCESS => false, ERROR => $message);
123+
Common::serializeObject($response);
124+
exit();
125+
}
126+
127+
/**
128+
* tableName method - Get the table name
129+
* @return string Associate controller class name
130+
*/
131+
public function tableName()
132+
{
133+
return get_called_class();
134+
}
135+
136+
/**
137+
* keyField method - Get Key field
138+
* @return int table key field
139+
*/
140+
public function keyField()
141+
{
142+
return $this->tableName() . ID;
143+
}
144+
145+
/**
146+
* getProperties method - Get associate controller class properties
147+
* @return Array Associate controller class properties
148+
*/
149+
public function getProperties()
150+
{
151+
return get_object_vars($this);
152+
}
153+
}

0 commit comments

Comments
 (0)
0