I wrote an ASP.NET Core 8.0 Web API for an Angular frontend web site with node express and Postgresql 2 years ago we had a habit from that project with frontend programmers and my Web API. I give some columns in the database that I never involved the frontenders use that column to save their kind of settings on it. I'm using Dapper because I love to have SQL in my hands instead of EF Core.
Now we are writing a new project with ASP.NET Core 8 Web API with Angular, but I have problems with json columns.
Let's set an example
Database:
create table users
(
user_id bigserial,
username character varying[50],
fullname character varying[200],
password character varying[300],
salt character varying[300],
frontends_field jsonb,
adress_data jsonb
)
public class User
{
[Required]
public long? User_Id { get; set; }
[Required]
[MaxLength(50)]
public string UserName { get; set; } = string.Empty;
[Required]
[MaxLength(200)]
public string FullName { get; set; } = string.Empty;
[Required]
[MaxLength(300)]
public string Password { get; set; } = string.Empty;
[MaxLength(300)]
public string Salt { get; set; } = string.Empty;
public ??WHICH_TYPE?? frontends_field { get; set; }
public ??WHICH_TYPE?? adress_data { get; set; }
}
public class Adress_Data
{
public string? adress_name { get; set; }
public string? streetinfo { get; set; }
public string? buildinginfo { get; set; }
public string? city { get; set; }
}
In a controller I save this data like this
[HttpPost]
public async Task<ActionResult<User>> PostUser([FromBody] User req)
{
try
{
using var MyCnt = _db.GetConnection(); //Npgsqlconnection here
{
string MySql = """
INSERT INTO public.users (username, fullname, password, salt, frontends_field, adress_data)
VALUES (@username, @fullname, @password, @salt, @frontends_field, @adress_data)
returning *;
""";
var result = await MyCnt.QueryFirstOrDefaultAsync<User>(MySql,req);
return Ok(result);
}
}
catch (Exception ex)
{
return BadRequest(ex);
}
}
Sample JSON for req
{
"username" : "{username}",
"fullname" : "John DOE",
"password" : "{password}",
"salt" "asdşfmi<sdmfpwefkpüpowefi",
"frontends_field" :
{ "somenumfield" : 15,
"sometextfield" : "Cry me a river"
},
"adress_data" :
{
"adress_name" : "home",
"streetinfo" : "Susame Streeet No:21",
"buildinginfo" : "A Block",
"city" : "IZMIR"
}
}
I am really stuck at the JSON side of the code...
Thanks all
