private async Task <ApplicationUser> GetUserWorkerFromRequest(PutWorkerSettings data = null) { string userId; int workerId; if (data == null) { if (!Request.Query.ContainsKey("guid") && !Request.Query.ContainsKey("workerId")) { return(null); } userId = Request.Query["guid"]; if (!int.TryParse(Request.Query["workerId"], out workerId)) { return(null); } } else { userId = data.Guid; workerId = data.WorkerId; } ApplicationUser user = await _context.Users.Where(u => u.Id == userId && u.WorkerId == workerId) .Include(u => u.Worker) .SingleOrDefaultAsync(); return(user); }
public async Task <IActionResult> PutWorker([FromBody] PutWorkerSettings data) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } ApplicationUser userWorker = await GetUserWorkerFromRequest(data); if (userWorker == null) { return(Unauthorized()); } userWorker.Worker.WorkerSettingJson = data.GetWorkerSettingsJSON(); if (string.IsNullOrEmpty(userWorker.Worker.WorkerSettingJson)) { BadRequest(); } _context.Entry(userWorker.Worker).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!WorkerExists(data.WorkerId)) { return(NotFound()); } else { return(NoContent()); } } return(Ok()); }