0

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?

Alma
  • 3,780
  • 11
  • 42
  • 78

1 Answers1

0

First make Label property of type FormDataEnum inside the class.

Then you can easily perform:

switch(varLabel)//the enum variable
{
   case FormDataEnum.FirstName:
   {
   //code
   break;
   }
  case FormDataEnum.Lastname:
  {
  //code
  break;
  }
 default:
 {
 //something
 }
}

Update:

 public class FormData
    {
        public enum FormDataEnum
        {
            Firstname = 0,
            Lastname = 1,
            DateofBirth = 2,
            Gender = 3
        }

        public FormDataEnum Label {get;set;}    

        public FormData(){}    
    }    

class Program
    {
        static void Main(string[] args)
        {
            List<FormData> list = new List<FormData>();

            FormData f1 = new FormData();
            f1.Label = FormData.FormDataEnum.Firstname;
            list.Add(f1);

            FormData f2 = new FormData();
            f2.Label = FormData.FormDataEnum.DateofBirth;
            list.Add(f2);

            FormData f3 = new FormData();
            f3.Label = FormData.FormDataEnum.Gender;
            list.Add(f3);    

            foreach (var formData in list)
            {    
                switch (formData.Label)
                {
                    case FormData.FormDataEnum.Firstname:
                        {
                            Console.WriteLine("FirstName");
                            break;
                        }
                    case FormData.FormDataEnum.DateofBirth:
                        {
                            Console.WriteLine("DateofBirth");
                            break;
                        }
                    default:
                        {
                            Console.WriteLine("default");
                            break;
                        }

                }

            }
            Console.ReadLine();

        }
    }
jmvcollaborator
  • 2,141
  • 1
  • 6
  • 17
  • I am getting this error for all of the fields "errors": { "formData[0].Label": [ "Error converting value \"First name\" to type 'Ossur.ProApp.Api.V2_0.Models.PatientDtos.FormDataEnum'. Path 'formData[0].Label', line 1, position 502." ], – Alma Sep 22 '22 at 20:01
  • can you put the whole code ? – jmvcollaborator Sep 22 '22 at 21:26
  • This is my Label: public FormDataEnum Label { get; set; } - and this is the endpoint: foreach (var item in dto.FormData) { switch (item.Label) { case FormDataEnum.Firstname: saveDto.genericData.FirstName = item.Value.ToString(); break; case FormDataEnum.Lastname: saveDto.genericData.LastName = item.Value.ToString(); break; – Alma Sep 23 '22 at 18:08
  • let me do a proof of concept and iwill update the code, theres no need to convert ToString() – jmvcollaborator Sep 23 '22 at 19:01
  • 1
    check the update section in the answer, please let me know if it works. – jmvcollaborator Sep 23 '22 at 19:17