Exemple #1
0
        public static bool VoteSection(int sectionID, string username, bool votetype)
        {
            if (String.IsNullOrWhiteSpace(username))
            {
                return false;
            }

            SectionsVM section = getSectionFromId(sectionID);

            if (section != null
                && IsValidStory(section.StoryID)
                && GetCurrentSeq(section.StoryID) == section.SequenceNumber)
            {
                try
                {
                    using (var db = new WriteEntities())
                    {

                        List<SectionTransaction> t = db.SectionTransactions.Where(tr => tr.Username == username
                                    && tr.SectionID == section.SectionID).ToList();

                        if (t != null && t.Count == 1)
                        {
                            if (votetype == true)
                            {
                                t.FirstOrDefault().Vote = 1;
                            }
                            else
                            {
                                t.FirstOrDefault().Vote = -1;
                            }

                            db.SaveChanges();

                            return true;
                        }
                        else if (t != null && t.Count == 0)
                        {
                            SectionTransaction firstTrans = new SectionTransaction();

                            if (votetype == true)
                            {
                                firstTrans.Vote = 1;
                            }
                            else
                            {
                                firstTrans.Vote = -1;
                            }

                            firstTrans.Username = username;
                            firstTrans.SectionID = sectionID;

                            db.SectionTransactions.Add(firstTrans);
                            db.SaveChanges();

                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
                catch (Exception ex)
                {
                    return false;
                }
            }

            return false;
        }
Exemple #2
0
        public static bool InsertSection(SectionsVM submission)
        {
            if (IsValidStory(submission.StoryID))
            {
                if (GetCurrentSeq(submission.StoryID) == submission.SequenceNumber)
                {
                    //map submission back to data model: Section
                    Section dataModel = new Section();

                    dataModel.SectionContent = submission.SectionContent;
                    dataModel.SequenceNumber = submission.SequenceNumber;
                    dataModel.StoryID = submission.StoryID;

                    //default properties upon submission:
                    dataModel.IsCommitted = false;
                    dataModel.WrittenDate = DateTime.Now;
                    //dataModel.Downvotes = 0;
                    //dataModel.Upvotes = 0;
                    dataModel.Author = "Frontend User";

                    try
                    {
                        using (var db = new WriteEntities())
                        {
                            db.Sections.Add(dataModel);
                            db.SaveChanges();

                            return true;
                        }
                    }
                    catch (Exception ex)
                    {
                        return false;
                    }
                }
            }

            return false;
        }