Should the POST method check for the existence of the object in DataStore and return error message? Or is it the responsibility of the client to check and call the appropriate POST/PUT method.
Currently implementing - object existence check in post method..
public HttpResponseMessage Post([FromBody]List<MachineLearningAgentObjectMappingHeader> machineLearningAgentObjectMappingObject)
{
string errorString = string.Empty;
StringBuilder badRequestErrorStrings = new StringBuilder();
try
{
User user = Request.Properties["User"] as User;
var dal = DalFactory.GetDal();
var mlAgentRowId = machineLearningAgentObjectMappingObject.FirstOrDefault().MachineLearningAgentRowId;
var objRowId = machineLearningAgentObjectMappingObject.FirstOrDefault().ObjectRowId;
var objectMappingsinDb = dal.MachineLearningAgentObjectMapping.GetMachineLearningAgentObjectMappings(machineLearningAgentRowId: mlAgentRowId,objectRowId:objRowId);
//check if duplicate objectmappings exist
var duplicatetagMappings = objectMappingsinDb.Where(om => machineLearningAgentObjectMappingObject.Any(item => item.ObjectRowId ==
om.ObjectRowId && item.MachineLearningAgentRowId == om.MachineLearningAgentRowId)).ToList();
if (duplicatetagMappings?.Count > 0)
{
StringBuilder duplicateEquipmentSetErr = new StringBuilder();
duplicatetagMappings.ForEach(item => duplicateEquipmentSetErr.AppendLine(string.Format("MachineLearningAgentTagMapping already defined - Object:{0} - MachineLearningAgent:{1}",
item.ObjectId, item.MachineLearningAgentId)));
return Request.CreateResponse<string>(HttpStatusCode.BadRequest, duplicateEquipmentSetErr.ToString());
}
machineLearningAgentObjectMappingObject = dal.MachineLearningAgentObjectMapping.SaveMachineLearningAgentObjectMappings(machineLearningAgentObjectMappingObject, user.RowId.Value);
return Request.CreateResponse(HttpStatusCode.OK, machineLearningAgentObjectMappingObject);
}
catch (Exception ex)
{
HandleException(ex);
return Request.CreateResponse<string>(HttpStatusCode.InternalServerError, ex.Message);
}
}
Aucun commentaire:
Enregistrer un commentaire