示例#1
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);
        }
示例#2
0
        public Reply Edit([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 = db.Animals.Find(model.Id);
                    oAnimal.Name  = model.Name;
                    oAnimal.Patas = model.Patas;

                    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);
        }
示例#3
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);
        }