Esempio n. 1
0
        public HttpResponseMessage Modify(int idTarget, [FromBody] TargetDTO dto)
        {
            // CAD, CEN, returnValue
            TargetRESTCAD targetRESTCAD = null;
            TargetCEN     targetCEN     = null;
            TargetDTOA    returnValue   = null;

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

            try
            {
                SessionInitializeTransaction();


                targetRESTCAD = new TargetRESTCAD(session);
                targetCEN     = new TargetCEN(targetRESTCAD);

                // Modify
                targetCEN.Modify(idTarget,
                                 dto.DesiredValue
                                 ,
                                 dto.Description
                                 ,
                                 dto.DueDate
                                 );

                // Return modified object
                returnValue = TargetAssembler.Convert(targetRESTCAD.ReadOIDDefault(idTarget), session);

                SessionCommit();
            }

            catch (Exception e)
            {
                SessionRollBack();

                if (e.GetType() == typeof(HttpResponseException))
                {
                    throw e;
                }
                else if (e.GetType() == typeof(MoSIoTGenNHibernate.Exceptions.ModelException) && e.Message.Equals("El token es incorrecto"))
                {
                    throw new HttpResponseException(HttpStatusCode.Forbidden);
                }
                else if (e.GetType() == typeof(MoSIoTGenNHibernate.Exceptions.ModelException) || e.GetType() == typeof(MoSIoTGenNHibernate.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);
            }
        }
Esempio n. 2
0
        public HttpResponseMessage New_([FromBody] TargetDTO dto)
        {
            // CAD, CEN, returnValue, returnOID
            TargetRESTCAD targetRESTCAD = null;
            TargetCEN     targetCEN     = null;
            TargetDTOA    returnValue   = null;
            int           returnOID     = -1;

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

            try
            {
                SessionInitializeTransaction();


                targetRESTCAD = new TargetRESTCAD(session);
                targetCEN     = new TargetCEN(targetRESTCAD);

                // Create
                returnOID = targetCEN.New_(

                    //Atributo OID: p_goal
                    // attr.estaRelacionado: true
                    dto.Goal_oid                     // association role

                    , dto.DesiredValue               //Atributo Primitivo: p_desiredValue
                    , dto.Description                //Atributo Primitivo: p_description
                    , dto.DueDate                    //Atributo Primitivo: p_dueDate
                    );
                SessionCommit();

                // Convert return
                returnValue = TargetAssembler.Convert(targetRESTCAD.ReadOIDDefault(returnOID), session);
            }

            catch (Exception e)
            {
                SessionRollBack();

                if (e.GetType() == typeof(HttpResponseException))
                {
                    throw e;
                }
                else if (e.GetType() == typeof(MoSIoTGenNHibernate.Exceptions.ModelException) && e.Message.Equals("El token es incorrecto"))
                {
                    throw new HttpResponseException(HttpStatusCode.Forbidden);
                }
                else if (e.GetType() == typeof(MoSIoTGenNHibernate.Exceptions.ModelException) || e.GetType() == typeof(MoSIoTGenNHibernate.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("GetOIDTarget", routeValues);
             * response.Headers.Location = new Uri(uri);
             */

            return(response);
        }