Пример #1
0
        /// <summary>
        /// Executes the command.
        /// </summary>
        /// <param name="parameter"></param>
        public async void Execute(object parameter)
        {
            ContentDialogResult result = await delete.ShowAsync();

            if (result == ContentDialogResult.Primary)
            {
                // Get the selected note
                Models.NoteModel noteToDelete = this.noteViewModel.SelectedNote;

                // Remove the selected note from the notes lists
                this.noteViewModel.Notes.Remove(noteToDelete);
                this.noteViewModel.NotesForLV.Remove(noteToDelete);
                this.noteViewModel.FirePropertyChanged("NotesForLV");

                // Set the selected note to null
                this.noteViewModel.SelectedNote = null;

                // Reset the notes properties
                this.noteViewModel.NoteTitle = "";
                this.noteViewModel.FirePropertyChanged("NoteTitle");
                this.noteViewModel.NoteContent = new Models.ContentModel();
                this.noteViewModel.FirePropertyChanged("NoteContent");

                // Notify that the selected note has changed
                this.noteViewModel.FirePropertyChanged("SelectedNote");

                // Delete the data file in the repo
                //Repositories.NotesRepo.DeleteNoteFile(noteToDelete);

                // Delete the record from the database
                Repositories.DatabaseRepo.DeleteNote(noteToDelete);
            }
        }
Пример #2
0
 /// <summary>
 /// Load existing note
 /// </summary>
 /// <param name="note"></param>
 /// <param name="noteModel"></param>
 /// <param name="deck"></param>
 public NoteViewModel(Models.Note note, Models.NoteModel noteModel, DeckViewModel deck)
 {
     _note      = note;
     _noteModel = noteModel;
     Deck       = deck;
     Initialize();
 }
Пример #3
0
        /// <summary>
        /// make sure weather json or query string is passed in, it goes into json format
        /// </summary>
        /// <returns></returns>
        private dynamic xqueryToJson()
        {
            string jq = WebUtility.UrlDecode(Request.RequestUri.Query);

            if (jq.StartsWith("?") && jq.Length > 1)
            {
                jq = jq.Substring(1);
            }

            if (jq.StartsWith("{")) // JSON
            {
                dynamic JsonInput = Newtonsoft.Json.JsonConvert.DeserializeObject(jq);
                return(JsonInput);
            }
            else
            {
                // parse
                string[] argv   = jq.Split('&');
                string   pvalue = "";
                foreach (string ctarg in argv)
                {
                    string[] ct = ctarg.Split('=');
                    if (ct[0].ToLower() == "query")
                    {
                        pvalue = ct[1];
                    }
                }
                Models.NoteModel thenote = new Models.NoteModel();
                thenote.body = pvalue;
                dynamic JsonInput = Newtonsoft.Json.JsonConvert.SerializeObject(thenote);
                JsonInput = Newtonsoft.Json.JsonConvert.DeserializeObject(JsonInput);
                return(JsonInput);
            }
        }
Пример #4
0
        // PUT api/values/5
        /// <summary>
        /// Update datastore
        /// </summary>
        /// <param name="id"></param>
        /// <param name="value"></param>
        public Models.NoteModel Put([FromBody] dynamic value)
        {
            Models.NoteModel thenote;
            int    pid = 0;
            string sid = (string)value.id;

            if (sid.Length > 0)
            {
                int.TryParse(sid, out pid);
            }

            if (pid > 0)
            {
                string   newvalue = (string)value.body;
                string[] oldlines = File.ReadAllLines(sfilepath);
                // update the new line
                oldlines[pid - 1] = newvalue;
                File.WriteAllLines(sfilepath, oldlines);
                thenote      = new Models.NoteModel(pid);
                thenote.body = newvalue;
            }
            else
            {
                thenote      = new Models.NoteModel(-1);
                thenote.body = "Empty or invalid id";
            }
            return(thenote);
        }
Пример #5
0
        /// <summary>
        /// Executes the command.
        /// </summary>
        /// <param name="parameter"></param>
        public void Execute(object parameter)
        {
            // Create a new note
            Models.NoteModel newNote = new Models.NoteModel();

            // Add the new note to the notes lists
            noteViewModel.Notes.Add(newNote);
            noteViewModel.NotesForLV.Add(newNote);

            // Make the new note the selected note
            // Then update the UI to go to the new note
            noteViewModel.SelectedNote = newNote;
            noteViewModel.FirePropertyChanged("SelectedNote");
        }
