public bool DeleteUserfromDB(int id) { bool result = default(bool); BaseDataAccess _db = new BaseDataAccess(); using (DbTransaction dbTransaction = _db.CreateTransaction()) { try { // 1- Select User From DB by ID UserBusiness userBusiness = new UserBusiness(); UserList userList = userBusiness.SelectRows(id, null, null, null); if (userList != null && userList.Count > 0) { // 2- Select User Groups From DB by User ID UserGroupBusiness userGroupBusiness = new UserGroupBusiness(); UserGroupList userGroupList = userGroupBusiness.SelectRows(null, id, null); if (userGroupList != null && userGroupList.Count > 0) { // 3- Delete User Groups first (we must remove children first because of DB relation) foreach (UserGroup userGroup in userGroupList) { userGroupBusiness = new UserGroupBusiness(); userGroupBusiness.DeleteRow(dbTransaction, userGroup); } } // 4- Then Delete The User itself userBusiness = new UserBusiness(); if (userBusiness.DeleteRow(dbTransaction, userList[0]) > 0) { dbTransaction.Commit(); result = true; } else { dbTransaction.Rollback(); throw new Exception("DataBase Operation Failure"); } } else { dbTransaction.Rollback(); throw new Exception("User Id Not Found in DB"); } } catch (Exception) { dbTransaction.Rollback(); throw new Exception("DataBase Operation Failure"); } } return(result); }
public UserDTO UpdateUserinDB(UserDTO userDTO) { BaseDataAccess _db = new BaseDataAccess(); using (DbTransaction dbTransaction = _db.CreateTransaction()) { try { // 1- Perform Mapping to Input (for Saving in DB) if (Mapper.MapUserAsInput(userDTO)) { ////User user = Mapper._User; ////List<UserGroup> userGroups = Mapper._UserGroupListInput; //UserBusiness userBusiness = new UserBusiness(); //if (userBusiness.InsertRow(dbTransaction, Mapper._User) > 0) //{ // UserGroupBusiness userGroupBusiness = new UserGroupBusiness(); // if (Mapper._UserGroupListInput != null && Mapper._UserGroupListInput.Count > 0) // { // foreach (UserGroup userGroup in Mapper._UserGroupListInput) // { // userGroup.userId = Mapper._User.Id; // userGroupBusiness = new UserGroupBusiness(); // userGroupBusiness.InsertRow(dbTransaction, userGroup); // } // dbTransaction.Commit(); // } // else // dbTransaction.Rollback(); //} //else // throw new DataException("DataBase Operation Failure"); // 2- Select User to be updated UserBusiness userBusiness = new UserBusiness(); UserList userList = userBusiness.SelectRows(Mapper._User.Id, null, null, null); if (userList != null && userList.Count > 0) { userList[0].userName = Mapper._User.userName; userList[0].fullName = Mapper._User.fullName; userList[0].Password = Mapper._User.Password; userList[0].status = Mapper._User.status; // 3- Update User Data by Input Values userBusiness = new UserBusiness(); if (userBusiness.UpdateRow(dbTransaction, userList[0]) > 0) { // 4- Remove User Groups Already Saved for that User in DB UserGroupBusiness userGroupBusiness = new UserGroupBusiness(); UserGroupList userGroupList = userGroupBusiness.SelectRows(null, Mapper._User.Id, null); if (userGroupList != null && userGroupList.Count > 0) { //foreach (UserGroup userGroup in Mapper._UserGroupList) //{ // userGroupBusiness = new UserGroupBusiness(); // userGroupBusiness.DeleteRow(dbTransaction, userGroup); //} foreach (UserGroup userGroup in userGroupList) { userGroupBusiness = new UserGroupBusiness(); userGroupBusiness.DeleteRow(dbTransaction, userGroup); } } // 5- Add New User Groups from Input if (Mapper._UserGroupListInput != null && Mapper._UserGroupListInput.Count > 0) { foreach (UserGroup userGroup in Mapper._UserGroupListInput) { userGroupBusiness = new UserGroupBusiness(); userGroupBusiness.InsertRow(dbTransaction, userGroup); } dbTransaction.Commit(); } } else { dbTransaction.Rollback(); throw new Exception("DataBase Operation Failure"); } } else { throw new Exception("User Id Not Found in DB"); } } else { throw new ArgumentNullException("userDTO"); } } catch (Exception ex) { dbTransaction.Rollback(); throw new Exception("DataBase Operation Failure"); } } return(userDTO); }