Currently my code, is not too fancy.
A Meeting ID value has to be present for each row. If there is no Meeting Id in meeting object, then return error and stop all process (Meeting Id is the same for each row in the list).
Agenda Item is required, and it cannot be a duplicate value. If missing, or duplicate, return error.
If Legistar Id is missing, then the value of Agenda Item will be assigned to the missing Legistar Id.
Neither Agenda Item, and/or Legistar Id, can contain duplicate values. Each value for these properties has to be unique. Otherwise, stop, return error.
The List I am trying to pass, and validate looks like:
AgendaItem LegistarID Title
1 74490 Public Comment. (October 31 2022)
2 74491 Welcome Affirmative Action Commission Appointment. <-- Agenda Item 2
2 73403 Nomination Open Update. <-- Agenda Item 2
4 74490 Communication Strategy Update. <-- Legistar Id same as row 1
How can I pass a message specifying such particular cases so that the client app, receives it an alerts the user?
This is my current version of my ImportData routine, here goes to nothing
public ActionResult ImportData(List<MeetingAgendaXref_WIP> meeting) // see comment above procedure name
{
bool status = false;
string message = "";
try
{
if (ModelState.IsValid)
{
var errors = new List<string>();
var rowCounter = 1;
using (ClerkEntities db = new ClerkEntities())
{
foreach (var i in meeting)
{
if (i.MeetingID == 0)
{
message = string.Format("No Meeting ID. Please make sure that the Cross Reference meets the required criteria for each row before clicking the ""Upload"" button.");
// log error to list
errors.Add($"Row {rowCounter}: No Meeting ID. Please make sure that the Cross Reference meets the required criteria for each row before clicking the ""Upload"" button.");
return new JsonResult { Data = new { status = status, message = message } };
}
if (i.AgendaItem == 0)
{
message = string.Format("No Agenda Item. Please make sure that the Cross Reference file meets the required criteria for each row before clicking the ""Upload"" button.");
// log error to list
errors.Add($"{rowCounter}:No Agenda Item. Please make sure that the Cross Reference file meets the required criteria for each row before clicking the ""Upload"" button.");
return new JsonResult { Data = new { status = status, message = message } };
}
// if Legistar ID,
if (i.LegistarID == 0)
{
// and Agenda Item are not present, return error message, log error message
if (i.AgendaItem == 0)
{
message = string.Format("Agenda Item, and Legistar Id are missing. Please make sure that the Cross Reference file meets the required criteria for each row before clicking the ""Upload"" button.");
// log error to list
errors.Add("Agenda Item, and Legistar Id are missing. Please make sure that the Cross Reference file meets the required criteria for each row before clicking the ""Upload"" button.");
return new JsonResult { Data = new { status = status, message = message } };
}
// otherwise, if legistar id, is empty, but agenda item is not, then assign Agenda Item to Legistar Id.
else
{
i.LegistarID = i.AgendaItem;
}
}
var compositeKey = db.MeetingAgendaXref_WIP.Find(i.MeetingID, i.AgendaItem);
if (compositeKey == null)
{
// Add new
db.MeetingAgendaXref_WIP.Add(i);
}
else
{
// Update previously saved values (same or new)
db.Entry(compositeKey).CurrentValues.SetValues(i);
db.Entry(compositeKey).State = EntityState.Modified;
}
rowCounter++;
}
// If there are errors do not save and return error message
if (errors.Count > 0)
{
return new JsonResult { Data = new { status = status, message = string.Join("\n", errors) } };
}
else
{
db.SaveChanges();
message = string.Format(@"Your Cross Reference file has been uploaded successfuly!");
status = true;
return new JsonResult { Data = new { status = status, message = message } };
}
}
}
else
{
message = string.Format(@"Please make sure that the Cross Reference file meets the required criteria for each row before clicking the ""Upload"" button.");
return new JsonResult {Data = new {status = status, message = message}};
}
}
catch (System.ArgumentException ex)
{
status = false;
ExceptionLogging.WriteLogError(ex.ToString());
}
return new JsonResult {Data = new {status = status, message = message}};
}