示例#1
0
        public HttpResponseMessage CreateNewSensorPessoal([FromBody] SensorPessoal sensor)
        {
            try
            {
                if (sensor == null || sensor.Temperatura == 0 || sensor.Humidade == 0 || sensor.Local == null)
                {
                    var form = new Dictionary <string, string>
                    {
                        { "Id", "(automatic) Type:Int" },
                        { "Temperatura", "(required) Type:Decimal" },
                        { "Humidade", "(required) Type:Decimal" },
                        { "Local", "(required) Type:String" },
                        { "Data", "(optional - DEFAULT Current DateTime) Type:Datetime" },
                        { "Valido", "(automatic - DEFAULT true) Type:Bool" },
                        { "Utilizador", "(automatic - DEFAULT Authenticated User), Type:Int" }
                    };

                    return(Request.CreateResponse(HttpStatusCode.BadRequest, form));
                }

                var identity = (ClaimsPrincipal)Thread.CurrentPrincipal;
                int id       = int.Parse(identity.Claims.Where(c => c.Type == ClaimTypes.Sid).Select(c => c.Value).SingleOrDefault());

                SensorPessoal newSensor = SensorPessoal.CreateNewSensor(sensor, id);

                if (newSensor == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.InternalServerError));
                }

                return(Request.CreateResponse(HttpStatusCode.OK, newSensor));
            }
            catch (Exception exception)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, exception));
            }
        }