Ejemplo n.º 1
0
        public JsonResult Post([FromBody] AgentViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newAgent = vm.ToEntity();
                    _logger.LogInformation("Attempting to save a new Agent");

                    _repository.Add(newAgent);

                    _unitOfWork.Commit();

                    Response.StatusCode = (int)HttpStatusCode.Created;
                    return(Json(AgentViewModel.FromEntity(newAgent)));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to save Agent", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json("Failed to save Agent"));
            }
            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json(new { Message = "Failed", ModelState = ModelState }));
        }
Ejemplo n.º 2
0
        public override JsonResult Get(int pageNo = 1, int pageSize = 50, string orderBy = "CreateDate")
        {
            try
            {
                var Agents = _repository.GetPaged(pageNo, pageSize, orderBy);

                var AgentsVm = Agents.Select(x => AgentViewModel.FromEntity(x));


                if (AgentsVm == null)
                {
                    return(Json(null));
                }

                int total = Agents.Count();
                return(Json(new PagedResult <AgentViewModel>(AgentsVm, pageNo, pageSize, total)));


                //return new string[] { "value1", "value2" };
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to get Agents", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json("Failed to get Agents"));
            }
        }
Ejemplo n.º 3
0
        public override JsonResult Get(int id)
        {
            try
            {
                var Ag   = _repository.GetSingle(id);
                var AgVm = AgentViewModel.FromEntity(Ag);

                if (AgVm == null)
                {
                    return(Json(null));
                }

                return(Json(AgVm));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to get Agent", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json("Failed to get Agent"));
            }
        }
Ejemplo n.º 4
0
        public override JsonResult Get()
        {
            try
            {
                var Agents = _repository.GetAll();

                var AgentsVm = Agents.Select(x => AgentViewModel.FromEntity(x));

                if (AgentsVm == null)
                {
                    return(Json(null));
                }
                return(Json(AgentsVm));

                //return new string[] { "value1", "value2" };
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to get Agents", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json("Failed to get Agents"));
            }
        }