I wrote an webapi for an angular frontend web site with node express and postgresql 2 years ago we had a habbit from that project with frontend programmers and my web api. I give some fields on database that I never involved the frontenders use that field to save their kind of settings on that field. I’m using dapper becouse I love to have SQL in my hands instead of EFCore..
Now we are writing a new project with Asp.net.core 8 web api with angular but I have problems with json fields.
Lets set a sample
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" : "johndoe",
"fullname" : "John DOE",
"password" : "Please_123",
"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 really stuck at json side of the code…
Thanks All