Esempio n. 1
0
        public async Task <ActionResult> AddExpenseForUser(string userName, [FromBody] ExpenseForCreationDto expense)
        {
            var    userIdFromToken = User.GetUserIdAsGuid();
            string currentUserName = User.GetUserName();

            if (currentUserName != userName)
            {
                return(Unauthorized());
            }

            var expenseEntity            = _mapper.Map <Expense>(expense);
            var categoryTitleFromExpense = expenseEntity.Category;

            if (!await _categoryService
                .CategoryExistForSpecificUser(userIdFromToken, categoryTitleFromExpense))
            {
                return(BadRequest());
            }

            await _expenseService.AddExpense(userIdFromToken, expenseEntity);

            var expenseToReturn = _mapper.Map <ExpenseDto>(expenseEntity);

            return(CreatedAtRoute("GetUserExpenseById",
                                  new { userName = currentUserName, expenseId = expenseToReturn.Id },
                                  expenseToReturn));
        }
Esempio n. 2
0
        public async Task <IActionResult> AddExpense(ExpenseForCreationDto expenseForCreationDto)
        {
            var success = await _expenseService.AddExpense(expenseForCreationDto);

            if (success)
            {
                return(Ok());
            }
            return(NotFound());
        }
        public async Task <IActionResult> CreateExpense(int userId, ExpenseForCreationDto expenseForCreationDto)
        {
            var command = new CreateExpenseCommand(expenseForCreationDto);
            var result  = await Mediator.Send(command);

            if (result != null)
            {
                return(CreatedAtAction(nameof(GetExpenseById),
                                       new { expenseId = result.Id, userId }, result));
            }

            throw new Exception("Creating expense failed on save.");
        }
Esempio n. 4
0
 public Task <bool> AddExpense(ExpenseForCreationDto expenseForCreationDto)
 {
     foreach (var assetId in expenseForCreationDto.AssetIds)
     {
         var expense = new Expense();
         expense.AssetId     = assetId;
         expense.DateCreated = expenseForCreationDto.ExpenseDate;
         expense.Description = expenseForCreationDto.Description;
         expense.ExpenseType = (ExpenseTypeEnum)expenseForCreationDto.ExpenseType;
         expense.Sum         = expenseForCreationDto.Amount / expenseForCreationDto.AssetIds.Count;
         _expenseRepository.AddExpense(expense);
     }
     return(_expenseRepository.SaveAll());
 }
Esempio n. 5
0
        public async Task <ActionResult <ExpenseDto> > PostExpense(ExpenseForCreationDto expenseForCreationDto)
        {
            try
            {
                var expense = _mapper.Map <Expense>(expenseForCreationDto);
                await _repository.CreateAsync(expense);

                var expenseDto = _mapper.Map <ExpenseDto>(expense);

                _logger.LogInfo($"{nameof(PostExpense)} : return CreatedAtAction({nameof(GetExpense)}, {expenseDto.Id}) from database");
                return(CreatedAtAction(nameof(GetExpense), new { id = expenseDto.Id }, expenseDto));
            }
            catch (Exception ex)
            {
                _logger.LogError($"{nameof(PostExpense)} : something went wrong with {JsonSerializer.Serialize(expenseForCreationDto)}" +
                                 $"{Environment.NewLine}\t{ex.Message}");
                throw;
            }
        }
        public async Task <IActionResult> Create(int userId, ExpenseForCreationDto expenseForCreationDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            expenseForCreationDto.UserId = userId;
            var expenseToCreate = _mapper.Map <Expense>(expenseForCreationDto);

            _commonRepo.Add(expenseToCreate);

            if (await _commonRepo.SaveAll())
            {
                var expenseToReturn = _mapper.Map <ExpenseToReturnDto>(expenseToCreate);
                return(CreatedAtRoute("GetExpense", new { id = expenseToCreate.Id }, expenseToReturn));
            }
            throw new Exception("Creating the expense failed on save");
        }
        public async Task CreateTest()
        {
            var expenseForCreation = new ExpenseForCreationDto
            {
                TransactionDate = DateTime.Today,
                Type            = ExpenseType.Food,
                Amount          = 42.3,
                Currency        = "LGB",
                Recipient       = "Dan"
            };

            var content  = new StringContent(JsonConvert.SerializeObject(expenseForCreation), Encoding.UTF8, "application/json");
            var response = await _client.PostAsync("/api/v1/expenses", content);

            Assert.Equal(HttpStatusCode.Created, response.StatusCode);

            var createdContent = await response.Content.ReadAsStringAsync();

            var expense = JsonConvert.DeserializeObject <ExpenseDto>(createdContent);

            Assert.Equal(DateTime.Today, expense.TransactionDate);
            Assert.Equal(ExpenseType.Food, expense.Type);
            Assert.Equal(42.3, expense.Amount);
            Assert.Equal("LGB", expense.Currency);
            Assert.Equal("Dan", expense.Recipient);

            var createdResponse = await _client.GetAsync($"/api/v1/expenses/{expense.Id}");

            Assert.Equal(HttpStatusCode.OK, createdResponse.StatusCode);

            var newContent = await createdResponse.Content.ReadAsStringAsync();

            var newExpense = JsonConvert.DeserializeObject <ExpenseDto>(newContent);

            Assert.Equal(DateTime.Today, newExpense.TransactionDate);
            Assert.Equal(ExpenseType.Food, newExpense.Type);
            Assert.Equal(42.3, newExpense.Amount);
            Assert.Equal("LGB", newExpense.Currency);
            Assert.Equal("Dan", newExpense.Recipient);
        }
Esempio n. 8
0
        public async Task <ActionResult> CreateExpenseByType([FromBody] ExpenseForCreationDto expenseForCreationDto)
        {
            if (!(expenseForCreationDto.Type == "sistema" || expenseForCreationDto.Type == "diario"))
            {
                ModelState.AddModelError("expenseType", "expense type must be iether sistema or diario");
                return(BadRequest(ModelState));
            }

            var expenseToCreate = _mapper.Map <Expense>(expenseForCreationDto);

            _repo.Add(expenseToCreate);

            if (await _repo.SaveAll())
            {
                var newTallyExpense = new TallyExpense()
                {
                    TallyId   = expenseToCreate.TallyId,
                    ExpenseId = expenseToCreate.Id
                };

                _repo.Add(newTallyExpense);
            }
            else
            {
                throw new Exception("failed on creation tallyExpense");
            }


            if (await _repo.SaveAll())
            {
                return(Ok(expenseToCreate.Id));
            }
            // return CreatedAtRoute("GetExpenseByType", new {id = message.Id}, messageToReturn);


            throw new Exception("failed on creation");
        }
Esempio n. 9
0
 public CreateExpenseCommand(ExpenseForCreationDto expenseForCreationDto)
 {
     ExpenseForCreationDto = expenseForCreationDto;
 }