Beispiel #1
0
        /// <summary>
        ///     Create a new <see cref="DebitEntry"/>
        /// </summary>
        /// <param name="item"> New entry's attributes </param>
        /// <returns>
        ///     HTTP response including:
        ///         - Response code. We should return 201 for POST request results, according to the HTTP/1.1 protocol
        ///         - Location. When the server creates a resource, it should include the URI of the new resource in the Location header of the response.
        /// </returns>
        public IHttpActionResult Post(DebitEntry item)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            int id = repository.InsertDebitEntry(1, item);

            return Created(Url.Link("DefaultApi", new { id }), item); // Status 201 (Created) with location
        }
Beispiel #2
0
        public void NewDebitEntryTest()
        {
            var controller = new DebitsController(@"Data Source=(local)\SQLEXPRESS;Initial Catalog=Budget;Integrated Security=True");
            SetupPostControllerForTest(controller, "Post");

            var item = new DebitEntry { LogTime = new DateTime(2015, 1, 1), Amount = 50.05m, Description = "Bla bla" };
            var response = controller.Post(item).ExecuteAsync(new CancellationToken()).Result;

            Assert.AreSame(response.StatusCode, HttpStatusCode.Created);
            Assert.AreEqual("http://localhost/api/debits/1", response.Headers.Location.ToString());
        }
Beispiel #3
0
        /// <summary>
        ///     Update an entry
        /// </summary>
        /// <param name="id"> Entry's ID </param>
        /// <param name="item"> Properties </param>
        public IHttpActionResult Put(int id, DebitEntry item)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            item.ID = id;
            int count = repository.BatchInsertOrUpdate(1, new[] {item}, true);
            if (count == 0)
                throw new HttpResponseException(HttpStatusCode.NotFound);

            return Ok(id);
        }
Beispiel #4
0
        public void CsvUploadTest()
        {
            var controller = new DebitsController(@"Data Source=(local)\SQLEXPRESS;Initial Catalog=Budget;Integrated Security=True");
            SetupPostControllerForTest(controller, "CsvUpload");

            var item = new DebitEntry { LogTime = new DateTime(2015, 1, 1), Amount = 50.05m, Description = "Bla bla" };
            var responses = controller.CsvUpload().Result;

            ObjectContent content = responses.ExecuteAsync(new CancellationToken()).Result.Content as ObjectContent;
            var result = (DebitEntry)content.Value;
            Assert.AreSame(result, item);
        }