Example #1
0
        public static NotaInformativaEN Convert(NotaInformativaDTO dto)
        {
            NotaInformativaEN newinstance = null;

            try
            {
                if (dto != null)
                {
                    newinstance = new NotaInformativaEN();



                    newinstance.Id = dto.Id;
                    if (dto.UsuarioAdministrador_oid != -1)
                    {
                        ReciclaUAGenNHibernate.CAD.ReciclaUA.IUsuarioAdministradorCAD usuarioAdministradorCAD = new ReciclaUAGenNHibernate.CAD.ReciclaUA.UsuarioAdministradorCAD();

                        newinstance.UsuarioAdministrador = usuarioAdministradorCAD.ReadOIDDefault(dto.UsuarioAdministrador_oid);
                    }
                    newinstance.Titulo = dto.Titulo;
                    newinstance.Cuerpo = dto.Cuerpo;
                    newinstance.Fecha  = dto.Fecha;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(newinstance);
        }
        public HttpResponseMessage Modificar(int idNotaInformativa, [FromBody] NotaInformativaDTO dto)
        {
            // CAD, CEN, returnValue
            NotaInformativaRESTCAD notaInformativaRESTCAD = null;
            NotaInformativaCEN     notaInformativaCEN     = null;
            NotaInformativaDTOA    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);



                notaInformativaRESTCAD = new NotaInformativaRESTCAD(session);
                notaInformativaCEN     = new NotaInformativaCEN(notaInformativaRESTCAD);

                // Modify
                notaInformativaCEN.Modificar(idNotaInformativa,
                                             dto.Titulo
                                             ,
                                             dto.Cuerpo
                                             ,
                                             dto.Fecha
                                             );

                // Return modified object
                returnValue = NotaInformativaAssembler.Convert(notaInformativaRESTCAD.ReadOIDDefault(idNotaInformativa), 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);
            }
        }
        public HttpResponseMessage Crear([FromBody] NotaInformativaDTO dto)
        {
            // CAD, CEN, returnValue, returnOID
            NotaInformativaRESTCAD notaInformativaRESTCAD = null;
            NotaInformativaCEN     notaInformativaCEN     = null;
            NotaInformativaDTOA    returnValue            = null;
            int returnOID = -1;

            // 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);



                notaInformativaRESTCAD = new NotaInformativaRESTCAD(session);
                notaInformativaCEN     = new NotaInformativaCEN(notaInformativaRESTCAD);

                // Create
                returnOID = notaInformativaCEN.Crear(
                    //Atributo OID: p_usuarioAdministrador
                    // attr.estaRelacionado: true
                    dto.UsuarioAdministrador_oid                    // association role

                    ,                                               //Atributo Primitivo: p_titulo
                    dto.Titulo,                                     //Atributo Primitivo: p_cuerpo
                    dto.Cuerpo);
                SessionCommit();

                // Convert return
                returnValue = NotaInformativaAssembler.Convert(notaInformativaRESTCAD.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("GetOIDNotaInformativa", routeValues);
             * response.Headers.Location = new Uri(uri);
             */

            return(response);
        }