Пример #6
0
        /// <summary>
        /// Deletes the give note's data file.
        /// </summary>
        /// <param name="selected"></param>
        public async static void DeleteNoteFile(Models.NoteModel selected)
        {
            string fileName = selected.Title.Replace(" ", "_") + ".txt";

            try {
                // Get the targeted file, then delete it asynchronously
                StorageFile noteToDelete =
                    await notesFolder.GetFileAsync(fileName);

                await noteToDelete.DeleteAsync();
            } catch (Exception) {
                Debug.WriteLine("An error occurred when deleting the note file.");
            }
        }
Пример #7
0
        /// <summary>
        /// Saves the given note's data to a file.
        /// </summary>
        /// <param name="selected"></param>
        public async static void SaveNoteToFile(Models.NoteModel selected)
        {
            string fileName = selected.Title.Replace(" ", "_") + ".txt";

            try {
                // Create the file asynchronously, then add the content asynchronously
                StorageFile noteFile =
                    await notesFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);

                await FileIO.AppendTextAsync(noteFile, selected.Content.Rtf);
            } catch (Exception) {
                Debug.WriteLine("An error occured when saving the note file.");
            }
        }
Пример #8
0
        public void AddFieldSettings(Models.NoteModel noteModel)
        {
            var prefix = noteModel.crowdanki_uuid;

            foreach (var fld in noteModel.flds)
            {
                FieldSettings.Add($"{prefix}_{fld.name}", new FieldSettings()
                {
                    Language        = _defaultLanguage,
                    Keep            = false,
                    ScriptOverwrite = false,
                    Script          = _defaultScript,
                });
            }
        }
Пример #9
0
        /// <summary>
        /// Create a new note
        /// </summary>
        /// <param name="noteModel"></param>
        /// <param name="deck"></param>
        public NoteViewModel(Models.NoteModel noteModel, DeckViewModel deck)
        {
            _note = new Models.Note()
            {
                __type__        = "Note",
                data            = string.Empty,
                fields          = new List <string>(new string[noteModel.flds.Count]),
                flags           = 0,
                guid            = Utils.Guid64(),
                note_model_uuid = noteModel.crowdanki_uuid,
                tags            = new List <string>()
            };
            _noteModel = noteModel;
            Deck       = deck;

            Initialize();
        }
        public void AddNote_NotExistingUser_ThrowsUserNotFoundException()
        {
            //arrange
            var notes       = new List <NoteDto>();
            var noteService = new NoteService(new FakeUserRepository(Enumerable.Empty <UserDto>()),
                                              new FakeNoteRepository(notes));

            //act

            //assert
            var note = new Models.NoteModel
            {
                UserId = 1
            };

            Assert.Throws <UserNotFoundException>(() => noteService.AddNote(note));
        }
Пример #11
0
        // DELETE api/values/5
        /// <summary>
        /// Indicate deleted in datastore
        /// </summary>
        /// <param name="id"></param>
        public Models.NoteModel Delete(int id)
        {
            Models.NoteModel thenote;

            if (id > 0)
            {
                string[] oldlines = File.ReadAllLines(sfilepath);
                // update the new line
                oldlines[id - 1] = "";
                File.WriteAllLines(sfilepath, oldlines);
                thenote      = new Models.NoteModel(id);
                thenote.body = "Deleted";
            }
            else
            {
                thenote      = new Models.NoteModel(-1);
                thenote.body = "Empty or invalid id";
            }
            return(thenote);
        }
Пример #12
0
        public async void Execute(object parameter)
        {
            SaveDialog          saveDialog = new SaveDialog();
            ContentDialogResult result     = await saveDialog.ShowAsync();

            // Creates an error message and returns.
            if (result != ContentDialogResult.Primary)
            {
                new ContentDialog()
                {
                    Title = "Cancelled Creation.", PrimaryButtonText = "Okay."
                };
                return;
            }

            TextBox box = (TextBox)parameter;

            Models.NoteModel newNote = new Models.NoteModel(saveDialog.NoteTitle, box.Text);

            // Trys to save the new note to the file system.
            try
            {
                Repository.NotesRepo.SaveNotesToFile(newNote);
            }
            catch (Exception)
            {
                Debug.WriteLine("Error saving new note to folder.");
                return;
            }

            // Adds note to the list, changes the selected node, and refreshes the list visually.
            notesViewModel.AllNotes.Add(newNote);
            notesViewModel.SelectedNote = newNote;
            notesViewModel.FilterNotes();

            new ContentDialog()
            {
                Title = "Saved Note Sucessfully.", PrimaryButtonText = "Okay."
            };
        }
