コード例 #1
0
        public static string ColoredCheckBoxes(this HtmlHelper htmlHelper, NoteWithCategories model, object allAvailableCategories, string checkBoxName)
        {
            IEnumerable <Category> allCategories = allAvailableCategories as IEnumerable <Category>;

            if (allCategories != null)
            {
                List <string> modelCategoryColors = (model != null && model.Categories.Any()) ?
                                                    model.Categories.Select(color => color.Color).ToList() :
                                                    new List <string>();

                StringBuilder tmp = new StringBuilder();
                foreach (Category category in allCategories)
                {
                    tmp.AppendFormat(
                        CultureInfo.InvariantCulture,
                        Format,
                        !String.IsNullOrEmpty(category.Id) ? category.Id : category.Color,
                        (modelCategoryColors.Contains(category.Color) ? " checked=\"checked\"" : String.Empty),
                        category.Color,
                        category.Name,
                        checkBoxName);
                }

                return("<ul>" + tmp + "</ul>");
            }

            return("Wrong Data");
        }
コード例 #2
0
        public void Create(Note noteToAdd, IEnumerable <Category> newCategories)
        {
            NoteWithCategories note = NoteWithCategories.Convert(noteToAdd, newCategories);

            session.Store(note);
            session.SaveChanges();
        }
コード例 #3
0
        public void ShouldReadAll()
        {
            // Arrange
            var note = new NoteWithCategories {
                Message = "Message"
            };
            var expectedNotes = new List <NoteWithCategories> {
                note
            };

            // Depedent-On Component
            var session = Substitute.For <IDocumentSession>();
            var query   = Substitute.For <IRavenQueryable <NoteWithCategories> >();

            // Indirect Input
            session.Query <NoteWithCategories>().Returns(query);
            query.GetEnumerator().Returns(expectedNotes.GetEnumerator());

            // System under Test
            var repository = new RavenDbRepository(session);

            // Act
            IEnumerable <NoteWithCategories> notes = repository.ReadAll();

            // Assert
            Assert.That(notes, Is.EquivalentTo(expectedNotes));
            Assert.That(notes.First().Message, Is.EqualTo(note.Message));
        }
コード例 #4
0
        public void EditNote(int id, NoteWithCategories newNote)
        {
            var oldNote = _context.Notes.Where(n => n.NoteID == id &&
                                               n.Timestamp == newNote.Timestamp).FirstOrDefault();

            if (oldNote == null)
            {
                throw new Exception("The file does not exist or has been changed");
            }
            // Find differences in standard fields and replace if needed
            if (oldNote.Title != newNote.Title ||
                oldNote.Description != newNote.Description ||
                oldNote.NoteDate != newNote.NoteDate ||
                oldNote.IsMarkdown != newNote.IsMarkdown)
            {
                oldNote.Title       = newNote.Title;
                oldNote.Description = newNote.Description;
                oldNote.NoteDate    = newNote.NoteDate;
                oldNote.IsMarkdown  = newNote.IsMarkdown;
            }

            // Find differences in categories

            var categoriesInDb = _context.NoteCategories
                                 .Where(nc => nc.Note == oldNote)
                                 .Include(nc => nc.Category);

            var newCategories = newNote.Categories
                                .Except(categoriesInDb.Select(c => c.Category.Title));

            foreach (var newCategory in newCategories)
            {
                var categoryInDb = _context.Categories
                                   .Where(c => c.Title == newCategory)
                                   .FirstOrDefault();

                if (categoryInDb == null)
                {
                    categoryInDb = new Category
                    {
                        Title = newCategory
                    };
                }
                _context.Add(new NoteCategory
                {
                    Note     = oldNote,
                    Category = categoryInDb
                });
            }

            var deletedCategories = categoriesInDb
                                    .Where(c => !newNote.Categories.Contains(c.Category.Title));

            _context.RemoveRange(deletedCategories);
            _context.SaveChanges();
        }
コード例 #5
0
        public void Create(Note noteToAdd, IEnumerable <Category> newCategories)
        {
            NoteWithCategories note = NoteWithCategories.Convert(noteToAdd, newCategories);

            using (var notes = redisClient.GetTypedClient <NoteWithCategories>())
            {
                note.Id = notes.GetNextSequence().AsString();
                notes.Store(note);
            }
        }
コード例 #6
0
 public ActionResult PostNote([FromBody] NoteWithCategories note)
 {
     if (_service.DoesTheFileExist(note.Title))
     {
         return(BadRequest());
     }
     _service.AddNote(note);
     _service.DeleteDanglingCategories();
     return(Ok());
 }
コード例 #7
0
        public void Update(Note noteToEdit, IEnumerable <Category> newCategories)
        {
            NoteWithCategories note = Read(noteToEdit.Id);

            note.Title      = noteToEdit.Title;
            note.Message    = noteToEdit.Message;
            note.Categories = newCategories;

            var query = Query.EQ("_id", ObjectId.Parse(noteToEdit.Id));

            notes.Update(query, Builders.Update.Replace(note));
        }
コード例 #8
0
        public void Update(Note noteToEdit, IEnumerable <Category> newCategories)
        {
            using (var notes = redisClient.GetTypedClient <NoteWithCategories>())
            {
                NoteWithCategories note = Read(noteToEdit.Id);

                note.Title      = noteToEdit.Title;
                note.Message    = noteToEdit.Message;
                note.Categories = newCategories;

                notes.Store(note);
            }
        }
コード例 #9
0
 public ActionResult UpdateNote(int id, [FromBody] NoteWithCategories note)
 {
     try
     {
         _service.EditNote(id, note);
         _service.DeleteDanglingCategories();
     }
     catch
     {
         return(BadRequest());
     }
     return(Ok());
 }
