Example #1
0
        public ActionResult Add(EntryModel model)
        {
            if (ModelState.IsValid)
            {
                if (Request.IsAuthenticated)
                {
                    try
                    {
                        // TODO: Add insert logic here
                        Entry entry = new Entry();
                        entry.ParentID = null;
                        entry.AccountID = GetFormsAuthenticationID();
                        entry.Title = model.Title;
                        entry.Contents = model.Contents;
                        entry.Timestamp = DateTime.Now;
                        entry = this.dataRepository.Add(entry);

                        if (entry != null && entry.ID >= 0)
                        {
                            #region Tags
                            string[] tags = model.TagListString.Split(new char[] { ' ', ',' });
                            List<Tag> tagList = new List<Tag>();
                            foreach (string tag in tags)
                            {
                                tagList.Add(new Tag(tag.ToLower()));
                            }
                            this.dataRepository.AddTagListForEntry(entry.ID, tagList);
                            #endregion

                            return RedirectToAction("Details", "Entry", new { id = entry.ID });
                        }
                        else
                        {
                            ModelState.AddModelError("", "Unable to create entry");
                        }

                        return RedirectToAction("Add", "Entry");
                    }
                    catch
                    {
                        return View();
                    }
                }
            }
            return View();
        }
Example #2
0
        public Entry Add(Entry entry)
        {
            try
            {
                using (SQLiteConnection sqliteConnection = new SQLiteConnection(this.connectionString))
                {
                    sqliteConnection.Open();
                    SQLiteCommand sqlCommand = new SQLiteCommand(SQLiteDataRepository.SQL_ENTRY_INSERT, sqliteConnection);
                    //ParentID,AccountID,Title,Content,Timestamp
                    sqlCommand.Parameters.AddWithValue("@ParentID", entry.ParentID);
                    sqlCommand.Parameters.AddWithValue("@AccountID", entry.AccountID);
                    sqlCommand.Parameters.AddWithValue("@Title", entry.Title);
                    sqlCommand.Parameters.AddWithValue("@Contents", entry.Contents);
                    sqlCommand.Parameters.AddWithValue("@Timestamp", entry.Timestamp);
                    object returnValue = sqlCommand.ExecuteScalar();
                    int id = int.Parse(returnValue.ToString());
                    entry.ID = id;
                }

                return entry;
            }
            catch (Exception exception)
            {
                return null;
            }
        }
Example #3
0
        private Entry ReadEntryAuthorVote(SQLiteDataReader reader)
        {
            Entry entry = new Entry();
            entry.ID = reader.GetInt32(0);
            if (!reader.IsDBNull(1))
            {
                entry.ID = reader.GetInt32(1);
            }
            else
            {
                entry.ParentID = null;
            }
            entry.AccountID = reader.GetInt32(2);
            entry.Title = reader.GetString(3);
            entry.Contents = reader.GetString(4);
            entry.Timestamp = reader.GetDateTime(5);

            entry.Author = new Account();
            entry.Author.ID = entry.AccountID;
            entry.Author.Name = reader.GetString(6);
            entry.Author.Email = reader.GetString(7);

            if (reader.IsDBNull(9))
            {
                entry.Vote = 0;
                entry.AuthorVote = false;
                entry.AuthorVoteUp = false;
                entry.AuthorVoteDown = false;
            }
            else
            {
                int vote = reader.GetInt32(9);
                entry.AuthorVote = true;
                if (vote > 0)
                {
                    entry.AuthorVoteUp = true;
                    entry.AuthorVoteDown = false;
                }
                else
                {
                    entry.AuthorVoteUp = false;
                    entry.AuthorVoteDown = true;
                }
            }

            return entry;
        }
Example #4
0
        private Entry ReadEntryAuthor(SQLiteDataReader reader)
        {
            Entry entry = new Entry();
            entry.ID = reader.GetInt32(0);
            if (!reader.IsDBNull(1))
            {
                entry.ParentID = reader.GetInt32(1);
            }
            else
            {
                entry.ParentID = null;
            }
            entry.AccountID = reader.GetInt32(2);
            entry.Title = reader.GetString(3);
            entry.Contents = reader.GetString(4);
            entry.Timestamp = reader.GetDateTime(5);

            entry.Author = new Account();
            entry.Author.ID = entry.AccountID;
            entry.Author.Name = reader.GetString(6);
            entry.Author.Email = reader.GetString(7);

            return entry;
        }
Example #5
0
        public List<Entry> Search(string searchString, int page, int pageSize)
        {
            List<Entry> entryList = new List<Entry>();
            try
            {
                using (SQLiteConnection sqliteConnection = new SQLiteConnection(this.connectionString))
                {
                    //
                    // "SELECT ID FROM Entry ORDER BY Timestamp ASC LIMIT 2 OFFSET 1"
                    //
                    // LIMIT = pageSize
                    // OFFSET = page * pageSize
                    //
                    sqliteConnection.Open();
                    SQLiteCommand sqlCommand = new SQLiteCommand(SQLiteDataRepository.SQL_SEARCH, sqliteConnection);
                    sqlCommand.Parameters.AddWithValue("@Search", "%" + searchString + "%");
                    sqlCommand.Parameters.AddWithValue("@Limit", pageSize);
                    sqlCommand.Parameters.AddWithValue("@Offset", (page - 1) * pageSize);
                    using (SQLiteDataReader reader = sqlCommand.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Entry entry = new Entry();
                            entry.ID = reader.GetInt32(0);
                            if (!reader.IsDBNull(1))
                            {
                                entry.ParentID = reader.GetInt32(1);
                            }
                            else
                            {
                                entry.ParentID = null;
                            }
                            entry.AccountID = reader.GetInt32(2);
                            entry.Title = reader.GetString(3);
                            entry.Timestamp = reader.GetDateTime(4);

                            entry.Author = new Account();
                            entry.Author.ID = entry.AccountID;
                            entry.Author.Name = reader.GetString(5);
                            entryList.Add(entry);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                return null;
            }
            return entryList;
        }
Example #6
0
        public Entry GetEntry(int id)
        {
            try
            {
                Entry entry = new Entry();
                using (SQLiteConnection sqliteConnection = new SQLiteConnection(this.connectionString))
                {
                    sqliteConnection.Open();
                    SQLiteCommand sqlCommand = new SQLiteCommand(SQLiteDataRepository.SQL_ENTRY_SELECT_BY_ID_WITH_Account, sqliteConnection);
                    sqlCommand.Parameters.AddWithValue("@ID", id);

                    using (SQLiteDataReader reader = sqlCommand.ExecuteReader())
                    {
                        //
                        // Read the first record only
                        //
                        if (reader.Read())
                        {
                            entry = ReadEntryAuthor(reader);
                        }
                    }
                }
                return entry;
            }
            catch (Exception exception)
            {
                return null;
            }
        }