public ProfileViewModel GetProfile() { ProfileEntity entity; using (var ctx = new ElevenNoteDbContext()) { entity = ctx .Profiles .SingleOrDefault(e => e.Email == _email); } if (entity == null) { entity = UpdateEmail(); } return (new ProfileViewModel { Email = entity.Email, FirstName = entity.FirstName, LastName = entity.LastName, Address = entity.Address, PhoneNumber = entity.PhoneNumber }); }
private NoteEntity GetNoteFromDatabase(ElevenNoteDbContext context, int noteId) { return (context .Notes .SingleOrDefault(e => e.NoteId == noteId && e.OwnerId == _userId)); }
public NoteDetailViewModel GetNoteById(int noteId) { NoteEntity entity; using (var ctx = new ElevenNoteDbContext()) { entity = ctx .Notes .SingleOrDefault(e => e.OwnerId == _userId && e.NoteId == noteId); } // TODO: Handle note not found return (new NoteDetailViewModel { NoteId = entity.NoteId, Title = entity.Title, Content = entity.Content, IsStarred = entity.IsStarred, CreatedUtc = entity.CreatedUtc, ModifiedUtc = entity.ModifiedUtc }); }
public NoteDetailViewModel GetNoteById(int noteId) { NoteEntity entity; using (var context = new ElevenNoteDbContext()) { entity = context .Notes .SingleOrDefault(e => e.OwnerId == _userId && e.NoteId == noteId); } if (entity == null) { return(new NoteDetailViewModel()); } return (new NoteDetailViewModel { NoteId = entity.NoteId, Title = entity.Title, Content = entity.Content, //IsStarred = entity.IsStarred, CreatedUtc = entity.CreatedUtc, ModifiedUtc = entity.ModifiedUtc }); }
public bool DeleteNote(int noteId) { using (var ctx = new ElevenNoteDbContext()) { var entity = ctx .Notes .SingleOrDefault(e => e.OwnerId == _userId && e.NoteId == noteId); // TODO: Handle note not found ctx.Notes.Remove(entity); return(ctx.SaveChanges() == 1); } }
public bool DeleteNote(int noteId) { using (var ctx = new ElevenNoteDbContext()) { var entity = GetNoteFromDatabase(ctx, noteId); if (entity == null) { return(false); } ctx.Notes.Remove(entity); return(ctx.SaveChanges() == 1); } }
public bool UpdateProfile(ProfileViewModel vm) { using (var ctx = new ElevenNoteDbContext()) { var entity = ctx .Profiles .SingleOrDefault(e => e.Email == vm.Email); entity.FirstName = vm.FirstName; entity.LastName = vm.LastName; entity.Address = vm.Address; entity.PhoneNumber = vm.PhoneNumber; return(ctx.SaveChanges() == 1); } }
public ProfileEntity UpdateEmail() { ProfileEntity entity; using (var ctx = new ElevenNoteDbContext()) { entity = new ProfileEntity { Email = _email }; ctx.Profiles.Add(entity); ctx.SaveChanges(); } return(entity); }
public bool CreateNote(NoteCreateViewModel vm) { using (var ctx = new ElevenNoteDbContext()) { var entity = new NoteEntity { OwnerId = _userId, Title = vm.Title, Content = vm.Content, CreatedUtc = DateTimeOffset.UtcNow }; ctx.Notes.Add(entity); return(ctx.SaveChanges() == 1); } }
public bool Delete(int id, Guid userId) { using (var ctx = new ElevenNoteDbContext()) { var note = ctx .Notes .Where( n => n.Id == id && n.ApplicationUserId == userId) .SingleOrDefault(); if (note == null) return false; ctx.Notes.Remove(note); return ctx.SaveChanges() == 1; } }
public bool Create(NoteDetailViewModel vm, Guid userId) { using (var ctx = new ElevenNoteDbContext()) { var note = new Note { Title = vm.Title, Contents = vm.Contents, DateCreated = DateTime.UtcNow, ApplicationUserId = userId, IsFavorite = vm.IsFavorite }; ctx.Notes.Add(note); return ctx.SaveChanges() == 1; } }
public bool UpdateNote(NoteEditViewModel vm) { using (var ctx = new ElevenNoteDbContext()) { var entity = ctx .Notes .SingleOrDefault(e => e.OwnerId == _userId && e.NoteId == vm.NoteId); // TODO: Handle note not found entity.Title = vm.Title; entity.Content = vm.Content; entity.IsStarred = vm.IsStarred; entity.ModifiedUtc = DateTimeOffset.UtcNow; return(ctx.SaveChanges() == 1); } }
public bool UpdateNote(NoteEditModel model) { using (var ctx = new ElevenNoteDbContext()) { var entity = GetNoteFromDatabase(ctx, model.NoteId); if (entity == null) { return(false); } entity.Title = model.Title; entity.Content = model.Content; entity.ModifiedUtc = DateTime.UtcNow; entity.IsStarred = model.IsStarred; return(ctx.SaveChanges() == 1); } }
public IEnumerable <NoteListItemViewModel> GetNotes() { using (var ctx = new ElevenNoteDbContext()) { return (ctx .Notes .Where(e => e.OwnerId == _userId) .Select( e => new NoteListItemViewModel { NoteId = e.NoteId, Title = e.Title, IsStarred = e.IsStarred, CreatedUtc = e.CreatedUtc }) .ToArray()); } }
// Create // Read // Update // Delete public IEnumerable<NoteListItemViewModel> GetAllForUser(Guid userId) { using (var ctx = new ElevenNoteDbContext()) { return ctx .Notes .Where(n => n.ApplicationUserId == userId) .Select( n => new NoteListItemViewModel { Id = n.Id, Title = n.Title, DateCreated = n.DateCreated, DateModified = n.DateModified, IsFavorite = n.IsFavorite }) .ToArray(); } }
public bool CreateNote(NoteCreateViewModel vm) { using (var context = new ElevenNoteDbContext()) { var entity = new NoteEntity { OwnerId = _userId, Title = vm.Title, Content = vm.Content, CreatedUtc = DateTime.UtcNow }; context.Notes.Add(entity); // return true if exactly one item added to the database // else, returns false return(context.SaveChanges() == 1); } }
public NoteDetailViewModel GetById(int id, Guid userId) { using (var ctx = new ElevenNoteDbContext()) { return ctx .Notes .Where( n => n.Id == id && n.ApplicationUserId == userId) .Select( n => new NoteDetailViewModel { Id = n.Id, Title = n.Title, Contents = n.Contents, IsFavorite = n.IsFavorite }) .SingleOrDefault(); } }
public bool UpdateNote(NoteEditViewModel model) { using (var context = new ElevenNoteDbContext()) { var entity = context .Notes .SingleOrDefault(e => e.NoteId == model.NoteId && e.OwnerId == _userId); if (entity == null) { return(false); } entity.Title = model.Title; entity.Content = model.Content; entity.ModifiedUtc = DateTimeOffset.Now; entity.IsStarred = model.IsStarred; return(context.SaveChanges() == 1); } }
public NoteDetailModel GetNoteById(int id) { NoteEntity entity; using (var ctx = new ElevenNoteDbContext()) { entity = GetNoteFromDatabase(ctx, id); } if (entity == null) { return(new NoteDetailModel()); } return (new NoteDetailModel { NoteId = entity.NoteId, Title = entity.Title, Content = entity.Content, CreatedUtc = entity.CreatedUtc, ModifiedUtc = entity.ModifiedUtc }); }
public bool Update(NoteDetailViewModel vm, Guid userId) { using (var ctx = new ElevenNoteDbContext()) { var note = ctx .Notes .Where( n => n.Id == vm.Id && n.ApplicationUserId == userId) .SingleOrDefault(); if (note == null) return false; note.Contents = vm.Contents; note.Title = vm.Title; note.IsFavorite = vm.IsFavorite; note.DateModified = DateTime.UtcNow; return ctx.SaveChanges() == 1; } }