コード例 #1
0
 public Notes GetNote(int noteId)
 {
     using (var context = new MiniCMSEntities())
     {
         return GetNoteInternal(noteId, context);
     }
 }
コード例 #2
0
 public int GetNotesCount()
 {
     using (var context = new MiniCMSEntities())
     {
         return context.Notes.Count();
     }
 }
コード例 #3
0
        private static Notes GetNoteInternal(int noteId, MiniCMSEntities context)
        {
            var note = context.Notes.FirstOrDefault(x => x.Id == noteId);
            if (note != null)
                return note;

            throw new InvalidOperationException("Note with this id does not exist");
        }
コード例 #4
0
 public void DeleteNote(int noteId)
 {
     using (var context = new MiniCMSEntities())
     {
         var note = GetNoteInternal(noteId, context);
         context.Notes.Remove(note);
         context.SaveChanges();
     }
 }
コード例 #5
0
        public IList<Notes> GetNotes(int currentPage)
        {
            using (var context = new MiniCMSEntities())
            {
                var skip = (currentPage - 1) * PageSize; 

                return context.Notes.OrderByDescending(x => x.Id).Skip(skip).Take(10).ToList();
            }
        }
コード例 #6
0
        public int AddNote(Notes note)
        {
            using (var context = new MiniCMSEntities())
            {
                if (IsExistName(note.Name, context))
                    throw new InvalidOperationException("This name is already used");

                context.Notes.Add(note);
                context.SaveChanges();

                return note.Id;
            }
        }
コード例 #7
0
        public void ChangeNote(int noteId, string noteName, string htmlContent, string shortNote)
        {
            using (var context = new MiniCMSEntities())
            {
                var note = GetNoteInternal(noteId, context);

                if (note.Name != noteName)
                {
                    if (IsExistName(noteName, context))
                        throw new InvalidOperationException("This name is already used");
                }

                note.Name = noteName;
                note.HTML = htmlContent;
                note.ShortNote = shortNote;

                context.SaveChanges();
            }
        }
コード例 #8
0
 bool IsExistName(string name, MiniCMSEntities context)
 {
     return context.Notes.Count(x => x.Name == name) > 0;
 }