in one of my functionality I need to add images along with other form data so I am using form data to send the files. In the get request, I just need to show the images so I am getting the image information like path stored, name to display the images. I have created 3 separate models i.e BaseModel, GetModel, and PostModel for the same with only 1 parameter difference in getting and post. Here are my models.
public class PatientInformationBaseViewModel
{
public Guid id { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string email { get; set; }
public int age { get; set; }
public string phone { get; set; }
public GenderViewModel gender { get; set; }
public string history { get; set; }
public string caseNo { get; set; }
}
public class GetPatientInfomationViewModel: PatientInformationBaseViewModel
{
public FileInformationViewModel patientPhotoInformation { get; set; }
}
public class PostPatientInformationViewModel : PatientInformationBaseViewModel
{
[JsonIgnore]
public IFormFile patientPhoto { get; set; }
}
I have created a model to accept the formdata in the controller as
public class PatientInformationFormDataViewModel
{
public string patientInformation { get; set; }
public IFormFile patientPhoto { get; set; }
}
In the controller, I am deserializing into the post model and then sending it to my service layer. In my service layer, I am adding patient information to the database and returning the GUID which is my primary key of the table. Using the Id field I am uploading the image i.e saving files to the file system and returning the details and then calling a query to save file information in the table.
While getting the data to be displayed in the Angular table I am using GetModel as I do not need the actual file. If I combine all the models one of the parameters will be useless and remain null so I chose this approch.
Is this the correct approach or is there any better way in terms of conventions. Thank you in advance
question from:
https://stackoverflow.com/questions/65936737/regarding-image-upload-and-get-file-information-models-in-net-core-api-and-angu 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…