diff --git a/MyYearlyCountings/Local.db b/MyYearlyCountings/Local.db index c59cc3f..67281a7 100644 Binary files a/MyYearlyCountings/Local.db and b/MyYearlyCountings/Local.db differ diff --git a/MyYearlyCountings/Local.db-shm b/MyYearlyCountings/Local.db-shm new file mode 100644 index 0000000..fe9ac28 Binary files /dev/null and b/MyYearlyCountings/Local.db-shm differ diff --git a/MyYearlyCountings/Local.db-wal b/MyYearlyCountings/Local.db-wal new file mode 100644 index 0000000..e69de29 diff --git a/MyYearlyCountings/Migrations/20230703200419_refIdsInCalItems.Designer.cs b/MyYearlyCountings/Migrations/20230703200419_refIdsInCalItems.Designer.cs new file mode 100644 index 0000000..2313647 --- /dev/null +++ b/MyYearlyCountings/Migrations/20230703200419_refIdsInCalItems.Designer.cs @@ -0,0 +1,188 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using MyYearlyCountings.Data; + +#nullable disable + +namespace MyYearlyCountings.Migrations +{ + [DbContext(typeof(DataContext))] + [Migration("20230703200419_refIdsInCalItems")] + partial class refIdsInCalItems + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.5"); + + modelBuilder.Entity("MyYearlyCountings.Models.AccountRecord", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Avisering") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Belopp") + .HasColumnType("REAL"); + + b.Property("BetalDatum") + .HasColumnType("TEXT"); + + b.Property("Konto") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Mottagare") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("AccountRecords"); + }); + + modelBuilder.Entity("MyYearlyCountings.Models.CalDay", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Day") + .HasColumnType("INTEGER"); + + b.Property("DayComment") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("DayName") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Month") + .HasColumnType("INTEGER"); + + b.Property("MonthId") + .HasColumnType("INTEGER"); + + b.Property("Year") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.ToTable("CalDays"); + }); + + modelBuilder.Entity("MyYearlyCountings.Models.CalHour", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Day") + .HasColumnType("INTEGER"); + + b.Property("DayId") + .HasColumnType("INTEGER"); + + b.Property("Hour") + .HasColumnType("INTEGER"); + + b.Property("HourComment") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Month") + .HasColumnType("INTEGER"); + + b.Property("Year") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.ToTable("CalHours"); + }); + + modelBuilder.Entity("MyYearlyCountings.Models.CalMonth", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Month") + .HasColumnType("INTEGER"); + + b.Property("MonthComment") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("MonthName") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Year") + .HasColumnType("INTEGER"); + + b.Property("YearId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.ToTable("CalMonths"); + }); + + modelBuilder.Entity("MyYearlyCountings.Models.CalYear", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Year") + .HasColumnType("INTEGER"); + + b.Property("YearComment") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("CalYears"); + }); + + modelBuilder.Entity("MyYearlyCountings.Models.Member", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("NickName") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("PersonType") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Members"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MyYearlyCountings/Migrations/20230703200419_refIdsInCalItems.cs b/MyYearlyCountings/Migrations/20230703200419_refIdsInCalItems.cs new file mode 100644 index 0000000..b69eabc --- /dev/null +++ b/MyYearlyCountings/Migrations/20230703200419_refIdsInCalItems.cs @@ -0,0 +1,51 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MyYearlyCountings.Migrations +{ + /// + public partial class refIdsInCalItems : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "YearId", + table: "CalMonths", + type: "INTEGER", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "DayId", + table: "CalHours", + type: "INTEGER", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "MonthId", + table: "CalDays", + type: "INTEGER", + nullable: false, + defaultValue: 0); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "YearId", + table: "CalMonths"); + + migrationBuilder.DropColumn( + name: "DayId", + table: "CalHours"); + + migrationBuilder.DropColumn( + name: "MonthId", + table: "CalDays"); + } + } +} diff --git a/MyYearlyCountings/Migrations/DataContextModelSnapshot.cs b/MyYearlyCountings/Migrations/DataContextModelSnapshot.cs index 0a5db6b..9e6b18b 100644 --- a/MyYearlyCountings/Migrations/DataContextModelSnapshot.cs +++ b/MyYearlyCountings/Migrations/DataContextModelSnapshot.cs @@ -66,6 +66,9 @@ namespace MyYearlyCountings.Migrations b.Property("Month") .HasColumnType("INTEGER"); + b.Property("MonthId") + .HasColumnType("INTEGER"); + b.Property("Year") .HasColumnType("INTEGER"); @@ -83,6 +86,9 @@ namespace MyYearlyCountings.Migrations b.Property("Day") .HasColumnType("INTEGER"); + b.Property("DayId") + .HasColumnType("INTEGER"); + b.Property("Hour") .HasColumnType("INTEGER"); @@ -121,6 +127,9 @@ namespace MyYearlyCountings.Migrations b.Property("Year") .HasColumnType("INTEGER"); + b.Property("YearId") + .HasColumnType("INTEGER"); + b.HasKey("Id"); b.ToTable("CalMonths"); diff --git a/MyYearlyCountings/Models/CalDay.cs b/MyYearlyCountings/Models/CalDay.cs index e2b5e7e..540eace 100644 --- a/MyYearlyCountings/Models/CalDay.cs +++ b/MyYearlyCountings/Models/CalDay.cs @@ -4,15 +4,15 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace MyYearlyCountings.Models +namespace MyYearlyCountings.Models; + +public class CalDay { - public class CalDay - { - public int Id { get; set; } - public int Year { get; set; } - public int Month { get; set; } - public int Day { get; set; } - public string DayName { get; set; } = string.Empty; - public string DayComment { get; set; } = string.Empty; - } + public int Id { get; set; } + public int MonthId { get; set; } + public int Year { get; set; } + public int Month { get; set; } + public int Day { get; set; } + public string DayName { get; set; } = string.Empty; + public string DayComment { get; set; } = string.Empty; } diff --git a/MyYearlyCountings/Models/CalHour.cs b/MyYearlyCountings/Models/CalHour.cs index 9688486..52e837a 100644 --- a/MyYearlyCountings/Models/CalHour.cs +++ b/MyYearlyCountings/Models/CalHour.cs @@ -9,6 +9,7 @@ namespace MyYearlyCountings.Models public class CalHour { public int Id { get; set; } + public int DayId { get; set; } public int Year { get; set; } public int Month { get; set; } public int Day { get; set; } diff --git a/MyYearlyCountings/Models/CalMonth.cs b/MyYearlyCountings/Models/CalMonth.cs index ce9f617..d421918 100644 --- a/MyYearlyCountings/Models/CalMonth.cs +++ b/MyYearlyCountings/Models/CalMonth.cs @@ -9,6 +9,7 @@ namespace MyYearlyCountings.Models public class CalMonth { public int Id { get; set; } + public int YearId { get; set; } public int Year { get; set; } public int Month { get; set; } public string MonthName { get; set; } = string.Empty; diff --git a/MyYearlyCountings/MyYearlyCountings.csproj b/MyYearlyCountings/MyYearlyCountings.csproj index 8af53a9..db599de 100644 --- a/MyYearlyCountings/MyYearlyCountings.csproj +++ b/MyYearlyCountings/MyYearlyCountings.csproj @@ -36,7 +36,7 @@ Always - PreserveNewest + Always diff --git a/MyYearlyCountings/Program.cs b/MyYearlyCountings/Program.cs index a8320b0..e9f956f 100644 --- a/MyYearlyCountings/Program.cs +++ b/MyYearlyCountings/Program.cs @@ -33,6 +33,7 @@ static IHostBuilder CreateHostBuilder(string[] args) services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); services.AddScoped(); services.AddDbContext(); diff --git a/MyYearlyCountings/Repositories/CalenderRepository.cs b/MyYearlyCountings/Repositories/CalenderRepository.cs new file mode 100644 index 0000000..2c04aa5 --- /dev/null +++ b/MyYearlyCountings/Repositories/CalenderRepository.cs @@ -0,0 +1,143 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using MyYearlyCountings.Data; +using MyYearlyCountings.Models; +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MyYearlyCountings.Repositories; + +public class CalenderRepository : ICalenderRepository +{ + private readonly IConfiguration _configuration; + private readonly ILogger _logger; + private readonly DataContext _dataContext; + + public CalenderRepository(IConfiguration configuration, ILogger logger, DataContext dataContext) + { + _configuration = configuration; + _logger = logger; + _dataContext = dataContext; + } + + public bool AddYear(int year, string comment) + { + var result = false; + var exists = _dataContext.CalYears.Select(x => x.Year == year); + if (exists == null) + { + var yearRec = new CalYear() { Year = year, YearComment = comment }; + var insres = _dataContext.CalYears.Add(yearRec); + _dataContext.SaveChanges(); + result = true; + } + return result; + } + + + + public bool AddMonth( + int year, + int month, + string monthName, + string comment + ) + { + var result = false; + + var calyear = GetYear(year); + if (calyear == null) + { + _logger.LogError($"Årspost för år {year} saknas!"); + return false; + } + + var exists = _dataContext.CalMonths.Select(x => x.Year == year && x.Month == month); + if (exists == null) + { + var monthRec = new CalMonth() { Year = year, YearId = calyear.Id, Month = month, MonthName = monthName, MonthComment = comment }; + var insres = _dataContext.CalMonths.Add(monthRec); + _dataContext.SaveChanges(); + result = true; + } + return result; + } + + public bool AddDay( + int year, + int month, + int day, + string dayName, + string comment + ) + { + var result = false; + + var calmonth = GetMonth(year, month); + if (calmonth == null) + { + _logger.LogError($"Månadspost för år {year}, månad {month} saknas!"); + return false; + } + + var exists = _dataContext.CalDays.Select(x => x.Year == year && x.Day == day && x.Day == day); + if (exists == null) + { + var dayRec = new CalDay() { Year = year, Month = month, MonthId = calmonth.Id, Day = day, DayName = dayName, DayComment = comment }; + var insres = _dataContext.CalDays.Add(dayRec); + _dataContext.SaveChanges(); + result = true; + } + return result; + } + + public bool AddHour( + int year, + int month, + int day, + int hour, + string comment + ) + { + var result = false; + + var calday = GetDay(year, month, day); + if (calday == null) + { + _logger.LogError($"Dagspost för år {year}, månad {month}, dag {day} saknas!"); + return false; + } + + var exists = _dataContext.CalHours.Select(x => x.Year == year && x.Month == month && x.Day == day && x.Hour == hour); + if (exists == null) + { + var hourRec = new CalHour() { Year = year, Month = month, Day = day, DayId = calday.Id, Hour = hour, HourComment = comment }; + var insres = _dataContext.CalHours.Add(hourRec); + _dataContext.SaveChanges(); + result = true; + } + return result; + } + + + + public CalYear? GetYear(int year) + { + return _dataContext.CalYears.FirstOrDefault(x => x.Year == year); + } + + public CalMonth? GetMonth(int year, int month) + { + return _dataContext.CalMonths.FirstOrDefault(x => x.Year == year && x.Month == month); + + } + + public CalDay? GetDay(int year, int month, int day) + { + return _dataContext.CalDays.FirstOrDefault(x => x.Year == year && x.Month == month && x.Day == day); + } +} diff --git a/MyYearlyCountings/Repositories/ICalenderRepository.cs b/MyYearlyCountings/Repositories/ICalenderRepository.cs new file mode 100644 index 0000000..3d78717 --- /dev/null +++ b/MyYearlyCountings/Repositories/ICalenderRepository.cs @@ -0,0 +1,13 @@ +namespace MyYearlyCountings.Repositories +{ + public interface ICalenderRepository + { + bool AddDay(int year, int month, int day, string dayName, string comment); + bool AddHour(int year, int month, int day, int hour, string comment); + bool AddMonth(int year, int month, string monthName, string comment); + bool AddYear(int year, string comment); + CalDay? GetDay(int year, int month, int day); + CalMonth? GetMonth(int year, int month); + CalYear? GetYear(int year); + } +} \ No newline at end of file