Skip to content

Commit cebb35a

Browse files
committed
Added sql lite db with entity framework
Added a console project to save default data in db
1 parent a0efe47 commit cebb35a

17 files changed

+154
-72
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,4 @@ MigrationBackup/
348348

349349
# Ionide (cross platform F# VS Code tools) working folder
350350
.ionide/
351+
/OpenBoardAnim/Resources/undraw_love_is_in_the_air_4uud.svg
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.6">
12+
<PrivateAssets>all</PrivateAssets>
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14+
</PackageReference>
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\OpenBoardAnim.Library\OpenBoardAnim.Library.csproj" />
19+
</ItemGroup>
20+
21+
</Project>

DefaultDataInsert/Program.cs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// See https://aka.ms/new-console-template for more information
2+
//Add-Migration InitialCreate
3+
//Update-Database
4+
using Microsoft.EntityFrameworkCore;
5+
using OpenBoardAnim.Library;
6+
using OpenBoardAnim.Library.Repositories;
7+
8+
DataContext context = new DataContext();
9+
GraphicRepository gr = new(context);
10+
var tableNames = context.Model.GetEntityTypes()
11+
.Select(t => t.GetTableName())
12+
.Distinct()
13+
.ToList();
14+
context.Database.ExecuteSqlRaw("Delete from [Graphics]");
15+
context.Database.ExecuteSqlRaw("DELETE FROM SQLITE_SEQUENCE WHERE name='Graphics';");
16+
List<GraphicEntity> graphicEntities = gr.GetAllGraphics();
17+
GraphicEntity entity1 = new GraphicEntity
18+
{
19+
Name = "peep-43",
20+
FilePath = "resources\\peep-43.svg"
21+
};
22+
GraphicEntity entity2 = new GraphicEntity
23+
{
24+
Name = "peep-61",
25+
FilePath = "resources\\peep-61.svg"
26+
};
27+
gr.AddNewGraphic(entity1);
28+
gr.AddNewGraphic(entity2);

