File size: 2,504 Bytes
27f4352
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: GET,POST,PUT,DELETE");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

include_once '../config/Database.php';

class Item {
    private $conn;
    private $table_name = "items";

    public $id;
    public $title;
    public $image_path;
    public $ranking;
    public $item_type;
    public $tier_list_id;

    public function __construct($db) {
        $this->conn = $db;
    }

    public function read($item_type, $tier_list_id = null) {
        $query = "SELECT id, title, image_path, ranking, item_type, tier_list_id 
                 FROM " . $this->table_name . "
                 WHERE item_type = :item_type";
        
        if ($tier_list_id) {
            $query .= " AND tier_list_id = :tier_list_id";
        }

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(":item_type", $item_type);
        
        if ($tier_list_id) {
            $stmt->bindParam(":tier_list_id", $tier_list_id);
        }

        $stmt->execute();
        return $stmt;
    }

    public function create() {
        $query = "INSERT INTO " . $this->table_name . "
                (title, image_path, ranking, item_type, tier_list_id)
                VALUES
                (:title, :image_path, :ranking, :item_type, :tier_list_id)";

        $stmt = $this->conn->prepare($query);

        // Sanitize inputs
        $this->title = htmlspecialchars(strip_tags($this->title));
        $this->image_path = htmlspecialchars(strip_tags($this->image_path));

        $stmt->bindParam(":title", $this->title);
        $stmt->bindParam(":image_path", $this->image_path);
        $stmt->bindParam(":ranking", $this->ranking);
        $stmt->bindParam(":item_type", $this->item_type);
        $stmt->bindParam(":tier_list_id", $this->tier_list_id);

        if($stmt->execute()) {
            return true;
        }
        return false;
    }

    public function update() {
        $query = "UPDATE " . $this->table_name . "
                SET ranking = :ranking
                WHERE id = :id";

        $stmt = $this->conn->prepare($query);

        $stmt->bindParam(":ranking", $this->ranking);
        $stmt->bindParam(":id", $this->id);

        if($stmt->execute()) {
            return true;
        }
        return false;
    }
}
?>