-
Notifications
You must be signed in to change notification settings - Fork 0
/
Post.pm
120 lines (107 loc) · 2.61 KB
/
Post.pm
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package Post;
use DBI;
use Data::Dumper;
use Moose;
extends 'Blog';
my $dbh = Blog->DbConnect();
sub new {
my $class = shift;
my $self = $class->SUPER::new; # attrs inherited from Blog
$self->{extended} = 1;
return $self;
}
sub GetPostsByCategory {
my $Category = $_[1];
my $sth = $dbh->prepare("
SELECT *, Users.UserName,Users.UserEmail
FROM Users, Posts
WHERE
Posts.PostOwner = Users.UserID
AND
Posts.PostCategory=$Category"
);
$sth->execute() or die $DBI::errstr;
my $results = $sth->fetchall_arrayref({});
$sth->finish();
return($results);
}
sub GetPost() {
my $PostID = $_[1];
my $sql = "
SELECT *, Users.UserName,Users.UserEmail
FROM Users, Posts
WHERE
Posts.PostOwner = Users.UserID
AND
PostID = $PostID
";
my $sth = $dbh->prepare($sql);
$sth->execute() or die $DBI::errstr;
my $results = $sth->fetchall_arrayref({});
$sth->finish();
return($results);
}
sub GetAllPosts {
my $sth = $dbh->prepare("
SELECT *, Users.UserName,Users.UserEmail
FROM Users, Posts
WHERE
Posts.PostOwner = Users.UserID"
);
$sth->execute() or die $DBI::errstr;
my $results = $sth->fetchall_arrayref({});
$sth->finish();
return($results);
}
sub DeletePost()
{
my $cat = $_[1];
my $sth = $dbh->prepare("
DELETE FROM Posts WHERE PostID = '$cat'
");
$sth->execute() or die $DBI::errstr;
print "<script>location.href='./ViewPosts.pl' </script>";
$sth->finish();
}
sub AddPost() {
my $CatID = $_[1];
my $Title = $_[2];
my $Summary = $_[3];
my $Content = $_[4];
my $sth = $dbh->prepare("
INSERT INTO Posts (PostOwner, PostCategory, PostPublished, PostTitle, PostSummary, PostContent)
VALUES
(
1,$CatID,1,'$Title','$Summary','$Content'
)
");
$sth->execute() or die $DBI::errstr;
print "<script>location.href='./ViewPosts.pl' </script>";
$sth->finish();
}
sub UpdatePost() {
my $sql = "
UPDATE Posts
SET PostTitle = '$_[1]->{title}',
PostSummary = '$_[1]->{summary}',
PostContent = '$_[1]->{content}'
WHERE PostID = $_[1]->{'id'}";
my $sth = $dbh->prepare($sql);
$sth->execute() or die $DBI::errstr;
print "<script>location.href='./ViewPosts.pl' </script>";
$sth->finish();
}
sub GetTop3Posts() {
my $sth = $dbh->prepare("
SELECT * FROM Posts, Users
WHERE
Posts.PostOwner = Users.UserID
ORDER BY RAND()
LIMIT 6"
);
$sth->execute() or die $DBI::errstr;
my $results = $sth->fetchall_arrayref({});
$sth->finish();
return($results);
}
1;