Refactor code structure for improved readability and maintainability
This commit is contained in:
83
DaireApplication/DataBase/UserTable.cs
Normal file
83
DaireApplication/DataBase/UserTable.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using DaireApplication.DataBase;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AvaloniaApplication1.DataBase
|
||||
{
|
||||
public class UserTable
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string UserName { get; set; }
|
||||
public string Password { get; set; }
|
||||
public bool CanEdit { get; set; }
|
||||
public bool IsAdmin { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
|
||||
public List<UserTable> ReadUsers()
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Users.csv");
|
||||
List<UserTable> users = new List<UserTable>();
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
|
||||
for (int i = 1; i < lines.Length; i++) // Skip header
|
||||
{
|
||||
string[] columns = lines[i].Split(',');
|
||||
users.Add(new UserTable
|
||||
{
|
||||
Id = int.Parse(columns[0]),
|
||||
UserName = columns[1],
|
||||
Password = columns[2],
|
||||
CanEdit = columns[3] =="1" ?true:false,
|
||||
IsAdmin=columns[4] =="1" ?true: false,
|
||||
IsActive=columns[5] =="1" ?true: false,
|
||||
});
|
||||
}
|
||||
return users;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public bool UpdateUser(UserTable updatedUser)
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Users.csv");
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
bool userFound = false;
|
||||
|
||||
for (int i = 1; i < lines.Length; i++)
|
||||
{
|
||||
string[] columns = lines[i].Split(',');
|
||||
if (int.Parse(columns[0]) == updatedUser.Id)
|
||||
{
|
||||
columns[1] = updatedUser.UserName;
|
||||
columns[2] = updatedUser.Password;
|
||||
columns[3] = updatedUser.CanEdit ? "1" : "0";
|
||||
columns[4] = updatedUser.IsAdmin ? "1" : "0";
|
||||
columns[5] = updatedUser.IsActive ? "1" : "0";
|
||||
lines[i] = string.Join(",", columns);
|
||||
userFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (userFound)
|
||||
{
|
||||
File.WriteAllLines(filePath, lines);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user