Exemplo n.º 1
0
 public async Task <IActionResult> GetContactListByPhone(PhoneRequestDto phoneRequest)
 {
     if (ModelState.IsValid)
     {
         return(buildResponse(await _service.GetContactsByPhone(phoneRequest)));
     }
     return(null);
 }
Exemplo n.º 2
0
        public async Task <ClientResponse <List <ContactDto> > > GetContactsByPhone(PhoneRequestDto request)
        {
            var response = new ClientResponse <List <ContactDto> >();

            try
            {
                request.Pagination ??= new PaginationDto();
                // If it got up to here, prefix and number should be ints given that they passed the model validation, so we can parse to string
                var phones = await _repository.Phones
                             .Where(p => p.Active && p.Prefix.Contains(request.Prefix.ToString()) && p.Number.Contains(request.Number.ToString()))
                             .Skip((request.Pagination.Page - 1) * request.Pagination.Limit).Take(request.Pagination.Limit)
                             .ToListAsync();

                // Distinct contacts since the above returns them all.
                if (phones != null && phones.Count > 0)
                {
                    var phonesDistinct = phones.DistinctBy(p => p.ContactID);
                    var result         = new List <ContactDto>();
                    foreach (Mo.Phone pho in phonesDistinct)
                    {
                        var c = (await GetContactById(pho.ContactID)).Response;
                        result.Add(c);
                    }
                    response.Status   = System.Net.HttpStatusCode.OK;
                    response.Response = result;
                    return(response);
                }
                response.Status  = System.Net.HttpStatusCode.NoContent;
                response.Message = E.ErrNoPhonesFound;
                return(response);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                response.Status  = System.Net.HttpStatusCode.InternalServerError;
                return(response);
            }
        }