public static void InitializeData()
 {
     // Console.WriteLine("Init");
     foreach (ItemEN i in itemCEN.BuscarTodos(0, -1))
     {
         if (i.EsValido == EstadoEnum.enProceso)
         {
             changed_ids.Add(i.Id);
             itemCEN.Modificar(i.Id, i.Nombre, i.Descripcion, i.Imagen, EstadoEnum.descartado, 10);
             // Console.WriteLine("Mod " +  i.Id);
         }
     }
     for (int i = 0; i < new_elements; i++)
     {
         int id = itemCEN.Crear("Item Test", "Item Test", "TEST IMG", -1, -1);
         item = itemCEN.BuscarPorId(id);
         itemCEN.Modificar(id, item.Nombre, item.Descripcion, item.Imagen, EstadoEnum.enProceso, 10);
         ids.Add(id);
         // Console.WriteLine("Added " + id);
     }
 }
        public void CrearAccionItem(int p_oid)
        {
            /*PROTECTED REGION ID(ReciclaUAGenNHibernate.CP.ReciclaUA_Item_crearAccionItem) ENABLED START*/

            IItemCAD       itemCAD       = null;
            ItemCEN        itemCEN       = null;
            AccionWebCEN   accionWebCEN  = null;
            ITipoAccionCAD tipoAccionCAD = null;
            TipoAccionCEN  tipoAccionCEN = null;
            TipoAccionEN   tipoAccion    = null;
            IUsuarioWebCAD usuarioWebCAD = null;
            UsuarioWebCEN  usuarioWebCEN = null;
            ItemEN         item          = null;

            try
            {
                SessionInitializeTransaction();
                itemCAD       = new ItemCAD(session);
                itemCEN       = new ItemCEN(itemCAD);
                tipoAccionCAD = new TipoAccionCAD(session);
                tipoAccionCEN = new TipoAccionCEN(tipoAccionCAD);
                usuarioWebCAD = new UsuarioWebCAD(session);
                usuarioWebCEN = new UsuarioWebCEN(usuarioWebCAD);
                accionWebCEN  = new AccionWebCEN();

                item = itemCEN.BuscarPorId(p_oid);

                if (item.Usuario != null && usuarioWebCEN.BuscarPorId(item.Usuario.Id) != null)
                {
                    tipoAccion = tipoAccionCEN.BuscarTodos(0, -1).Where(t => t.Nombre.Equals("Item")).FirstOrDefault();

                    if (tipoAccion == null)
                    {
                        var id = tipoAccionCEN.Crear(10, "Item");
                        tipoAccion = tipoAccionCEN.BuscarPorId(id);
                    }
                    accionWebCEN.Crear(item.Usuario.Id, tipoAccion.Id);
                }
                SessionCommit();
            }
            catch (Exception ex)
            {
                SessionRollBack();
                throw ex;
            }
            finally
            {
                SessionClose();
            }


            /*PROTECTED REGION END*/
        }
        public Byte[] GetImage(int id, string imageName)
        {
            ItemCEN itemCEN = new ItemCEN();
            ItemEN  item    = itemCEN.BuscarPorId(id);

            // string tempName = id + imageName;

            if (!item.Imagen.StartsWith("http"))
            {
                var  filePath = HttpContext.Current.Server.MapPath("~/ItemsImages");
                bool exists   = System.IO.Directory.Exists(filePath);
                if (!exists)
                {
                    return(null);
                }
                exists = System.IO.File.Exists(item.Imagen);
                if (!exists)
                {
                    return(null);
                }
                Byte[] image = File.ReadAllBytes(item.Imagen);
                return(image);
            }
            else
            {
                using (var webClient = new WebClient())
                {
                    try
                    {
                        Byte[] image = webClient.DownloadData(item.Imagen);
                        return(image);
                    }
                    catch (Exception e)
                    {
                        return(null);
                    }
                }
            }
        }
        public bool RemoveImage(int id, string imageName)
        {
            ItemCEN itemCEN  = new ItemCEN();
            ItemEN  item     = itemCEN.BuscarPorId(id);
            var     filePath = HttpContext.Current.Server.MapPath("~/ItemsImages");
            bool    exists   = System.IO.Directory.Exists(filePath);

            if (!exists)
            {
                return(false);
            }

            if (!item.Imagen.StartsWith("http"))
            {
                exists = System.IO.File.Exists(item.Imagen);
                if (!exists)
                {
                    return(false);
                }
                File.Delete(item.Imagen);
                return(true);
            }
            return(false);
        }
 public void ThenSeCreoElItem()
 {
     item = itemCEN.BuscarPorId(itemId);
     Assert.IsNotNull(item);
 }
        public void ThenObtengoElItemConLosDatosModificados()
        {
            ItemEN UpdatedItem = itemCEN.BuscarPorId(itemId);

            Assert.AreEqual(UpdatedItem.Nombre, item.Nombre + " modificado");
        }
 public void GivenExisteUnItemConIdEspecifico()
 {
     itemCEN = new ItemCEN();
     item    = itemCEN.BuscarPorId(itemId);
 }
 public void WhenBuscoElItemPosSuId()
 {
     item = itemCEN.BuscarPorId(itemId);
 }
