File tree Expand file tree Collapse file tree 4 files changed +86
-0
lines changed Expand file tree Collapse file tree 4 files changed +86
-0
lines changed Original file line number Diff line number Diff line change @@ -55,3 +55,6 @@ Desktop.ini
5555* .opendb
5656* .scc
5757
58+
59+ Database.db
60+ Migrations /
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 >
You can’t perform that action at this time.
0 commit comments