-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstory.aspx.cs
153 lines (137 loc) · 5.6 KB
/
story.aspx.cs
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace medium_clone
{
public partial class story : System.Web.UI.Page
{
public DataTable data_table = new DataTable();
public Boolean isLoading = true;
public Boolean isEdit = false;
public string user_id;
public void isLoggedIn()
{
if (Session["isLoggedIn"] == null)
{
Response.Redirect("home");
}
}
public void isRemember()
{
if (Request.Cookies["remember_me"] != null)
{
Session.Add("isLoggedIn", "true");
}
}
public void userId()
{
if (Session["user_id"] != null)
{
user_id = Session["user_id"].ToString();
}
else if (Request.Cookies["user_id"] != null)
{
user_id = Request.Cookies["user_id"].Value.ToString();
}
else
{
user_id = "12";
}
}
static double GetReadTime(string text)
{
int wordCount = 0, index = 0;
while (index < text.Length && char.IsWhiteSpace(text[index]))
index++;
while (index < text.Length)
{
while (index < text.Length && !char.IsWhiteSpace(text[index]))
index++;
wordCount++;
while (index < text.Length && char.IsWhiteSpace(text[index]))
index++;
}
return wordCount <= 200 ? 1 : wordCount / 200;
}
protected void getPost()
{
data_table.Columns.Add("title", typeof(string));
data_table.Columns.Add("content", typeof(string));
DataRow data_row = data_table.NewRow();
SqlConnection connect = new SqlConnection(@"Data Source=.\SqlExpress;Initial Catalog=Blog;Integrated Security=True");
connect.Open();
SqlCommand command = new SqlCommand("select title, content from Posts where id='" + Request.QueryString.ToString() + "'", connect);
SqlDataReader read = command.ExecuteReader();
if (read.Read())
{
data_row["title"] = read.GetString(0);
data_row["content"] = read.GetString(1);
data_table.Rows.Add(data_row);
current_title.Value = data_row["title"].ToString();
current_block_list.Value = data_row["content"].ToString();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
isLoggedIn();
isRemember();
userId();
if (user_id == null)
{
Response.Redirect("/home");
}
}
if (Request.Url.AbsolutePath.Contains("edit"))
{
isEdit = true;
getPost();
}
}
protected void func_logout(object sender, EventArgs e)
{
Session.Clear();
HttpCookie remember_cookie = new HttpCookie("remember_me");
remember_cookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(remember_cookie);
Response.Redirect("home");
}
protected void send_post(object sender, EventArgs e)
{
string Post_Title = post_title.Value;
string Post_Content = post_content.Value.Trim();
SqlConnection connect = new SqlConnection(@"Data Source=.\SqlExpress;Initial Catalog=Blog;Integrated Security=True");
connect.Open();
SqlCommand command = new SqlCommand("INSERT INTO Posts(author_id, title, content, created_at, read_time) VALUES(@author_id, @title, @content, @created_at, @read_time)", connect);
command.Parameters.Add("@author_id", System.Data.SqlDbType.SmallInt).Value = Int16.Parse(user_id) != null ? Int16.Parse(user_id) : 12;
command.Parameters.Add("@title", System.Data.SqlDbType.VarChar).Value = Post_Title;
command.Parameters.Add("@content", System.Data.SqlDbType.NText).Value = Post_Content;
command.Parameters.Add("@created_at", System.Data.SqlDbType.SmallDateTime).Value = DateTime.Now.ToString("dd/MM/yyyy");
command.Parameters.Add("@read_time", System.Data.SqlDbType.TinyInt).Value = GetReadTime(Post_Content);
command.ExecuteNonQuery();
connect.Close();
Response.Redirect("/");
}
protected void update_post(object sender, EventArgs e)
{
string Post_Title = post_title.Value;
string Post_Content = post_content.Value.Trim();
SqlConnection connect = new SqlConnection(@"Data Source=.\SqlExpress;Initial Catalog=Blog;Integrated Security=True");
connect.Open();
SqlCommand command = new SqlCommand("Update Posts set title='" + Post_Title + "', content='" + Post_Content + "' where id='" + Request.QueryString.ToString() + "' ", connect);
command.ExecuteNonQuery();
connect.Close();
Response.Redirect("/");
}
protected void give_up(object sender, EventArgs e)
{
Response.Redirect("/");
}
}
}