From 1abf67fdb32da529a2386f0083082cd404b7bdaf Mon Sep 17 00:00:00 2001 From: Torben Nehmer Date: Fri, 19 Jul 2024 19:08:37 +0200 Subject: [PATCH] Planet model, started adding CSVHelper --- .gitignore | 3 ++ Stars Assistant/Model/Planet.cs | 46 ++++++++++++++++++++++++++ Stars Assistant/Model/StarsDatabase.cs | 31 +++++++++++++++++ Stars Assistant/Program.cs | 39 ++++++++++++++++++++-- Stars Assistant/Stars Assistant.csproj | 7 +++- Stars Assistant/demo.cs | 39 ++++++++++++++++++++++ 6 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 Stars Assistant/Model/Planet.cs create mode 100644 Stars Assistant/Model/StarsDatabase.cs create mode 100644 Stars Assistant/demo.cs diff --git a/.gitignore b/.gitignore index 104b544..34ad284 100644 --- a/.gitignore +++ b/.gitignore @@ -482,3 +482,6 @@ $RECYCLE.BIN/ # Vim temporary swap files *.swp + +# Drop sqlite test files +*.sqlite diff --git a/Stars Assistant/Model/Planet.cs b/Stars Assistant/Model/Planet.cs new file mode 100644 index 0000000..f8a5cc3 --- /dev/null +++ b/Stars Assistant/Model/Planet.cs @@ -0,0 +1,46 @@ +using System.ComponentModel.DataAnnotations; +using CSV = CsvHelper.Configuration.Attributes; + +namespace net.nehmer.sa.model; + +public class Planet +{ + [Key, CSV.Index(1)] + public required string Name { get; set; } + + public string? Owner { get; set; } + public string? StarbaseType { get; set; } + public int ReportAge { get; set; } = 0; + public int? Population { get; set; } + public int? Value { get; set; } + public int? Mines { get; set; } + public int? Factories { get; set; } + public decimal? DefPercent { get; set; } + public int? SurfaceIronium { get; set; } + public int? SurfaceBoranium { get; set; } + public int? SurfaceGermanium { get; set; } + public int? MRIronium { get; set; } + public int? MRBoranium { get; set; } + public int? MRGermanium { get; set; } + public int? MCIronium { get; set; } + public int? MCBoranium { get; set; } + public int? MCGErmanium { get; set; } + public int? IroniumMC { get; set; } + public int? Resources { get; set; } + public decimal? Gravity { get; set; } + public decimal? Temperature { get; set; } + public decimal? Radiation { get; set; } + public decimal? GravityOrig { get; set; } + public decimal? TemperatureOrig { get; set; } + public decimal? RadiationOrig { get; set; } + public int? MaxTerraforming { get; set; } + public int? CapacityPercent { get; set; } + public int? ScanRange { get; set; } + public int? PenScanRange { get; set; } + public int? Driver { get; set; } + public int? DriverWarp { get; set; } + public string? RouteTarget { get; set; } + public int? GateRange { get; set; } + public int? GateMass { get; set; } + public int? PctDamage { get; set; } +} \ No newline at end of file diff --git a/Stars Assistant/Model/StarsDatabase.cs b/Stars Assistant/Model/StarsDatabase.cs new file mode 100644 index 0000000..7fea20e --- /dev/null +++ b/Stars Assistant/Model/StarsDatabase.cs @@ -0,0 +1,31 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Numerics; + +namespace net.nehmer.sa.model; + +public class StarsDatabase(string DbPath) : DbContext +{ + #region Configuration and Construction + + public string DbPath { get; } = DbPath; + + /// + /// Configure for SQLite and set the DB Path given. + /// + /// Options to update + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlite($"Data Source={DbPath}"); + + #endregion + + #region Entities + + /// + /// List of all Planets read. + /// + public DbSet Planets { get; set; } + + #endregion +} \ No newline at end of file diff --git a/Stars Assistant/Program.cs b/Stars Assistant/Program.cs index 3751555..1638ef1 100644 --- a/Stars Assistant/Program.cs +++ b/Stars Assistant/Program.cs @@ -1,2 +1,37 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +using System; +using System.Linq; +using Microsoft.EntityFrameworkCore; +using net.nehmer.sa.model; + +using var db = new StarsDatabase("stars.sqlite"); + +db.Database.EnsureDeleted(); +db.Database.EnsureCreated(); + +// Note: This sample requires the database to be created before running. +Console.WriteLine($"Database path: {db.DbPath}."); + +/* +// Create +Console.WriteLine("Inserting a new blog"); +db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" }); +db.SaveChanges(); + +// Read +Console.WriteLine("Querying for a blog"); +var blog = db.Blogs + .OrderBy(b => b.BlogId) + .First(); + +// Update +Console.WriteLine("Updating the blog and adding a post"); +blog.Url = "https://devblogs.microsoft.com/dotnet"; +blog.Posts.Add( + new Post { Title = "Hello World", Content = "I wrote an app using EF Core!" }); +db.SaveChanges(); + +// Delete +// Console.WriteLine("Delete the blog"); +// db.Remove(blog); +// db.SaveChanges(); +*/ \ No newline at end of file diff --git a/Stars Assistant/Stars Assistant.csproj b/Stars Assistant/Stars Assistant.csproj index c5b8644..f091885 100644 --- a/Stars Assistant/Stars Assistant.csproj +++ b/Stars Assistant/Stars Assistant.csproj @@ -3,9 +3,14 @@ Exe net8.0 - Stars_Assistant + net.nehmer.sa enable enable + + + + + diff --git a/Stars Assistant/demo.cs b/Stars Assistant/demo.cs new file mode 100644 index 0000000..dbbc483 --- /dev/null +++ b/Stars Assistant/demo.cs @@ -0,0 +1,39 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; + +public class BloggingContext : DbContext +{ + public DbSet Blogs { get; set; } + public DbSet Posts { get; set; } + + public string DbPath { get; } + + public BloggingContext() + { + DbPath = "./test.sqlite"; + } + + // The following configures EF to create a Sqlite database file in the + // special "local" folder for your platform. + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlite($"Data Source={DbPath}"); +} + +public class Blog +{ + public int BlogId { get; set; } + public string Url { get; set; } + + public List Posts { get; } = new(); +} + +public class Post +{ + public int PostId { get; set; } + public string Title { get; set; } + public string Content { get; set; } + + public int BlogId { get; set; } + public Blog Blog { get; set; } +} \ No newline at end of file