public async Task<IHttpActionResult> PostKeyRegistration(KeyRegistration keyRegistration)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.KeyRegistrations.Add(keyRegistration);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (KeyRegistrationExists(keyRegistration.KeycodeId))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = keyRegistration.KeycodeId }, keyRegistration);
        }
        public async Task<IHttpActionResult> PutKeyRegistration(string id, KeyRegistration keyRegistration)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != keyRegistration.KeycodeId)
            {
                return BadRequest();
            }

            db.Entry(keyRegistration).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!KeyRegistrationExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
        public async Task<IHttpActionResult> GetKeyRegistration(string id)
        {
            KeyRegistration keyRegistration = await db.KeyRegistrations.FindAsync(id);
            if (keyRegistration == null)
            {
                return NotFound();
            }

            return Ok(keyRegistration);
        }
        public async Task<IHttpActionResult> DeleteKeyRegistration(string id)
        {
            KeyRegistration keyRegistration = await db.KeyRegistrations.FindAsync(id);
            if (keyRegistration == null)
            {
                return NotFound();
            }

            db.KeyRegistrations.Remove(keyRegistration);
            await db.SaveChangesAsync();

            return Ok(keyRegistration);
        }
Esempio n. 5
0
        /// <summary>
        /// Send a request to create a new user with a secret key
        /// </summary>
        /// <param name="service"></param>
        /// <param name="user">The user to create</param>
        /// <param name="usernameCredentials">The user's key credentials</param>
        /// <returns>An authentication token and user information, or <code>null</code> if the request failed</returns>
        public static async Task <AuthResponse?> Register(this HttpService service, User user, KeyCredentials keyCredentials)
        {
            var registration = new KeyRegistration(keyCredentials, user);

            return(await service.Session.Send <AuthResponse>(() => HttpRequestMessageExtensions.Create(service.Session, "v1/register/key", HttpMethod.Post, registration)));
        }