Example #1
0
        // Add item
        public CredentialFull AddCredential(CredentialAdd credential)
        {
            // Map from view model
            var c = ds.Credentials.Add(Mapper.Map <Models.Credential>(credential));

            // Create the HTTP Basic Authentication encoding
            byte[] bytesToEncode =
                System.Text.Encoding.ASCII.GetBytes
                    (c.Username.Trim().ToLower() + ":" + c.Password);
            c.Token = System.Convert.ToBase64String(bytesToEncode);

            ds.SaveChanges();

            return(Mapper.Map <CredentialFull>(c));
        }
        // Add item
        public CredentialFull AddCredential(CredentialAdd credential)
        {
            // Map from view model
            var c = ds.Credentials.Add(Mapper.Map<Models.Credential>(credential));

            // Create the HTTP Basic Authentication encoding
            byte[] bytesToEncode =
                System.Text.Encoding.ASCII.GetBytes
                (c.Username.Trim().ToLower() + ":" + c.Password);
            c.Token = System.Convert.ToBase64String(bytesToEncode);

            ds.SaveChanges();

            return Mapper.Map<CredentialFull>(c);
        }
        // POST api/credentials
        public HttpResponseMessage Post(CredentialAdd credential)
        {
            if (ModelState.IsValid)
            {
                // Add the new credential
                CredentialFull c = r.AddCredential(credential);

                // Prepare the response
                HttpResponseMessage response =
                    Request.CreateResponse<CredentialFull>(HttpStatusCode.Created, c);
                // Add a Location header
                response.Headers.Location =
                    new Uri(Url.Link("DefaultApi", new { id = c.Id }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
            }
        }