public async Task <IActionResult> PutAgent(long id, Agent agent) { if (id != agent.Id) { return(BadRequest()); } _context.Entry(agent).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AgentExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutCustomer(int id, Customer customer) { if (id != customer.Id) { return(BadRequest()); } _context.Entry(customer).State = EntityState.Modified; if (customer.Name != null) { _context.Entry(customer.Name).State = EntityState.Modified; } try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutAgent(int id, Agent agent) { if (id != agent.Id) { return(BadRequest()); } _context.Entry(agent).State = EntityState.Modified; if (agent.Phone != null) { _context.Entry(agent.Phone).State = EntityState.Modified; } try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AgentExists(id)) { return(NotFound()); } else { throw; } } await _context.Phones.ToListAsync(); var existingAgents = await _context.Agents.FindAsync(id); return(NoContent()); }
public async Task <ActionResult <AgentDto> > PutAgentItem(long id) { if (!AgentItemExists(id)) { return(BadRequest()); } //read raw and deserialize it to AgentDto object if (Request.Body.CanSeek) { // Reset the position to zero to read from the beginning. Request.Body.Position = 0; } var rawRequestBody = new StreamReader(Request.Body).ReadToEnd(); AgentDto agent = JsonConvert.DeserializeObject <AgentDto>(rawRequestBody); _context.AgentItems.Load(); if (id != agent.id) { return(BadRequest()); } AgentItem agentItem = null; List <Resource> resources = null; //find agentItem in database with given id, then load its resources foreach (var a in _context.AgentItems.Local) { if (a.id == id) { agentItem = _context.AgentItems.Single(a => a.id == id); _context.Entry(agentItem).Collection(a => a.resources).Load(); resources = a.resources; foreach (var s in resources) { _context.Resources.Remove(s); } break; } } await _context.SaveChangesAsync(); foreach (var s in agent.resources) { var resource = new Resource() { Name = s }; _context.Resources.Add(resource); resources.Add(resource); } await _context.SaveChangesAsync(); _context.Entry(agentItem).State = EntityState.Detached; agentItem = new AgentItem() { id = agent.id, name = agent.name, os = agent.os, status = agent.status, type = agent.type, ip = agent.ip, location = agent.location, resources = resources, }; _context.Entry(agentItem).State = EntityState.Modified; _context.AgentItems.Update(agentItem); try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AgentItemExists(id)) { return(NotFound()); } else { throw; } } return(new AgentDto() { id = agentItem.id, name = agentItem.name, os = agentItem.os, status = agentItem.status, type = agentItem.type, ip = agentItem.ip, location = agentItem.location, resources = agentItem.resources.Select(r => r.Name).ToArray() }); }