Skip to content
Snippets Groups Projects
Commit 1d66ed63 authored by Genvictus's avatar Genvictus
Browse files

add: postcontroller to insert post into db

parent 6b673d5b
Branches
Tags
No related merge requests found
...@@ -49,7 +49,7 @@ CREATE TABLE "posts" ( ...@@ -49,7 +49,7 @@ CREATE TABLE "posts" (
); );
-- CURRENT IMPLEMENTATION ONLY ACCEPTS "refer_type" values of {'Repost', 'Reply'} -- CURRENT IMPLEMENTATION ONLY ACCEPTS "refer_type" values of {'Repost', 'Reply'}
CREATE TABLE "images" ( CREATE TABLE "post_resources" (
"post_id" integer, "post_id" integer,
"post_owner_id" integer, "post_owner_id" integer,
"path" varchar "path" varchar
...@@ -83,11 +83,7 @@ COMMENT ON COLUMN "posts"."body" IS 'Content of the post'; ...@@ -83,11 +83,7 @@ COMMENT ON COLUMN "posts"."body" IS 'Content of the post';
COMMENT ON COLUMN "posts"."refer_type" IS 'Refer type, e.g. Repost(Retweet), Reply'; COMMENT ON COLUMN "posts"."refer_type" IS 'Refer type, e.g. Repost(Retweet), Reply';
COMMENT ON COLUMN "images"."path" IS 'Path to the image in filesystem'; COMMENT ON COLUMN "post_resources"."path" IS 'Path to the resource in filesystem';
COMMENT ON COLUMN "videos"."path" IS 'Path to the video in filesystem';
COMMENT ON COLUMN "audios"."path" IS 'Path to the audio in filesystem';
COMMENT ON COLUMN "likes"."user_id" IS 'The user which likes the post'; COMMENT ON COLUMN "likes"."user_id" IS 'The user which likes the post';
...@@ -103,11 +99,7 @@ ALTER TABLE "posts" ADD FOREIGN KEY ("owner_id") REFERENCES "users" ("id"); ...@@ -103,11 +99,7 @@ ALTER TABLE "posts" ADD FOREIGN KEY ("owner_id") REFERENCES "users" ("id");
ALTER TABLE "posts" ADD FOREIGN KEY ("refer_post", "refer_post_owner") REFERENCES "posts" ("post_id", "owner_id"); ALTER TABLE "posts" ADD FOREIGN KEY ("refer_post", "refer_post_owner") REFERENCES "posts" ("post_id", "owner_id");
ALTER TABLE "images" ADD FOREIGN KEY ("post_id", "post_owner_id") REFERENCES "posts" ("post_id", "owner_id"); ALTER TABLE "post_resources" ADD FOREIGN KEY ("post_id", "post_owner_id") REFERENCES "posts" ("post_id", "owner_id");
ALTER TABLE "videos" ADD FOREIGN KEY ("post_id", "post_owner_id") REFERENCES "posts" ("post_id", "owner_id");
ALTER TABLE "audios" ADD FOREIGN KEY ("post_id", "post_owner_id") REFERENCES "posts" ("post_id", "owner_id");
ALTER TABLE "likes" ADD FOREIGN KEY ("post_id", "post_owner_id") REFERENCES "posts" ("post_id", "owner_id"); ALTER TABLE "likes" ADD FOREIGN KEY ("post_id", "post_owner_id") REFERENCES "posts" ("post_id", "owner_id");
......
...@@ -142,7 +142,7 @@ abstract class BaseManager ...@@ -142,7 +142,7 @@ abstract class BaseManager
return $stmt->fetch(); return $stmt->fetch();
} }
public function insert($model, $attributes, $idName = null) // attributes[$attribute] = $type public function insert($model, $attributes, $retIDName = null) // attributes[$attribute] = $type
{ {
$sql = "INSERT INTO $this->tableName ("; $sql = "INSERT INTO $this->tableName (";
...@@ -164,7 +164,7 @@ abstract class BaseManager ...@@ -164,7 +164,7 @@ abstract class BaseManager
} }
$stmt->execute(); $stmt->execute();
return $this->pdo->lastInsertId($idName); return $this->pdo->lastInsertId($retIDName);
} }
public function update($model, $attributes) // attributes[$attribute] = $type public function update($model, $attributes) // attributes[$attribute] = $type
......
...@@ -3,7 +3,11 @@ ...@@ -3,7 +3,11 @@
require_once SRC_ROOT_PATH . "/app/baseclasses/BaseController.php"; require_once SRC_ROOT_PATH . "/app/baseclasses/BaseController.php";
require_once SRC_ROOT_PATH . "app/core/FileAccess.php"; require_once SRC_ROOT_PATH . "app/core/FileAccess.php";
require_once SRC_ROOT_PATH . "/app/models/PostModel.php";
require_once SRC_ROOT_PATH . "/app/models/PostResourceModel.php";
require_once SRC_ROOT_PATH . "/app/modelmanagers/PostManager.php"; require_once SRC_ROOT_PATH . "/app/modelmanagers/PostManager.php";
require_once SRC_ROOT_PATH . "/app/modelmanagers/PostResourceManager.php";
class PostController extends BaseController class PostController extends BaseController
{ {
...@@ -24,10 +28,70 @@ class PostController extends BaseController ...@@ -24,10 +28,70 @@ class PostController extends BaseController
return self::$instance; return self::$instance;
} }
protected function post() protected function createPost(
$owner_id,
$body,
$refer_type = null,
$refer_post = null,
$refer_post_owner = null
)
{
preg_match_all("/(#\w+)/u", $body, $matches);
if ($matches) {
$tagsArray = array_count_values($matches[0]);
$tags = array_keys($tagsArray);
}
$postArr = [
'owner_id' => $owner_id,
'body' => $body
];
if(!is_null($refer_type)) {
$postArr['refer_type'] = $refer_type;
$postArr['refer_post'] = $refer_post;
$postArr['refer_post_owner'] = $refer_owner;
}
if(isset($tags)) {
$postArr['tags'] = $tags;
}
$postObj = new PostModel();
$postObj = $postObj->constructFromArray($postArr);
$post_id = $srv->insert($postObj, array_keys($postArr), 'post_id');
return $post_id;
}
protected function insertResources($post_id, $owner_id, $resources)
{
}
public function compose()
{ {
$fileAccess = new FileAccess(FileAccess::POSTS_PATH); $resources = [];
$newFileName = $fileAccess->saveFileAuto($_FILES['file']['tmp_name']);
if(isset($_FILES['file']['tmp_name'])) {
$fileAccess = new FileAccess(FileAccess::POSTS_PATH);
$newFileName = $fileAccess->saveFileAuto($_FILES['file']['tmp_name']);
$resources[] = $newFileName;
}
// insert post tweetpost here // insert post tweetpost here
$user_id = $_SESSION['user_id'];
$post_id = $this->createPost(
owner_id: $user_id,
body: $_POST['body'],
);
$this->insertResources(
$post_id,
$user_id,
$resources
);
} }
} }
\ No newline at end of file
<?php
require_once SRC_ROOT_PATH . "/baseclasses/BaseManager.php";
class PostResourceManager extends BaseManager
{
protected static $instance;
protected $tableName = 'post_resources';
protected function __construct()
{
parent::__construct();
}
public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new static();
}
return self::$instance;
}
}
\ No newline at end of file
...@@ -15,7 +15,7 @@ class PostModel extends BaseModel ...@@ -15,7 +15,7 @@ class PostModel extends BaseModel
public $tags = []; public $tags = [];
private function __construct() public function __construct()
{ {
$this->_primary_key = ['post_id', 'owner_id']; $this->_primary_key = ['post_id', 'owner_id'];
} }
......
<?php
require_once SRC_ROOT_PATH . '/app/baseclasses/BaseModel.php';
class PostResourceModel extends BaseModel
{
public $post_id;
public $post_owner_id;
public $body;
public function __construct()
{
$this->_primary_key = ['post_id', 'post_owner_id'];
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment