83 lines
2.8 KiB
C#
83 lines
2.8 KiB
C#
using System.ComponentModel.DataAnnotations;
|
|
using BuchhaltungBlazor.Data;
|
|
|
|
namespace Buchhaltung;
|
|
|
|
public class Booking {
|
|
//Primary Key
|
|
[Key] public uint Id { get; set; }
|
|
|
|
//Foreign Key
|
|
public uint CategoryId { get; set; }
|
|
public uint SubCategoryId { get; set; }
|
|
|
|
//Navigation property
|
|
public Category? Category { get; set; }
|
|
public SubCategory? SubCategory { get; set; }
|
|
|
|
//Properties
|
|
public DateOnly BookingDay { get; set; }
|
|
public DateOnly Valuta { get; set; }
|
|
public string? Typ { get; set; }
|
|
public string? Details { get; set; }
|
|
public float Amount { get; set; }
|
|
|
|
public static Booking FromCsv(string csvLine) {
|
|
using BookingDb db = new();
|
|
if (db.Categories is null) throw new NotImplementedException();
|
|
if (db.SubCategories is null) throw new NotImplementedException();
|
|
|
|
string[] values = csvLine.Split(';');
|
|
|
|
Category category = GetCategory(values[5]);
|
|
SubCategory subCategory = GetSubCategory(values[6]);
|
|
|
|
Booking booking = new() {
|
|
BookingDay = DateOnly.Parse(values[0]),
|
|
Valuta = DateOnly.Parse(values[1]),
|
|
Typ = values[2],
|
|
Details = values[3],
|
|
Amount = float.Parse(values[4]),
|
|
CategoryId = category.Id,
|
|
SubCategoryId = subCategory.Id
|
|
};
|
|
|
|
// Console.WriteLine($"{booking.BookingDay} | {booking.Valuta} | {booking.Typ} | {booking.Details} | {booking.Amount} | {booking.CategoryId}| {booking.SubCategoryId}");
|
|
|
|
return booking;
|
|
}
|
|
|
|
private static Category GetCategory(string value) {
|
|
using BookingDb db = new();
|
|
if (db.Categories is null) throw new NotImplementedException();
|
|
|
|
Category? category = db.Categories.FirstOrDefault(c => c.Name.Contains(value));
|
|
if (category is not null)
|
|
// Console.WriteLine($"Category {category.Id}:{category.Name} already exists.");
|
|
return category;
|
|
|
|
Console.WriteLine($"Category {value} does not exists! Creating new one...");
|
|
|
|
category = new(value);
|
|
db.Categories.Add(category);
|
|
db.SaveChanges();
|
|
return category;
|
|
}
|
|
|
|
private static SubCategory GetSubCategory(string value) {
|
|
using BookingDb db = new();
|
|
if (db.SubCategories is null) throw new NotImplementedException();
|
|
|
|
SubCategory? subCategory = db.SubCategories.FirstOrDefault(c => c.Name.Contains(value));
|
|
if (subCategory is not null)
|
|
// Console.WriteLine($"SubCategory {subCategory.Id}:{subCategory.Name} already exists.");
|
|
return subCategory;
|
|
|
|
Console.WriteLine($"SubCategory {value} does not exists! Creating new one...");
|
|
|
|
subCategory = new(value);
|
|
db.SubCategories.Add(subCategory);
|
|
db.SaveChanges();
|
|
return subCategory;
|
|
}
|
|
} |