Skip to content

Setup database #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
*/obj/
*/bin/
.idea
.idea

# Postgres data
data/
25 changes: 25 additions & 0 deletions Epsilon/Controllers/TestController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Epsilon.Data;
using Epsilon.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Epsilon.Controllers;

[ApiController]
[Route("api")]
public class TestController
{

private readonly EpsilonDbContext _epsilonDbContext;

public TestController(EpsilonDbContext epsilonDbContext)
{
_epsilonDbContext = epsilonDbContext;
}

[HttpGet("users")]
public async Task<ActionResult<IList<User>>> GetUsers()
{
return await _epsilonDbContext.Users.ToListAsync();
}
}
14 changes: 14 additions & 0 deletions Epsilon/Data/EpsilonDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Epsilon.Models;
using Microsoft.EntityFrameworkCore;

namespace Epsilon.Data;

public class EpsilonDbContext : DbContext
{
public EpsilonDbContext(DbContextOptions<EpsilonDbContext> options) : base(options)
{
}

public DbSet<Message> Messages { get; set; }
public DbSet<User> Users { get; set; }
}
77 changes: 77 additions & 0 deletions Epsilon/DatabaseInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System.Runtime.InteropServices.JavaScript;
using Epsilon.Data;
using Epsilon.Models;
using Microsoft.EntityFrameworkCore;

namespace Epsilon;
public class DatabaseInitializer
{
public static void Seed(WebApplication app)
{
using (var serviceScope = app.Services.CreateScope())
{
var context = serviceScope.ServiceProvider.GetService<EpsilonDbContext>();

if (context != null)
{
if (!context.Users.Any())
InitUsers(context);

if (!context.Messages.Any())
InitMessages(context);
}
}

}

public static void Migrate(WebApplication app)
{
// Migrate db
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<EpsilonDbContext>();
db.Database.Migrate();
}
}

private static byte[] GenerateRandomBytes(int length)
{
byte[] randomBytes = new byte[length];
Random random = new Random();

random.NextBytes(randomBytes);

return randomBytes;
}

private static void InitUsers(EpsilonDbContext context)
{
var usersToAdd = new List<User>
{
new User { Id = GenerateRandomBytes(32), PublicKey = GenerateRandomBytes(256), Username = "user_1"},
new User { Id = GenerateRandomBytes(32), PublicKey = GenerateRandomBytes(256), Username = "user_2"},
new User { Id = GenerateRandomBytes(32), PublicKey = GenerateRandomBytes(256), Username = "user_3"},
new User { Id = GenerateRandomBytes(32), PublicKey = GenerateRandomBytes(256), Username = "user_4"},
};
foreach (var user in usersToAdd)
context.Users.Add(user);

context.SaveChanges();
}

private static void InitMessages(EpsilonDbContext context)
{
var messagesToAdd = new List<Message>
{
new Message { EncryptedMessage = GenerateRandomBytes(256), SenderId = GenerateRandomBytes(32), RecipientId = GenerateRandomBytes(32), CreatedAt = new DateTime() },
new Message { EncryptedMessage = GenerateRandomBytes(256), SenderId = GenerateRandomBytes(32), RecipientId = GenerateRandomBytes(32), CreatedAt = new DateTime() },
new Message { EncryptedMessage = GenerateRandomBytes(256), SenderId = GenerateRandomBytes(32), RecipientId = GenerateRandomBytes(32), CreatedAt = new DateTime() },
new Message { EncryptedMessage = GenerateRandomBytes(256), SenderId = GenerateRandomBytes(32), RecipientId = GenerateRandomBytes(32), CreatedAt = new DateTime() }
};

foreach (var message in messagesToAdd)
context.Messages.Add(message);

context.SaveChanges();
}
}
2 changes: 2 additions & 0 deletions Epsilon/Epsilon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.2-dev-00338" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.1" />
<PackageReference Include="Serilog.Exceptions" Version="8.4.0" />
Expand Down
12 changes: 12 additions & 0 deletions Epsilon/IMessageManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Epsilon.Models;

namespace Epsilon;

public interface IMessageManager
{
public Task AddMessage(Message message);
public Task DeleteMessage(byte[] messageId);
public Task<List<Message>> GetAllMessages();
public Task<List<Message>> GetMessagesBetweenUsers(byte[] userId1, byte[] userId2, DateTime? startDate = null, DateTime? endDate = null);
public Task<Message> GetMessageById(byte[] messageId);
}
11 changes: 11 additions & 0 deletions Epsilon/IUserManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Epsilon.Models;

namespace Epsilon;

public interface IUserManager
{
Task AddUser(User user);
Task DeleteUser(byte[] userId);
Task<List<User>> GetAllUsers();
Task<User> GetUserById(byte[] userId);
}
32 changes: 32 additions & 0 deletions Epsilon/MessageManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Epsilon.Models;

namespace Epsilon;

public class MessageManager : IMessageManager
{
public Task AddMessage(Message message)
{
throw new NotImplementedException();
}

public Task DeleteMessage(byte[] messageId)
{
throw new NotImplementedException();
}

public Task<List<Message>> GetAllMessages()
{
throw new NotImplementedException();
}

public Task<List<Message>> GetMessagesBetweenUsers(byte[] userId1, byte[] userId2, DateTime? startDate = null,
DateTime? endDate = null)
{
throw new NotImplementedException();
}

public Task<Message> GetMessageById(byte[] messageId)
{
throw new NotImplementedException();
}
}
78 changes: 78 additions & 0 deletions Epsilon/Migrations/20240625191035_InitialCreate.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions Epsilon/Migrations/20240625191035_InitialCreate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace Epsilon.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Messages",
columns: table => new
{
MessageId = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
SenderId = table.Column<byte[]>(type: "bytea", nullable: false),
RecipientId = table.Column<byte[]>(type: "bytea", nullable: false),
EncryptedMessage = table.Column<byte[]>(type: "bytea", nullable: false),
CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Messages", x => x.MessageId);
});

migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<byte[]>(type: "bytea", nullable: false),
PublicKey = table.Column<byte[]>(type: "bytea", nullable: false),
Username = table.Column<string>(type: "character varying(16)", maxLength: 16, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Messages");

migrationBuilder.DropTable(
name: "Users");
}
}
}
Loading
Loading