Пример #13
0
        public void AddNote_NotExistingUser_ThrowsUserNotFoundException()
        {
            //arrange
            var notes = new List <NoteDto>();
            var mockUserRepository = new Mock <IRepository <UserDto> >();

            mockUserRepository.Setup(repo => repo.GetAll())
            .Returns(Enumerable.Empty <UserDto>);
            var mockNoteRepository = new Mock <IRepository <NoteDto> >();
            var noteService        = new NoteService(mockUserRepository.Object,
                                                     mockNoteRepository.Object);

            //act

            //assert
            var note = new Models.NoteModel
            {
                UserId = 1
            };

            Assert.Throws <UserNotFoundException>(() => noteService.AddNote(note));
        }
Пример #14
0
        // POST api/values
        /// <summary>
        /// Save to data store. Return New ID
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public Models.NoteModel Post([FromBody] dynamic value)
        {
            Models.NoteModel thenote = new Models.NoteModel();

            string bod = (string)value.body;

            if (bod.Trim().Length > 0)
            {
                fileCheck(sfilepath);
                using (StreamWriter sw = File.AppendText(sfilepath))
                {
                    sw.WriteLine(bod);
                    sw.Dispose();
                }
                // now get the count
                int linect = 0;
                using (StreamReader sr = new StreamReader(sfilepath))
                {
                    string aline = "";
                    while ((aline = sr.ReadLine()) != null)
                    {
                        linect++;
                    }
                    sr.Dispose();
                }

                thenote      = new Models.NoteModel(linect);
                thenote.body = bod;
            }
            else
            {
                thenote      = new Models.NoteModel(-1);
                thenote.body = "Empty or invalid note";
            }
            return(thenote);
        }
Пример #15
0
 public ActionResult CreateNote(Models.NoteModel note)
 {
     return(new JsonResult(noteService.SetNote(note)));
 }
Пример #16
0
        /// <summary>
        /// Get by id. if id=0 get all. if filter not empty then apply filter
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Models.Notes Get(int id)
        {
            // get all - open text file, loop over every item
            Models.Notes thenotes = new Models.Notes();

            fileCheck(sfilepath);
            int lineno = 0;

            using (StreamReader sr = new StreamReader(sfilepath))
            {
                String aline = "";
                if (id > 0)
                {  // skip to specific item
                    while ((aline = sr.ReadLine()) != null)
                    {
                        lineno++;
                        if (lineno < id)
                        {
                            continue;
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (aline == null)
                    {
                        aline = "";
                    }
                    if (aline.Length > 0)
                    {
                        Models.NoteModel anote = new Models.NoteModel(lineno, aline);
                        thenotes.notes.Add(anote);
                    }
                }

                else   // pid=0 or invalid or blank
                       // read all
                {
                    while ((aline = sr.ReadLine()) != null)
                    {
                        lineno++;
                        if (aline.Length > 0)
                        {
                            if (sfilter.Length == 0 || (sfilter.Length > 0 && aline.ToLower().Contains(sfilter.ToLower())))
                            {
                                Models.NoteModel anote = new Models.NoteModel(lineno, aline);
                                thenotes.notes.Add(anote);
                            }
                        }
                        if (id > 0) // onlywant 1
                        {
                            break;
                        }
                    }
                }
                sr.Dispose();
            }
            if (thenotes.notes.Count == 0)
            {
                if (id > 0)
                {
                    Models.NoteModel anote = new Models.NoteModel(id, "Note not found");
                    thenotes.notes.Add(anote);
                }
                else
                {
                    Models.NoteModel anote = new Models.NoteModel(-1, "Notes are empty");
                    thenotes.notes.Add(anote);
                }
            }
            return(thenotes);
        }