public bool DeleteUser(int id) { try { //establishing database connection using (ORSDatabaseEntities db = new ORSDatabaseEntities()) { //deletes the user with specific id User user = db.Users.Where(f => f.userId == id).FirstOrDefault(); User objUser = new User() { userId = user.userId, password = user.password, email = user.email, userName = user.userName, phone = user.phone, gender = user.gender, roleId = user.roleId }; if (objUser != null) { //db.Users.Remove(user); ////updates the changes in the database //db.SaveChanges(); db.Entry(objUser).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); return(true); } else { //throws user defined exception throw new UserException("User does not exist"); } } } catch (Exception ex) { //throws user defined exception throw new UserException(ex.Message); } }
public List <User> GetUsers() { try { //establishing database connection using (ORSDatabaseEntities db = new ORSDatabaseEntities()) { //List of users fetched from database List <User> userList = db.Users.ToList(); //list of user return(userList); } } catch (Exception ex) { //throws user defined exception throw new UserException(ex.Message); } }
public User GetUser(int?id) { try { //establishing database connection using (ORSDatabaseEntities db = new ORSDatabaseEntities()) { //First user with the same id that was passed User user = db.Users.Where(f => f.userId == id).FirstOrDefault(); //returns user type data return(user); } } catch (Exception ex) { //throws user defined exception throw new UserException(ex.Message); } }
public bool PutUser(int?id, User user) { try { //establishing database connection using (ORSDatabaseEntities db = new ORSDatabaseEntities()) { //First user with the same id that was passed is fetched and updated as per data passed by the client User item = db.Users.Where(f => f.userId == id).FirstOrDefault(); //if the user exists if (item != null) { // updates values into the user item.password = user.password; item.userName = user.userName; item.email = user.email; item.phone = user.phone; item.gender = user.gender; //saving updated values in the user db.SaveChanges(); return(true); } else { //throws user defined exception throw new UserException("User does not exist."); } } } catch (Exception ex) { //throws user defined exception throw new UserException(ex.Message); } }
/// <summary> /// This method takes the id input and verifies if the user exists or not /// </summary> /// <param name="id"></param> /// <returns>boolean value as true or false </returns> private bool UserExists(int id) { using (ORSDatabaseEntities db = new ORSDatabaseEntities()) return(db.Users.Count(e => e.userId == id) > 0); }