Skip to content

Instantly share code, notes, and snippets.

@andySF
Created September 4, 2012 20:20
.net installed programs
if (Request.ContentType == Common.ContentTypeAsXml)
{
//Rails logic:
//mark all programs for this machine to installed=false
//then for each program if exists mark installed as true
//else add
var installedProgramsXML = new StreamReader(Request.InputStream).ReadToEnd();
var installedPrograms = Utils.DeserializeObjects(installedProgramsXML, typeof(InstalledProgram[]));
//if they are more than one installed program
if (installedPrograms.Count() >= 1)
{
//get the first installed program from the list
var firstInstalledProgram = installedPrograms[0] as InstalledProgram;
//if the installed program is not null
if (firstInstalledProgram != null)
{
//use the machine id from the first installed program. the same machine id should be the same for all the installed programs
var machineId = firstInstalledProgram.MachineId;
//var installedProgramsForMachine = from p in db.InstalledPrograms
// where p.MachineId == machineId
// select p;
//recive the machine from db
Machine machine = db.Machines.Find(machineId);
//for each installed program on this machine
foreach(var existingInstalledProgram in machine.InstalledPrograms.ToList())
{
//mark Installed property as false
existingInstalledProgram.Installed = false;
}
//for each installed program sended for this machine
foreach (var installedProgram in installedPrograms)
{
//search for installedProgram if allready is in the db
var duplicateInstalledPrograms =
machine.InstalledPrograms.ToList().Where(
p => p.DisplayName == (installedProgram as InstalledProgram).DisplayName && p.Version==(installedProgram as InstalledProgram).Version);
//if the installed program allready exist in the db one time or more
if (duplicateInstalledPrograms.Count() >= 1)
{
//new instance of installed program
InstalledProgram duplicateInstalledProgram = new InstalledProgram();
//get the last installed program from duplicateInstalledPrograms
duplicateInstalledProgram = duplicateInstalledPrograms.Last();
//mark it as installed
duplicateInstalledProgram.Installed = true;
//add it to the db
db.InstalledPrograms.Add(duplicateInstalledProgram);
//remove all the duplicates from db
foreach (var program in duplicateInstalledPrograms)
{
db.InstalledPrograms.Remove(program);
}
}
//if is not in the db
else
{
//add the installed program to the db
db.InstalledPrograms.Add((InstalledProgram)installedProgram);
}
}
//and save changes to db
db.SaveChanges();
}
}
return this.Content("<result>success</result>");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment