Пример #1
0
        public SnippetsViewModel GetSnippetCategories(SnippetsViewModel model)
        {
            var categories = from r in db.Categories
                             where r.CategoryName != ""
                             select r;

            model.CategoryList = categories.ToList <Category>();
            return(model);
        }
Пример #2
0
        public SnippetsViewModel EditSnippet(SnippetsViewModel model, int id)
        {
            Snippet snippet = db.Snippets.Find(id);

            CategorySnippetAssociations snippetCategoryAssociation = db.CategorySnippetAssociations.Find(id);

            //If we have new notes being passed to the controller
            if (model.NoteList.Any() && model.NoteList != null)
            {
                //Remove all of the previous notes
                List <Note> removeNotes = (from n in db.Notes
                                           where n.NoteSnippetId == id
                                           select n).ToList();

                for (int i = 0; i < removeNotes.Count(); i++)
                {
                    db.Notes.Remove(removeNotes[i]);
                }

                //Add all the new notes
                for (int i = 0; i < model.NoteCount; i++)
                {
                    Note newNote = new Note
                    {
                        NoteSnippetId = snippet.SnippetId,
                        NoteTitle     = model.NoteList[i].NoteTitle,
                        NoteContent   = model.NoteList[i].NoteContent,
                        NoteCount     = model.NoteCount
                    };

                    db.Notes.Add(newNote);
                }
            }

            snippet.SnippetName        = Sanitizer.GetSafeHtmlFragment(model.SnippetName);
            snippet.SnippetContent     = model.SnippetContent;
            snippet.SnippetDescription = model.SnippetDescription;
            snippet.ReferenceURL       = Sanitizer.GetSafeHtmlFragment(model.ReferenceUrl);
            snippet.SnippetLanguage    = Sanitizer.GetSafeHtmlFragment(model.SnippetLanguage);

            snippetCategoryAssociation.CategoryAssociationId = model.SelectedCategoryId;

            db.Entry(snippetCategoryAssociation).State = EntityState.Modified;
            db.Entry(snippet).State = EntityState.Modified;
            db.SaveChanges();

            return(model);
        }
Пример #3
0
        public void CreateNewSnippet(SnippetsViewModel model)
        {
            Snippet snippet = new Snippet
            {
                SnippetName        = Sanitizer.GetSafeHtmlFragment(model.SnippetName),
                SnippetContent     = model.SnippetContent,
                SnippetDescription = Sanitizer.GetSafeHtmlFragment(model.SnippetDescription),
                ReferenceURL       = Sanitizer.GetSafeHtmlFragment(model.ReferenceUrl),
                SnippetLanguage    = Sanitizer.GetSafeHtmlFragment(model.SnippetLanguage)
            };

            CategorySnippetAssociations snippetCategory = new CategorySnippetAssociations
            {
                SnippetAssociationId  = snippet.SnippetId,
                CategoryAssociationId = model.SelectedCategoryId
            };

            if (model.NoteList != null)
            {
                for (int i = 0; i < model.NoteCount; i++)
                {
                    Note newNote = new Note
                    {
                        NoteSnippetId = snippet.SnippetId,
                        NoteTitle     = model.NoteList[i].NoteTitle,
                        NoteContent   = model.NoteList[i].NoteContent,
                        NoteCount     = model.NoteCount,
                    };

                    db.Notes.Add(newNote);
                }
            }

            db.CategorySnippetAssociations.Add(snippetCategory);
            db.Snippets.Add(snippet);
            db.SaveChanges();
        }