コード例 #10
0
        public ActionResult Edit(Note noteToEdit, int[] newCategories)
        {
            #region using the validator
            //// UNDONE: ValidatorIntroduceAspect - using the interface
            #if false
            if (!this.ServiceAsValidator.IsValid(noteToEdit))
            {
                IValidationResults result = this.ServiceAsValidator.Validate(noteToEdit);
                this.ReplaceModelState(result);
                return(this.Edit(noteToEdit.NoteId));
            }
            #endif
            #endregion

            NoteWithCategories changedNote = this.WebNoteService.Update(noteToEdit, newCategories);
            return(View(changedNote));
        }
コード例 #11
0
        public void CreateACollectionWithSomeData()
        {
            CreateCollectionIfNecessary(database);

            MongoCollection <Note> notes = database.GetCollection <Note>(CollectionName);

            // Let's create a couple of documents!
            var note = new Note
            {
                Title   = "MongoDB is fun for developers!",
                Message = "With Mongo Documents, there is hardly any need for excessive OR Mapping, since documents resemble objects closer that relations do!"
            };

            var note2 = new Note
            {
                Title   = "Hello",
                Message = "Welcome to MongoDB!"
            };

            var noteWithCategories = new NoteWithCategories
            {
                Title      = "RavenDb",
                Message    = "RavenDb is also pretty cool, too!",
                Categories = new[] { new Category {
                                         Name = "Very Important"
                                     } }
            };

            // Insert
            notes.Insert(note);
            notes.Insert(note2);
            notes.Insert(noteWithCategories);

            // Let's find them
            List <Note> allNotes = notes.FindAllAs <Note>().ToList();

            Assert.That(allNotes.Count, Is.EqualTo(3));

            // You can also marshal them as the subclass, but for this example only one will have the cargories set!
            List <NoteWithCategories> allNotesWithcategories = notes.FindAllAs <NoteWithCategories>().ToList();

            Assert.That(allNotesWithcategories.Count, Is.EqualTo(3));
            Assert.That(allNotesWithcategories.Count(n => n.Categories != null && n.Categories.Any()), Is.EqualTo(1));
        }
コード例 #12
0
        public int AddNote(NoteWithCategories note)
        {
            var oldCategories = _context.Categories
                                .Where(c => note.Categories.Contains(c.Title))
                                .Include(c => c.NoteCategories)
                                .ToList();

            var newCategories = note.Categories
                                .Except(oldCategories.Select(oc => oc.Title));

            var newNote = new Note
            {
                NoteDate    = note.NoteDate,
                Title       = note.Title,
                Description = note.Description,
                IsMarkdown  = note.IsMarkdown
            };

            _context.Add(newNote);
            foreach (var oldCat in oldCategories)
            {
                var newNoteCat = new NoteCategory
                {
                    Note     = newNote,
                    Category = oldCat
                };
                _context.Add(newNoteCat);
            }
            foreach (var newCat in newCategories)
            {
                var newNoteCat = new NoteCategory
                {
                    Note     = newNote,
                    Category = new Category
                    {
                        Title = newCat
                    }
                };
                _context.Add(newNoteCat);
            }
            _context.SaveChanges();
            return(newNote.NoteID);
        }
コード例 #13
0
        public void ShouldReadOne()
        {
            // Arrange
            const string id           = "Id";
            var          expectedNote = new NoteWithCategories {
                Id = id, Message = "Message"
            };

            // Depedent-On Component
            var session = Substitute.For <IDocumentSession>();

            // Indirect Input
            session.Load <NoteWithCategories>(Arg.Is(id)).Returns(expectedNote);

            // System under Test
            var repository = new RavenDbRepository(session);

            // Act
            NoteWithCategories note = repository.Read(id);

            // Assert
            Assert.That(note.Message, Is.EqualTo(note.Message));
        }
コード例 #14
0
        public void ShouldDeleteANote()
        {
            // Arrange
            const string id   = "id";
            var          note = new NoteWithCategories {
                Id = id, Message = "Message"
            };

            // Depedent-On Component
            var session = Substitute.For <IDocumentSession>();

            session.Load <NoteWithCategories>(id).Returns(note);

            // System under Test
            var repository = new RavenDbRepository(session);

            // Act
            repository.Delete(id);

            // Assert / Indirect Output
            session.Received().Delete(Arg.Is(note));
            session.Received().SaveChanges();
        }
コード例 #15
0
        public static string ColoredCheckBoxes(this HtmlHelper htmlHelper, NoteWithCategories model, object allAvailableCategories, string checkBoxName)
        {
            IEnumerable <Category> allCategories = allAvailableCategories as IEnumerable <Category>;

            if (allCategories != null)
            {
                StringBuilder tmp = new StringBuilder();
                foreach (Category category in allCategories)
                {
                    tmp.AppendFormat(
                        CultureInfo.InvariantCulture,
                        Format,
                        category.CategoryId,                                                                            // 1
                        (model != null && model.Categories.Contains(category) ? " checked=\"checked\"" : String.Empty), // 2
                        category.Color,                                                                                 // 3
                        category.Name,                                                                                  // 4
                        checkBoxName);                                                                                  // 5
                }

                return("<ul>" + tmp + "</ul>");
            }

            return("Wrong Data");
        }
コード例 #16
0
        public ActionResult Edit(string id)
        {
            NoteWithCategories note = Repository.Read(id);

            return(View(note));
        }
コード例 #17
0
        public void Create(Note noteToAdd, IEnumerable <Category> newCategories)
        {
            NoteWithCategories note = NoteWithCategories.Convert(noteToAdd, newCategories);

            notes.Insert(note);
        }
コード例 #18
0
        public ActionResult Edit(int id)
        {
            NoteWithCategories note = this.WebNoteService.Read(id);

            return(View(note));
        }