From 4c69b14120221e92746082ed1cce5bc3f5900b48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Mon, 3 Jul 2023 22:14:50 +0200 Subject: [PATCH] CalenderHandling infered --- MyYearlyCountings/Local.db | Bin 24576 -> 40960 bytes MyYearlyCountings/Local.db-shm | Bin 0 -> 32768 bytes MyYearlyCountings/Local.db-wal | 0 ...0230703200419_refIdsInCalItems.Designer.cs | 188 ++++++++++++++++++ .../20230703200419_refIdsInCalItems.cs | 51 +++++ .../Migrations/DataContextModelSnapshot.cs | 9 + MyYearlyCountings/Models/CalDay.cs | 20 +- MyYearlyCountings/Models/CalHour.cs | 1 + MyYearlyCountings/Models/CalMonth.cs | 1 + MyYearlyCountings/MyYearlyCountings.csproj | 2 +- MyYearlyCountings/Program.cs | 1 + .../Repositories/CalenderRepository.cs | 143 +++++++++++++ .../Repositories/ICalenderRepository.cs | 13 ++ 13 files changed, 418 insertions(+), 11 deletions(-) create mode 100644 MyYearlyCountings/Local.db-shm create mode 100644 MyYearlyCountings/Local.db-wal create mode 100644 MyYearlyCountings/Migrations/20230703200419_refIdsInCalItems.Designer.cs create mode 100644 MyYearlyCountings/Migrations/20230703200419_refIdsInCalItems.cs create mode 100644 MyYearlyCountings/Repositories/CalenderRepository.cs create mode 100644 MyYearlyCountings/Repositories/ICalenderRepository.cs diff --git a/MyYearlyCountings/Local.db b/MyYearlyCountings/Local.db index c59cc3f1a169279dbaf3223c6119309101406df0..67281a7d4032dc21ac973ac1b3306bf5f8578765 100644 GIT binary patch literal 40960 zcmeI&&u-&H90zbaZu1X<_ZFem(!qe(XuHKt+l_jHv~{~y{xoD=v=v%K-ek5|BzD(! z5It_T55S2t53oD{cb+JT*x3tX?OFr_D!3yUNmu(yDcCf?938QEi`- zns!dAXU3`CWTiK>rm-28c1}%qy1Xd2;n@Bc^RwAiCuTOy0ab{hJ!V%KUTIV^3q5oO z18V!R6AJlFqVV6HSU!ZWJ=w{^)aClvP0+$Fc5Jzx zpD+;C&BW}c)%`Ldy2YyH*v8dm{LaJ>T5IXzy?f$0?;+mvK3h`%CE7Bf-@)ffcBlsONFp$O?_$}Q2omCsB765v0&C3FCL4T zVx=OEPPtvDqa8P_3)l25$Mzmtp6|F0Rk$e9pWl0jam8{Twf4>LEbz{-6En9~%T9009U<00Izz00bZa0SG_<0xK+l_5TVt zE~*9r2tWV=5P$##AOHafKmY;|2nDeIM^}IV1Rwwb2tWV=5P$##AOHaftiAx&|Eu4| zs38O(009U<00Izz00bZa0SG_<>wokC2tWV=5P$##AOHafKmY;|fWYbtVEw=PZHyX1 u00Izz00bZa0SG_<0uX=z1hD=`AAkS^AOHafKmY;|fB*y_009WBzQA8!dL?QA delta 96 zcmZoTz|?Snae}lU69WSS8xX?)^F$qEaV7@6WJX^89}KKK_6&TN_}B7I<8|S&-z+F_ rl6$i~&rxPJM*as3{0}w@8eHR_{6Jm>$bZJb{|v~#!@v2NzLWs~wU`&_ diff --git a/MyYearlyCountings/Local.db-shm b/MyYearlyCountings/Local.db-shm new file mode 100644 index 0000000000000000000000000000000000000000..fe9ac2845eca6fe6da8a63cd096d9cf9e24ece10 GIT binary patch literal 32768 zcmeIuAr62r3 +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