コード例 #1
0
ファイル: AnimalController.cs プロジェクト: Straadlin/Course
        public Reply Get([FromBody] SecurityViewModel model)
        {
            Reply oReply = new Reply();

            oReply.result = 0;

            if (!Verify(model.token))
            {
                oReply.message = "Not authorized";
                return(oReply);
            }

            try
            {
                using (var dbContext = new Example002Entities())
                {
                    oReply.data = (from a in dbContext.Animals
                                   join s in dbContext.States on a.IdState equals s.Id
                                   where s.Name.Equals("Active")
                                   select new ListAnimalsViewModel
                    {
                        Name = a.Name,
                        Foots = a.Foots
                    }).ToList();

                    oReply.result = 1;
                }
            }
            catch (Exception exception)
            {
                oReply.message = string.Format("There was an error. {0}", exception.Message);
            }

            return(oReply);
        }
コード例 #2
0
ファイル: AnimalController.cs プロジェクト: Straadlin/Course
        public Reply Add([FromBody] AnimalViewModel model)
        {
            var oReply = new Reply();

            oReply.result = 0;

            if (!Verify(model.token))
            {
                oReply.message = "Not authorized";
                return(oReply);
            }

            if (!Validate(model))
            {
                oReply.message = error;
                return(oReply);
            }

            try
            {
                using (var dbContext = new Example002Entities())
                {
                    var oAnimal = new Animal();
                    oAnimal.IdState = 1;
                    oAnimal.Name    = model.Name;
                    oAnimal.Foots   = model.Foots;

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

                    oReply.result = 1;

                    oReply.data = (from a in dbContext.Animals
                                   join s in dbContext.States on a.IdState equals s.Id
                                   where s.Name.Equals("Active")
                                   select new ListAnimalsViewModel
                    {
                        Name = a.Name,
                        Foots = a.Foots
                    }).ToList();
                }
            }
            catch (Exception exception)
            {
                oReply.message = string.Format("There was an error. {0}", exception);
            }

            return(oReply);
        }
コード例 #3
0
ファイル: BaseController.cs プロジェクト: Straadlin/Course
        public bool Verify(string token)
        {
            using (var dbContext = new Example002Entities())
            {
                var query = (from u in dbContext.Users
                             join s in dbContext.States on u.IdState equals s.Id
                             where u.Token.Equals(token) && s.Name.Equals("Active")
                             select u);

                if (query.Count() == 1)
                {
                    return(true);
                }
            }

            return(false);
        }
コード例 #4
0
ファイル: AccessController.cs プロジェクト: Straadlin/Course
        public Reply Login([FromBody] AccessViewModel model)
        {
            Reply oReplay = new Reply();

            try
            {
                using (var dbContext = new Example002Entities())
                {
                    var query = from u in dbContext.Users
                                join s in dbContext.States on u.IdState equals s.Id
                                where s.Name.Equals("Active") && u.Email.Equals(model.email) && u.Password.Equals(model.password)
                                select u;

                    if (query.Count() == 1)
                    {
                        oReplay.result = 1;
                        oReplay.data   = Guid.NewGuid().ToString();

                        User oUser = query.First();
                        oUser.Token = oReplay.data.ToString();
                        dbContext.Entry(oUser).State = System.Data.Entity.EntityState.Modified;
                        dbContext.SaveChanges();
                    }
                    else
                    {
                        oReplay.message = "User's data are incorrect.";
                    }
                }
            }
            catch (Exception exception)
            {
                oReplay.message = string.Format("There was an error.", exception.Message);
            }

            return(oReplay);
        }
コード例 #5
0
ファイル: AnimalController.cs プロジェクト: Straadlin/Course
        public async Task <Reply> Photo([FromUri] AnimalPictureViewModel model)
        {
            var oReply = new Reply();

            oReply.result = 0;

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

            if (!Verify(model.token))
            {
                oReply.message = "Not authorized";
                return(oReply);
            }

            if (!Request.Content.IsMimeMultipartContent())
            {
                oReply.message = "Doesn't contain image";
                return(oReply);
            }

            await Request.Content.ReadAsMultipartAsync(provider);

            FileInfo oFileInfoPicture = null;

            foreach (var item in provider.FileData)
            {
                // Didn't work with this 'if' line.
                if (item.Headers.ContentDisposition.Name.Replace("\\", "").Replace("\"", "").Equals("picture"))
                {
                    oFileInfoPicture = new FileInfo(item.LocalFileName);
                }
            }

            if (oFileInfoPicture != null)
            {
                using (FileStream oFileStream = oFileInfoPicture.Open(FileMode.Open, FileAccess.Read))
                {
                    byte[] b    = new byte[oFileInfoPicture.Length];
                    var    temp = new UTF8Encoding(true);

                    while (oFileStream.Read(b, 0, b.Length) > 0)
                    {
                        ;
                    }

                    try
                    {
                        using (var dbContext = new Example002Entities())
                        {
                            var oAnimal = dbContext.Animals.Find(model.Id);
                            oAnimal.picture = b;
                            dbContext.Entry(oAnimal).State = System.Data.Entity.EntityState.Modified;
                            dbContext.SaveChanges();
                            oReply.result = 1;
                        }
                    }
                    catch (Exception exception)
                    {
                        oReply.message = string.Format("Try again later. {0}", exception);
                    }
                }
            }

            return(oReply);
        }