//lay 1 note theo id public static Note getANote(int id) { using (var _context = new APPNOTEEntities()) { var note = (from u in (from i in _context.Note.AsEnumerable() select i) where u.Id == id select new { Id = u.Id, Tittle = u.Tittle, Content = u.Content, Info = u.Info, isTrash = u.isTrash, isPin = u.isPin, Tags = u.Tag }) .Select(x => new Note { Id = x.Id, Tittle = x.Tittle, Content = x.Content, Info = x.Info, isTrash = x.isTrash, isPin = x.isPin, Tag = x.Tags }).SingleOrDefault(); return(note); } }
//lay list tat ca note theo 1 chuoi tim kiem searchstr public static List <Note> getAllNotes(string searchstr) { using (var _context = new APPNOTEEntities()) { var notes = (from u in _context.Note.AsEnumerable() where u.Content.Contains(searchstr) select new { Id = u.Id, Tittle = u.Tittle, Content = u.Content, Info = u.Info, isTrash = u.isTrash, isPin = u.isPin, Tags = u.Tag }) .Select(x => new Note { Id = x.Id, Tittle = x.Tittle, Content = x.Content, Info = x.Info, isTrash = x.isTrash, isPin = x.isPin, Tag = x.Tags }).ToList(); return(notes); } }
//cap nhat note public static bool UpdateNote(Note note) { using (var _context = new APPNOTEEntities()) { _context.Note.AddOrUpdate(note); _context.SaveChanges(); return(true); } }
//them tag public static bool AddTag(Tag tag) { using (var _context = new APPNOTEEntities()) { _context.Tag.Add(tag); _context.SaveChanges(); return(true); } }
//lay Id tiep theo public static int getIdFromDb() { using (var _context = new APPNOTEEntities()) { var id = (from t in _context.Note select t.Id).ToList(); if (id.Count <= 0) { return(1); } return(id.Max() + 1); } }
//lay tag theo tagname public static Tag getATag(string tagname) { using (var _context = new APPNOTEEntities()) { var tag = (from u in _context.Tag where u.tagname == tagname select u).ToList(); if (tag.Count == 1) { return(tag[0]); } else { return(null); } } }
//lay list tat ca tag public static List <Tag> getAllTags() { using (var _context = new APPNOTEEntities()) { var tags = (from u in _context.Tag.AsEnumerable() select new { tagname = u.tagname, Notes = u.Note }) .Select(x => new Tag { tagname = x.tagname, Note = x.Notes }).ToList(); return(tags); } }
//xoa note public static bool DeleteNote(Note note) { using (var _context = new APPNOTEEntities()) { var dbnote = (from u in _context.Note where u.Id == note.Id select u).SingleOrDefault(); foreach (var tag in dbnote.Tag) { foreach (var u in tag.Note) { if (u.Id == note.Id) { tag.Note.Remove(u); break; } } } _context.Note.Remove(dbnote); _context.SaveChanges(); return(true); } }
//xoa tag public static bool DeleteTag(Tag tag) { using (var _context = new APPNOTEEntities()) { var dbtag = (from u in _context.Tag where u.tagname == tag.tagname select u).SingleOrDefault(); foreach (var note in dbtag.Note) { foreach (var u in note.Tag) { if (u.tagname == tag.tagname) { note.Tag.Remove(u); break; } } } _context.Tag.Remove(dbtag); _context.SaveChanges(); return(true); } }
//them note public static bool AddNote(Note note) { try { using (var _context = new APPNOTEEntities()) { foreach (var tag in note.Tag) { var tagdb = (from u in _context.Tag where u.tagname == tag.tagname select u).Single(); tagdb.Note.Add(note); } note.Tag.Clear(); _context.Note.Add(note); _context.SaveChanges(); return(true); } } catch { return(false); } }