private async void LoadUserInformation(object sender, RoutedEventArgs e) { MemberCredential memberCredential = ProjectConfiguration.CurrentMemberCredential; if (ProjectConfiguration.CurrentMemberCredential == null) { memberCredential = await this._fileService.ReadMemberCredentialFromFile(); } if (memberCredential == null) { this.Frame.Navigate(typeof(LoginPage)); } if (memberCredential != null) { var member = this._memberService.GetInformation(memberCredential.token); FirstName.Text = member.FirstName; LastName.Text = member.LastName; Email.Text = member.Email; Phone.Text = member.Phone; Address.Text = member.Address; Introduction.Text = member.Introduction; Gender.Text = member.Gender == 1 ? "Male" : (member.Gender == 0 ? "Female" : "Other"); Birthday.Text = member.Birthday; } }
public List <Song> GetAllSong(MemberCredential memberCredential) { var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(memberCredential.token); var response = httpClient.GetAsync(ProjectConfiguration.SONG_GET_ALL).GetAwaiter().GetResult(); return(JsonConvert.DeserializeObject <List <Song> >(response.Content.ReadAsStringAsync().Result)); }
private async void SaveTokenToFile(MemberCredential memberCredential) { var storageFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("SavedFile", CreationCollisionOption.OpenIfExists); var storageFile = await storageFolder.CreateFileAsync("token.txt", CreationCollisionOption.OpenIfExists); await FileIO.WriteTextAsync(storageFile, JsonConvert.SerializeObject(memberCredential)); }
public Member GetInformation(MemberCredential memberCredential) { var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(memberCredential.token); var response = httpClient.GetAsync(ProjectConfiguration.MEMBER_GET_INFORMATION).GetAwaiter().GetResult(); return(JsonConvert.DeserializeObject <Member>(response.Content.ReadAsStringAsync().Result)); }
public Song CreateSong(MemberCredential memberCredential, Song song) { var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(memberCredential.token); var content = new StringContent(JsonConvert.SerializeObject(song), Encoding.UTF8, "application/json"); var response = httpClient.PostAsync(ProjectConfiguration.SONG_CREATE_URL, content).GetAwaiter().GetResult(); return(JsonConvert.DeserializeObject <Song>(response.Content.ReadAsStringAsync().Result)); }
private async void SaveTokenToFile(MemberCredential responseLoginMember) { StorageFolder storageFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("SavedFile", CreationCollisionOption.OpenIfExists); StorageFile storageFile = await storageFolder.CreateFileAsync("token.txt", CreationCollisionOption.ReplaceExisting); await FileIO.WriteTextAsync(storageFile, JsonConvert.SerializeObject(responseLoginMember)); }
public List <Song> GetMineSongs(MemberCredential memberCredential) { var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(memberCredential.token); var response = httpClient.GetAsync(ProjectAPI.SONG_GETMINE_URL).GetAwaiter().GetResult(); var listSong = JsonConvert.DeserializeObject <List <Song> >(response.Content.ReadAsStringAsync().Result); return(listSong); }
private void ButtonLogin_OnClick(object sender, RoutedEventArgs e) { var loginMember = new LoginMember { email = this.Email.Text, password = this.Password.Password }; memberCredential = _memberService.Login(loginMember); this._fileService.SeveToFile(memberCredential); this.Frame.Navigate(typeof(View.UserInformation)); }
private async void CheckMemberCredential(object sender, RoutedEventArgs e) { MemberCredential memberCredential = ProjectConfiguration.CurrentMemberCredential; if (ProjectConfiguration.CurrentMemberCredential == null) { memberCredential = await this.fileService.ReadMemberCredentialFromFile(); } if (memberCredential == null) { this.Frame.Navigate(typeof(LoginPage)); } }
private async void LoadUserInformation(object sender, RoutedEventArgs e) { MemberCredential memberCredential = ProjectConfiguration.CurrentMemberCredential; if (ProjectConfiguration.CurrentMemberCredential == null) { memberCredential = await this._fileService.ReadMemberCredentialFromFile(); } else { this.LoginDialog.Visibility = Visibility.Collapsed; this.LogOut.Visibility = Visibility.Visible; } }
public Song CreateSong(MemberCredential memberCredential, Song song) { var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(memberCredential.token); var dataToSend = JsonConvert.SerializeObject(song); var content = new StringContent(dataToSend, Encoding.UTF8, "application/json"); var response = httpClient.PostAsync(ProjectAPI.SONG_CREATE_URL, content).GetAwaiter().GetResult(); var jsonContent = response.Content.ReadAsStringAsync().Result; var responseSong = JsonConvert.DeserializeObject <Song>(jsonContent); Debug.WriteLine("Create success with song name: " + responseSong.name); return(responseSong); }
public async Task <bool> SaveMemberCredentialToFile(MemberCredential memberCredential) { try { var storageFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("SavedFile", CreationCollisionOption.OpenIfExists); var storageFile = await storageFolder.CreateFileAsync("token.txt", CreationCollisionOption.ReplaceExisting); await FileIO.WriteTextAsync(storageFile, JsonConvert.SerializeObject(memberCredential)); return(true); } catch (Exception e) { return(false); } }
private async void LoadUserInformation(object sender, RoutedEventArgs e) { MemberCredential memberCredential = ProjectConfiguration.CurrentMemberCredential; if (ProjectConfiguration.CurrentMemberCredential == null) { memberCredential = await this._fileService.ReadMemberCredentialFromFile(); } if (memberCredential == null) { this.Frame.Navigate(typeof(LoginPage)); } if (memberCredential != null) { var member = this._memberService.GetInformation(memberCredential.token); Email.Text = member.email; Name.Text = member.firstName + " " + member.lastName; } }
private async void LoadUserInformation(object sender, RoutedEventArgs e) { MemberCredential memberCredential = Api.CurrentMemberCredential; _token = memberCredential.token; if (Api.CurrentMemberCredential == null) { memberCredential = await this._fileService.ReadFile(); } if (memberCredential == null) { this.Frame.Navigate(typeof(login)); } if (memberCredential != null) { var member = this._memberService.GetInformation(_token); Email.Text = member.email; Name.Text = member.firstName + " " + member.lastName; } }
public Song CreateSong(MemberCredential memberCredential, Song song) { // tạo đối tượng httpclient giúp gửi dữ liệu đi. (hoặc lấy dữ liệu về) var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(memberCredential.token); // chuyển kiểu dữ liệu c# thành kiểu dữ liệu json. var dataToSend = JsonConvert.SerializeObject(song); // gói gém, gắn mác cho dữ liệu gửi đi, xác định kiểu dữ liệu là json, encode là utf8. var content = new StringContent(dataToSend, Encoding.UTF8, "application/json"); // thực hiện gửi dữ liệu với phương thức post. var response = httpClient.PostAsync(ProjectConfiguration.SONG_CREATE_URL, content).GetAwaiter().GetResult(); // lấy kết quả trả về từ server. var jsonContent = response.Content.ReadAsStringAsync().Result; // ép kiểu kết quả từ dữ liệu json sang dữ liệu của C# var responseSong = JsonConvert.DeserializeObject <Song>(jsonContent); // in ra id của member trả về. Debug.WriteLine("Create success with name: " + responseSong.name); return(responseSong); }
private async void ReadTokenFromFile() { var storageFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("SavedFile", CreationCollisionOption.OpenIfExists); try { var storageFile = await storageFolder.GetFileAsync("token.txt"); if (storageFile != null) { var jsonContent = await FileIO.ReadTextAsync(storageFile); MemberCredential memberCredential = JsonConvert.DeserializeObject <MemberCredential>(jsonContent); TokenKey.Text = memberCredential.token; // Lấy thông tin người dùng // qua api https://2-dot-backup-server-003.appspot.com/_api/v2/members/information } } catch (Exception e) { TokenKey.Text = "Need login."; } //Debug.WriteLine(memberCredential.token); //Debug.WriteLine(content); //var memberCredential = new MemberCredential(); //memberCredential.token = Guid.NewGuid().ToString(); //memberCredential.secretToken = memberCredential.token; //memberCredential.createdTimeMLS = DateTime.Now.Millisecond; //memberCredential.expiredTimeMLS = DateTime.Today.AddDays(7).Millisecond; //memberCredential.status = 1; //Debug.WriteLine(memberCredential.token); }
public List <Song> GetMineSongs(MemberCredential memberCredential) { throw new NotImplementedException(); }
public Song CreateSong(MemberCredential memberCredential, Song song) { throw new NotImplementedException(); }
private async void ReadTokenFromFile() { var storageFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("SavedFile", CreationCollisionOption.OpenIfExists); try { var storageFile = await storageFolder.GetFileAsync("token.txt"); if (storageFile != null) { var jsonContent = await FileIO.ReadTextAsync(storageFile); MemberCredential memberCredential = JsonConvert.DeserializeObject <MemberCredential>(jsonContent); TokenKey.Text = memberCredential.token; var httpWebRequest = (HttpWebRequest)WebRequest.Create(string.Format(GetInforUrl)); //httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "GET"; httpWebRequest.Headers.Add("Authorization", "Basic" + " " + memberCredential.token); var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); Debug.WriteLine(httpWebResponse.StatusCode); Debug.WriteLine(httpWebResponse.Server); string jsonString; using (Stream stream = httpWebResponse.GetResponseStream()) { StreamReader reader = new StreamReader(stream ?? throw new InvalidOperationException(), Encoding.UTF8); jsonString = reader.ReadToEnd(); } Member member = JsonConvert.DeserializeObject <Member>(jsonString); UId.Text = member.id.ToString(); FirstName.Text = member.firstName; LastName.Text = member.lastName; Avatar.Text = member.avatar; Phone.Text = member.phone; Address.Text = member.address; Gender.Text = member.gender.ToString(); Birthday.Text = member.birthday; Email.Text = member.email; //Debug.WriteLine(jsonString); // Lấy thông tin người dùng\ // qua api https://2-dot-backup-server-003.appspot.com/_api/v2/members/information } } catch (Exception) { Frame.Navigate(typeof(LoginPage)); } //Debug.WriteLine(memberCredential.token); //Debug.WriteLine(content); //var memberCredential = new MemberCredential(); //memberCredential.token = Guid.NewGuid().ToString(); //memberCredential.secretToken = memberCredential.token; //memberCredential.createdTimeMLS = DateTime.Now.Millisecond; //memberCredential.expiredTimeMLS = DateTime.Today.AddDays(7).Millisecond; //memberCredential.status = 1; //Debug.WriteLine(memberCredential.token); }
public Task <bool> SaveMemberCredentialToFile(MemberCredential memberCredential) { throw new NotImplementedException(); }
private async void ButtonBase_OnClick(object sender, RoutedEventArgs e) { var song = new Song { name = this.Name.Text, description = this.Description.Text, singer = this.Singer.Text, author = this.Author.Text, thumbnail = this.Thumbnail.Text, link = this.Link.Text, }; Dictionary <String, String> errors = song.Validate(); if (errors.Count == 0) { var storageFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("SavedFile", CreationCollisionOption.OpenIfExists); try { var storageFile = await storageFolder.GetFileAsync("token.txt"); if (storageFile != null) { var jsonContent = await FileIO.ReadTextAsync(storageFile); MemberCredential memberCredential = JsonConvert.DeserializeObject <MemberCredential>(jsonContent); var httpWebRequest = (HttpWebRequest)WebRequest.Create(UploadSongUrl); httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "POST"; httpWebRequest.Headers.Add("Authorization", "Basic" + " " + memberCredential.token); using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { var json = JsonConvert.SerializeObject(song); streamWriter.Write(json); } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream() ?? throw new InvalidOperationException())) { var result = streamReader.ReadToEnd(); Debug.WriteLine(result); } } } catch (Exception ex) { Debug.WriteLine("Exception Occurred: " + ex.Message); } } else { if (errors.ContainsKey("name")) { NameError.Text = errors["name"]; NameError.Visibility = Visibility.Visible; } else { NameError.Visibility = Visibility.Collapsed; } if (errors.ContainsKey("singer")) { SingerError.Text = errors["singer"]; SingerError.Visibility = Visibility.Visible; } else { SingerError.Visibility = Visibility.Collapsed; } if (errors.ContainsKey("author")) { AuthorError.Text = errors["author"]; AuthorError.Visibility = Visibility.Visible; } else { AuthorError.Visibility = Visibility.Collapsed; } if (errors.ContainsKey("description")) { DescriptionError.Text = errors["description"]; DescriptionError.Visibility = Visibility.Visible; } else { DescriptionError.Visibility = Visibility.Collapsed; } if (errors.ContainsKey("link")) { LinkError.Text = errors["link"]; LinkError.Visibility = Visibility.Visible; } else { LinkError.Visibility = Visibility.Collapsed; } if (errors.ContainsKey("thumbnail")) { ThumbnailError.Text = errors["thumbnail"]; ThumbnailError.Visibility = Visibility.Visible; } else { ThumbnailError.Visibility = Visibility.Collapsed; } } }