/// <summary> /// Method to read properties of a category /// </summary> /// <param name="categoryId">The ID of the category whose properties are to be read</param> /// <param name="property">The option number of the property</param> /// <returns>Returns the specfic property of mentioned category</returns> public static string ReadCategoryProperties(int categoryId, int property) { try { using (KeepNotesDBContext context = new KeepNotesDBContext()) { var category = context.Categories.Where(c => c.CategoryID == categoryId).FirstOrDefault(); if (category != null) { switch (property) { case 1: return(category.Description); case 2: return(category.UserId); default: throw new InvalidOperationException("Invalid operation!!. Please choose a correct option"); } } else { throw new IDNotFoundException("Category", categoryId.ToString()); } } } catch (Exception ex) { throw ex; } }
/// <summary> /// Method to create a new Category /// </summary> /// <param name="newCategory">The Category object containing properties for new category</param> /// <returns>True if created successfully else false</returns> public static bool CreateNewCategory(Category newCategory) { try { using (KeepNotesDBContext context = new KeepNotesDBContext()) { if (!context.Users.Any(user => string.Equals(user.UserId, newCategory.UserId))) { throw new IDNotFoundException("User", newCategory.UserId); } else if (context.Categories.Any(category => category.CategoryID == newCategory.CategoryID)) { throw new IDAlreadyExistsException("Category", newCategory.CategoryID.ToString()); } else { context.Categories.Add(newCategory); return(context.SaveChanges() > 0); } } } catch (Exception ex) { throw ex; } }
/// <summary> /// Method to update properties of a category /// </summary> /// <param name="updatedCategory">Category object with new details</param> /// <param name="property">The property that is to be updated</param> /// <returns>Returns true if value was updated else false</returns> public static bool UpdateCategory(Category updatedCategory, int property) { try { using (KeepNotesDBContext context = new KeepNotesDBContext()) { var oldCategory = context.Categories.Where(category => category.CategoryID == updatedCategory.CategoryID) .FirstOrDefault(); if (oldCategory != null) { switch (property) { case 0: oldCategory.Description = updatedCategory.Description; if (!context.Users.Any(user => string.Equals(user.UserId, updatedCategory.UserId))) { throw new IDNotFoundException("User", updatedCategory.UserId); } else { oldCategory.UserId = updatedCategory.UserId; return(context.SaveChanges() > 0); } case 1: oldCategory.Description = updatedCategory.Description; return(context.SaveChanges() > 0); case 2: if (!context.Users.Any(user => string.Equals(user.UserId, updatedCategory.UserId))) { throw new IDNotFoundException("User", updatedCategory.UserId); } else { oldCategory.UserId = updatedCategory.UserId; return(context.SaveChanges() > 0); } default: throw new InvalidOperationException("Invalid operation!!. Please choose a correct option"); } } else { throw new IDNotFoundException("Category", updatedCategory.CategoryID.ToString()); } } } catch (Exception ex) { throw ex; } }
/// <summary> /// Method to get all categories in the table /// </summary> /// <returns>Returns list of cateogry details</returns> public static List <Category> ReadAllCategories() { try { using (KeepNotesDBContext context = new KeepNotesDBContext()) { return(context.Categories?.ToList()); } } catch (Exception ex) { throw ex; } }
/// <summary> /// Method to read all users from table /// </summary> /// <returns>List of users in table</returns> public static List <User> ReadAllUsers() { try { using (KeepNotesDBContext context = new KeepNotesDBContext()) { return(context.Users?.ToList()); } } catch (Exception ex) { throw ex; } }
/// <summary> /// Method to update properties of a note /// </summary> /// <param name="updatedNote">The updated note object</param> /// <param name="property">The option number of property that is to be updated</param> /// <returns></returns> public static bool UpdateNote(Note updatedNote, int property) { try { using (KeepNotesDBContext context = new KeepNotesDBContext()) { var oldNote = context.Notes.Where(note => note.NoteId == updatedNote.NoteId).FirstOrDefault(); if (oldNote != null) { switch (property) { case 0: oldNote.Title = updatedNote.Title; oldNote.Description = updatedNote.Description; oldNote.Status = updatedNote.Status; oldNote.CategoryId = updatedNote.CategoryId; return(context.SaveChanges() > 0); case 1: oldNote.Title = updatedNote.Title; return(context.SaveChanges() > 0); case 2: oldNote.Description = updatedNote.Description; return(context.SaveChanges() > 0); case 3: oldNote.Status = updatedNote.Status; return(context.SaveChanges() > 0); case 4: oldNote.CategoryId = updatedNote.CategoryId; return(context.SaveChanges() > 0); default: throw new InvalidOperationException("Invalid operation!!. Please choose a correct option"); } } else { throw new IDNotFoundException("Note", updatedNote.NoteId.ToString()); } } } catch (Exception ex) { throw ex; } }
/// <summary> /// Method to read all properties of a categories /// </summary> /// <param name="categoryId">The id of the category whose properties are to be read</param> /// <returns>Returns the category object corresponding to the id provided</returns> public static Category ReadACategoryAllProperties(int categoryId) { try { using (KeepNotesDBContext context = new KeepNotesDBContext()) { var category = context.Categories.Where(c => c.CategoryID == categoryId).FirstOrDefault(); if (category != null) { return(category); } else { throw new IDNotFoundException("Category", categoryId.ToString()); } } } catch (Exception ex) { throw ex; } }
/// <summary> /// Method to create a new user /// </summary> /// <param name="newUser">The User object containing properties for new user</param> /// <returns>True if created successfully else false</returns> public static bool CreateNewUser(User newUser) { try { using (KeepNotesDBContext context = new KeepNotesDBContext()) { if (context.Users.Any(user => string.Equals(user.UserId, newUser.UserId))) { throw new IDAlreadyExistsException("User", newUser.UserId); } else { context.Users.Add(newUser); return(context.SaveChanges() > 0); } } } catch (Exception ex) { throw ex; } }
/// <summary> /// Method to read properties of a user /// </summary> /// <param name="userId">Id of the user whose properties is to be read</param> /// <returns>Return the user object</returns> public static User ReadUserProperties(string userId) { try { using (KeepNotesDBContext context = new KeepNotesDBContext()) { var user = context.Users.Where(u => string.Equals(u.UserId, userId)).FirstOrDefault(); if (user != null) { return(user); } else { throw new IDNotFoundException("User", userId); } } } catch (Exception ex) { throw ex; } }
/// <summary> /// Method to read all properties of a note /// </summary> /// <param name="noteId">the id of the note whose properties are to be read</param> /// <returns>Returns the note object corresponding to the note id</returns> public static Note ReadANotesAllProperties(int noteId) { try { using (KeepNotesDBContext context = new KeepNotesDBContext()) { var note = context.Notes.Where(n => n.NoteId == noteId).FirstOrDefault(); if (note != null) { return(note); } else { throw new IDNotFoundException("Note", noteId.ToString()); } } } catch (Exception ex) { throw ex; } }
/// <summary> /// Method to create a new note /// </summary> /// <param name="newNote">The note object containing properties for new note</param> /// <returns>Returns true if creation was successful else false</returns> public static bool CreateNote(Note newNote) { try { using (KeepNotesDBContext context = new KeepNotesDBContext()) { if (context.Notes.Any(note => note.NoteId == newNote.NoteId)) { throw new IDAlreadyExistsException("Note", newNote.NoteId.ToString()); } else { context.Notes.Add(newNote); return(context.SaveChanges() > 0); } } } catch (Exception ex) { throw ex; } }
/// <summary> /// Method to read properties of a note /// </summary> /// <param name="noteId">The id of note to be read</param> /// <param name="property">The option number of property that is to be read</param> /// <returns>Returns the specific property</returns> public static string ReadNotesProperty(int noteId, int property) { try { using (KeepNotesDBContext context = new KeepNotesDBContext()) { var note = context.Notes.Where(n => n.NoteId == noteId).FirstOrDefault(); if (note != null) { switch (property) { case 1: return(note.Title); case 2: return(note.Description); case 3: return(note.Status); case 4: return(note.CategoryId.ToString()); default: throw new InvalidOperationException("Invalid operation!!. Please choose a correct option"); } } else { throw new IDNotFoundException("Note", noteId.ToString()); } } } catch (Exception ex) { throw ex; } }
/// <summary> /// Method to delete a category /// </summary> /// <param name="categoryId">The id of category to be removed</param> /// <returns>Returns true if category details gets removed successfully</returns> public static bool DeleteCategory(int categoryId) { try { using (KeepNotesDBContext context = new KeepNotesDBContext()) { var category = context.Categories.Where(c => c.CategoryID == categoryId).FirstOrDefault(); if (category != null) { context.Categories.Remove(category); return(context.SaveChanges() > 0); } else { throw new IDNotFoundException("Category", categoryId.ToString()); } } } catch (Exception ex) { throw ex; } }
/// <summary> /// Method to delete a note /// </summary> /// <param name="noteId">The id of the note that is to be deleted</param> /// <returns>Returns true if deleted successfuly else false</returns> public static bool DeleteNote(int noteId) { try { using (KeepNotesDBContext context = new KeepNotesDBContext()) { var note = context.Notes.Where(n => n.NoteId == noteId).FirstOrDefault(); if (note != null) { context.Notes.Remove(note); return(context.SaveChanges() > 0); } else { throw new IDNotFoundException("Note", noteId.ToString()); } } } catch (Exception ex) { throw ex; } }
/// <summary> /// Method to delete a user /// </summary> /// <param name="userId">The details of User to be removed</param> /// <returns>Returns true if user details gets removed successfully</returns> public static bool DeleteUser(string userId) { try { using (KeepNotesDBContext context = new KeepNotesDBContext()) { var user = context.Users.Where(u => string.Equals(u.UserId, userId)).FirstOrDefault(); if (user != null) { context.Users.Remove(user); return(context.SaveChanges() > 0); } else { throw new IDNotFoundException("User", userId); } } } catch (Exception ex) { throw ex; } }
/// <summary> /// Method to update details of an existing user /// </summary> /// <param name="updatedUser">Details of new user</param> /// <returns>TReturns true if updation was succesful else false</returns> public static bool UpdateUser(User updatedUser) { try { using (KeepNotesDBContext context = new KeepNotesDBContext()) { var oldUser = context.Users.Where(user => string.Equals(user.UserId, updatedUser.UserId)) .FirstOrDefault(); if (oldUser != null) { oldUser.UserName = updatedUser.UserName; return(context.SaveChanges() > 0); } else { throw new IDNotFoundException("User", updatedUser.UserId); } } } catch (Exception ex) { throw ex; } }