Skip to content

Commit

Permalink
Merge pull request #14 from PetrusAriaa/dev
Browse files Browse the repository at this point in the history
feat: fixing bugs in username and password box
  • Loading branch information
PetrusAriaa authored Oct 18, 2023
2 parents 42212ad + 60a1834 commit 6516d3b
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 57 deletions.
24 changes: 17 additions & 7 deletions Berkati-Backend/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,38 @@ public AdminController()
}

[HttpGet]
public List<Admin> Get()
public IActionResult Get()
{
return adminRepos.GetAllAdmin();
List<Admin> _data = adminRepos.GetAllAdmin();
var res = new
{
data = _data,
length = _data.Count,
accessedAt = DateTime.UtcNow
};
return Ok(res);
}

[HttpPost]
public void Post([FromBody] Admin admin)
public IActionResult Post([FromBody]Admin admin)
{
adminRepos.AddAdmin(admin);
Guid Id = adminRepos.AddAdmin(admin);
return Created(Id.ToString(), admin);
}

[HttpPut("{id}")]
public void Put(Admin admin)
public IActionResult Put(Admin admin)
{
adminRepos.UpdateAdmin(admin);
return NoContent();
}

// API DELETE nya jangan lupa ditambahin yaa :D
[HttpDelete("{id}")]
public IActionResult Delete(int id)
public IActionResult Delete(Guid id)
{
string deleteString = $"Hello, DELETE ke-{id}!";
return Ok(deleteString);
return NoContent();
}

}
Expand Down
24 changes: 17 additions & 7 deletions Berkati-Backend/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Berkati_Backend.Controllers
{
[EnableCors("AllowSpecificOrigin")]
[ApiController]
[Route("[controller]")]
[Route("user")]
public class UserController : ControllerBase
{
private readonly UserRepository userRepos;
Expand All @@ -18,27 +18,37 @@ public UserController()
}

[HttpGet]
public List<User> Get()
public IActionResult Get()
{
return userRepos.GetAllUser();
List<User> _data = userRepos.GetAllUser();
var res = new
{
data = _data,
length = _data.Count,
accessedAt = DateTime.UtcNow
};
return Ok(res);
}

[HttpPost]
public void Post(User user)
public IActionResult Post([FromBody]User user)
{
userRepos.AddUser(user);
Guid userId = userRepos.AddUser(user);
return Created(userId.ToString(), user);
}

[HttpPut("{id}")]
public void Put(User user)
public IActionResult Put(User user)
{
userRepos.UpdateUser(user);
return NoContent();
}

[HttpDelete("{id}")]
public void Delete(string id)
public IActionResult Delete(Guid id)
{
userRepos.DeleteUser(id);
return NoContent();
}
}
}
2 changes: 1 addition & 1 deletion Berkati-Backend/Models/Requests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class Requests

