Ejemplo n.º 1
0
        public ResponseModel <PagedResponse <TResponse> > GetAll(TFilter filter, int pageNumber = 1, int pageSize = 10)
        {
            try
            {
                var pagedResponse = new PagedResponse <TResponse>();
                var listResponse  = new List <TResponse>();

                var allEntities = Repository.GetAll();

                if (allEntities != null && allEntities.Count() > 0)
                {
                    pagedResponse.Total       = allEntities.Count();
                    pagedResponse.CurrentPage = pageNumber;

                    allEntities = Repository.GetAll(filter, pageNumber, pageSize, true);

                    var listEntity = allEntities.ToList();

                    listResponse = Mapper.Map <List <TResponse> >(listEntity);

                    pagedResponse.TotalPage = listResponse.Count;
                    pagedResponse.ListData  = listResponse;
                }

                var resultList = ResponseModel <PagedResponse <TResponse> > .GetResponse(pagedResponse);

                return(resultList);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Ejemplo n.º 2
0
 public override ResponseModel <PessoaResponse> GetBy(PessoaFilter filter)
 {
     if (filter.Id > 0 || !string.IsNullOrEmpty(filter.Nome))
     {
         return(base.GetBy(filter));
     }
     else
     {
         return(ResponseModel <PessoaResponse> .GetResponse(null));
     }
 }
Ejemplo n.º 3
0
        public async Task Invoke(HttpContext context)
        {
            try
            {
                await _next(context);
            }
            catch (Exception e)
            {
                var response = context.Response;
                response.StatusCode = (int)HttpStatusCode.InternalServerError;

                var result = JsonSerializer.Serialize(ResponseModel.GetResponse((int)HttpStatusCode.InternalServerError, e?.Message));

                await response.WriteAsync(result);
            }
        }
Ejemplo n.º 4
0
        public ResponseModel <bool?> Delete(int id)
        {
            try
            {
                var entity = Repository.GetAll().FirstOrDefault(x => x.Id == id);

                if (entity != null)
                {
                    var delete = Repository.Delete(id);
                    return(ResponseModel <bool?> .GetResponse(delete));
                }
                else
                {
                    return(ResponseModel <bool?> .GetReponseErrors("Id não encontrado para exclusão"));
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public async Task <IActionResult> ConversionRate([FromQuery] string currencyPair)
        {
            if (string.IsNullOrEmpty(currencyPair) || currencyPair.Trim().Length != 6)
            {
                return(Json(ResponseModel.GetResponse((int)HttpStatusCode.BadRequest, "Currency pair text is invalid.")));
            }

            var currency1 = currencyPair.Substring(0, 3);
            var currency2 = currencyPair.Substring(3, 3);

            var rate = await _rateCalculatorService.CalculateConversionRate(currency1, currency2);

            if (rate == -1)
            {
                return(Json(ResponseModel.GetResponse((int)HttpStatusCode.BadRequest, "Currency pair text is invalid.")));
            }

            var message = $"1 {currency1} = {rate:0.0000} {currency2}";

            return(Json(ResponseModel.GetResponse((int)HttpStatusCode.OK, message, new { rate = $"{rate:0.0000}" })));
        }
Ejemplo n.º 6
0
        public ResponseModel <bool?> Update(TRequest request)
        {
            try
            {
                var entity         = Mapper.Map <TEntity>(request);
                var entityValidate = Validate.Validate(entity);

                if (entityValidate.IsValid)
                {
                    var updated = Repository.Update(entity);
                    return(ResponseModel <bool?> .GetResponse(updated));
                }
                else
                {
                    return(ResponseModel <bool?> .GetReponseErrors(entityValidate.Errors));
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Ejemplo n.º 7
0
        public virtual ResponseModel <TResponse> GetBy(TFilter filter)
        {
            try
            {
                TResponse response = null;

                var allEntities = Repository.GetAll(filter);

                if (allEntities != null && allEntities.Count() > 0)
                {
                    var entity = allEntities.FirstOrDefault();
                    response = Mapper.Map <TResponse>(entity);
                }

                var result = ResponseModel <TResponse> .GetResponse(response);

                return(result);
            }
            catch (Exception)
            {
                throw;
            }
        }
Ejemplo n.º 8
0
        public ResponseModel <bool?> Add(TRequest request)
        {
            try
            {
                var entity         = Mapper.Map <TEntity>(request);
                var entityValidate = Validate.Validate(entity);

                if (entityValidate.IsValid)
                {
                    var objResponse = Mapper.Map <TResponse>(Repository.Add(entity));
                    var added       = objResponse.Id > 0;
                    return(ResponseModel <bool?> .GetResponse(added));
                }
                else
                {
                    return(ResponseModel <bool?> .GetReponseErrors(entityValidate.Errors));
                }
            }
            catch (Exception)
            {
                throw;
            }
        }