Esempio n. 1
0
        public async Task<IHttpActionResult> GetMember(string memberID, string password)
        {
            PetterResultType<Member> petterResultType = new PetterResultType<Member>();
            List<Member> members = new List<Member>();
            Ciphers ciphers = new Ciphers();

            //var member = await db.Members.Where(p => p.MemberID == memberID.Trim().ToLower() & p.Password == password).SingleOrDefaultAsync();
            var member = await db.Members.Where(p => p.MemberID == memberID.Trim().ToLower() ).SingleOrDefaultAsync();
            bool isPasswordMatch = ciphers.getPasswordMatch(member.Password, password);

            if (!isPasswordMatch)
            {
                member = null;
            }

            if (member == null)
            {
                await AddMemberAccess(memberID, AccessResult.Failure);
                return NotFound();
            }

            await AddMemberAccess(memberID, AccessResult.Success);

            members.Add(member);
            petterResultType.IsSuccessful = true;
            petterResultType.JsonDataSet = members;

            return Ok(petterResultType);
        }
        public async Task<IHttpActionResult> PostMember()
        {
            PetterResultType<Member> petterResultType = new PetterResultType<Member>();
            Ciphers ciphers = new Ciphers();
            List<Member> members = new List<Member>();
            Member member = new Member();

            if (Request.Content.IsMimeMultipartContent())
            {
                string folder = HostingEnvironment.MapPath(UploadPath.MemberPath);
                Utilities.CreateDirectory(folder);

                var provider = await Request.Content.ReadAsMultipartAsync();

                foreach (var content in provider.Contents)
                {
                    string fieldName = content.Headers.ContentDisposition.Name.Trim('"');
                    if (!string.IsNullOrEmpty(content.Headers.ContentDisposition.FileName))
                    {
                        var file = await content.ReadAsByteArrayAsync();

                        string fileName = Utilities.additionFileName(content.Headers.ContentDisposition.FileName.Trim('"'));

                        if (!FileExtension.MemberExtensions.Any(x => x.Equals(Path.GetExtension(fileName.ToLower()), StringComparison.OrdinalIgnoreCase)))
                        {
                            petterResultType.IsSuccessful = false;
                            petterResultType.JsonDataSet = null;
                            petterResultType.ErrorMessage = ResultErrorMessage.FileTypeError;
                            return Ok(petterResultType);
                        }

                        string fullPath = Path.Combine(folder, fileName);
                        File.WriteAllBytes(fullPath, file);
                        string thumbnamil = Path.GetFileNameWithoutExtension(fileName) + "_thumbnail" + Path.GetExtension(fileName);

                        Utilities.ResizeImage(fullPath, thumbnamil, FileSize.MemberWidth, FileSize.MemberHeight, ImageFormat.Png);
                        member.FileName = fileName;
                        member.FilePath = UploadPath.MemberPath.Replace("~", "");
                    }
                    else
                    {
                        string str = await content.ReadAsStringAsync();
                        string item = HttpUtility.UrlDecode(str);

                        #region switch case
                        switch (fieldName)
                        {
                            //case "MemberNo":
                            //    member.MemberNo = int.Parse(item);
                            //    break;
                            case "MemberID":
                                member.MemberID = item.ToLower();
                                break;
                            case "Password":
                                member.Password = ciphers.getCryptPassword(item);
                                break;
                            case "NickName":
                                member.NickName = item;
                                break;
                            case "Latitude":
                                member.Latitude = Convert.ToDouble(item);
                                break;
                            case "Longitude":
                                member.Longitude = Convert.ToDouble(item);
                                break;
                            default:
                                break;
                        }
                        #endregion switch case
                    }
                }

                string point = string.Format("POINT({0} {1})", member.Longitude, member.Latitude);
                member.Coordinate = DbGeography.FromText(point);
                member.StateFlag = StateFlags.Use;
                member.Route = Route.App;
                member.DateCreated = DateTime.Now;
                member.DateModified = DateTime.Now;
                db.Members.Add(member);
                int num = await this.db.SaveChangesAsync();

                members.Add(member);
                petterResultType.IsSuccessful = true;
                petterResultType.JsonDataSet = members;
            }
            else
            {
                petterResultType.IsSuccessful = false;
                petterResultType.JsonDataSet = null;
            }

            return Ok(petterResultType);
        }
        public async Task<IHttpActionResult> PutMember(string memberID, string password)
        {
            PetterResultType<Member> petterResultType = new PetterResultType<Member>();
            Ciphers ciphers = new Ciphers();
            List<Member> members = new List<Member>();

            string hashcode = ciphers.getCryptPassword(password);

            var member = await db.Members.Where(p => p.MemberID == memberID).SingleOrDefaultAsync();

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            //db.Entry(member).State = EntityState.Modified;

            member.Password = hashcode;

            await db.SaveChangesAsync();

            members.Add(member);
            petterResultType.IsSuccessful = true;
            petterResultType.JsonDataSet = members;

            return Ok(petterResultType);
        }