Skip to content
Permalink
Browse files
implemented database functionality
  • Loading branch information
farthingt committed Apr 5, 2024
1 parent c9d2d02 commit 8aa6fc5d3a642f883cb4b4ce6fb54b0c95321dec
Show file tree
Hide file tree
Showing 15 changed files with 262 additions and 48 deletions.
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34723.18
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebAppCW", "WebAppCW\WebAppCW.csproj", "{EA2E2843-3C6A-4142-A68F-414A1E55EA07}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebAppCW", "WebAppCW\WebAppCW.csproj", "{EA2E2843-3C6A-4142-A68F-414A1E55EA07}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="WebAppCW.AccountPage"
Title="AccountPage">
<VerticalStackLayout>
<Label
Text="Account Details"
VerticalOptions="Center"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ContentPage>
@@ -0,0 +1,9 @@
namespace WebAppCW;

public partial class AccountPage : ContentPage
{
public AccountPage()
{
InitializeComponent();
}
}
@@ -2,11 +2,14 @@
{
public partial class App : Application
{
public App()
public static UserDB Userdb { get; private set; }
public App(UserDB db)
{
InitializeComponent();

MainPage = new AppShell();

Userdb = db;
}
}
}
@@ -1,3 +1,6 @@

//using Windows.System;

namespace WebAppCW;

public partial class CreateAccountPage : ContentPage
@@ -21,13 +24,13 @@ public partial class CreateAccountPage : ContentPage
return;
}

bool isUsernameAvailable = checkUsernameAvailability(username);
//bool isUsernameAvailable = checkUsernameAvailability(username);

if (!isUsernameAvailable)
{
CreateAccountStatusLabel.Text = "Username is already taken. Please choose another";
return;
}
//if (!isUsernameAvailable)
//{
// CreateAccountStatusLabel.Text = "Username is already taken. Please choose another";
// return;
//}

bool isPasswordSame = checkPasswordValidity(password1, password2);

@@ -53,20 +56,25 @@ public partial class CreateAccountPage : ContentPage

private bool checkUsernameAvailability(string username)
{
return true;
}
return App.Userdb.IsUsernameAvailable(username);
}

private bool checkPasswordValidity(string password1, string password2)
{
if (password1 == password2)
{
return true;
}
return false;
return password1 == password2;
}

private bool createAccount(string username, string password, string email)
{
return true;
try
{
App.Userdb.AddNewUser(username, password, email);
return true;
}
catch (Exception ex)
{
CreateAccountStatusLabel.Text = $"Failed to create account: {ex.Message}";
return false;
}
}
}
@@ -0,0 +1,22 @@
using SQLite;

namespace WebAppCW
{
[Table("User")]
public class User
{
[PrimaryKey]
[AutoIncrement]
[Column("id")]
public int Id { get; set; }

[Column("username")]
public string? Username { get; set; }

[Column("password")]
public string? Password { get; set; }

[Column("email")]
public string? Email { get; set; }
}
}
@@ -0,0 +1,114 @@
using Microsoft.Maui.Storage;
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace WebAppCW
{
public class UserDB
{
string _dbPath;

public string? StatusMessage { get; set; }

private SQLiteConnection conn;

private void Init()
{
if (conn != null)
return;

conn = new SQLiteConnection(_dbPath);
conn.CreateTable<User>();
}

public UserDB(string dbPath)
{
_dbPath = dbPath;
conn = new SQLiteConnection(_dbPath);
}

public void AddNewUser(string username, string password, string email)
{
int result = 0;
try
{
Init();

result = conn.Insert(new User { Username = username, Password = password, Email = email});
result = 0;

StatusMessage = string.Format("{0} record(s) added (Name: {1})", result, username);
}
catch (Exception ex)
{
StatusMessage = string.Format("Failed to add {0}. Error: {1}", username, ex.Message);
}

}

public List<User> GetAllUsers()
{
try
{
Init();
return conn.Table<User>().ToList();
}
catch (Exception ex)
{
StatusMessage = string.Format("Failed to retrieve data. {0}", ex.Message);
}

return new List<User>();
}

public User GetByUsername(string username)
{
try
{
Init();
var user = from u in conn.Table<User>()
where u.Username == username
select u;
return user.FirstOrDefault();
}
catch (Exception ex)
{
StatusMessage = string.Format("Failed to retrieve data. {0}", ex.Message);
}
return null;
}

public int UpdateUser(User user)
{
int result = 0;
result = conn.Update(user);
return result;
}

public int DeleteUser(int userID)
{
int result = 0;
result = conn.Delete<User>(userID);
return result;
}
public bool IsUsernameAvailable(string username)
{
try
{
Init();
var existingUser = conn.Table<User>().FirstOrDefault(u => u.Username == username);
return existingUser == null;
}
catch (Exception ex)
{
StatusMessage = $"Failed to check username availability. Error: {ex.Message}";
return false;
}
}
}
}
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebAppCW
{
public class FileAccessHelper
{
public static string GetLocalFilePath(string filename)
{
return System.IO.Path.Combine(FileSystem.AppDataDirectory, filename);
}
}
}
@@ -27,17 +27,19 @@ public partial class LoginPage : ContentPage
}

private bool AuthenticateUser(string username, string password)
{
if (username == "user" && password == "pass")
{
return true;
}
else
{
return false;
}
}
private void OnCreateAccountButtonClicked(object sender, EventArgs e)
{
var user = App.Userdb.GetByUsername(username);

if (user != null && user.Password == password)
{
return true;
}
else
{
return false;
}
}
private void OnCreateAccountButtonClicked(object sender, EventArgs e)
{
Navigation.PushAsync(new CreateAccountPage());
}
@@ -8,6 +8,18 @@
<VerticalStackLayout
Padding="30,0"
Spacing="25">
<HorizontalStackLayout
HorizontalOptions="FillAndExpand"
Spacing="10">
<Button
Text="Account Details"
Clicked="OnAccountDetailsClicked"
HorizontalOptions="FillAndExpand" />
<Button
Text="Logout"
Clicked="OnLogoutClicked"
HorizontalOptions="FillAndExpand" />
</HorizontalStackLayout>
<Image
Source="raven_tor_img"
HeightRequest="185"
@@ -2,13 +2,22 @@
{
public partial class MainPage : ContentPage
{
int count = 0;

public MainPage()
{
InitializeComponent();
}

private void OnAccountDetailsClicked(object sender, EventArgs e)
{
Navigation.PushAsync(new AccountPage());
}

private void OnLogoutClicked(object sender, EventArgs e)
{
Navigation.PushAsync(new LoginPage());
}

private void OnRavenClicked(object sender, EventArgs e)
{
Navigation.PushAsync(new RavenTorPage());
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;


namespace WebAppCW
{
public static class MauiProgram
@@ -14,9 +15,11 @@ namespace WebAppCW
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
string dbPath = FileAccessHelper.GetLocalFilePath("User.db3");
builder.Services.AddSingleton<UserDB>(s => ActivatorUtilities.CreateInstance<UserDB>(s, dbPath));

#if DEBUG
builder.Logging.AddDebug();
builder.Logging.AddDebug();
#endif

return builder.Build();

0 comments on commit 8aa6fc5

Please sign in to comment.