0% found this document useful (0 votes)
2 views3 pages

Coding

The document outlines an asynchronous method for inserting transportation line data from an Excel file into a database. It includes error handling for missing files and validation for empty parameters, while also logging any errors encountered during processing. Finally, it returns a response indicating the success or failure of the operation along with any error messages generated.

Uploaded by

ducfhamg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Coding

The document outlines an asynchronous method for inserting transportation line data from an Excel file into a database. It includes error handling for missing files and validation for empty parameters, while also logging any errors encountered during processing. Finally, it returns a response indicating the success or failure of the operation along with any error messages generated.

Uploaded by

ducfhamg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

public async Task<BaseResponseWithData<string>>

InserttransportionLineExcel([FromForm] IFormFile dto)


{
BaseResponseWithData<string> Response = new BaseResponseWithData<string>();
[Link] = true;
[Link] = new List<Error>();
var errorList = new List<string>();

try
{
if (dto == null)
throw new Exception("Object is not received...");

if (dto == null)
throw new Exception("File is Not Received...");

// Create the Directory if it is not exist


string dirPath = [Link](_host.WebRootPath, "ReceivedReports");

// Get Data set


DataSet transportionLineDs = [Link](dto, dirPath);

var allTransportionLine = new List<TransportationLine>();

if (transportionLineDs != null && [Link] > 0)


{
DataTable TransportionLine = [Link][0];

List<string> errorMessages = new List<string>();

// Process each row in the Excel file


for (int i = 1; i < [Link]; i++)
{
var row = [Link][i];

var transportationVehicleId = row[0].ToString();


var lineName = row[1].ToString();
var lineCost = row[2].ToString();
var oneWay = row[3].ToString();
var levelId = row[11].ToString();
var specialDeptId = row[5].ToString();
var specialDept = row[6].ToString();
var email = row[7].ToString() ?? null;
var nationalityId = row[8].ToString() ?? null;
var yearId = row[12].ToString() ?? null;
var role = row[10].ToString();
var subscribeToSubjects = row[13].ToString();

// Validation
if ([Link](transportationVehicleId) ||
[Link](lineName)
|| [Link](lineCost) || [Link](oneWay))
{
[Link]($"This User {transportationVehicleId} {lineName}
{lineCost} Some Parameters is Empty");
continue;
}

//if ([Link](email))
//{
// [Link]($"This User {firstName} {middleName} {lastName}
Email is already registered");
// continue;
//}

//if ([Link](phoneNumber))
//{
// [Link]($"This User {firstName} {middleName} {lastName}
PhoneNumber is already registered");
// continue;
//}

//if ([Link](firstName + " " + middleName + " " + lastName))


//{
// [Link]($"This User {firstName} {middleName} {lastName}
FullName is already registered");
// continue;
//}

TransportationLine newtransportionLine = new TransportationLine


{
TransportationVehicleId = [Link](transportationVehicleId),
LineName = lineName,
LineCost = [Link](lineCost),
OneWay = [Link](oneWay),
IsApproved = true,
ApprovedBy="1",
CreationDate = [Link],
CreationBy = 1,
Active = true,
ModifiedBy=1,
ModifiedDate = [Link],
};

[Link](newtransportionLine);

// Handle bulk insert of new users, user departments, and competition users
await _unitOfWork.[Link](allTransportionLine);

// Send file URL or other tasks asynchronously


// Assuming file URL handling can be refactored to run in parallel

_unitOfWork.Complete();

string LOGPath = null;


if ([Link]())
{
string logsPath = [Link](_host.WebRootPath, "Logs");
//string filePath = [Link](_hostEnvironment.WebRootPath, "Logs",
"[Link]");

//// Check if the file exists


//if([Link](filePath))
//{
// // Clear the contents of the file by writing an empty string to it
// [Link](filePath , [Link]);
//}

// string filePath = [Link](_hostEnvironment.WebRootPath,


"Logs/[Link]");
// var dateNow =[Link]("yyyy-MM-dd-hh-ss");
string FileName = $"[Link]";
LOGPath = "Logs/";
string SavePath = [Link](_host.WebRootPath, LOGPath);
if (![Link](SavePath))
{
[Link](SavePath); //Create directory if it
doesn't exist
}
SavePath = SavePath + FileName;
//using FileStream fileStream = new(SavePath, [Link]);
// LOGPath="/"+LOGPath+FileName;
////[Link](fileStream);

// Create a StreamWriter and write lines to the file


using (StreamWriter writer = new StreamWriter(SavePath, append: false,
encoding: Encoding.UTF8)) // append: true to add to an existing file
{

foreach (var error in errorMessages)


{
[Link]($"Error message: " + error);
}

}
[Link] = [Link] + "/Logs/" + FileName;

}
return Response;
}
catch (Exception ex)
{
[Link] = false;
Error error = new Error();
[Link] = "Err10";
[Link] = [Link];
[Link](error);
return Response;
}
}

You might also like