Skip to content

Commit

Permalink
backend: Config MySQL Connection
Browse files Browse the repository at this point in the history
  • Loading branch information
ShinjiMC committed May 26, 2024
1 parent 48414cd commit 94a190c
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 3 deletions.

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

44 changes: 44 additions & 0 deletions users-microservice/src/Migrations/20240526051431_InitialCreate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace src.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterDatabase()
.Annotation("MySql:CharSet", "utf8mb4");

migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Name = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
Email = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
Password = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4")
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
})
.Annotation("MySql:CharSet", "utf8mb4");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Users");
}
}
}
48 changes: 48 additions & 0 deletions users-microservice/src/Migrations/MySqlContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using users_microservice.repositories;

#nullable disable

namespace src.Migrations
{
[DbContext(typeof(MySqlContext))]
partial class MySqlContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.0-preview.1.24081.2")
.HasAnnotation("Relational:MaxIdentifierLength", 64);

MySqlModelBuilderExtensions.AutoIncrementColumns(modelBuilder);

modelBuilder.Entity("users_microservice.models.UserModel", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");

MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));

b.Property<string>("Email")
.HasColumnType("longtext");

b.Property<string>("Name")
.HasColumnType("longtext");

b.Property<string>("Password")
.HasColumnType("longtext");

b.HasKey("Id");

b.ToTable("Users");
});
#pragma warning restore 612, 618
}
}
}
5 changes: 5 additions & 0 deletions users-microservice/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@
app.UseAuthorization();
app.MapControllers();
app.MapGet("/", () => "Hello World!");
using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<MySqlContext>();
dbContext.Database.Migrate();
}
app.Run();
2 changes: 1 addition & 1 deletion users-microservice/src/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ConnectionStrings": {
"MySqlConnection": "Server=roundhouse.proxy.rlwy.net;Database=railway;User=root;Password=HnRwKMVDiNfbuMZrpwFHgsraNbIglHbm"
"MySqlConnection": "Server=roundhouse.proxy.rlwy.net;Port=38511;Database=railway;User=root;Password=HnRwKMVDiNfbuMZrpwFHgsraNbIglHbm"
},
"Logging": {
"LogLevel": {
Expand Down
21 changes: 20 additions & 1 deletion users-microservice/src/repository/UserRepository.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.EntityFrameworkCore;
using users_microservice.models;

namespace users_microservice.repositories;
Expand All @@ -10,6 +11,24 @@ public interface IUserRepository

public class UserRepository : IUserRepository
{
private readonly MySqlContext _context;
public UserRepository(MySqlContext context)
{
_context = context;
}

public async Task<UserModel> CreateUser(UserModel user)
{
await _context.Users.AddAsync(user);
await _context.SaveChangesAsync();
return user;
}

public async Task<List<UserModel>> GetUsers()
{
return await _context.Users.ToListAsync();
}
/*
private int nextId = 3;
private static readonly List<UserModel> Users = [
new() {
Expand Down Expand Up @@ -44,5 +63,5 @@ public async Task<List<UserModel>> GetUsers()
}
return Users;
}

*/
}
2 changes: 1 addition & 1 deletion users-microservice/src/src.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.5">
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0-preview.1.24081.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down

0 comments on commit 94a190c

Please sign in to comment.