/// <summary> /// Stores the passed in note information. If a note exsists with the passed in note id it will get updated. If a note does not exsist /// with the passed in note id or the id is Guid.Empty a new note will be created. /// </summary> /// <param name="note">The note object that will be stored in the database, either created or updated information</param> /// <returns>The stored <see cref="Note"/> object</returns> public Note SaveOrUpdateNote(Note note) { // If the guid is empty than we save a new note. if (note.Id == Guid.Empty) { note.Id = Guid.NewGuid(); note = _noteContext.Notes.Add(note); // Make sure to set the board as exsisting and not try to craete a new board _noteContext.Boards.Attach(note.Board); _noteContext.Entry(note.Board).State = EntityState.Unchanged; } else { // if the passed in note alreasy has a guid it has been created before // so we atach it as state modified. _noteContext.Notes.Attach(note); _noteContext.Entry(note).State = EntityState.Modified; // Make sure to set the board as exsisting and not try to craete a new board _noteContext.Boards.Attach(note.Board); _noteContext.Entry(note.Board).State = EntityState.Unchanged; } _noteContext.SaveChanges(); return note; }
/// <summary> /// Map the domain model for the Note object to the NoteViewModel object. /// </summary> /// <param name="note">The domain <see cref="Note"/> object</param> /// <returns>Returns a <see cref="NoteViewModel"/> object</returns> public static NoteViewModel ToNoteViewModel(Note note) { var noteViewModel = new NoteViewModel() { Id = note.Id, Text = note.Text, BoardId = note.Board == null ? Guid.Empty : note.Board.Id, Title = note.Title, Top = note.Top, Left = note.Left }; return noteViewModel; }
/// <summary> /// Maps a given note view model to the Note model entitiy class. /// </summary> /// <param name="noteViewModel">The note view model we want to transform to a note entity class object.</param> /// <returns><see cref="Note"/> object transformed from the note view model.</returns> public static Note ToNoteEntity(NoteViewModel noteViewModel) { var note = new Note() { Id = noteViewModel.Id, Board = new Board() { Id = noteViewModel.BoardId }, Text = noteViewModel.Text, Title = noteViewModel.Title, Top = noteViewModel.Top, Left = noteViewModel.Left, }; return note; }
/// <summary> /// Stores the note object. If the note is already saved based on the unique id, it will get updated. If there is /// no note with the given Id the note will be created. /// </summary> /// <param name="note">The note object that will be stored in the database.</param> /// <returns>The note object that was stored.</returns> /// <remarks>When a new note is created the Note object returned contains the stored unique id for the new note.</remarks> public Note SaveOrUpdateNote(Note note) { try { // just call the repository SaveOrUpdate Method for the note var savedNote = _noteRepository.SaveOrUpdateNote(note); // Return the note we got back from the repo (can be null - failiure) return savedNote; } catch (Exception ex) { // If something goes wrong with the actual call to the repo, do not throw a exceptopn, //just return null return null; } }