From d784d2f04e2e702bd70c501a74fe8d8721941a0c Mon Sep 17 00:00:00 2001 From: PetrusAriaa Date: Mon, 9 Oct 2023 15:31:38 +0700 Subject: [PATCH 1/2] refactor: return appropriate HTTP Response with IActionResult --- .../Controllers/AdminController.cs | 24 +++++++++----- Berkati-Backend/Controllers/UserController.cs | 24 +++++++++----- Berkati-Backend/Models/Requests.cs | 2 +- Berkati-Backend/Models/User.cs | 12 +++---- Berkati-Backend/Services/AdminRepository.cs | 17 +++++----- Berkati-Backend/Services/UserRepository.cs | 31 +++++++++---------- 6 files changed, 65 insertions(+), 45 deletions(-) diff --git a/Berkati-Backend/Controllers/AdminController.cs b/Berkati-Backend/Controllers/AdminController.cs index 7db7f99..e9df74f 100644 --- a/Berkati-Backend/Controllers/AdminController.cs +++ b/Berkati-Backend/Controllers/AdminController.cs @@ -17,28 +17,38 @@ public AdminController() } [HttpGet] - public List Get() + public IActionResult Get() { - return adminRepos.GetAllAdmin(); + List _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(); } } diff --git a/Berkati-Backend/Controllers/UserController.cs b/Berkati-Backend/Controllers/UserController.cs index 41161f1..a80d7bc 100644 --- a/Berkati-Backend/Controllers/UserController.cs +++ b/Berkati-Backend/Controllers/UserController.cs @@ -7,7 +7,7 @@ namespace Berkati_Backend.Controllers { [EnableCors("AllowSpecificOrigin")] [ApiController] - [Route("[controller]")] + [Route("user")] public class UserController : ControllerBase { private readonly UserRepository userRepos; @@ -18,27 +18,37 @@ public UserController() } [HttpGet] - public List Get() + public IActionResult Get() { - return userRepos.GetAllUser(); + List _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(); } } } \ No newline at end of file diff --git a/Berkati-Backend/Models/Requests.cs b/Berkati-Backend/Models/Requests.cs index 284f576..a481344 100644 --- a/Berkati-Backend/Models/Requests.cs +++ b/Berkati-Backend/Models/Requests.cs @@ -32,7 +32,7 @@ public class Requests public Requests() { - + } } } diff --git a/Berkati-Backend/Models/User.cs b/Berkati-Backend/Models/User.cs index ced2295..8687666 100644 --- a/Berkati-Backend/Models/User.cs +++ b/Berkati-Backend/Models/User.cs @@ -2,17 +2,17 @@ { public class User { - private string _id; + private Guid _id; private string nama; private string telp; - private string? _reqID; - private List? requests; + //private string? _reqID; + //private List? 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? Requests { get => requests; set => requests = value; } + //public string? ReqID { get => _reqID; set => _reqID = value; } + //public List? Requests { get => requests; set => requests = value; } public User() { } } diff --git a/Berkati-Backend/Services/AdminRepository.cs b/Berkati-Backend/Services/AdminRepository.cs index b8c3405..d1a53fe 100644 --- a/Berkati-Backend/Services/AdminRepository.cs +++ b/Berkati-Backend/Services/AdminRepository.cs @@ -24,12 +24,12 @@ public virtual List 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")), @@ -54,16 +54,17 @@ public virtual List 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), @@ -81,6 +82,8 @@ public void AddAdmin(Admin admin) { connection.Close(); } + + return admin.Id; } public void UpdateAdmin(Admin admin) @@ -88,14 +91,12 @@ 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(); diff --git a/Berkati-Backend/Services/UserRepository.cs b/Berkati-Backend/Services/UserRepository.cs index 16b57d8..56575b4 100644 --- a/Berkati-Backend/Services/UserRepository.cs +++ b/Berkati-Backend/Services/UserRepository.cs @@ -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); } @@ -25,22 +23,20 @@ public List 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) { @@ -53,12 +49,13 @@ public List 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 = { @@ -68,7 +65,6 @@ public void AddUser(User user) } }; cmd.ExecuteNonQuery(); - } catch (NpgsqlException ex) { @@ -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 { @@ -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 = { From a8e54e0d70cefd6a91f225ad321cd93509b0cb07 Mon Sep 17 00:00:00 2001 From: KhairunNisaZ Date: Tue, 17 Oct 2023 20:08:21 +0700 Subject: [PATCH 2/2] feat: fixing bugs in username and password box --- Berkati-Frontend/LoginWindow.xaml | 40 +++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/Berkati-Frontend/LoginWindow.xaml b/Berkati-Frontend/LoginWindow.xaml index d56426b..8edb537 100644 --- a/Berkati-Frontend/LoginWindow.xaml +++ b/Berkati-Frontend/LoginWindow.xaml @@ -6,12 +6,19 @@ 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:HintAssist.Hint="Input User Name"/> + materialDesign:HintAssist.Hint="Input Password" + Padding="0,5" + Width="255" + BorderThickness="0,0,0,1" + HorizontalAlignment="Left"/>