POST方法是否应检查DataStore中是否存在对象并返回错误消息?还是由客户负责检查并调用适当的POST / PUT方法。
当前正在实现-在post方法中检查对象是否存在。
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);
}
}