I try to create a Rest API C# endpoint so they can post the JSON to that and it will process it. This is sample JSON they will post to my endpoint is looks like this:
{
"Guid": "abc123",
"ID": "68AA101C-111-888-9CC1-1265",
"Name": "test",
"formData": [
{
"FieldGuid": "454545454545",
"FieldType": "a",
"Label": "First name",
"Value": "Sam"
},
{
"FieldGuid": "121212121254545",
"FieldType": "a",
"Label": "Last name",
"Value": "DummyData"
},
{
"FieldGuid": "787878787854545",
"FieldType": "b",
"Label": "Date of Birth",
"Value": "1999-01-01T16:05:00.000Z"
},
{
"FieldGuid": "2323212121545",
"FieldType": "c",
"Label": "Gender",
"Value": "Male"
},
......
] }
As you see "formData" is a lot of properties (it is like 40) . what is the best way to add "formData" to my model?
This is my model so far:
public class Data
{
}
public class FormData
{
public string FieldGuid { get; set; }
public string FieldType { get; set; }
public string Label { get; set; }
public object Value { get; set; }
}
public class Root
{
public string Guid { get; set; }
public string ID { get; set; }
public string Name { get; set; }
public Data Data { get; set; }
public List<FormData > formData { get; set; }
}
and this is my API endpoint:
[HttpPost("CreateOT")]
public OPIEPatientDto CreateOT([FromBody] OTDto dto)
{
SaveDto saveDto = new SaveDto();
foreach (var item in dto.FormData)
{
switch (item.Label)
{
case "First name":
saveDto.genericData.FirstName = item.Value.ToString();
break;
case "Last name":
savePatientCommandDto.genericData.LastName = item.Value.ToString();
break;
case "Date of Birth":
savePatientCommandDto.genericData.DateOfBirth = Convert.ToDateTime(item.Value);
break;
}
}
...
}
Even this model and using foreach loop okay here? and also if instead of case "First name": I wanted to use enum what can I so?
I created an enum like:
public enum FormDataEnum
{
Firstname = 0,
Lastname = 1,
DateofBirth = 2,
Gender = 3,
.....
but when I wanted to use like this:
switch (item.Label)
{
case FormDataEnum.Firstname.ToString():
does not recognize Firstname.I know we don't have enum as string in C#, I can use struct or const, what how I can use enum?