Exemplo n.º 1
0
        public HttpResponseMessage Modificar(string idEstancia, [FromBody] EstanciaDTO dto)
        {
            // CAD, CEN, returnValue
            EstanciaRESTCAD estanciaRESTCAD = null;
            EstanciaCEN     estanciaCEN     = null;
            EstanciaDTOA    returnValue     = null;

            // HTTP response
            HttpResponseMessage response = null;
            string uri = null;

            try
            {
                SessionInitializeTransaction();
                string token = "";
                if (Request.Headers.Authorization != null)
                {
                    token = Request.Headers.Authorization.ToString();
                }
                int id = new UsuarioCEN().CheckToken(token);



                estanciaRESTCAD = new EstanciaRESTCAD(session);
                estanciaCEN     = new EstanciaCEN(estanciaRESTCAD);

                // Modify
                estanciaCEN.Modificar(idEstancia,
                                      dto.Actividad
                                      ,
                                      dto.Latitud
                                      ,
                                      dto.Longitud
                                      ,
                                      dto.Nombre
                                      );

                // Return modified object
                returnValue = EstanciaAssembler.Convert(estanciaRESTCAD.ReadOIDDefault(idEstancia), session);

                SessionCommit();
            }

            catch (Exception e)
            {
                SessionRollBack();

                if (e.GetType() == typeof(HttpResponseException))
                {
                    throw e;
                }
                else if (e.GetType() == typeof(ReciclaUAGenNHibernate.Exceptions.ModelException) && e.Message.Equals("El token es incorrecto"))
                {
                    throw new HttpResponseException(HttpStatusCode.Forbidden);
                }
                else if (e.GetType() == typeof(ReciclaUAGenNHibernate.Exceptions.ModelException) || e.GetType() == typeof(ReciclaUAGenNHibernate.Exceptions.DataLayerException))
                {
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
                }
                else
                {
                    throw new HttpResponseException(HttpStatusCode.InternalServerError);
                }
            }
            finally
            {
                SessionClose();
            }

            // Return 404 - Not found
            if (returnValue == null)
            {
                return(this.Request.CreateResponse(HttpStatusCode.NotFound));
            }
            // Return 200 - OK
            else
            {
                response = this.Request.CreateResponse(HttpStatusCode.OK, returnValue);

                return(response);
            }
        }
Exemplo n.º 2
0
        public HttpResponseMessage Crear([FromBody] EstanciaDTO dto)
        {
            // CAD, CEN, returnValue, returnOID
            EstanciaRESTCAD estanciaRESTCAD = null;
            EstanciaCEN     estanciaCEN     = null;
            EstanciaDTOA    returnValue     = null;
            string          returnOID       = null;

            // HTTP response
            HttpResponseMessage response = null;
            string uri = null;

            try
            {
                SessionInitializeTransaction();
                string token = "";
                if (Request.Headers.Authorization != null)
                {
                    token = Request.Headers.Authorization.ToString();
                }
                int id = new UsuarioCEN().CheckToken(token);



                estanciaRESTCAD = new EstanciaRESTCAD(session);
                estanciaCEN     = new EstanciaCEN(estanciaRESTCAD);

                // Create
                returnOID = estanciaCEN.Crear(
                    //Atributo Primitivo: p_id
                    dto.Id,                            //Atributo Primitivo: p_actividad
                    dto.Actividad,                     //Atributo Primitivo: p_latitud
                    dto.Latitud,                       //Atributo Primitivo: p_longitud
                    dto.Longitud,                      //Atributo Primitivo: p_nombre
                    dto.Nombre,                        //Atributo OID: p_edificio
                    // attr.estaRelacionado: true
                    dto.Edificio_oid                   // association role

                    ,                                  //Atributo OID: p_planta
                    // attr.estaRelacionado: true
                    dto.Planta_oid                     // association role

                    );
                SessionCommit();

                // Convert return
                returnValue = EstanciaAssembler.Convert(estanciaRESTCAD.ReadOIDDefault(returnOID), session);
            }

            catch (Exception e)
            {
                SessionRollBack();

                if (e.GetType() == typeof(HttpResponseException))
                {
                    throw e;
                }
                else if (e.GetType() == typeof(ReciclaUAGenNHibernate.Exceptions.ModelException) && e.Message.Equals("El token es incorrecto"))
                {
                    throw new HttpResponseException(HttpStatusCode.Forbidden);
                }
                else if (e.GetType() == typeof(ReciclaUAGenNHibernate.Exceptions.ModelException) || e.GetType() == typeof(ReciclaUAGenNHibernate.Exceptions.DataLayerException))
                {
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
                }
                else
                {
                    throw new HttpResponseException(HttpStatusCode.InternalServerError);
                }
            }
            finally
            {
                SessionClose();
            }

            // Return 201 - Created
            response = this.Request.CreateResponse(HttpStatusCode.Created, returnValue);

            // Location Header

            /*
             * Dictionary<string, object> routeValues = new Dictionary<string, object>();
             *
             * // TODO: y rolPaths
             * routeValues.Add("id", returnOID);
             *
             * uri = Url.Link("GetOIDEstancia", routeValues);
             * response.Headers.Location = new Uri(uri);
             */

            return(response);
        }