OpenBoardAnim.Library/DataContext.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace OpenBoardAnim.Library
4+
{
5+
public class DataContext : DbContext
6+
{
7+
public string DbPath { get; }
8+
public DbSet<GraphicEntity> Graphics { get; set; }
9+
public DbSet<ProjectEntity> Projects { get; set; }
10+
public DataContext(DbContextOptions<DataContext> options) : base(options) { }
11+
public DataContext()
12+
{
13+
var folder = Environment.SpecialFolder.LocalApplicationData;
14+
var path = Environment.GetFolderPath(folder);
15+
DbPath = Path.Join(path, "OpenBoardAnim.db");
16+
}
17+
18+
protected override void OnConfiguring(DbContextOptionsBuilder options)
19+
=> options.UseSqlite($"Data Source={DbPath}");
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace OpenBoardAnim.Library
5+
{
6+
public class GraphicEntity
7+
{
8+
[Key]
9+
public int GraphicID { get; set; }
10+
[Required]
11+
public string Name { get; set; }
12+
[Required]
13+
public string FilePath { get; set; }
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace OpenBoardAnim.Library
4+
{
5+
public class ProjectEntity
6+
{
7+
[Key]
8+
public int ProjectID { get; set; }
9+
[Required]
10+
public string Title { get; set; }
11+
[Required]
12+
public string FilePath { get; set; }
13+
[Required]
14+
public DateTime CreatedOn { get; set; }
15+
[Required]
16+
public DateTime LatestLaunchTime { get; set; }
17+
[Required]
18+
public int SceneCount { get; set; }
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace OpenBoardAnim.Library
4+
{
5+
public class SceneEntity
6+
{
7+
[Required]
8+
public int SceneId { get; set; }
9+
[Required]
10+
public string Name { get; set; }
11+
public List<int> GraphicIDs { get; } = new();
12+
}
13+
}

OpenBoardAnim.Library/GraphicEntity.cs

-8
This file was deleted.

OpenBoardAnim.Library/OpenBoardAnim.Library.csproj

+8
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,12 @@
66
<Nullable>disable</Nullable>
77
</PropertyGroup>
88

9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.6" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.6">
12+
<PrivateAssets>all</PrivateAssets>
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14+
</PackageReference>
15+
</ItemGroup>
16+
917
</Project>

OpenBoardAnim.Library/ProjectEntity.cs

-12
This file was deleted.

OpenBoardAnim.Library/Repositories/GraphicRepository.cs

+13-30
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,21 @@
22
{
33
public class GraphicRepository
44
{
5-
public GraphicRepository()
6-
{
7-
GraphicEntities = new List<GraphicEntity>()
8-
{
9-
new GraphicEntity()
10-
{
11-
Name = "peep-102",
12-
FilePath = "Resources\\peep-102.svg"
13-
14-
},
15-
new GraphicEntity()
16-
{
17-
Name = "peep-64",
18-
FilePath = "Resources\\peep-64.svg"
19-
20-
},
21-
new GraphicEntity()
22-
{
23-
Name = "peep-43",
24-
FilePath = "Resources\\peep-43.svg"
25-
26-
},
27-
new GraphicEntity()
28-
{
29-
Name = "peep-61",
30-
FilePath = "Resources\\peep-61.svg"
31-
32-
}
33-
};
5+
private readonly DataContext _context;
346

7+
public GraphicRepository(DataContext context)
8+
{
9+
_context = context;
10+
}
11+
public List<GraphicEntity> GetAllGraphics()
12+
{
13+
return _context.Graphics.ToList();
3514
}
36-
public List<GraphicEntity> GraphicEntities { get; set; }
3715

16+
public void AddNewGraphic(GraphicEntity entity)
17+
{
18+
_context.Graphics.Add(entity);
19+
_context.SaveChanges();
20+
}
3821
}
3922
}

OpenBoardAnim.Library/Repositories/SceneRepository.cs

-4
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,21 @@ public SceneRepository()
1515
new SceneEntity()
1616
{
1717
Name = "peep-102",
18-
FilePath = "Resources\\peep-102.svg"
1918

2019
},
2120
new SceneEntity()
2221
{
2322
Name = "peep-64",
24-
FilePath = "Resources\\peep-64.svg"
2523

2624
},
2725
new SceneEntity()
2826
{
2927
Name = "peep-43",
30-
FilePath = "Resources\\peep-43.svg"
3128

3229
},
3330
new SceneEntity()
3431
{
3532
Name = "peep-61",
36-
FilePath = "Resources\\peep-61.svg"
3733

3834
}
3935
};

OpenBoardAnim.Library/SceneEntity.cs

-8
This file was deleted.

OpenBoardAnim.sln

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenBoardAnim.VideoTools",
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenBoardAnim.Library", "OpenBoardAnim.Library\OpenBoardAnim.Library.csproj", "{62FBC3EE-6D55-4F13-B0A3-48F20C14C84A}"
1111
EndProject
12-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenBoardAnim", "OpenBoardAnim\OpenBoardAnim.csproj", "{5472CFF7-E929-4D81-98E3-B68F39326C95}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenBoardAnim", "OpenBoardAnim\OpenBoardAnim.csproj", "{5472CFF7-E929-4D81-98E3-B68F39326C95}"
13+
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DefaultDataInsert", "DefaultDataInsert\DefaultDataInsert.csproj", "{C3FFE920-F209-4983-B991-89B84CA6D481}"
1315
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -33,6 +35,10 @@ Global
3335
{5472CFF7-E929-4D81-98E3-B68F39326C95}.Debug|Any CPU.Build.0 = Debug|Any CPU
3436
{5472CFF7-E929-4D81-98E3-B68F39326C95}.Release|Any CPU.ActiveCfg = Release|Any CPU
3537
{5472CFF7-E929-4D81-98E3-B68F39326C95}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{C3FFE920-F209-4983-B991-89B84CA6D481}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{C3FFE920-F209-4983-B991-89B84CA6D481}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{C3FFE920-F209-4983-B991-89B84CA6D481}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{C3FFE920-F209-4983-B991-89B84CA6D481}.Release|Any CPU.Build.0 = Release|Any CPU
3642
EndGlobalSection
3743
GlobalSection(SolutionProperties) = preSolution
3844
HideSolutionNode = FALSE

OpenBoardAnim/App.xaml.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using Microsoft.Extensions.DependencyInjection;
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.Extensions.DependencyInjection;
23
using OpenBoardAnim.Core;
4+
using OpenBoardAnim.Library;
35
using OpenBoardAnim.Library.Repositories;
46
using OpenBoardAnim.Services;
57
using OpenBoardAnim.ViewModels;
@@ -24,6 +26,7 @@ public App()
2426
{
2527
DataContext = provider.GetRequiredService<MainViewModel>()
2628
});
29+
services.AddSingleton<DataContext>();
2730
services.AddSingleton<MainViewModel>();
2831
services.AddSingleton<LaunchViewModel>();
2932
services.AddSingleton<EditorActionsViewModel>();
@@ -34,7 +37,7 @@ public App()
3437
services.AddSingleton<SceneRepository>();
3538
services.AddSingleton<GraphicRepository>();
3639
services.AddSingleton<Func<Type, ViewModel>>(sp => vMType => (ViewModel)sp.GetRequiredService(vMType));
37-
40+
3841
_serviceProvider = services.BuildServiceProvider();
3942
}
4043
protected override void OnStartup(StartupEventArgs e)

OpenBoardAnim/Models/GraphicModel.cs

-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ namespace OpenBoardAnim.Models
88
public class GraphicModel : ObservableObject
99
{
1010
public string Name { get; set; }
11-
public string ImgPath { get; set; }
1211
public DrawingGroup ImgGeometry { get; set; }
13-
public string Shape { get; set; }
1412
public ICommand AddGraphicCommand { get; set; }
1513
public double Width { get; set; } = 100;
1614
public double Height { get; set; } = 100;
@@ -31,9 +29,7 @@ public GraphicModel Clone()
3129
Height = Height,
3230
Width = Width,
3331
ImgGeometry = ImgGeometry,
34-
ImgPath = ImgPath,
3532
Name = Name,
36-
Shape = Shape,
3733
X = X,
3834
Y = Y
3935
};

OpenBoardAnim/ViewModels/EditorLibraryViewModel.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,18 @@ public EditorLibraryViewModel(IPubSubService pubSub,SceneRepository sRepo,Graphi
2222
_sRepo = sRepo;
2323
_gRepo = gRepo;
2424
string folder = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
25-
List <SceneModel> scenes = sRepo.SceneEntities.Select(e =>
25+
List <SceneModel> scenes = _sRepo.SceneEntities.Select(e =>
2626
new SceneModel
2727
{
2828
Name = e.Name,
2929
ReplaceScene = ReplaceSceneHandler,
3030
}).ToList();
3131
Scenes = new BindingList<SceneModel>(scenes);
3232

33-
List<GraphicModel> graphics = gRepo.GraphicEntities.Select(e =>
33+
List<GraphicModel> graphics = _gRepo.GetAllGraphics().Select(e =>
3434
new GraphicModel
3535
{
3636
Name = e.Name,
37-
ImgPath = e.FilePath,
3837
AddGraphic = AddGraphicHandler,
3938
ImgGeometry = GetPathGeometryFromSVG(Path.Combine(folder, e.FilePath))
4039
}).ToList();

0 commit comments

Comments
 (0)