Rebuild data storage system so that database dependencies get taken into account
This commit is contained in:
@@ -1,17 +1,54 @@
|
||||
using HopFrame.Api.Logic;
|
||||
using HopFrame.Database.Models;
|
||||
using HopFrame.Security.Authorization;
|
||||
using HopFrame.Security.Claims;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using RestApiTest.Models;
|
||||
|
||||
namespace RestApiTest.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("test")]
|
||||
public class TestController(ITokenContext userContext) : ControllerBase {
|
||||
public class TestController(ITokenContext userContext, DatabaseContext context) : ControllerBase {
|
||||
|
||||
[HttpGet("permissions"), Authorized]
|
||||
public ActionResult<IList<Permission>> Permissions() {
|
||||
return new ActionResult<IList<Permission>>(userContext.User.Permissions);
|
||||
}
|
||||
|
||||
[HttpGet("generate")]
|
||||
public async Task<ActionResult> GenerateData() {
|
||||
var employee = new Employee() {
|
||||
Name = "Max Mustermann"
|
||||
};
|
||||
|
||||
await context.AddAsync(employee);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var address = new Address() {
|
||||
City = "Musterstadt",
|
||||
Country = "Musterland",
|
||||
State = "Musterbundesland",
|
||||
ZipCode = 12345,
|
||||
AddressDetails = "Musterstraße 5",
|
||||
Employee = employee
|
||||
};
|
||||
|
||||
await context.AddAsync(address);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
return LogicResult.Ok();
|
||||
}
|
||||
|
||||
[HttpGet("employees")]
|
||||
public async Task<ActionResult<IList<Employee>>> GetEmployees() {
|
||||
return LogicResult<IList<Employee>>.Ok(await context.Employees.Include(e => e.Address).ToListAsync());
|
||||
}
|
||||
|
||||
[HttpGet("addresses")]
|
||||
public async Task<ActionResult<IList<Address>>> GetAddresses() {
|
||||
return LogicResult<IList<Address>>.Ok(await context.Addresses.Include(e => e.Employee).ToListAsync());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +1,25 @@
|
||||
using HopFrame.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using RestApiTest.Models;
|
||||
|
||||
namespace RestApiTest;
|
||||
|
||||
public class DatabaseContext : HopDbContextBase {
|
||||
|
||||
public DbSet<Employee> Employees { get; set; }
|
||||
public DbSet<Address> Addresses { get; set; }
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
|
||||
optionsBuilder.UseSqlite("Data Source=C:\\Users\\Remote\\Documents\\Projekte\\HopFrame\\test\\RestApiTest\\bin\\Debug\\net8.0\\test.db;Mode=ReadWrite;");
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder) {
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
modelBuilder.Entity<Employee>()
|
||||
.HasOne(e => e.Address)
|
||||
.WithOne(a => a.Employee);
|
||||
}
|
||||
}
|
||||
18
test/RestApiTest/Models/Address.cs
Normal file
18
test/RestApiTest/Models/Address.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
|
||||
namespace RestApiTest.Models;
|
||||
|
||||
public class Address {
|
||||
[ForeignKey("Employee")]
|
||||
public int AddressId { get; set; }
|
||||
public string AddressDetails { get; set; }
|
||||
public string City { get; set; }
|
||||
public int ZipCode { get; set; }
|
||||
public string State { get; set; }
|
||||
public string Country { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public virtual Employee Employee { get; set; }
|
||||
}
|
||||
8
test/RestApiTest/Models/Employee.cs
Normal file
8
test/RestApiTest/Models/Employee.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace RestApiTest.Models;
|
||||
|
||||
public class Employee {
|
||||
public int EmployeeId { get; set; }
|
||||
public string Name { get; set; }
|
||||
|
||||
public virtual Address Address { get; set; }
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<Nullable>disable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user