Added critter database operations
This commit is contained in:
@@ -31,6 +31,6 @@ public partial class ProfileButton : UserControl
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
|
||||
ProfileNameLabel.Content = string.IsNullOrEmpty(critter.Name) ? "Critter" : critter.Name ;
|
||||
ProfileNameLabel.Content = string.IsNullOrEmpty(critter.Name) ? "New Critter" : critter.Name ;
|
||||
}
|
||||
}
|
||||
@@ -10,12 +10,16 @@ public class Critter
|
||||
public int Id { get; set; }
|
||||
|
||||
public string ProfileImagePath { get; set; } = string.Empty;
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string Gender { get; set; } = string.Empty;
|
||||
public DateTime DateOfBirth { get; set; }
|
||||
|
||||
public string Name { get; set; } = "New Critter";
|
||||
public Gender Gender { get; set; } = Gender.Female;
|
||||
public DateTime DateOfBirth { get; set; } = DateTime.Today;
|
||||
public int FatherId { get; set; }
|
||||
public int MotherId { get; set; }
|
||||
|
||||
public string Notes { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public enum Gender
|
||||
{
|
||||
Male,
|
||||
Female
|
||||
}
|
||||
@@ -6,6 +6,7 @@ using Avalonia.Markup.Xaml;
|
||||
using Avalonia.Media;
|
||||
using CritterFolio.Controls;
|
||||
using CritterFolio.DataModels;
|
||||
using CritterFolio.Services;
|
||||
|
||||
namespace CritterFolio.Pages;
|
||||
|
||||
@@ -43,22 +44,46 @@ public partial class HomePage : Page
|
||||
Sys.Navigation?.AddHeaderButton(settingsBttn);
|
||||
}
|
||||
|
||||
private void RefreshProfileButtons()
|
||||
private async void RefreshProfileButtons()
|
||||
{
|
||||
CritterStack.Children.Clear();
|
||||
|
||||
try
|
||||
{
|
||||
var critters = await DatabaseService.GetAllCritters();
|
||||
|
||||
foreach (var critter in critters)
|
||||
{
|
||||
AddProfileButton(critter);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void AddButtonClicked(object? sender, RoutedEventArgs e)
|
||||
private void AddProfileButton(Critter critter)
|
||||
{
|
||||
var newCritter = new Critter()
|
||||
{
|
||||
Name = "Toriel",
|
||||
};
|
||||
|
||||
var newProfButton = new ProfileButton();
|
||||
newProfButton.Init(newCritter);
|
||||
|
||||
newProfButton.Init(critter);
|
||||
CritterStack.Children.Add(newProfButton);
|
||||
|
||||
newProfButton.Clicked += (o, args) => { Console.WriteLine($"{critter.Name} button clicked"); };
|
||||
}
|
||||
|
||||
private async void AddButtonClicked(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
var newCritter = new Critter();
|
||||
try
|
||||
{
|
||||
var result = await DatabaseService.AddCritter(newCritter);
|
||||
AddProfileButton(newCritter);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Console.WriteLine(exception);
|
||||
}
|
||||
}
|
||||
|
||||
private void SettingsPage(object? sender, RoutedEventArgs args)
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
<StackPanel Spacing="5">
|
||||
<Label FontSize="20" HorizontalAlignment="Stretch">Database operations</Label>
|
||||
<Button HorizontalAlignment="Stretch" Classes="normal">Insert test data</Button>
|
||||
<Button HorizontalAlignment="Stretch" Classes="normal">Clear database</Button>
|
||||
<Button HorizontalAlignment="Stretch" Classes="normal">Re-init Database</Button>
|
||||
<Button x:Name="ClearDbButton" HorizontalAlignment="Stretch" Classes="normal">Clear database</Button>
|
||||
<Button x:Name="ReInitButton" HorizontalAlignment="Stretch" Classes="normal">Re-init Database</Button>
|
||||
</StackPanel>
|
||||
</ScrollViewer>
|
||||
</UserControl>
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using CritterFolio.Services;
|
||||
|
||||
namespace CritterFolio.Pages;
|
||||
|
||||
@@ -11,6 +15,20 @@ public partial class TestPage : Page
|
||||
public TestPage()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
ClearDbButton.Click += ClearDbButtonClicked;
|
||||
ReInitButton.Click += ReInitButtonClicked;
|
||||
}
|
||||
|
||||
private void ClearDbButtonClicked(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
var databasePath = Path.Combine(AppContext.BaseDirectory, "CritterFolio.db");
|
||||
File.Delete(databasePath);
|
||||
}
|
||||
|
||||
private async void ReInitButtonClicked(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
await DatabaseService.Init();
|
||||
}
|
||||
|
||||
public override void Refresh()
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using CritterFolio.DataModels;
|
||||
@@ -29,4 +30,40 @@ public static class DatabaseService
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
|
||||
#region Critter operations
|
||||
|
||||
public static async Task<bool> AddCritter(Critter critter)
|
||||
{
|
||||
await Init();
|
||||
var result = await _db?.InsertAsync(critter)!;
|
||||
return !(result <= 0);
|
||||
}
|
||||
|
||||
public static async Task<bool> UpdateCritter(Critter critter)
|
||||
{
|
||||
await Init();
|
||||
return await _db?.UpdateAsync(critter)! != 0;
|
||||
}
|
||||
|
||||
public static async Task<bool> DeleteCritter(Critter critter)
|
||||
{
|
||||
await Init();
|
||||
return await _db?.DeleteAsync(critter)! != 0;
|
||||
}
|
||||
|
||||
public static async Task<bool> DeleteCritter(int id)
|
||||
{
|
||||
await Init();
|
||||
return await _db?.DeleteAsync<Critter>(id)! != 0;
|
||||
}
|
||||
|
||||
public static async Task<List<Critter>> GetAllCritters()
|
||||
{
|
||||
await Init();
|
||||
var result = await _db?.Table<Critter>().ToListAsync()!;
|
||||
return result ?? [];
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user