Reputation: 682
I am working on a C# method that receives a json object containing the following values.
meetingid
o_agendaitem
o_legistarid
o_title
n_agendaitem
n_legistarid
n_title
The values with an "o" represent the record that comes from the database. The values with an "n" represent the edited values from a web form.
I put all together to pass it to the C# method:
var data = JSON.stringify({
'meetingid': $scope.meetingId,
'o_agendaitem': o_xref.AgendaItem,
'o_legistarid': o_xref.LegistarID,
'o_title': o_xref.Title,
'n_agendaitem': n_xref.AgendaItem,
'n_legistarid': n_xref.LegistarID,
'n_title': n_xref.Title,
I am having trouble writing the logic in a clear and simple way.
For example, lets say I have the data below:
44841 1 62704 Title#1
44841 2 62218 Title#2
44841 3 62663 Title#3
44841 4 62679 Title#4
44841 5 62709 Title#5
The user edits the values, and changes the 1 for a 6, then the data would change to:
44841 2 62218 Title#2
44841 3 62663 Title#3
44841 4 62679 Title#4
44841 5 62709 Title#5
44841 6 62704 Title#6
Meeting Id and Agenda Item are a Composite Key.
I use the "o" agenda item value and meeting id, to find that record, and if exists, delete it, and then create a new entry with the "n" values.
But I need to check that the meeting id + the "n" agenda item don't exist already too.
Example:
44841 1 62704 Title#1
44841 2 62218 Title#2 < I edit this row, I want to change the 2 for a 6, but I made a mistake and instead entered a 5, then based on my current logic, I would be deleting a record I did not mean to delete. How can I add a new check to make sure that the user is made aware of this error?
44841 3 62663 Title#3
44841 4 62679 Title#4
44841 5 62709 Title#5
This is my attempt to accomplish so far (I added notes in the parts I am not sure if I am doing it right or not):
public ActionResult UpdateXRefItem(Parameters data)
{
bool status = false;
string message = "";
using (Entities entities = new Entities())
{
//use the "o" value to find the record
var oldRec = entities.XRef_WIP.Find(data.meetingId, data.o_agendaItem);
if (oldRec != null) {
//I am not sure if I should remove the record here, or better do a few more checks.
//entities.XRef_WIP.Remove(oldRec);
//use the "n" value combined with the meeting id to see if already exists
var newVal = entities.XRef_WIP.Find(data.meetingId, data.n_agendaItem);
//if the value from the form already exists, return a message to the user
if (newVal != null)
{
status = false;
message = "This Agenda Item already exists for this Cross-Reference List. Please verfy your data and try again.";
}
else{
//after removing the "old" record, and check that the combination of meeting id + agenda item
//do not exist alreay, then create a new table entry.
//I cannot update agenda item because it is part of the composite key.
//entities.XRef_WIP.Add(new XRefWIP
//{
// MeetingID = data.meetingId,
// AgendaItem = data.n_agendaItem,
// LegistarID = data.n_legistarId,
// Title = data.n_title
//});
//status = true;
//message = "Cross-Reference record has been succssfully updated.";
//entities.SaveChanges();
}
}
}
return new JsonResult { Data = new { status = status, message = message } };
}
I hope this makes sens and someone is willing to offer a hand to complete the logic. I think I am close, but I am having trouble putting my ideas together.
Thank you, Erasmo
Upvotes: 0
Views: 234
Reputation: 1107
See if the solution works, the idea being that you check if the record you want to add exists and if it does you stop the user from proceeding.
public ActionResult UpdateXRefItem(Parameters data)
{
using (Entities entities = new Entities())
{
// First check if the item you are trying to add exists
var currentRec = entities.XRef_WIP.Find(data.meetingId, data.n_agendaItem);
// Stop the user from continueing with the transaction
if (currentRec != null)
return new JsonResult { Data = new { status = false, message = "Record already exists." } };
// Use the "o" value to find the record
var oldRec = entities.XRef_WIP.Find(data.meetingId, data.o_agendaItem);
// If it exists then delete it
if (oldRec != null) {
entities.XRef_WIP.Remove(oldRec);
// Add new record
entities.XRef_WIP.Add(new XRefWIP()
{
MeetingID = data.meetingId,
AgendaItem = data.n_agendaItem,
LegistarID = data.n_legistarId,
Title = data.n_title
});
// Return a new result
return new JsonResult { Data = new { status = true, message = "Success!" } };
}
Upvotes: 1