Advertisement
AuriR

August 2015 - NoteService.cs innards

Sep 2nd, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.51 KB | None | 0 0
  1. using ElevenNote.DataAccess;
  2. using ElevenNote.Models;
  3. using ElevenNote.Models.ViewModels;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace ElevenNote.Services
  11. {
  12.     public class NoteService
  13.     {
  14.         /// <summary>
  15.         /// Create a note.
  16.         /// </summary>
  17.         /// <param name="model"></param>
  18.         /// <param name="userId"></param>
  19.         /// <returns></returns>
  20.         public bool Create(NoteEditViewModel model, Guid userId)
  21.         {
  22.             using (var context = new ElevenNoteDataContext())
  23.             {
  24.                 var note = new Note();
  25.                 note.Title = model.Title;
  26.                 note.Contents = model.Contents;
  27.                 note.DateCreated = DateTime.UtcNow;
  28.                 note.ApplicationUserId = userId;
  29.  
  30.                 context.Notes.Add(note);
  31.                 var result = context.SaveChanges();
  32.                 return result == 1;
  33.             }
  34.  
  35.         }
  36.  
  37.         /// <summary>
  38.         /// Gets all notes for the passed user ID.
  39.         /// </summary>
  40.         /// <param name="userId"></param>
  41.         /// <returns></returns>
  42.         public List<NoteListViewModel> GetAllForUser(Guid userId)
  43.         {
  44.             using (var context = new ElevenNoteDataContext())
  45.             {
  46.                 var result = (from note in context.Notes
  47.                               where note.ApplicationUserId == userId
  48.                               select new NoteListViewModel()
  49.                               {
  50.                                   DateCreated = note.DateCreated.Value,
  51.                                   DateModified = note.DateModified,
  52.                                   Id = note.Id,
  53.                                   Title = note.Title,
  54.                                   IsFavorite = note.IsFavorite
  55.                               }).ToList();
  56.  
  57.                 return result;
  58.             }
  59.         }
  60.  
  61.         /// <summary>
  62.         /// Retrieves the passed note for the passed user.
  63.         /// </summary>
  64.         /// <param name="id"></param>
  65.         /// <param name="userId"></param>
  66.         /// <returns></returns>
  67.         public NoteEditViewModel GetById(int id, Guid userId)
  68.         {
  69.             using (var context = new ElevenNoteDataContext())
  70.             {
  71.                 var result = (from note in context.Notes
  72.                               where note.ApplicationUserId == userId
  73.                               && note.Id == id
  74.                               select new NoteEditViewModel()
  75.                               {
  76.                                   Contents = note.Contents,
  77.                                   Id = note.Id,
  78.                                   Title = note.Title
  79.                               }).SingleOrDefault();
  80.  
  81.                 return result;
  82.             }
  83.         }
  84.  
  85.         public bool Update(NoteEditViewModel model, Guid userId)
  86.         {
  87.             using (var context = new ElevenNoteDataContext())
  88.             {
  89.                 // Attempt to get the note from the database.
  90.                 var note = context.Notes.Where(w => w.Id == model.Id && w.ApplicationUserId == userId).SingleOrDefault();
  91.  
  92.                 // Functionally equivalent expressive syntax:
  93.                 //var note2 = (from w in context.Notes
  94.                 //             where w.Id == model.Id && w.ApplicationUserId == userId
  95.                 //             select w).SingleOrDefault();
  96.  
  97.                 // Make sure we actually received a note back before updating.
  98.                 if (note == null) return false;
  99.  
  100.                 // Update the note.
  101.                 note.Contents = model.Contents;
  102.                 note.Title = model.Title;
  103.                 note.DateModified = DateTime.UtcNow;
  104.  
  105.                 // Save the changes to the database.
  106.                 var result = context.SaveChanges();
  107.                 return result == 1 /* was 1 record (success) or 0 records (unsuccessful) updated? */;
  108.             }
  109.         }
  110.  
  111.         public bool Delete(int id, Guid userId)
  112.         {
  113.             using (var context = new ElevenNoteDataContext())
  114.             {
  115.                 // Attempt to get the note from the database.
  116.                 var note = context.Notes.Where(w => w.Id == id && w.ApplicationUserId == userId).SingleOrDefault();
  117.  
  118.                 // Make sure we actually received a note back before updating.
  119.                 if (note == null) return false;
  120.  
  121.                 // Delete the note.
  122.                 context.Notes.Remove(note);
  123.  
  124.                 // Save the changes to the database.
  125.                 var result = context.SaveChanges();
  126.  
  127.                 // Return the result.
  128.                 return result == 1;
  129.             }
  130.  
  131.  
  132.         }
  133.  
  134.         public bool ToggleFavorite(int id, Guid userId)
  135.         {
  136.             using (var context = new ElevenNoteDataContext())
  137.             {
  138.                 // Attempt to get the note from the database.
  139.                 var note = context.Notes.Where(w => w.Id == id && w.ApplicationUserId == userId).SingleOrDefault();
  140.  
  141.                 // Make sure we actually received a note back before updating.
  142.                 if (note == null) return false;
  143.  
  144.                 // Toggle the note IsFavorite flag.
  145.                 note.IsFavorite = !note.IsFavorite;
  146.  
  147.                 // Save the changes to the database.
  148.                 var result = context.SaveChanges();
  149.  
  150.                 // Return the result.
  151.                 return result == 1;
  152.             }
  153.  
  154.  
  155.         }
  156.  
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement