Esempio n. 1
0
        public async Task <DebtReturnDTO> AddDebtAsync(DebtAddDTO model)
        {
            var idClaim = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;

            if (!long.TryParse(idClaim, out var ownerId))
            {
                throw new UnauthorizedAccessException();
            }
            var owner = await _userRepository.GetByIdAsync(ownerId);

            var friend = await _friendRepository.GetByIdAsync(model.FriendId, friend1 => friend1.FriendUser);

            if (friend == null)
            {
                throw new ArgumentException("Friend with this id does not exist");
            }

            Currency currency = null;

            if (model.IsMoney)
            {
                if (model.Value == null)
                {
                    throw new ArgumentException("Value can't be null");
                }

                if (model.CurrencyId == null)
                {
                    throw new ArgumentException("Currency id can't be null");
                }

                currency = await _currenciesRepository.GetByIdAsync(model.CurrencyId.Value);
            }

            else if (string.IsNullOrWhiteSpace(model.Name))
            {
                throw new ArgumentException("Thing name can't be empty");
            }

            var newDebt = model.DebtAddDTOToDebt(friend, owner, currency);

            var addedDebt = await _debtRepository.InsertAsync(newDebt);

            if (friend.FriendUser != null && addedDebt.Synchronize)
            {
                await _hubContext.Clients.User(friend.FriendUser.Id.ToString()).SendAsync("UpdateDebts");
            }
            return(addedDebt.DebtToReturnDebtDTO());
        }
Esempio n. 2
0
        public async Task <IActionResult> AddDebt(DebtAddDTO model)
        {
            try
            {
                var data = await _debtService.AddDebtAsync(model);

                return(Ok(data));
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }
            catch (Exception e)
            {
                return(BadRequest(new { e.Message }));
            }
        }
Esempio n. 3
0
 public static Debt DebtAddDTOToDebt(this DebtAddDTO model, Friend friend, User owner, Currency currency)
 {
     return(new Debt
     {
         Name = model.Name,
         Description = model.Description,
         Friend = friend,
         Owner = owner,
         CreatedBy = owner.Id,
         Synchronize = model.Synchronize,
         Value = model.Value,
         IsOwnerDebter = model.IsOwnerDebter,
         DateOfOverdue = model.DateOfOverdue,
         IsMoney = model.IsMoney,
         CurrentValue = model.CurrentValue,
         Currency = currency,
         PendingValue = model.IsMoney ? 0 : (decimal?)null
     });
 }