Skip to content

Commit 0742816

Browse files
authored
feat: database (#9)
* Add EntityFramework package reference to project file * feat: Implement database structure and database models. Also added database function specified on board * refactor: Remove unnecessary using directive from SQLite.cs * refactor: Remove database and migration files, update .gitignore to exclude Database.db and Migrations/
1 parent dce920f commit 0742816

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,6 @@ Desktop.ini
5555
*.opendb
5656
*.scc
5757

58+
59+
Database.db
60+
Migrations/

Database/SQLite.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
public class Database : DbContext
4+
{
5+
public DbSet<DBUser> Users { get; set; }
6+
public DbSet<DBInbox> Inbox { get; set; }
7+
8+
private readonly string _dbPath;
9+
public Database(string dbPath = "./Database.db")
10+
{
11+
_dbPath = dbPath;
12+
}
13+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
14+
{
15+
optionsBuilder.UseSqlite($"Data Source={_dbPath}");
16+
}
17+
18+
public void RegisterUser(Guid userId, byte[]? rsaPublicKey)
19+
{
20+
var user = new DBUser
21+
{
22+
UserID = userId,
23+
RsaPublicKey = rsaPublicKey
24+
};
25+
Users.Add(user);
26+
SaveChanges();
27+
}
28+
public Dictionary<Guid, byte[]?> ListUsers()
29+
{
30+
var users = new Dictionary<Guid, byte[]?>();
31+
foreach (var user in Users)
32+
{
33+
users.Add(user.UserID, user.RsaPublicKey);
34+
}
35+
return users;
36+
}
37+
public List<byte[]?> GetMsgsByUserID(Guid userId)
38+
{
39+
var inbox = new List<byte[]?>();
40+
foreach (var message in Inbox.Where(m => m.UserID == userId))
41+
{
42+
inbox.Add(message.EncryptedMessage);
43+
}
44+
return inbox;
45+
}
46+
public void AddMsgToUser(Guid userId, byte[]? encryptedMessage)
47+
{
48+
var message = new DBInbox
49+
{
50+
UserID = userId,
51+
EncryptedMessage = encryptedMessage
52+
};
53+
Inbox.Add(message);
54+
SaveChanges();
55+
}
56+
}

Models/DBmodels.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using System.ComponentModel.DataAnnotations.Schema;
3+
4+
public class DBUser{
5+
[Key]
6+
public Guid UserID { get; set; }
7+
public byte[]? RsaPublicKey { get; set; }
8+
}
9+
10+
public class DBInbox{
11+
[Key]
12+
public int Id { get; set; }
13+
[ForeignKey("UserID")]
14+
public DBUser? User { get; set; }
15+
public Guid UserID { get; set; }
16+
public byte[]? EncryptedMessage { get; set; }
17+
}

sutor-aes/sutor-aes.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,14 @@
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<PackageReference Include="EntityFramework" Version="6.5.1" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.4" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.4">
14+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15+
<PrivateAssets>all</PrivateAssets>
16+
</PackageReference>
17+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.4" />
18+
</ItemGroup>
19+
1020
</Project>

0 commit comments

Comments
 (0)