public async Task <IList <DebtReturnDTO> > GetAllDebtsAsync(DebtSearchObjectDTO model) { var idClaim = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; if (!long.TryParse(idClaim, out var ownerId)) { throw new UnauthorizedAccessException(); } var ownerUser = await(await _userRepository.GetAllAsync(user => user.Id == ownerId)) .Include(user => user.Friends) .ThenInclude(friend => friend.FriendUser) .Select(user => new { user.Friends }) .FirstOrDefaultAsync(); var userFriends = ownerUser.Friends; var debtsToReturn = await(await _debtRepository.GetAllAsync(debt => debt.Owner.Id == ownerId || debt.Friend.FriendUser.Id == ownerId && debt.Synchronize, debt => debt.Currency)) .Include(debt => debt.Friend).Where(model.GetExpression(ownerId)) .OrderByDescending(debt => debt.Created) .Skip(model.Offset) .Take(model.Take) .Select(debt => debt.DebtToReturnDebtDTO( debt.Owner.Id == ownerId, userFriends.FirstOrDefault(friend => friend.FriendUser.Id == debt.Owner.Id) )) .ToListAsync(); return(debtsToReturn); }
public async Task <IActionResult> GetAllDebts(DebtSearchObjectDTO model) { try { var data = await _debtService.GetAllDebtsAsync(model); return(Ok(data)); } catch (UnauthorizedAccessException) { return(Unauthorized()); } catch (Exception e) { return(BadRequest(new { e.Message })); } }
public static Expression <Func <Debt, bool> > GetExpression(this DebtSearchObjectDTO model, long reqUserId) { var currentDate = DateTime.UtcNow.Date; return(debt => (!model.DebtOwner.HasValue || (debt.Owner.Id == reqUserId) == model.DebtOwner.Value) && //Is owner of debt (!model.IsClosed.HasValue || debt.IsClosed == model.IsClosed) && //Is closed (!model.IsMoney.HasValue || debt.IsMoney == model.IsMoney) && //Is closed (!model.IsSynchronized.HasValue || debt.Synchronize == model.IsSynchronized) && //Is synchronized (string.IsNullOrWhiteSpace(model.FriendName) || debt.Friend.Name.ToUpper().Equals(model.FriendName.ToUpper())) && //Friend name (string.IsNullOrWhiteSpace(model.Name) || debt.Name.ToUpper().Contains(model.Name.ToUpper())) && //Name (string.IsNullOrWhiteSpace(model.Description) || debt.Description.ToUpper().Contains(model.Description.ToUpper())) && //Description (!model.Overdued.HasValue || (model.Overdued.Value //Overdued ? debt.DateOfOverdue.Value.Date <currentDate : debt.DateOfOverdue> currentDate)) && (!model.ReqUserOwe.HasValue || (model.ReqUserOwe.Value //Is debter ? debt.IsOwnerDebter == (debt.Owner.Id == reqUserId) : debt.IsOwnerDebter != (debt.Owner.Id == reqUserId))) && (!model.CreatedFrom.HasValue || debt.Created >= model.CreatedFrom) && //Created from (!model.CreatedBefore.HasValue || debt.Created <= model.CreatedBefore) && //Created before (!model.ValueLessThan.HasValue || debt.Value <= model.ValueLessThan) && //Value less than (!model.ValueMoreThan.HasValue || debt.Value >= model.ValueMoreThan) //Value more than ); }