public void EditPreset(int presetId, string presetName, int userId, List <int> appIds) { // update the possible name change PresetName dbPreset = new PresetName() { preset_id = presetId, preset_name = presetName, user_id = userId, }; DbHelper.GetInstance().UpdateData(dbPreset); // update all the applications // 1. first delete existing applications with preset id - because we wont know if there is any app id deleted PresetApplications dbPresetApp = new PresetApplications() { preset_name_id = presetId, }; DbHelper.GetInstance().RemoveData(dbPresetApp); // 2. add back all the application foreach (int appId in appIds) { PresetApplications dbPresetAppAdd = new PresetApplications() { preset_name_id = presetId, preset_application_id = appId, }; DbHelper.GetInstance().AddData(dbPresetAppAdd); } }
/// <summary> /// /// </summary> /// <param name="presetName"></param> /// <param name="userId"></param> /// <param name="appIds"></param> /// <returns>number of data added</returns> public int AddPreset(string presetName, int userId, List <int> appIds) { int count = 0; PresetName dbPreset = new PresetName() { preset_name = presetName, user_id = userId, }; if (DbHelper.GetInstance().AddData(dbPreset)) { // get the preset id just added int presetId = GetPresetIdByPresetNameUserId(presetName, userId); foreach (int appId in appIds) { PresetApplications dbPresetApp = new PresetApplications() { preset_name_id = presetId, preset_application_id = appId, }; if (DbHelper.GetInstance().AddData(dbPresetApp)) { count++; } } } return(count); }
public IList <PresetData> GetPresetByUserId(int userId) { List <PresetData> presetList = new List <PresetData>(); // get all applications IList <ApplicationData> applicationList = GetAllApplications(); // get the preset name id list PresetName dbPresetName = new PresetName() { user_id = userId }; DataTable dataTablePresetName = DbHelper.GetInstance().ReadData(dbPresetName); foreach (DataRow presetNameDataRow in dataTablePresetName.Rows) { // fill the preset name PresetData presetData = new PresetData(); presetData.Name = presetNameDataRow[PresetName.PRESET_NAME].ToString(); // preset name id presetData.Id = int.Parse(presetNameDataRow[PresetName.PRESET_ID].ToString()); // get the application ids releated to this preset List <ApplicationData> presetAppList = new List <ApplicationData>(); PresetApplications dbPresetApp = new PresetApplications() { // preset id preset_name_id = presetData.Id, }; DataTable dataTablePresetApp = DbHelper.GetInstance().ReadData(dbPresetApp); foreach (DataRow presetAppDataRow in dataTablePresetApp.Rows) { // get the application id from table int appId = int.Parse(presetAppDataRow[PresetApplications.APPLICATION_ID].ToString()); // get the application data from app full list with match appId ApplicationData appData = applicationList.First(ApplicationData => ApplicationData.id == appId); // add to the app list presetAppList.Add(appData); } presetData.AppDataList = presetAppList; presetList.Add(presetData); } //// get the allowed list by a user //IList<ApplicationData> appsList = GetAppsWithUserId(userId); //PresetName dbPreset = new PresetName() {user_id = userId}; //DataTable dataTable = DbHelper.GetInstance().ReadData(dbPreset); //foreach(DataRow presetDataRow in dataTable.Rows) //{ // PresetData presetData = new PresetData() // { // name = presetDataRow[PresetName.PRESET_NAME].ToString(), // appData = appsList.First(ApplicationData => ApplicationData.id == int.Parse(presetDataRow[PresetName.APPLICATION_ID].ToString())) // }; // presetList.Add(presetData); //} return(presetList.AsReadOnly()); }