public Requests()
{

}
}
}
12 changes: 6 additions & 6 deletions Berkati-Backend/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
{
public class User
{
private string _id;
private Guid _id;
private string nama;
private string telp;
private string? _reqID;
private List<UserRequests>? requests;
//private string? _reqID;
//private List<UserRequests>? requests;

public string Id { get => _id; set => _id = value; }
public Guid Id { get => _id; set => _id = value; }
public string Nama { get => nama; set => nama = value; }
public string Telp { get => telp; set => telp = value; }
public string? ReqID { get => _reqID; set => _reqID = value; }
public List<UserRequests>? Requests { get => requests; set => requests = value; }
//public string? ReqID { get => _reqID; set => _reqID = value; }
//public List<UserRequests>? Requests { get => requests; set => requests = value; }

public User() { }
}
Expand Down
17 changes: 9 additions & 8 deletions Berkati-Backend/Services/AdminRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public virtual List<Admin> GetAllAdmin()
try
{
connection.Open();
NpgsqlCommand cmd = new NpgsqlCommand("SELECT * FROM \"admin\"", connection);
NpgsqlCommand cmd = new ("SELECT * FROM \"admin\"", connection);
var reader = cmd.ExecuteReader();

while (reader.Read())
{
Admin admin = new()
Admin admin = new ()
{

Id = reader.GetGuid(reader.GetOrdinal("id")),
Expand All @@ -54,16 +54,17 @@ public virtual List<Admin> GetAllAdmin()
return ListAdmin;
}

public void AddAdmin(Admin admin)
public Guid AddAdmin(Admin admin)
{
try
{
connection.Open();
NpgsqlCommand cmd = new NpgsqlCommand("INSERT INTO \"admin\" (id, username, password, last_login, is_super_user) VALUES(@id, @username, @password, @last_login, @is_super_user)", connection)
admin.Id = Guid.NewGuid();
NpgsqlCommand cmd = new("INSERT INTO \"admin\" (id, username, password, last_login, is_super_user) VALUES(@id, @username, @password, @last_login, @is_super_user)", connection)
{
Parameters =
{
new("id", Guid.NewGuid()),
new("id", admin.Id),
new("username", admin.Username),
new("password", admin.Password),
new("last_login", DateTime.Now),
Expand All @@ -81,21 +82,21 @@ public void AddAdmin(Admin admin)
{
connection.Close();
}

return admin.Id;
}

public void UpdateAdmin(Admin admin)
{
try
{
connection.Open();
NpgsqlCommand cmd = new NpgsqlCommand("UPDATE \"admin\" SET username=@username, password=@password, last_login=@last_login WHERE id = @id;", connection)
NpgsqlCommand cmd = new ("UPDATE \"admin\" SET username=@username, password=@password WHERE id = @id;", connection)
{
Parameters =
{
//new("id", admin.Id),
new("username", admin.Username),
new("password", admin.Password),
new("last_login", admin.LastLogin)
}
};
cmd.ExecuteNonQuery();
Expand Down
31 changes: 15 additions & 16 deletions Berkati-Backend/Services/UserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public UserRepository()
{
Env.Load("./Build/.env");
string? _connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING");

// Initialize the NpgsqlConnection in the constructor
connection = new NpgsqlConnection(_connectionString);
}

Expand All @@ -25,22 +23,20 @@ public List<User> GetAllUser()
try
{
connection.Open();
NpgsqlCommand cmd = new NpgsqlCommand("SELECT * FROM \"user\"", connection);
NpgsqlCommand cmd = new ("SELECT * FROM \"user\"", connection);
var reader = cmd.ExecuteReader();

while (reader.Read())
{
User user = new()
{

Id = reader.GetString(reader.GetOrdinal("_id")),
Id = reader.GetGuid(reader.GetOrdinal("id")),
Nama = reader.GetString(reader.GetOrdinal("nama")),
Telp = reader.GetString(reader.GetOrdinal("telp")),
};

ListUser.Add(user);
}

}
catch (NpgsqlException ex)
{
Expand All @@ -53,12 +49,13 @@ public List<User> GetAllUser()
return ListUser;
}

public void AddUser(User user)
public Guid AddUser(User user)
{
try
{
connection.Open();
NpgsqlCommand cmd = new NpgsqlCommand("INSERT INTO \"user\" (_id, nama, telp) VALUES(@id, @nama, @telp)", connection)
user.Id = Guid.NewGuid();
NpgsqlCommand cmd = new ("INSERT INTO \"user\" (id, nama, telp) VALUES(@id, @nama, @telp)", connection)
{
Parameters =
{
Expand All @@ -68,7 +65,6 @@ public void AddUser(User user)
}
};
cmd.ExecuteNonQuery();

}
catch (NpgsqlException ex)
{
Expand All @@ -78,25 +74,28 @@ public void AddUser(User user)
{
connection.Close();
}
return user.Id;
}
public void DeleteUser(string id)
public void DeleteUser(Guid userId)
{
try
{
connection.Open();
NpgsqlCommand cmd = new NpgsqlCommand("DELETE FROM \"user\" WHERE _id = @id;", connection)
NpgsqlCommand cmd = new ("DELETE FROM \"user\" WHERE id = @id;", connection)
{
Parameters =
{
new("id", id)
new("id", userId)
}
};
cmd.ExecuteNonQuery();

}
catch (NpgsqlException ex)
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
if (ex is NpgsqlException)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
finally
{
Expand All @@ -109,7 +108,7 @@ public void UpdateUser(User user)
try
{
connection.Open();
NpgsqlCommand cmd = new NpgsqlCommand("UPDATE \"user\" SET nama = @nama, telp = @telp WHERE _id = @id;", connection)
NpgsqlCommand cmd = new ("UPDATE \"user\" SET nama = @nama, telp = @telp WHERE id = @id;", connection)
{
Parameters =
{
Expand Down
40 changes: 28 additions & 12 deletions Berkati-Frontend/LoginWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,37 @@
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:Berkati_Frontend"
mc:Ignorable="d"
Title="LoginWindow" Height="450" Width="800"
Title="LoginWindow" Height="550" Width="800"
TextElement.Foreground="{DynamicResource MaterialDesignBody}"
Background="White"
WindowStartupLocation="CenterScreen">

<materialDesign:Card UniformCornerRadius="15" Background="AliceBlue" Height="400" Width="300">

<Grid>

<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<!-- Card Login -->
<materialDesign:Card UniformCornerRadius="15" Background="AliceBlue" Height="400" Width="300">
<materialDesign:Card.Effect>
<DropShadowEffect BlurRadius="15"
Direction ="-90"
RenderingBias ="Quality"
ShadowDepth ="2"
Color ="Gray" />
</materialDesign:Card.Effect>
<StackPanel Orientation="Vertical"
Margin="20">
<TextBlock Text="Login"
<StackPanel Orientation="Vertical" Margin="20">
<!-- Teks "Berkati" -->
<TextBlock Text="Berkati"
FontSize="27"
HorizontalAlignment="Center"
VerticalAlignment="Top"
FontWeight="Bold"
Margin="0,20,0,0"
/>
<!-- Teks "Login" -->
<TextBlock Text="Login"
FontSize="25"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Expand All @@ -45,17 +60,17 @@
Width="255"
BorderThickness="0,0,0,1"
HorizontalAlignment="Left"
Text="Input User Name"/>
materialDesign:HintAssist.Hint="Input User Name"/>
<TextBlock Text="Password"
Grid.Row="2"
Margin="0,15"
HorizontalAlignment="Left"/>
<PasswordBox Grid.Row="3"
Password="mpcodes"
Padding="0,5"
Width="255"
BorderThickness="0,0,0,1"
HorizontalAlignment="Left"/>
materialDesign:HintAssist.Hint="Input Password"
Padding="0,5"
Width="255"
BorderThickness="0,0,0,1"
HorizontalAlignment="Left"/>
</Grid>
<Button x:Name="LoginBtn"
Content="Login"
Expand Down Expand Up @@ -105,4 +120,5 @@
</Button>
</StackPanel>
</materialDesign:Card>
</Grid>
</Window>

0 comments on commit 6516d3b

Please sign in to comment.