Ejemplo n.º 9
0
        public HttpResponseMessage BuscarPorId(int idItem)
        {
            // CAD, CEN, EN, returnValue
            ItemRESTCAD itemRESTCAD = null;
            ItemCEN     itemCEN     = null;
            ItemEN      itemEN      = null;
            ItemDTOA    returnValue = null;

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



                itemRESTCAD = new ItemRESTCAD(session);
                itemCEN     = new ItemCEN(itemRESTCAD);

                // Data
                itemEN = itemCEN.BuscarPorId(idItem);

                // Convert return
                if (itemEN != null)
                {
                    returnValue = ItemAssembler.Convert(itemEN, session);
                }
            }

            catch (Exception e)
            {
                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
            {
                return(this.Request.CreateResponse(HttpStatusCode.OK, returnValue));
            }
        }
Ejemplo n.º 10
0
 public static void InitializeData()
 {
     Console.WriteLine("Init");
     id   = itemCEN.Crear("Item Test", "Item Test", "IMG TEST", -1, -1);
     item = itemCEN.BuscarPorId(id);
 }
        public async Task <HttpResponseMessage> UploadImage(int p_oid)
        {
            ItemCEN itemCEN = new ItemCEN();
            ItemEN  item    = null;

            Dictionary <string, object> dict = new Dictionary <string, object>();

            try
            {
                var httpRequest = HttpContext.Current.Request;

                foreach (string file in httpRequest.Files)
                {
                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created);

                    var postedFile = httpRequest.Files [file];
                    if (postedFile != null && postedFile.ContentLength > 0)
                    {
                        int MaxContentLength = 1024 * 1024 * 5;         //Size = 1 MB

                        IList <string> AllowedFileExtensions = new List <string> {
                            ".jpg", ".jpeg", ".png"
                        };
                        var ext       = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.'));
                        var extension = ext.ToLower();
                        if (!AllowedFileExtensions.Contains(extension))
                        {
                            var message = string.Format("Please Upload image of type .jpg,.gif,.png.");

                            dict.Add("error", message);
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, dict));
                        }
                        else if (postedFile.ContentLength > MaxContentLength)
                        {
                            var message = string.Format("Please Upload a file upto 1 mb.");

                            dict.Add("error", message);
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, dict));
                        }
                        else
                        {
                            string imageName = p_oid + Path.GetExtension(postedFile.FileName);

                            var  filePath = HttpContext.Current.Server.MapPath("~/ItemsImages");
                            bool exists   = System.IO.Directory.Exists(filePath);
                            if (!exists)
                            {
                                System.IO.Directory.CreateDirectory(filePath);
                            }

                            var final_path = filePath + "/" + imageName;
                            postedFile.SaveAs(final_path);
                            item = itemCEN.BuscarPorId(p_oid);
                            itemCEN.Modificar(p_oid, item.Nombre, item.Descripcion, final_path, item.EsValido, item.Puntuacion);
                        }
                    }

                    var message1 = string.Format("Image Updated Successfully.");
                    return(Request.CreateErrorResponse(HttpStatusCode.Created, message1));;
                }
                var res = string.Format("Please Upload a image.");
                dict.Add("error", res);
                return(Request.CreateResponse(HttpStatusCode.NotFound, dict));
            }
            catch (Exception ex)
            {
                var res = string.Format("some Message");
                dict.Add("error", res);
                return(Request.CreateResponse(HttpStatusCode.NotFound, dict));
            }
        }
 public void ThenSeBorraElItem()
 {
     item = itemCEN.BuscarPorId(itemId);
     Assert.IsNull(item);
 }