Ejemplo n.º 1
0
        public Reply Get([FromBody] SecurityViewModel model)
        {
            Reply oR = new Reply();

            oR.Result = 0;

            if (!Verify(model.token))
            {
                oR.Message = "No Autorizado";
                return(oR);
            }

            try
            {
                using (NorthwindDBCon db = new NorthwindDBCon())
                {
                    oR.Data = (from d in db.Animals
                               where d.idState == 1
                               select new ListAnimalsViewModel
                    {
                        Name = d.Name,
                        Patas = d.Patas
                    }).ToList();

                    oR.Result = 1;
                }
            }
            catch (Exception ex)
            {
                oR.Message = "Ocurrio un error con el servidor, por favor intenta mas tarde.";
                //throw;
            }

            return(oR);
        }
Ejemplo n.º 2
0
 public bool Verify(string token)
 {
     using (NorthwindDBCon db = new NorthwindDBCon())
     {
         return(db.Users.Where(d => d.Token == token && d.idEstatus == 1).Count() > 0 ? true : false);
     }
 }
Ejemplo n.º 3
0
        public Reply Login([FromBody] AccessViewModel model)
        {
            Reply oR = new Reply();

            try
            {
                using (NorthwindDBCon db = new NorthwindDBCon()) {
                    var lst = db.Users.Where(d => d.Email == model.email && d.Password == model.password && d.idEstatus == 1);

                    if (lst.Count() > 0)
                    {
                        oR.Result = 1;
                        oR.Data   = Guid.NewGuid().ToString();

                        Users other = lst.First();
                        other.Token = (string)oR.Data;

                        db.Entry(other).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    else
                    {
                        oR.Message = "Datos incorrectos.";
                    }
                }
            }
            catch (Exception ex)
            {
                oR.Result  = 0;
                oR.Message = "Ocurrio un error, lo estamos corrigiendo.";
                //throw;
            }

            return(oR);
        }
Ejemplo n.º 4
0
 private List <ListAnimalsViewModel> List(NorthwindDBCon db)
 {
     return((from d in db.Animals
             where d.idState == 1
             select new ListAnimalsViewModel
     {
         Name = d.Name,
         Patas = d.Patas
     }).ToList());
 }
Ejemplo n.º 5
0
        public Reply Add([FromBody] AnimalViewModel model)
        {
            Reply oR = new Reply();

            oR.Result = 0;

            if (!Verify(model.token))
            {
                oR.Message = "No Autorizado";
                return(oR);
            }

            // Agregar validaciones del modelo
            if (!Validate(model))
            {
                oR.Message = error;
                return(oR);
            }


            try
            {
                using (NorthwindDBCon db = new NorthwindDBCon())
                {
                    Animals oAnimal = new Animals();
                    oAnimal.idState = 1;
                    oAnimal.Name    = model.Name;
                    oAnimal.Patas   = model.Patas;

                    db.Animals.Add(oAnimal);
                    db.SaveChanges();

                    // se devuelve la lista de los registros para que no se  haga una nueva peticion
                    oR.Data = List(db);

                    oR.Result = 1;
                }
            }
            catch (Exception ex)
            {
                //Poner log en bd
                oR.Message = "Ocurrio un error con el servidor, por favor intenta mas tarde.";
                //throw;
            }

            return(oR);
        }
Ejemplo n.º 6
0
        public Reply Delete([FromBody] AnimalViewModel model)
        {
            Reply oR = new Reply();

            oR.Result = 0;

            if (!Verify(model.token))
            {
                oR.Message = "No Autorizado";
                return(oR);
            }

            try
            {
                using (NorthwindDBCon db = new NorthwindDBCon())
                {
                    Animals oAnimal = db.Animals.Find(model.Id);
                    oAnimal.idState = 2;

                    db.Entry(oAnimal).State = EntityState.Modified;
                    db.SaveChanges();

                    // se devuelve la lista de los registros para que no se  haga una nueva peticion
                    oR.Data = List(db);

                    oR.Result = 1;
                }
            }
            catch (Exception ex)
            {
                //Poner log en bd
                oR.Message = "Ocurrio un error con el servidor, por favor intenta mas tarde.";
                //throw;
            }

            return(oR);
        }
Ejemplo n.º 7
0
        public async Task <Reply> Photo([FromUri] AnimalPictureViewModel model)
        {
            Reply oR = new Reply();

            oR.Result = 0;

            string root     = HttpContext.Current.Server.MapPath("~/App_Data");
            var    provider = new MultipartFormDataStreamProvider(root);

            if (!Verify(model.token))
            {
                oR.Message = "No Autorizado";
                return(oR);
            }

            // Verificar si viene multipart
            if (!Request.Content.IsMimeMultipartContent())
            {
                oR.Message = "No viene imagen";
                return(oR);
            }

            await Request.Content.ReadAsMultipartAsync(provider);

            FileInfo fileInfoPicture = null;

            foreach (MultipartFileData fileData in provider.FileData)
            {
                if (fileData.Headers.ContentDisposition.Name.Replace("\\", "").Replace("\"", "").Equals("picture"))
                {
                    fileInfoPicture = new FileInfo(fileData.LocalFileName);
                }
            }

            if (fileInfoPicture != null)
            {
                // Guardar el archivo
                using (FileStream fs = fileInfoPicture.Open(FileMode.Open, FileAccess.Read))
                {
                    byte[]       b    = new byte[fileInfoPicture.Length];
                    UTF8Encoding temp = new UTF8Encoding(true);
                    while (fs.Read(b, 0, b.Length) > 0)
                    {
                        ;
                    }

                    // Guardar el archivo
                    try
                    {
                        using (NorthwindDBCon db = new NorthwindDBCon())
                        {
                            var oAnimal = db.Animals.Find(model.Id);
                            oAnimal.Picture         = b;
                            db.Entry(oAnimal).State = EntityState.Modified;
                            db.SaveChanges();

                            oR.Result = 1;
                        }
                    }
                    catch (Exception ex)
                    {
                        oR.Message = "Intenta mas tarde.";
                    }
                }
            }

            return(oR);
        }