Skip to content

Commit

Permalink
Merge pull request #21 from PetrusAriaa/dev
Browse files Browse the repository at this point in the history
 feat: connect get API to FE
  • Loading branch information
imelnath authored Oct 26, 2023
2 parents 75593a2 + a5974d3 commit aff6b18
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 40 deletions.
4 changes: 4 additions & 0 deletions Berkati-Backend/Models/Admin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ public bool Login(string username, string password)
admin.LastLogin = reader.GetDateTime(reader.GetOrdinal("last_login"));
admin.IsSuperUser = reader.GetBoolean(reader.GetOrdinal("is_super_user"));
}
else
{
return false;
}
}

if (BC.EnhancedVerify(password, admin.Password))
Expand Down
43 changes: 23 additions & 20 deletions Berkati-Frontend/LoginWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,30 @@ private async void LoginBtn_Click(object sender, RoutedEventArgs e)
var content = new StringContent(body, Encoding.UTF8, "application/json");
try
{
HttpResponseMessage res = await _httpClient.PostAsync(apiUri, content);
if (res.IsSuccessStatusCode)
{
string _res = await res.Content.ReadAsStringAsync();
//HttpResponseMessage res = await _httpClient.PostAsync(apiUri, content);
//if (res.IsSuccessStatusCode)
//{
// string _res = await res.Content.ReadAsStringAsync();

var json = JsonConvert.DeserializeObject<LoginResponse>(_res);
if (json.Data)
{
MainWindow mainWindow = new();
mainWindow.Show();
Close();
}
else
{
MessageBox.Show("Wrong Password");
}
}
else
{
Console.WriteLine("SWW");
}
// var json = JsonConvert.DeserializeObject<LoginResponse>(_res);
// if (json.Data)
// {
// MainWindow mainWindow = new();
// mainWindow.Show();
// Close();
// }
// else
// {
// MessageBox.Show("Wrong Password");
// }
//}
//else
//{
// Console.WriteLine("SWW");
//}
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
Close();
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion Berkati-Frontend/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
mc:Ignorable="d"
Title="MainWindow"
SnapsToDevicePixels="True"
Height="450"
Height="550"
Width="800">

<Grid>
Expand Down
2 changes: 1 addition & 1 deletion Berkati-Frontend/Pages/Page1.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Berkati_Frontend.Pages"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
d:DesignHeight="550" d:DesignWidth="800"
Title="Page1">

<DockPanel>
Expand Down
2 changes: 1 addition & 1 deletion Berkati-Frontend/Pages/Page2.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:local="clr-namespace:Berkati_Frontend.Pages"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
d:DesignHeight="550" d:DesignWidth="800"
Title="Page2">

<DockPanel VerticalAlignment="Stretch">
Expand Down
2 changes: 1 addition & 1 deletion Berkati-Frontend/Pages/Page3.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:Berkati_Frontend.Pages"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
d:DesignHeight="550" d:DesignWidth="800"
Title="Page3">

<DockPanel VerticalAlignment="Stretch">
Expand Down
57 changes: 42 additions & 15 deletions Berkati-Frontend/Pages/Page3.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net.Http;
using Newtonsoft.Json;

namespace Berkati_Frontend.Pages
{
Expand All @@ -24,28 +26,44 @@ public partial class Page3 : Page
// Daftar objek untuk menampung data
private ObservableCollection<UserData> userDataList;
private UserData selectedUser; // Deklarasikan variabel untuk menyimpan item yang dipilih
private readonly HttpClient _httpClient = new();

public Page3()
{
InitializeComponent();
// Inisialisasi daftar data
userDataList = new ObservableCollection<UserData>();
DataGrid.ItemsSource = userDataList;
GetAdminData();
}
private async void GetAdminData()
{
var apiUri = "https://localhost:7036/admin";
try
{
HttpResponseMessage res = await _httpClient.GetAsync(apiUri);
if (res.IsSuccessStatusCode)
{
var content = await res.Content.ReadAsStringAsync();
var json = JsonConvert.DeserializeObject<UserData>(content);
DataGrid.ItemsSource = json.Data;
}
}catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

private void AddAdminBtn_Click(object sender, RoutedEventArgs e)
{
// Mendapatkan nilai dari input
string username = UsernameTextBox.Text;
string password = PasswordBox.Password;
//// Mendapatkan nilai dari input
//string username = UsernameTextBox.Text;
//string password = PasswordBox.Password;

// Menambahkan data ke daftar
userDataList.Add(new UserData { Username = username, Password = password });
//// Menambahkan data ke daftar
//userDataList.Add(new UserData { Username = username, Password = password });


// Reset TextBoxes setelah menambahkan admin
UsernameTextBox.Clear();
PasswordBox.Clear();
//// Reset TextBoxes setelah menambahkan admin
//UsernameTextBox.Clear();
//PasswordBox.Clear();
}

private void DeleteAdminBtn_Click(object sender, RoutedEventArgs e)
Expand All @@ -69,11 +87,13 @@ private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs
if (DataGrid.SelectedItem != null)
{
// Mengambil item yang dipilih dari DataGrid
UserData selectedUser = (UserData)DataGrid.SelectedItem;
Admin selectedAdmin = (Admin)DataGrid.SelectedItem;

// Menampilkan nilai item yang dipilih di dalam inputan
UsernameTextBox.Text = selectedUser.Username;
PasswordBox.Password = selectedUser.Password;
UsernameTextBox.Text = selectedAdmin.Username;
PasswordBox.Password = "****";
UsernameTextBox.IsEnabled = false;
PasswordBox.IsEnabled = false;

// Mengatur DataGrid ke mode baca saja
DataGrid.IsReadOnly = true;
Expand All @@ -86,10 +106,17 @@ private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs
}

// Kelas untuk data pengguna
public class UserData
public class Admin
{
public Guid Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string IsSuperUser { get; set; }
public DateTime LastLogin { get; set; }
}
public class UserData
{
public List<Admin> Data { get; set; }
}
}
}
2 changes: 1 addition & 1 deletion Berkati-Frontend/Pages/Page4.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:Berkati_Frontend.Pages"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
d:DesignHeight="550" d:DesignWidth="800"
Title="Page4">

<DockPanel VerticalAlignment="Stretch">
Expand Down

0 comments on commit aff6b18

Please sign in to comment.