Пример #1
0
        public void Post_WhenInvoked_ShouldCallAddAsync()
        {
            //arrange
            this.mockUserResolverService
            .Setup(x => x.User)
            .Returns(new ClaimsPrincipal(new GenericIdentity(Guid.NewGuid().ToString())));

            var bindindModel = new AddExpenseBindingModel
            {
                Amount      = 100,
                CategoryId  = 1,
                Description = "test1"
            };

            //act
            var response = this.testClient
                           .PostAsJsonAsync(TestContants.EXPENSES_ENDPOINT, bindindModel).Result;

            //assert
            response.EnsureSuccessStatusCode();
            Assert.AreEqual(response.StatusCode, HttpStatusCode.Created);
            this.mockExpenseService.Verify(x =>
                                           x.AddAsync(It.IsAny <decimal>(), It.IsAny <string>(), It.IsAny <long>(), It.IsAny <string>()),
                                           Times.Once);
        }
Пример #2
0
        public async Task <ActionResult <Expense> > PostExpense(AddExpenseBindingModel model)
        {
            var expense = new Expense
            {
                Description  = model.Description,
                Amount       = model.Amount,
                Category     = model.Category,
                SpentDate    = model.SpentDate,
                CreationDate = DateTime.UtcNow,
            };

            await this.expenseRepository.AddAsync(expense);

            return(this.CreatedAtAction("GetExpense", new { id = expense.Id }, expense));
        }
Пример #3
0
        public async Task <IActionResult> AddAsync(AddExpenseBindingModel bindingModel)
        {
            try
            {
                var userId = this.userResolverService.User.FindFirst(ClaimTypes.Name).Value;

                var expense = await this.expensesService.AddAsync(
                    bindingModel.Amount,
                    bindingModel.Description,
                    bindingModel.CategoryId,
                    userId);

                return(this.Created("", expense));
            }
            catch (Exception e)
            {
                return(this.BadRequest("Unable to add expense, please contact support"));
            }
        }
Пример #4
0
        public async Task <Expense> AddAsync(
            string description,
            decimal amount,
            Category category,
            DateTime spentDate)
        {
            var expense = new AddExpenseBindingModel
            {
                Description = description,
                Amount      = amount,
                Category    = category,
                SpentDate   = spentDate
            };

            var expenseJson = new StringContent(JsonSerializer.Serialize(expense), Encoding.UTF8, "application/json");
            var response    = await this.httpClient.PostAsync(URL_BASE, expenseJson);

            if (response.IsSuccessStatusCode)
            {
                return(await JsonSerializer.DeserializeAsync <Expense>(await response.Content.ReadAsStreamAsync()));
            }

            return(null);
        }