internal async Task <bool> saveToDB() { /* First find out if there are any images in post. * If there are, then save them to local folder. * Then save the post to DB */ int start = 0, end = 0, s = 0, e = 0, index = 0, newStart = 0; int counter = 0; string subStr = "", replaceStr; string fileName, file_extension; do { start = _content.IndexOf("<a", newStart); end = _content.IndexOf("</a>", newStart); if (start >= 0 && end > 0) { subStr = _content.Substring(start, end - start); } if (start >= 0 && end > 0 && subStr.Contains("<img")) { // We found something. replaceStr = subStr = _content.Substring(start, end - start); s = subStr.IndexOf("src=\"") + 5; e = subStr.IndexOf('"', s); subStr = subStr.Substring(s, e - s); // This is Image URL. index = subStr.LastIndexOf("."); file_extension = subStr.Substring(index, 4); fileName = _id + "-" + counter + file_extension; // Download Image. string file = await downloadImage(subStr, fileName); // If download is success. Replace the File name string imgSrc = "<img class=\"alignnone\" src=\"file:" + file + "\">"; _content = _content.Replace(replaceStr, imgSrc); counter++; } newStart = end + 3; } while (start > 0 && end > 0); // Now start saving to DB String sql; // The query itself DBQuery query; // Query Object List <DBQuery> queryList = new List <DBQuery>(); // Check if this post exists in DB DBHelper db = DBHelper.getInstance(); if (db.checkPostExists(_id)) { // Update Post table sql = @"UPDATE Post SET Title = ?, PubDate = ?, ModDate = ?, PostContent = ? WHERE Id = ?"; query = new DBQuery(); query.Query = sql; query.addQueryData(_title); query.addQueryData(_pubDate); query.addQueryData(_modDate); query.addQueryData(_content); query.addQueryData(_id); queryList.Add(query); // Update Meta Data foreach (KeyValuePair <string, string> metadata in _metaData) { sql = @"UPDATE PostMeta SET MetaValue=? WHERE Id=? AND MetaKey=?;"; query = new DBQuery(); query.Query = sql; query.addQueryData(metadata.Value); query.addQueryData(_id); query.addQueryData(metadata.Key); queryList.Add(query); } // Update Comments foreach (PostComment comment in _postComments) { queryList.Add(comment.UpsertQuery()); } // Delete categories and tags. And insert again sql = @"DELETE FROM Terms WHERE PostId=?"; query = new DBQuery(); query.Query = sql; query.addQueryData(_id); queryList.Add(query); // Add queries for categories addQueriesForCategories(queryList); // Add queries for tags. addQueriesForTags(queryList); } else { // Insert new post sql = @"INSERT INTO Post (Id, PubDate, ModDate, Author, Title, PostContent) VALUES (?,?,?,?,?,?);"; query = new DBQuery(); query.Query = sql; query.addQueryData(_id); query.addQueryData(_pubDate); query.addQueryData(_modDate); query.addQueryData(_author); query.addQueryData(_title); query.addQueryData(_content); queryList.Add(query); // Update Meta Data foreach (KeyValuePair <string, string> metadata in _metaData) { sql = @"INSERT INTO PostMeta (PostId, MetaKey, MetaValue) VALUES (?,?,?);"; query = new DBQuery(); query.Query = sql; query.addQueryData(_id); query.addQueryData(metadata.Key); query.addQueryData(metadata.Value); queryList.Add(query); } // New Comments foreach (PostComment comment in _postComments) { queryList.Add(comment.UpsertQuery()); } // Add queries for categories addQueriesForCategories(queryList); // Add queries for tags. addQueriesForTags(queryList); } // Now Execute Queries return(DBHelper.getInstance().executeQueries(queryList)); }