Adding final project to dev branch
This commit is contained in:
230
WguApp/Services/DatabaseService.cs
Normal file
230
WguApp/Services/DatabaseService.cs
Normal file
@@ -0,0 +1,230 @@
|
||||
|
||||
using SQLite;
|
||||
using WguApp.Models;
|
||||
|
||||
namespace WguApp.Services;
|
||||
|
||||
public static class DatabaseService
|
||||
{
|
||||
|
||||
private static SQLiteAsyncConnection? _db;
|
||||
|
||||
public static async Task Init()
|
||||
{
|
||||
if (_db is not null) return;
|
||||
|
||||
var databasePath = Path.Combine(FileSystem.AppDataDirectory, "WguApp.db");
|
||||
_db = new SQLiteAsyncConnection(databasePath);
|
||||
|
||||
try
|
||||
{
|
||||
await _db.CreateTablesAsync<Term, Course, Assessment>();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
// -- Add Methods -- //
|
||||
#region Add methods
|
||||
|
||||
public static async Task<bool> AddTerm(Term term)
|
||||
{
|
||||
await Init();
|
||||
var result = await _db?.InsertAsync(term)!;
|
||||
return !(result <= 0);
|
||||
}
|
||||
|
||||
public static async Task<bool> AddCourse(Course course)
|
||||
{
|
||||
await Init();
|
||||
var result = await _db?.InsertAsync(course)!;
|
||||
return !(result <= 0);
|
||||
}
|
||||
|
||||
public static async Task<bool> AddAssessment(Assessment assessment)
|
||||
{
|
||||
await Init();
|
||||
var result = await _db?.InsertAsync(assessment)!;
|
||||
return !(result <= 0);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// -- Get Methods -- //
|
||||
#region Get Methods
|
||||
public static async Task<List<Term>> GetAllTerms()
|
||||
{
|
||||
await Init();
|
||||
var allTerms = await _db?.Table<Term>().ToListAsync()!;
|
||||
return allTerms ?? [];
|
||||
}
|
||||
|
||||
public static async Task<List<Course>> GetAllCourses()
|
||||
{
|
||||
await Init();
|
||||
var allCourses = await _db?.Table<Course>().ToListAsync()!;
|
||||
return allCourses ?? [];
|
||||
}
|
||||
|
||||
public static async Task<List<Assessment>> GetAllAssessments()
|
||||
{
|
||||
await Init();
|
||||
var allAssessments = await _db?.Table<Assessment>().ToListAsync()!;
|
||||
return allAssessments ?? [];
|
||||
}
|
||||
|
||||
public static async Task<List<Assessment>> GetAssessmentsByCourseId(int courseId)
|
||||
{
|
||||
await Init();
|
||||
var query = $"SELECT * FROM Assessments WHERE CourseId = {courseId}";
|
||||
var result = await _db?.QueryAsync<Assessment>(query)!;
|
||||
return result.ToList() ?? [];
|
||||
}
|
||||
|
||||
public static async Task<List<Course>> GetCoursesByTermId(int termId)
|
||||
{
|
||||
await Init();
|
||||
var query = $"SELECT * FROM Courses WHERE TermId = {termId}";
|
||||
var result = await _db?.QueryAsync<Course>(query)!;
|
||||
return result.ToList() ?? [];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// -- Update Methods
|
||||
#region Update Methods
|
||||
|
||||
public static async Task<bool> UpdateTerm(Term term)
|
||||
{
|
||||
await Init();
|
||||
return await _db?.UpdateAsync(term)! != 0;
|
||||
}
|
||||
|
||||
public static async Task<bool> UpdateCourse(Course course)
|
||||
{
|
||||
await Init();
|
||||
return await _db?.UpdateAsync(course)! != 0;
|
||||
}
|
||||
|
||||
public static async Task<bool> UpdateAssessment(Assessment assessment)
|
||||
{
|
||||
await Init();
|
||||
return await _db?.UpdateAsync(assessment)! != 0;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// -- Delete Methods
|
||||
#region Delete Methods
|
||||
public static async Task<bool> DeleteTerm(Term term)
|
||||
{
|
||||
await Init();
|
||||
return await _db?.DeleteAsync(term)! != 0;
|
||||
}
|
||||
|
||||
public static async Task<bool> DeleteTerm(int termId)
|
||||
{
|
||||
await Init();
|
||||
return await _db?.DeleteAsync<Term>(termId)! != 0;
|
||||
}
|
||||
|
||||
public static async Task<bool> DeleteCourse(Course course)
|
||||
{
|
||||
await Init();
|
||||
return await _db?.DeleteAsync(course)! != 0;
|
||||
}
|
||||
|
||||
public static async Task<bool> DeleteCourse(int courseId)
|
||||
{
|
||||
await Init();
|
||||
return await _db?.DeleteAsync<Course>(courseId)! != 0;
|
||||
}
|
||||
|
||||
public static async Task<bool> DeleteAssessment(Assessment assessment)
|
||||
{
|
||||
await Init();
|
||||
return await _db?.DeleteAsync(assessment)! != 0;
|
||||
}
|
||||
|
||||
public static async Task<bool> DeleteAssessment(int assessmentId)
|
||||
{
|
||||
await Init();
|
||||
return await _db?.DeleteAsync<Assessment>(assessmentId)! != 0;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
// -- Sample Data -- //
|
||||
public static async Task LoadSampleData()
|
||||
{
|
||||
await ClearDbData();
|
||||
|
||||
var term1 = new Term()
|
||||
{
|
||||
Name = "Term 1",
|
||||
StartDate = DateTime.Today,
|
||||
EndDate = DateTime.Today.AddDays(30)
|
||||
};
|
||||
|
||||
await AddTerm(term1);
|
||||
|
||||
var course1 = new Course()
|
||||
{
|
||||
TermId = term1.Id,
|
||||
Name = "Course 1",
|
||||
StartDate = DateTime.Today,
|
||||
EndDate = DateTime.Today.AddDays(30),
|
||||
InstructorName = "Anika Patel",
|
||||
InstructorEmail = "anika.patel@strimeuniversity.edu",
|
||||
InstructorPhone = "555-123-4567",
|
||||
StartNotifCheck = false,
|
||||
EndNotifCheck = false,
|
||||
Status = CourseStatus.InProgress,
|
||||
Notes = "Some notes"
|
||||
};
|
||||
|
||||
await AddCourse(course1);
|
||||
|
||||
var assessment1 = new Assessment()
|
||||
{
|
||||
CourseId = course1.Id,
|
||||
Name = "Performance Assessment",
|
||||
Type = AssessmentType.Performance,
|
||||
StartDate = DateTime.Today,
|
||||
EndDate = DateTime.Today.AddDays(30),
|
||||
StartNotifCheck = false,
|
||||
EndNotifCheck = false
|
||||
};
|
||||
|
||||
await AddAssessment(assessment1);
|
||||
|
||||
var assessment2 = new Assessment()
|
||||
{
|
||||
CourseId = course1.Id,
|
||||
Name = "Objective Assessment",
|
||||
Type = AssessmentType.Objective,
|
||||
StartDate = DateTime.Today,
|
||||
EndDate = DateTime.Today.AddDays(30),
|
||||
StartNotifCheck = false,
|
||||
EndNotifCheck = false
|
||||
};
|
||||
|
||||
await AddAssessment(assessment2);
|
||||
}
|
||||
|
||||
public static async Task ClearDbData()
|
||||
{
|
||||
await Init();
|
||||
|
||||
await _db?.DeleteAllAsync<Assessment>()!;
|
||||
await _db?.DeleteAllAsync<Course>()!;
|
||||
await _db?.DeleteAllAsync<Term>()!;
|
||||
|
||||
_db = null;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user