public async Task <IHttpActionResult> PutRequestSwap(int id, RequestSwap requestSwap) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != requestSwap.Id) { return(BadRequest()); } db.Entry(requestSwap).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RequestSwapExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IHttpActionResult> GetRequestSwap(int id) { RequestSwap requestSwap = await db.RequestsSwap.FindAsync(id); if (requestSwap == null) { return(NotFound()); } return(Ok(requestSwap)); }
public async Task <IHttpActionResult> PostRequestSwap(RequestSwap requestSwap) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.RequestsSwap.Add(requestSwap); await db.SaveChangesAsync(); return(CreatedAtRoute("DefaultApi", new { id = requestSwap.Id }, requestSwap)); }
public async Task <IHttpActionResult> DeleteRequestSwap(int id) { RequestSwap requestSwap = await db.RequestsSwap.FindAsync(id); if (requestSwap == null) { return(NotFound()); } db.RequestsSwap.Remove(requestSwap); await db.SaveChangesAsync(); return(Ok(requestSwap)); }
public void Swap(int userId, String credVal) { RequestSwap reqSwap = Front(userId); // Transform RequestSwap into ProxySwap var proxySwap = new ProxySwap(); proxySwap.Ip = reqSwap.Ip; proxySwap.Domain = reqSwap.Domain; proxySwap.RandToken = reqSwap.RandToken; proxySwap.UserId = reqSwap.UserId; proxySwap.Credential = credVal; // Add to ProxySwap Database, remove from RequestSwap Database _context.ProxySwaps.Add(proxySwap); _context.RequestSwaps.Remove(reqSwap); _context.SaveChanges(); }
public IActionResult Submit([FromBody] SubmitSwapModel model) { int userId = int.Parse(User.Identity.Name); // Grab the top request Swap as well as the credential the user wants to use RequestSwap reqSwap = _swapService.Front(userId); if (reqSwap == null) { return(BadRequest(new { Title = "User does not have any pending request Swaps" })); } Credential cred = new Credential(); cred.Id = model.CredentialId; cred.UserId = userId; cred.Domain = reqSwap.Domain; cred = _credService.Read(cred)[0]; if (cred == null) { return(BadRequest(new { Title = "User is not allowed to use this credential on this domain" })); } try { String valueHash = Decrypt(cred.ValueHash, model.PrivateKey); _swapService.Swap(userId, valueHash); return(Ok()); } catch (AppException e) { return(BadRequest(new { Title = e.Message })); // If private key is not correct, this exception will trigger } catch (FormatException e) { return(BadRequest(new { Title = e.Message })); } catch (Exception e) { return(BadRequest(new { Title = e.Message })); } }
public RequestSwap Front(int userId) { RequestSwap topReq = _context.RequestSwaps.OrderBy(r => r.Id).FirstOrDefault(r => r.UserId == userId); return(topReq); }
public void Enqueue(RequestSwap reqSwap) { _context.RequestSwaps.Add(reqSwap); _context.SaveChanges(); }