public void Edit() { int _id; int.TryParse(Console.ReadLine(), out _id); Console.WriteLine("Do you want edit note?"); string someChoose = Console.ReadLine(); if (someChoose.ToLower() == _stateAsk) { using (NoteContext db = new NoteContext()) { Note noteForEdit = db.Notes.FirstOrDefault(note => note.Id == _id); if (noteForEdit != null) { Console.WriteLine("Edit name"); string name = Console.ReadLine(); noteForEdit.Name = name; Console.WriteLine("Edit text"); string text = Console.ReadLine(); noteForEdit.Name = text; noteForEdit.Date = DateTime.UtcNow; db.Entry(noteForEdit).State = EntityState.Modified; db.SaveChanges(); } GetNote(); } } }
public void Add() { using (NoteContext db = new NoteContext()) { IDbSet <Note> _notes = db.Notes; Console.WriteLine("Do you want create new note"); string someChoose = Console.ReadLine(); if (someChoose.ToLower() == _stateAsk) { Console.WriteLine("Write name note"); string _name = Console.ReadLine(); Console.WriteLine("Write text note"); string _text = Console.ReadLine(); db.Notes.Add(new Note() { Name = _name, Text = _text, Date = DateTime.UtcNow }); db.SaveChanges(); GetNote(); } } }
public void SortedNote() { using (NoteContext db = new NoteContext()) { Console.WriteLine("You want sorted our data base?"); string ask = Console.ReadLine()?.ToLower(); while (someEvent == ask) { Console.WriteLine("How are you want sorted our notes: date, name, id"); string searchCriteria = Console.ReadLine()?.ToLower(); switch (searchCriteria) { case "id": IEnumerable <Note> sortedNodeId = db.Notes.OrderBy(note => note.Id); ShowSort(sortedNodeId); break; case "name": IEnumerable <Note> sortedNodeName = db.Notes.OrderBy(note => note.Name); ShowSort(sortedNodeName); break; case "date": IEnumerable <Note> sortedNodeDate = db.Notes.OrderBy(note => note.Date); ShowSort(sortedNodeDate); break; default: break; } Console.WriteLine("Do you want sorted again"); ask = Console.ReadLine(); } } }
public void GetNote() { using (NoteContext db = new NoteContext()) { Console.WriteLine("Show all Notes"); string someState = Console.ReadLine(); if (someState.ToLower() == _stateAsk) { foreach (var note in db.Notes) { Console.WriteLine($"{note.Id}.{note.Name} = {note.Text} : {note.Date}"); } } } }
static void Main(string[] args) { using (NoteContext db = new NoteContext()) { //db.Notes.Add(new Note { Name = "NoteMe", Text = "Get salery", Date = new DateTime(2015, 7, 20, 18, 30, 25) }); //db.Notes.Add(new Note { Name = "ANoteMeAgain", Text = "Get ny friend salery", Date = new DateTime(2017, 3, 16, 18, 10, 25) }); db.SaveChanges(); var note = db.Notes; foreach (var nt in note) { Console.WriteLine($"{nt.Id}.{nt.Name} = {nt.Text} : {nt.Date}"); } } Console.WriteLine("Do you want to do operation(yes/no): 1-delete, 2-modif 3-add"); string someAction = Console.ReadLine(); if (someAction.ToLower() == "yes") { Operation operation = new Operation(); operation.SomeOperation(); } }
public void Delete() { Console.WriteLine("How you want delete element "); int _id; int.TryParse(Console.ReadLine(), out _id); using (NoteContext db = new NoteContext()) { IDbSet <Note> notes = db.Notes; Note noteForDelete = notes.FirstOrDefault(note => note.Id == _id); if (noteForDelete != null) { db.Entry(noteForDelete).State = EntityState.Deleted; db.SaveChanges(); } else { Console.WriteLine("This index empty"); } GetNote(); } }