<?php
Class Post_model extends CI_Model{
    public function __construct(){
        parent::__construct();
        $this->load->database('default');
    }
    public function insert($args=array()){
        if($args){
            $this->db->insert('posts',$args);
        }
    }
    public function remove($id){
        $this->db->where('id', $id);
        $this->db->delete('posts');
    }
    public function update($id,$data){
        $this->db->where('id', $id);
        $this->db->update('posts',$data);

    }
    public function getAll(){
      $sql = "SELECT posts.id,posts.text,users.name,users.id as user_id FROM posts JOIN users on posts.user_id = users.id";
      $query = $this->db->query($sql);
      return $query->result();
    }
    public function getAllPosts($user_id){
      $sql = "SELECT posts.id,posts.text,users.name,users.id as user_id FROM posts JOIN users on posts.user_id = users.id where  users.id = ?";
      $query = $this->db->query($sql, array($user_id));
      return $query->result();
    }
    public function getAllFollowsPost($user_id){
      $sql = "SELECT users.id as user_id, posts.id as post_id,posts.text, users.name FROM user_follows JOIN users on user_follows.follows_id = users.id JOIN posts on posts.user_id = user_follows.follows_id where user_follows.user_id = ?";
      $query = $this->db->query($sql, array($user_id));
      return $query->result();
    }

}
