private AudioProgram UpdateProgram(Dictionary <string, object> programDict) { string currentGroupName = (string)programDict["currentGroupName"]; string updateGroupName = (string)programDict["updateGroupName"]; string currentProgramName = (string)programDict["currentName"]; string updateProgramName = (string)programDict["updateName"]; AudioProgram currentProgram = _programsCollection.FindOne(x => x.GroupName == currentGroupName && x.Name == currentProgramName); if (currentProgram != null) { //are we changing the group name if (!updateGroupName.Equals(currentProgram.GroupName)) { //we are changing the group name; so is the current program the last program in the current group? //if so then delete the old group IEnumerable <AudioProgram> programs = _programsCollection.Find(x => x.GroupName.Equals(currentProgram.GroupName)); if (programs.Count() == 1) { //this is the last program with a GroupName that is changing, so delete the group _groupsCollection.Delete(x => x.Name.Equals(currentProgram.GroupName)); } } //does the new group exist in db? int groupId; AudioGroup group = _groupsCollection.FindOne(x => x.Name.Equals(updateGroupName)); if (group == null) //if not then create it { groupId = AddGroup(updateGroupName); } else { groupId = group.Id; } AudioProgram audioProgram = new AudioProgram(); audioProgram.Id = currentProgram.Id; audioProgram.Name = updateProgramName; audioProgram.Start = (int)programDict["Start"]; audioProgram.Stop = (int)programDict["Stop"]; audioProgram.Dow = (ArrayList)programDict["Dow"]; audioProgram.Url = (string)programDict["Url"]; audioProgram.GroupName = updateGroupName; audioProgram.GroupId = groupId; bool found = _programsCollection.Update(audioProgram); //debug 'found' return(currentProgram); } else { return(null); } }
private AudioProgram AddProgram(Dictionary <string, object> programDict) { string groupName = (string)programDict["GroupName"]; string programName = (string)programDict["Name"]; //does the group exist in db? int groupId; AudioGroup group = _groupsCollection.FindOne(x => x.Name.Equals(groupName)); if (group == null) //if not then create it { groupId = AddGroup(groupName); } else { groupId = group.Id; } //does the program exist with this group AudioProgram program = _programsCollection.FindOne(x => x.GroupId == groupId && x.Name.Equals(programName)); if (program == null) { AudioProgram newProgram = new AudioProgram(); newProgram.Name = (string)programDict["Name"]; newProgram.Start = (int)programDict["Start"]; newProgram.Stop = (int)programDict["Stop"]; newProgram.Dow = (ArrayList)programDict["Dow"]; newProgram.Url = (string)programDict["Url"]; newProgram.GroupId = groupId; newProgram.GroupName = groupName; _programsCollection.Insert(newProgram); return(newProgram); } else { return(null); } }
public Handlers(string serverBaseFolder, string databasePath) { _serverBaseFolder = serverBaseFolder; _serializer = new JavaScriptSerializer(); _helpers = new Helpers(); _db = new LiteDatabase(databasePath); _programsCollection = _db.GetCollection <AudioProgram>("ProgramsCollection"); _groupsCollection = _db.GetCollection <AudioGroup>("GroupsCollection"); _sessionCollection = _db.GetCollection <AudioSession>("SessionCollection"); //create database if necessary if (!File.Exists(databasePath)) { Dictionary <string, object> program1 = new Dictionary <string, object>() { { "Name", "prog_1" }, { "Start", 654 }, { "Stop", 655 }, { "Dow", new ArrayList() { 0, 1, 2, 3, 4, 5, 6 } }, { "Url", @"https://dal-wku-stream-1.neighborhoodca.com/stream" }, { "GroupName", "BG Test" } }; AddProgram(program1); Dictionary <string, object> program2 = new Dictionary <string, object>() { { "Name", "prog_2" }, { "Start", 654 }, { "Stop", 655 }, { "Dow", new ArrayList() { 0, 1, 2, 3, 4, 5, 6 } }, { "Url", @"http://lpm.streamguys1.com/wfpl-popup" }, { "GroupName", "Louisville Test" } }; AddProgram(program2); AddSession("BG Test"); } ; _actions = new Dictionary <string, Action>() { { "getGroupNames", () => { List <string> groupNames = GetGroupNames(); string responseStr = _serializer.Serialize(groupNames); _helpers.SendHttpTextResponse(_httpDetails.Response, responseStr); } }, { "getSession", () => { string groupName = GetSession(); _helpers.SendHttpTextResponse(_httpDetails.Response, groupName); } }, { "setSession", () => { string groupName = (string)_requestDictionary["GroupName"]; bool found = UpdateSession(groupName); if (found) { _helpers.SendHttpTextResponse(_httpDetails.Response, "Successfully updated session."); } else { _helpers.SendHttpResponse(400, "Could not locate group", new byte[0], "text/html", "MoneyTracker Server", _httpDetails.Response); } } }, { "getPrograms", () => { string groupName = (string)_requestDictionary["GroupName"]; List <Dictionary <string, object> > programsList = GetPrograms(groupName); string responseStr = _serializer.Serialize(programsList); _helpers.SendHttpTextResponse(_httpDetails.Response, responseStr); } }, { "addProgram", () => { Dictionary <string, object> programDict = (Dictionary <string, object>)_requestDictionary["program"]; AudioProgram newProgram = AddProgram(programDict); if (newProgram != null) { //return both a list of group names and the new program List <string> groupNames = GetGroupNames(); Dictionary <string, object> responseDict = new Dictionary <string, object>() { { "backup_program", newProgram.Program() }, { "group_names", groupNames } }; string responseStr = _serializer.Serialize(responseDict); _helpers.SendHttpTextResponse(_httpDetails.Response, responseStr); } else { _helpers.SendHttpResponse(400, "Duplicate Program Found", new byte[0], "text/html", "MoneyTracker Server", _httpDetails.Response); } } }, { "deleteProgram", () => { Dictionary <string, object> programDict = (Dictionary <string, object>)_requestDictionary["program"]; string groupName = (string)programDict["GroupName"]; string programName = (string)programDict["Name"]; AudioProgram program = _programsCollection.FindOne(x => x.GroupName == groupName && x.Name == programName); if (program != null) { DeleteProgram(program.Id, program.GroupId); //return the current set of group names and backup program List <string> groupNames = GetGroupNames(); Dictionary <string, object> responseDict = new Dictionary <string, object>() { { "backup_program", program.Program() }, { "group_names", groupNames } }; string responseStr = _serializer.Serialize(responseDict); _helpers.SendHttpTextResponse(_httpDetails.Response, responseStr); } else { _helpers.SendHttpResponse(400, "Could not locate program", new byte[0], "text/html", "MoneyTracker Server", _httpDetails.Response); } } }, { "updateProgram", () => { Dictionary <string, object> programDict = (Dictionary <string, object>)_requestDictionary["program"]; AudioProgram backupProgram = UpdateProgram(programDict); if (backupProgram != null) { //return the current set of group names List <string> groupNames = GetGroupNames(); Dictionary <string, object> responseDict = new Dictionary <string, object>() { { "backup_program", backupProgram.Program() }, { "group_names", groupNames } }; string responseStr = _serializer.Serialize(responseDict); _helpers.SendHttpTextResponse(_httpDetails.Response, responseStr); } else { _helpers.SendHttpResponse(400, "Could not locate program. Try adding instead of updating ", new byte[0], "text/html", "MoneyTracker Server", _httpDetails.Response); } } } }; }