Ejemplo n.º 1
0
        public IHttpActionResult PostGateway([FromBody] CreateGatewayInput input)
        {
            GatewayDetailDto response = null;

            try
            {
                response = _service.Add(input);
            }
            catch (Exception e)
            {
                Helpers.ExceptionHelper.ThrowException(e);
            }
            return(Ok(response));
        }
Ejemplo n.º 2
0
        public GatewayDetailDto Add(CreateGatewayInput input)
        {
            var entity = Mapper.Map <CreateGatewayInput, Gateway>(input);

            // IPv4 Validation
            var isValidIPAddress = IPAddress.TryParse(input.IPv4, out var ipAddress);

            if (!isValidIPAddress)
            {
                return new GatewayDetailDto
                       {
                           StatusCode    = HttpStatusCode.BadRequest,
                           StatusMessage = "You must provide a valid IPv4. Ex: 192.168.1.1",
                       }
            }
            ;

            try
            {
                using (_uowFactory.Create())
                {
                    _gatewayRepo.Add(entity);
                }
            }
            catch (Exception e)
            {
                var dtoWithErrors = Mapper.Map <Gateway, GatewayDetailDto>(entity);

                dtoWithErrors.StatusCode = HttpStatusCode.BadRequest;
                var msg = new StringBuilder();
                msg.AppendLine(e.Message);
                msg.AppendLine(e.InnerException?.Message);
                msg.AppendLine(e.InnerException?.InnerException?.Message);
                dtoWithErrors.StatusMessage = $"Error occurred when processing request: {msg}";

                return(dtoWithErrors);
            }

            var dto = Mapper.Map <Gateway, GatewayDetailDto>(entity);

            dto.StatusCode    = HttpStatusCode.Created;
            dto.StatusMessage = "Gateway created successfully.";

            return(dto);
        }