big refactoring, and more
New routes, new juice flow, the tags on posts
This commit is contained in:
parent
d04a68db01
commit
1805cb3e17
46 changed files with 1339 additions and 515 deletions
|
|
@ -1,3 +1,7 @@
|
|||
2015-10-17 Paul Schneider <paul@pschneider.fr>
|
||||
|
||||
* NpgsqlBlogProvider.cs:
|
||||
|
||||
2015-10-13 Paul Schneider <paul@pschneider.fr>
|
||||
|
||||
* NpgsqlBlogProvider.cs: implements the tag methods on db
|
||||
|
|
|
|||
|
|
@ -33,8 +33,8 @@ namespace Npgsql.Web.Blog
|
|||
using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString))
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
|
||||
cmd.CommandText = "INSERT INTO tagged (tagid,postid) VALUES (:tid,:pid)";
|
||||
cmd.Parameters.AddWithValue("tid",tid);
|
||||
cmd.Parameters.AddWithValue("pid",postid);
|
||||
cmd.Parameters.AddWithValue ("tid", tid);
|
||||
cmd.Parameters.AddWithValue ("pid", postid);
|
||||
cnx.Open ();
|
||||
cmd.ExecuteNonQuery ();
|
||||
return tid;
|
||||
|
|
@ -47,8 +47,9 @@ namespace Npgsql.Web.Blog
|
|||
/// <param name="postid">Postid.</param>
|
||||
/// <param name="tagid">Tagid.</param>
|
||||
/// <param name="name">Name.</param>
|
||||
override public void Untag(long postid, string name) {
|
||||
Untag(postid, GetTagId (name));
|
||||
override public void Untag (long postid, string name)
|
||||
{
|
||||
Untag (postid, GetTagId (name));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -57,7 +58,8 @@ namespace Npgsql.Web.Blog
|
|||
/// <param name="postid">Postid.</param>
|
||||
/// <param name="tagid">Tagid.</param>
|
||||
/// <param name="tid">Tid.</param>
|
||||
override public void Untag(long postid, long tid) {
|
||||
override public void Untag (long postid, long tid)
|
||||
{
|
||||
using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString))
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
|
||||
cmd.CommandText = "DELETE FROM tagged WHERE postid = :pid AND tagid = :tid";
|
||||
|
|
@ -67,6 +69,7 @@ namespace Npgsql.Web.Blog
|
|||
cmd.ExecuteNonQuery ();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the comments.
|
||||
/// </summary>
|
||||
|
|
@ -77,32 +80,33 @@ namespace Npgsql.Web.Blog
|
|||
{
|
||||
List<Comment> cmts = new List<Comment> ();
|
||||
|
||||
using (NpgsqlConnection cnx=new NpgsqlConnection(connectionString))
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand()) {
|
||||
using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString))
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
|
||||
|
||||
cmd.CommandText = "select _id, username, bcontent, modified, posted, visible from comment " +
|
||||
"where applicationname = :appname and postid = :id" +
|
||||
((getHidden) ? " and visible = true ":" ") +
|
||||
"order by posted asc" ;
|
||||
"where applicationname = :appname and postid = :id" +
|
||||
((getHidden) ? " and visible = true " : " ") +
|
||||
"order by posted asc";
|
||||
cmd.Parameters.AddWithValue ("appname", applicationName);
|
||||
cmd.Parameters.AddWithValue ("id", postid);
|
||||
cnx.Open ();
|
||||
using (NpgsqlDataReader rdr = cmd.ExecuteReader()) {
|
||||
using (NpgsqlDataReader rdr = cmd.ExecuteReader ()) {
|
||||
while (rdr.Read ()) {
|
||||
Comment c = new Comment();
|
||||
Comment c = new Comment ();
|
||||
c.CommentText = rdr.GetString (rdr.GetOrdinal ("bcontent"));
|
||||
c.From = rdr.GetString (rdr.GetOrdinal ("username"));
|
||||
c.Modified = rdr.GetDateTime (rdr.GetOrdinal ("modified"));
|
||||
c.Posted = rdr.GetDateTime (rdr.GetOrdinal ("posted"));
|
||||
c.Visible = rdr.GetBoolean (rdr.GetOrdinal ("visible"));
|
||||
c.PostId = postid;
|
||||
c.Id = rdr.GetInt64(rdr.GetOrdinal("_id"));
|
||||
c.Id = rdr.GetInt64 (rdr.GetOrdinal ("_id"));
|
||||
cmts.Add (c);
|
||||
}
|
||||
}
|
||||
}
|
||||
return cmts.ToArray();
|
||||
return cmts.ToArray ();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the post.
|
||||
/// </summary>
|
||||
|
|
@ -112,9 +116,9 @@ namespace Npgsql.Web.Blog
|
|||
/// <param name="visible">If set to <c>true</c> visible.</param>
|
||||
/// <param name="cids">Circle identifiers</param>
|
||||
public override void UpdatePost (long postid, string title, string content,
|
||||
bool visible, long [] cids)
|
||||
bool visible, long[] cids)
|
||||
{
|
||||
using (NpgsqlConnection cnx = new NpgsqlConnection(connectionString)) {
|
||||
using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString)) {
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
|
||||
DateTime now = DateTime.Now;
|
||||
cmd.CommandText =
|
||||
|
|
@ -131,10 +135,11 @@ namespace Npgsql.Web.Blog
|
|||
cnx.Open ();
|
||||
cmd.ExecuteNonQuery ();
|
||||
}
|
||||
cnx.Close();
|
||||
cnx.Close ();
|
||||
}
|
||||
UpdatePostCircles (postid, cids);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the post.
|
||||
/// </summary>
|
||||
|
|
@ -146,9 +151,10 @@ namespace Npgsql.Web.Blog
|
|||
cmd.CommandText = "delete from blog where _id = :id";
|
||||
cmd.Parameters.AddWithValue ("id", postid);
|
||||
cnx.Open ();
|
||||
cmd.ExecuteNonQuery();
|
||||
cmd.ExecuteNonQuery ();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Comment the specified from, postid and content.
|
||||
/// </summary>
|
||||
|
|
@ -158,17 +164,17 @@ namespace Npgsql.Web.Blog
|
|||
public override long Comment (string from, long postid, string content)
|
||||
{
|
||||
if (from == null)
|
||||
throw new ArgumentNullException("from");
|
||||
throw new ArgumentNullException ("from");
|
||||
if (content == null)
|
||||
throw new ArgumentNullException("content");
|
||||
throw new ArgumentNullException ("content");
|
||||
bool visible = AutoValidatesComments;
|
||||
using (NpgsqlConnection cnx=
|
||||
new NpgsqlConnection(connectionString))
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand()) {
|
||||
using (NpgsqlConnection cnx =
|
||||
new NpgsqlConnection (connectionString))
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
|
||||
cmd.CommandText = "insert into comment (postid,bcontent," +
|
||||
"modified,posted,visible,username,applicationname)" +
|
||||
"values (:postid,:bcontent,:modified,:posted," +
|
||||
":visible,:username,:appname) returning _id";
|
||||
"modified,posted,visible,username,applicationname)" +
|
||||
"values (:postid,:bcontent,:modified,:posted," +
|
||||
":visible,:username,:appname) returning _id";
|
||||
cmd.Parameters.AddWithValue ("postid", postid);
|
||||
cmd.Parameters.AddWithValue ("bcontent", content);
|
||||
DateTime now = DateTime.Now;
|
||||
|
|
@ -178,9 +184,10 @@ namespace Npgsql.Web.Blog
|
|||
cmd.Parameters.AddWithValue ("username", from);
|
||||
cmd.Parameters.AddWithValue ("appname", applicationName);
|
||||
cnx.Open ();
|
||||
return (long) cmd.ExecuteScalar();
|
||||
return (long)cmd.ExecuteScalar ();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates the comment.
|
||||
/// </summary>
|
||||
|
|
@ -189,6 +196,7 @@ namespace Npgsql.Web.Blog
|
|||
{
|
||||
throw new NotImplementedException ();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the comment.
|
||||
/// </summary>
|
||||
|
|
@ -202,6 +210,7 @@ namespace Npgsql.Web.Blog
|
|||
}
|
||||
|
||||
private bool autoValidateComment = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether this <see cref="Npgsql.Web.Blog.NpgsqlBlogProvider"/> auto validate comment.
|
||||
/// </summary>
|
||||
|
|
@ -211,7 +220,7 @@ namespace Npgsql.Web.Blog
|
|||
return autoValidateComment;
|
||||
}
|
||||
set {
|
||||
autoValidateComment=value;
|
||||
autoValidateComment = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -227,6 +236,7 @@ namespace Npgsql.Web.Blog
|
|||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the specified name and config.
|
||||
/// </summary>
|
||||
|
|
@ -240,10 +250,12 @@ namespace Npgsql.Web.Blog
|
|||
config.Remove ("connectionStringName");
|
||||
applicationName = config ["applicationName"];
|
||||
config.Remove ("applicationName");
|
||||
defaultPageSize = int.Parse ( config ["pageLen"] ?? "10") ;
|
||||
defaultPageSize = int.Parse (config ["pageLen"] ?? "10");
|
||||
base.Initialize (name, config);
|
||||
}
|
||||
|
||||
#region implemented abstract members of BlogProvider
|
||||
|
||||
/// <summary>
|
||||
/// Gets the post.
|
||||
/// </summary>
|
||||
|
|
@ -252,14 +264,14 @@ namespace Npgsql.Web.Blog
|
|||
public override BlogEntry GetPost (long postid)
|
||||
{
|
||||
BlogEntry be = null;
|
||||
using (NpgsqlConnection cnx=new NpgsqlConnection(connectionString))
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand()) {
|
||||
using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString))
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
|
||||
cmd.CommandText = "select username, title, bcontent, modified, posted, visible, photo from blog " +
|
||||
"where applicationname = :appname and _id = :id";
|
||||
"where applicationname = :appname and _id = :id";
|
||||
cmd.Parameters.AddWithValue ("appname", applicationName);
|
||||
cmd.Parameters.AddWithValue ("id", postid);
|
||||
cnx.Open ();
|
||||
using (NpgsqlDataReader rdr = cmd.ExecuteReader()) {
|
||||
using (NpgsqlDataReader rdr = cmd.ExecuteReader ()) {
|
||||
if (rdr.Read ()) {
|
||||
be = new BlogEntry ();
|
||||
be.Title = rdr.GetString (rdr.GetOrdinal ("title"));
|
||||
|
|
@ -269,16 +281,18 @@ namespace Npgsql.Web.Blog
|
|||
be.Posted = rdr.GetDateTime (rdr.GetOrdinal ("posted"));
|
||||
be.Visible = rdr.GetBoolean (rdr.GetOrdinal ("visible"));
|
||||
int oph = rdr.GetOrdinal ("photo");
|
||||
if (!rdr.IsDBNull(oph))
|
||||
if (!rdr.IsDBNull (oph))
|
||||
be.Photo = rdr.GetString (oph);
|
||||
be.Id = postid;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (be!=null) Populate (be);
|
||||
if (be != null)
|
||||
Populate (be);
|
||||
return be;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the comment.
|
||||
/// </summary>
|
||||
|
|
@ -292,10 +306,11 @@ namespace Npgsql.Web.Blog
|
|||
cmd.CommandText = "delete from comment where _id = :id returning postid";
|
||||
cmd.Parameters.AddWithValue ("id", cmtid);
|
||||
cnx.Open ();
|
||||
postid = (long) cmd.ExecuteScalar ();
|
||||
postid = (long)cmd.ExecuteScalar ();
|
||||
}
|
||||
return postid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the post.
|
||||
/// </summary>
|
||||
|
|
@ -304,13 +319,13 @@ namespace Npgsql.Web.Blog
|
|||
/// <param name="title">Title.</param>
|
||||
public override UUTBlogEntryCollection GetPost (string username, string title)
|
||||
{
|
||||
UUTBlogEntryCollection bec = new UUTBlogEntryCollection (username,title);
|
||||
UUTBlogEntryCollection bec = new UUTBlogEntryCollection (username, title);
|
||||
using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString)) {
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
|
||||
cmd.CommandText = "select _id,bcontent,modified,posted,visible,photo from blog " +
|
||||
"where applicationname = :appname and username = :username and title = :title";
|
||||
cmd.Parameters.AddWithValue ("appname", NpgsqlDbType.Varchar, applicationName);
|
||||
cmd.Parameters.AddWithValue ("username", NpgsqlDbType.Varchar ,username);
|
||||
cmd.Parameters.AddWithValue ("username", NpgsqlDbType.Varchar, username);
|
||||
cmd.Parameters.AddWithValue ("title", NpgsqlDbType.Varchar, title);
|
||||
cnx.Open ();
|
||||
cmd.Prepare ();
|
||||
|
|
@ -352,19 +367,20 @@ namespace Npgsql.Web.Blog
|
|||
be.Tags = tags.ToArray ();
|
||||
}
|
||||
}
|
||||
if (bec!=null) Populate (bec);
|
||||
if (bec != null)
|
||||
Populate (bec);
|
||||
}
|
||||
}
|
||||
return bec;
|
||||
}
|
||||
|
||||
private void SetCirclesOn(BlogEntry be)
|
||||
private void SetCirclesOn (BlogEntry be)
|
||||
{
|
||||
List<long> circles = new List<long> ();
|
||||
using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString))
|
||||
using (NpgsqlCommand cmdcircles = cnx.CreateCommand ()) {
|
||||
cmdcircles.CommandText = "select a.circle_id from blog_access a " +
|
||||
"where a.post_id = :pid";
|
||||
"where a.post_id = :pid";
|
||||
cmdcircles.Parameters.AddWithValue ("pid", be.Id);
|
||||
cnx.Open ();
|
||||
using (NpgsqlDataReader rdr = cmdcircles.ExecuteReader ()) {
|
||||
|
|
@ -380,12 +396,12 @@ namespace Npgsql.Web.Blog
|
|||
/// Removes the tag.
|
||||
/// </summary>
|
||||
/// <param name="tagid">Tagid.</param>
|
||||
public override void DropTag(long tagid)
|
||||
public override void DropTag (long tagid)
|
||||
{
|
||||
using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString))
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
|
||||
cmd.CommandText = "DELETE from public.tag where _id = :tid";
|
||||
cmd.Parameters.AddWithValue("tagid",tagid);
|
||||
cmd.Parameters.AddWithValue ("tagid", tagid);
|
||||
cnx.Open ();
|
||||
cmd.ExecuteNonQuery ();
|
||||
cnx.Close ();
|
||||
|
|
@ -393,7 +409,9 @@ namespace Npgsql.Web.Blog
|
|||
}
|
||||
|
||||
private static string SelectTagsSql = "SELECT tag.name, tag._id FROM public.tag WHERE name like :name";
|
||||
private IEnumerable<string> GetSuggestion (string pattern) {
|
||||
|
||||
private IEnumerable<string> GetSuggestion (string pattern)
|
||||
{
|
||||
using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString))
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
|
||||
cmd.CommandText = SelectTagsSql;
|
||||
|
|
@ -402,7 +420,8 @@ namespace Npgsql.Web.Blog
|
|||
}
|
||||
|
||||
private static string InsertTagSql = "INSERT INTO tag (name) VALUES (:name) returning _id";
|
||||
private void InsertTag(long postid, long tagid)
|
||||
|
||||
private void InsertTag (long postid, long tagid)
|
||||
{
|
||||
using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString))
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
|
||||
|
|
@ -410,8 +429,8 @@ namespace Npgsql.Web.Blog
|
|||
throw new NotImplementedException ();
|
||||
}
|
||||
}
|
||||
|
||||
private long GetTagId(string tagname)
|
||||
|
||||
private long GetTagId (string tagname)
|
||||
{
|
||||
long id = 0;
|
||||
using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString))
|
||||
|
|
@ -419,20 +438,30 @@ namespace Npgsql.Web.Blog
|
|||
cmd.CommandText = "SELECT tag._id FROM public.tag WHERE name = :name";
|
||||
cmd.Parameters.AddWithValue ("name", tagname);
|
||||
cnx.Open ();
|
||||
id = (long) cmd.ExecuteScalar ();
|
||||
id = (long)cmd.ExecuteScalar ();
|
||||
}
|
||||
return id;
|
||||
}
|
||||
private long GetOrCreateTagId(string tagname)
|
||||
|
||||
private long GetOrCreateTagId (string tagname)
|
||||
{
|
||||
long id = 0;
|
||||
using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString))
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
|
||||
cmd.CommandText = "SELECT tag._id FROM public.tag WHERE name = :name;\n" +
|
||||
"IF NOT FOUND THEN INSERT INTO tag (name) values (:name) RETURNING _id; ENDIF;\n";
|
||||
cmd.Parameters.AddWithValue ("name", tagname);
|
||||
cnx.Open ();
|
||||
id = (long) cmd.ExecuteScalar ();
|
||||
using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString)) {
|
||||
try {
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
|
||||
cmd.CommandText = "SELECT tag._id FROM public.tag WHERE name = :name";
|
||||
cmd.Parameters.AddWithValue ("name", tagname);
|
||||
cnx.Open ();
|
||||
id = (long)cmd.ExecuteScalar ();
|
||||
}
|
||||
} catch (NullReferenceException) {
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
|
||||
cmd.CommandText = "INSERT INTO public.tag(name) VALUES (:name) RETURNING _id";
|
||||
cmd.Parameters.AddWithValue ("name", tagname);
|
||||
cnx.Open ();
|
||||
id = (long)cmd.ExecuteScalar ();
|
||||
}
|
||||
}
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
|
@ -440,13 +469,16 @@ namespace Npgsql.Web.Blog
|
|||
|
||||
|
||||
private static string SelectPostTagsSql = "SELECT tag.name FROM public.tag, public.tagged\n" +
|
||||
"WHERE tag._id = tagged.tagid AND tagged.postid = :pid \n";
|
||||
private void SetTagsOn(BlogEntryCollection bec){
|
||||
"WHERE tag._id = tagged.tagid AND tagged.postid = :pid \n";
|
||||
|
||||
private void SetTagsOn (BlogEntryCollection bec)
|
||||
{
|
||||
foreach (BlogEntry be in bec) {
|
||||
SetTagsOn (be);
|
||||
}
|
||||
}
|
||||
private void SetTagsOn(BlogEntry be)
|
||||
|
||||
private void SetTagsOn (BlogEntry be)
|
||||
{
|
||||
List<string> tags = new List<string> ();
|
||||
using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString))
|
||||
|
|
@ -464,16 +496,18 @@ namespace Npgsql.Web.Blog
|
|||
}
|
||||
|
||||
// Assert(bec!=null);
|
||||
private void Populate(BlogEntryCollection bec)
|
||||
private void Populate (BlogEntryCollection bec)
|
||||
{
|
||||
foreach (BlogEntry be in bec) Populate(be);
|
||||
foreach (BlogEntry be in bec)
|
||||
Populate (be);
|
||||
}
|
||||
|
||||
private void Populate(BlogEntry be)
|
||||
private void Populate (BlogEntry be)
|
||||
{
|
||||
SetTagsOn (be);
|
||||
SetCirclesOn (be);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Post the specified username, title, content and visible.
|
||||
/// </summary>
|
||||
|
|
@ -482,15 +516,15 @@ namespace Npgsql.Web.Blog
|
|||
/// <param name="content">Content.</param>
|
||||
/// <param name="visible">If set to <c>true</c> visible.</param>
|
||||
/// <param name="circles">.</param>
|
||||
public override long Post (string username, string title, string content, bool visible, long [] circles)
|
||||
public override long Post (string username, string title, string content, bool visible, long[] circles)
|
||||
{
|
||||
long pid = 0;
|
||||
if (username == null)
|
||||
throw new ArgumentNullException("username");
|
||||
throw new ArgumentNullException ("username");
|
||||
if (title == null)
|
||||
throw new ArgumentNullException("title");
|
||||
throw new ArgumentNullException ("title");
|
||||
if (content == null)
|
||||
throw new ArgumentNullException("content");
|
||||
throw new ArgumentNullException ("content");
|
||||
using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString)) {
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
|
||||
cmd.CommandText = "insert into blog (title,bcontent,modified,posted,visible,username,applicationname)" +
|
||||
|
|
@ -511,12 +545,13 @@ namespace Npgsql.Web.Blog
|
|||
UpdatePostCircles (pid, circles);
|
||||
return pid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the post photo.
|
||||
/// </summary>
|
||||
/// <param name="pid">Pid.</param>
|
||||
/// <param name="photo">Photo.</param>
|
||||
public override void UpdatePostPhoto ( long pid, string photo)
|
||||
public override void UpdatePostPhoto (long pid, string photo)
|
||||
{
|
||||
using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString)) {
|
||||
cnx.Open ();
|
||||
|
|
@ -530,7 +565,7 @@ namespace Npgsql.Web.Blog
|
|||
}
|
||||
}
|
||||
|
||||
private void UpdatePostCircles( long pid, long[] circles)
|
||||
private void UpdatePostCircles (long pid, long[] circles)
|
||||
{
|
||||
using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString)) {
|
||||
cnx.Open ();
|
||||
|
|
@ -539,21 +574,22 @@ namespace Npgsql.Web.Blog
|
|||
cmd.Parameters.AddWithValue ("pid", pid);
|
||||
cmd.ExecuteNonQuery ();
|
||||
}
|
||||
if (circles!=null)
|
||||
if (circles.Length>0)
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
|
||||
cmd.CommandText = "insert into blog_access (post_id,circle_id) values (:pid,:cid)";
|
||||
cmd.Parameters.AddWithValue ("pid", NpgsqlTypes.NpgsqlDbType.Bigint, pid);
|
||||
cmd.Parameters.Add ("cid", NpgsqlTypes.NpgsqlDbType.Bigint);
|
||||
cmd.Prepare ();
|
||||
foreach (long ci in circles) {
|
||||
cmd.Parameters ["cid"].Value = ci;
|
||||
cmd.ExecuteNonQuery ();
|
||||
if (circles != null)
|
||||
if (circles.Length > 0)
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
|
||||
cmd.CommandText = "insert into blog_access (post_id,circle_id) values (:pid,:cid)";
|
||||
cmd.Parameters.AddWithValue ("pid", NpgsqlTypes.NpgsqlDbType.Bigint, pid);
|
||||
cmd.Parameters.Add ("cid", NpgsqlTypes.NpgsqlDbType.Bigint);
|
||||
cmd.Prepare ();
|
||||
foreach (long ci in circles) {
|
||||
cmd.Parameters ["cid"].Value = ci;
|
||||
cmd.ExecuteNonQuery ();
|
||||
}
|
||||
}
|
||||
}
|
||||
cnx.Close ();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds the post.
|
||||
/// </summary>
|
||||
|
|
@ -568,40 +604,40 @@ namespace Npgsql.Web.Blog
|
|||
{
|
||||
BlogEntryCollection c = new BlogEntryCollection ();
|
||||
totalRecords = 0;
|
||||
using (NpgsqlConnection cnx=new NpgsqlConnection(connectionString))
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand()) {
|
||||
using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString))
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
|
||||
if (readersName != null) {
|
||||
cmd.CommandText = "select _id, title,bcontent, modified," +
|
||||
"posted,username,visible,photo " +
|
||||
"from blog b left outer join " +
|
||||
"(select count(*)>0 acc, a.post_id pid " +
|
||||
"from blog_access a," +
|
||||
" circle_members m, users u where m.circle_id = a.circle_id " +
|
||||
" and m.member = u.username and u.username = :uname " +
|
||||
" and u.applicationname = :appname " +
|
||||
" group by a.post_id) ma on (ma.pid = b._id) " +
|
||||
"where ( ((ma.acc IS NULL or ma.acc = TRUE) and b.Visible IS TRUE ) or b.username = :uname) ";
|
||||
"posted,username,visible,photo " +
|
||||
"from blog b left outer join " +
|
||||
"(select count(*)>0 acc, a.post_id pid " +
|
||||
"from blog_access a," +
|
||||
" circle_members m, users u where m.circle_id = a.circle_id " +
|
||||
" and m.member = u.username and u.username = :uname " +
|
||||
" and u.applicationname = :appname " +
|
||||
" group by a.post_id) ma on (ma.pid = b._id) " +
|
||||
"where ( ((ma.acc IS NULL or ma.acc = TRUE) and b.Visible IS TRUE ) or b.username = :uname) ";
|
||||
cmd.Parameters.AddWithValue ("uname", readersName);
|
||||
} else {
|
||||
cmd.CommandText = "select _id, title,bcontent,modified," +
|
||||
"posted,username,visible,photo " +
|
||||
"from blog b left outer join " +
|
||||
"(select count(*)>0 acc, a.post_id pid " +
|
||||
"from blog_access a" +
|
||||
" group by a.post_id) ma on (ma.pid = b._id)" +
|
||||
" where " +
|
||||
" ma.acc IS NULL and " +
|
||||
" b.Visible IS TRUE and " +
|
||||
" applicationname = :appname";
|
||||
"posted,username,visible,photo " +
|
||||
"from blog b left outer join " +
|
||||
"(select count(*)>0 acc, a.post_id pid " +
|
||||
"from blog_access a" +
|
||||
" group by a.post_id) ma on (ma.pid = b._id)" +
|
||||
" where " +
|
||||
" ma.acc IS NULL and " +
|
||||
" b.Visible IS TRUE and " +
|
||||
" applicationname = :appname";
|
||||
}
|
||||
cmd.Parameters.AddWithValue ("appname", applicationName);
|
||||
if (pattern != null) {
|
||||
if ((searchflags & FindBlogEntryFlags.MatchTag) > 0) {
|
||||
cmd.CommandText +=
|
||||
"AND EXISTS (SELECT tag._id FROM public.tag, public.tagged WHERE " +
|
||||
"public.tag._id = public.tagged.tagid " +
|
||||
"AND public.tagged.postid = a.post_id " +
|
||||
"public.tag.name like :tagname) ";
|
||||
"public.tag._id = public.tagged.tagid " +
|
||||
"AND public.tagged.postid = a.post_id " +
|
||||
"public.tag.name like :tagname) ";
|
||||
cmd.Parameters.AddWithValue ("tagname", pattern);
|
||||
}
|
||||
|
||||
|
|
@ -624,11 +660,11 @@ namespace Npgsql.Web.Blog
|
|||
|
||||
cmd.CommandText += " order by posted desc";
|
||||
cnx.Open ();
|
||||
using (NpgsqlDataReader rdr = cmd.ExecuteReader()) {
|
||||
using (NpgsqlDataReader rdr = cmd.ExecuteReader ()) {
|
||||
// pageIndex became one based
|
||||
int firstrec = pageIndex * pageSize;
|
||||
int lastrec = firstrec + pageSize - 1;
|
||||
while (rdr.Read()) {
|
||||
while (rdr.Read ()) {
|
||||
if (totalRecords >= firstrec && totalRecords <= lastrec) {
|
||||
BlogEntry be = new BlogEntry ();
|
||||
be.Title = rdr.GetString (rdr.GetOrdinal ("title"));
|
||||
|
|
@ -637,7 +673,7 @@ namespace Npgsql.Web.Blog
|
|||
be.Author = rdr.GetString (rdr.GetOrdinal ("username"));
|
||||
be.Posted = rdr.GetDateTime (rdr.GetOrdinal ("posted"));
|
||||
be.Modified = rdr.GetDateTime (rdr.GetOrdinal ("modified"));
|
||||
be.Visible = rdr.GetBoolean (rdr.GetOrdinal ("visible"));
|
||||
be.Visible = rdr.GetBoolean (rdr.GetOrdinal ("visible"));
|
||||
{
|
||||
int oph = rdr.GetOrdinal ("photo");
|
||||
if (!rdr.IsDBNull (oph))
|
||||
|
|
@ -650,10 +686,12 @@ namespace Npgsql.Web.Blog
|
|||
rdr.Close ();
|
||||
}
|
||||
}
|
||||
if (c!=null) Populate (c);
|
||||
if (c != null)
|
||||
Populate (c);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the post.
|
||||
/// </summary>
|
||||
|
|
@ -661,20 +699,21 @@ namespace Npgsql.Web.Blog
|
|||
/// <param name="title">Title.</param>
|
||||
public override void RemoveTitle (string username, string title)
|
||||
{
|
||||
using (NpgsqlConnection cnx=new NpgsqlConnection(connectionString))
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand()) {
|
||||
using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString))
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
|
||||
cmd.CommandText = "delete from blog where username = :username and applicationname = :appname and title=:title";
|
||||
cmd.Parameters.AddWithValue ("username",username);
|
||||
cmd.Parameters.AddWithValue ("username", username);
|
||||
cmd.Parameters.AddWithValue ("appname", applicationName);
|
||||
cmd.Parameters.AddWithValue ("title",title);
|
||||
cmd.Parameters.AddWithValue ("title", title);
|
||||
cnx.Open ();
|
||||
cmd.ExecuteNonQuery ();
|
||||
cnx.Close();
|
||||
cnx.Close ();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int defaultPageSize = 10;
|
||||
|
||||
/// <summary>
|
||||
/// Lasts the posts.
|
||||
/// </summary>
|
||||
|
|
@ -682,23 +721,23 @@ namespace Npgsql.Web.Blog
|
|||
/// <param name="pageIndex">Page index.</param>
|
||||
/// <param name="pageSize">Page size.</param>
|
||||
/// <param name="totalRecords">Total records.</param>
|
||||
public override BlogEntryCollection LastPosts(int pageIndex, int pageSize, out int totalRecords)
|
||||
public override BlogEntryCollection LastPosts (int pageIndex, int pageSize, out int totalRecords)
|
||||
{
|
||||
BlogEntryCollection c = new BlogEntryCollection ();
|
||||
using (NpgsqlConnection cnx=new NpgsqlConnection(connectionString))
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand()) {
|
||||
using (NpgsqlConnection cnx = new NpgsqlConnection (connectionString))
|
||||
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
|
||||
cmd.CommandText = "select * " +
|
||||
"from blog where applicationname = :appname and visible = true " +
|
||||
" order by posted desc limit :len" ;
|
||||
"from blog where applicationname = :appname and visible = true " +
|
||||
" order by posted desc limit :len";
|
||||
|
||||
cmd.Parameters.AddWithValue ("appname", applicationName);
|
||||
cmd.Parameters.AddWithValue ("len", defaultPageSize*10);
|
||||
cmd.Parameters.AddWithValue ("len", defaultPageSize * 10);
|
||||
cnx.Open ();
|
||||
using (NpgsqlDataReader rdr = cmd.ExecuteReader()) {
|
||||
using (NpgsqlDataReader rdr = cmd.ExecuteReader ()) {
|
||||
totalRecords = 0;
|
||||
int firstrec = pageIndex * pageSize;
|
||||
int lastrec = firstrec + pageSize - 1;
|
||||
while (rdr.Read()) {
|
||||
while (rdr.Read ()) {
|
||||
if (totalRecords >= firstrec && totalRecords <= lastrec) {
|
||||
BlogEntry be = new BlogEntry ();
|
||||
be.Id = rdr.GetInt64 (rdr.GetOrdinal ("_id"));
|
||||
|
|
@ -719,9 +758,11 @@ namespace Npgsql.Web.Blog
|
|||
}
|
||||
}
|
||||
}
|
||||
if (c!=null) Populate (c);
|
||||
if (c != null)
|
||||
Populate (c);
|
||||
return c;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue