public async Task<IHttpActionResult> PutCommandRelation(int id, CommandRelation CommandRelation) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != CommandRelation.Id) { return BadRequest(); } db.Entry(CommandRelation).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CommandRelationExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public async Task<IHttpActionResult> PostCommandRelation(CommandRelation CommandRelation) { mutex.WaitOne(); if (!ModelState.IsValid) { return BadRequest(ModelState); } //Check to see if the user has an exisiting command for the glow List<CommandRelation> commandList = db.CommandRelations.SqlQuery("select * from CommandRelations where UserId=" + CommandRelation.UserId + " and GlowHubId=" + CommandRelation.GlowHubId + ";").ToList(); if (commandList.Count() == 1) { // User has a command update it commandList.First().Command = CommandRelation.Command; } //The user does not have a command. Insert there command into the glow q. else if (commandList.Count() == 0) { CommandRelation = db.CommandRelations.Add(CommandRelation); await db.SaveChangesAsync(); List<CommandRelation> exitingCommands = db.CommandRelations.SqlQuery("select * from CommandRelations where GlowHubId=" + CommandRelation.GlowHubId + ";").ToList(); // The glow does not have a q making a new one if (exitingCommands.Count() == 1) { CommandRelation.NextCommandId = CommandRelation.Id; GlowHub emptyHub = db.GlowHubs.Find(CommandRelation.GlowHubId); emptyHub.CommandRelationId = CommandRelation.Id; await db.SaveChangesAsync(); } //The glow has a q add it to it. else { CommandRelation first = exitingCommands[0]; CommandRelation.NextCommandId = first.NextCommandId; first.NextCommandId = CommandRelation.Id; await db.SaveChangesAsync(); } } await db.SaveChangesAsync(); mutex.ReleaseMutex(); return CreatedAtRoute("DefaultApi", new { id = CommandRelation.Id }, CommandRelation); }