CalenderHandling infered

This commit is contained in:
2023-07-03 22:14:50 +02:00
parent 1089f31c1a
commit 4c69b14120
13 changed files with 418 additions and 11 deletions

Binary file not shown.

Binary file not shown.

View File

View File

@ -0,0 +1,188 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Avisering")
.IsRequired()
.HasColumnType("TEXT");
b.Property<double>("Belopp")
.HasColumnType("REAL");
b.Property<DateTime>("BetalDatum")
.HasColumnType("TEXT");
b.Property<string>("Konto")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Mottagare")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("AccountRecords");
});
modelBuilder.Entity("MyYearlyCountings.Models.CalDay", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<int>("Day")
.HasColumnType("INTEGER");
b.Property<string>("DayComment")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("DayName")
.IsRequired()
.HasColumnType("TEXT");
b.Property<int>("Month")
.HasColumnType("INTEGER");
b.Property<int>("MonthId")
.HasColumnType("INTEGER");
b.Property<int>("Year")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.ToTable("CalDays");
});
modelBuilder.Entity("MyYearlyCountings.Models.CalHour", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<int>("Day")
.HasColumnType("INTEGER");
b.Property<int>("DayId")
.HasColumnType("INTEGER");
b.Property<int>("Hour")
.HasColumnType("INTEGER");
b.Property<string>("HourComment")
.IsRequired()
.HasColumnType("TEXT");
b.Property<int>("Month")
.HasColumnType("INTEGER");
b.Property<int>("Year")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.ToTable("CalHours");
});
modelBuilder.Entity("MyYearlyCountings.Models.CalMonth", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<int>("Month")
.HasColumnType("INTEGER");
b.Property<string>("MonthComment")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("MonthName")
.IsRequired()
.HasColumnType("TEXT");
b.Property<int>("Year")
.HasColumnType("INTEGER");
b.Property<int>("YearId")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.ToTable("CalMonths");
});
modelBuilder.Entity("MyYearlyCountings.Models.CalYear", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<int>("Year")
.HasColumnType("INTEGER");
b.Property<string>("YearComment")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("CalYears");
});
modelBuilder.Entity("MyYearlyCountings.Models.Member", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("LastName")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("NickName")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("PersonType")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Members");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,51 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace MyYearlyCountings.Migrations
{
/// <inheritdoc />
public partial class refIdsInCalItems : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "YearId",
table: "CalMonths",
type: "INTEGER",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "DayId",
table: "CalHours",
type: "INTEGER",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "MonthId",
table: "CalDays",
type: "INTEGER",
nullable: false,
defaultValue: 0);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "YearId",
table: "CalMonths");
migrationBuilder.DropColumn(
name: "DayId",
table: "CalHours");
migrationBuilder.DropColumn(
name: "MonthId",
table: "CalDays");
}
}
}

View File

@ -66,6 +66,9 @@ namespace MyYearlyCountings.Migrations
b.Property<int>("Month") b.Property<int>("Month")
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");
b.Property<int>("MonthId")
.HasColumnType("INTEGER");
b.Property<int>("Year") b.Property<int>("Year")
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");
@ -83,6 +86,9 @@ namespace MyYearlyCountings.Migrations
b.Property<int>("Day") b.Property<int>("Day")
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");
b.Property<int>("DayId")
.HasColumnType("INTEGER");
b.Property<int>("Hour") b.Property<int>("Hour")
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");
@ -121,6 +127,9 @@ namespace MyYearlyCountings.Migrations
b.Property<int>("Year") b.Property<int>("Year")
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");
b.Property<int>("YearId")
.HasColumnType("INTEGER");
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("CalMonths"); b.ToTable("CalMonths");

View File

@ -4,15 +4,15 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MyYearlyCountings.Models namespace MyYearlyCountings.Models;
{
public class CalDay public class CalDay
{ {
public int Id { get; set; } public int Id { get; set; }
public int MonthId { get; set; }
public int Year { get; set; } public int Year { get; set; }
public int Month { get; set; } public int Month { get; set; }
public int Day { get; set; } public int Day { get; set; }
public string DayName { get; set; } = string.Empty; public string DayName { get; set; } = string.Empty;
public string DayComment { get; set; } = string.Empty; public string DayComment { get; set; } = string.Empty;
} }
}

View File

@ -9,6 +9,7 @@ namespace MyYearlyCountings.Models
public class CalHour public class CalHour
{ {
public int Id { get; set; } public int Id { get; set; }
public int DayId { get; set; }
public int Year { get; set; } public int Year { get; set; }
public int Month { get; set; } public int Month { get; set; }
public int Day { get; set; } public int Day { get; set; }

View File

@ -9,6 +9,7 @@ namespace MyYearlyCountings.Models
public class CalMonth public class CalMonth
{ {
public int Id { get; set; } public int Id { get; set; }
public int YearId { get; set; }
public int Year { get; set; } public int Year { get; set; }
public int Month { get; set; } public int Month { get; set; }
public string MonthName { get; set; } = string.Empty; public string MonthName { get; set; } = string.Empty;

View File

@ -36,7 +36,7 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
<None Update="Local.db"> <None Update="Local.db">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
</ItemGroup> </ItemGroup>

View File

@ -33,6 +33,7 @@ static IHostBuilder CreateHostBuilder(string[] args)
services.AddScoped<ISampleRepository, SampleRepository>(); services.AddScoped<ISampleRepository, SampleRepository>();
services.AddScoped<IMemberRepository, MemberRepository>(); services.AddScoped<IMemberRepository, MemberRepository>();
services.AddScoped<IAccountRecordRepository, AccountRecordRepository>(); services.AddScoped<IAccountRecordRepository, AccountRecordRepository>();
services.AddScoped<ICalenderRepository, CalenderRepository>();
services.AddScoped<IReadingIn, ReadingIn>(); services.AddScoped<IReadingIn, ReadingIn>();
services.AddDbContext<DataContext>(); services.AddDbContext<DataContext>();

View File

@ -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<CalenderRepository> _logger;
private readonly DataContext _dataContext;
public CalenderRepository(IConfiguration configuration, ILogger<CalenderRepository> 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);
}
}

View File

@ -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);
}
}