public async Task <ActionResult <ExpenseDto> > AddExpenseAsync(int groupId, [FromBody] ExpenseDto expense)
        {
            var mappedExpense = await _mapper.DtoToExpenseAsync(expense, _userRepository);

            mappedExpense.GroupId = groupId;
            mappedExpense         = await _expenseRepository.CreateAsync(mappedExpense);

            return(await _mapper.ExpenseToDtoAsync(mappedExpense, _userRepository));
        }
Пример #2
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 <ExpenseModel> Handle(CreateExpenseRequest request, CancellationToken cancellationToken)
        {
            if (request.Model.TypeId is not null)
            {
                var projectId = await fExpenseTypeRepository.GetProjectIdAsync(request.Model.TypeId.Value, cancellationToken);

                if (projectId == null || projectId.Value != request.Model.ProjectId)
                {
                    throw new BadRequestCodeException($"Expense type with ID '{request.Model.TypeId}' does not exist or does not belong to project '{request.Model.ProjectId}'.");
                }
            }

            var dto = fMapper.Map <CreateExpenseModel, CreateExpenseDto>(request.Model,
                                                                         opt => opt.AfterMap((_, dto) => dto.CreatorUserId = GetIdentity().Id));

            var detail = await fExpenseRepository.CreateAsync(dto, cancellationToken);

            return(fMapper.Map <ExpenseDto, ExpenseModel>(detail,
                                                          opt => opt.AfterMap((_, dto) => dto.CreatorUserEmail = GetIdentity().Email)));
        }