1 回答
TA贡献2016条经验 获得超9个赞
您在代码中声明了每种方法的验证都是相同的......但事实并非如此。
从获取相关对象:
if (relation.Target.Cardinality == MetaCardinality.Multiple)
{
return BadRequest($"Relation [{relationName}] has multiple cardinality.");
}
从获取相关对象:
if (relation.Target.Cardinality != MetaCardinality.Multiple)
{
return BadRequest("The cardinality is not multiple");
}
话虽这么说,我会做这样的事情,并将验证合并到一个方法中以减少代码重复,添加一个参数以在多个/单个基数之间切换。
未经测试,所以我不知道它是否真的有效;)
public ActionResult<IDataObject> GetRelatedObject([CanBeNull] string className, [CanBeNull] string primaryKey, [CanBeNull] string relationName)
{
var validationError = ValidateRequest(className, primaryKey, relationName, allowMultipleCardinality: false);
if (validationError) return validationError;
try
{
var relationValue = metaObject.GetObject<IDataObject>(relation);
return new ActionResult<IDataObject>(relationValue);
}
catch (InvalidAuthorizationException)
{
return Unauthorized();
}
}
public ActionResult<IEnumerable<IDataObject>> GetRelatedObjects([CanBeNull] string className, [CanBeNull] string primaryKey, [CanBeNull] string relationName)
{
var validationError = ValidateRequest(className, primaryKey, relationName, allowMultipleCardinality: true);
if (validationError) return validationError;
try
{
var list = _dataAccess.GetRelatedList<IDataObject>(DataSession, metaClass, primaryKeyValues, relation);
return new ActionResult<IEnumerable<IDataObject>>(list);
}
catch (InvalidAuthorizationException)
{
return Unauthorized();
}
}
private ActionResult ValidateRequest([CanBeNull] string className, [CanBeNull] string primaryKey, [CanBeNull] string relationName, bool allowMultipleCardinality)
{
if (string.IsNullOrWhiteSpace(primaryKey))
{
return BadRequest("Primary key cannot be empty.");
}
if (!TryGetClass(className, out var metaClass))
{
return NotFound($"Class [{className}] does not exist.");
}
if (!metaClass.Properties.TryGetValue(relationName, out var property))
{
return NotFound($"Class [{metaClass}] does not contain property [{relationName}].");
}
if (!(property is IMetaRelation relation))
{
return BadRequest($"Property [{relationName}] is not a relation.");
}
if (AllowMultipleCardinality)
{
if (relation.Target.Cardinality != MetaCardinality.Multiple)
{
return BadRequest("The cardinality is not multiple");
}
}
else
{
if (relation.Target.Cardinality == MetaCardinality.Multiple)
{
return BadRequest($"Relation [{relationName}] has multiple cardinality.");
}
}
if (!_primaryKeyHandler.TryParsePrimaryKey(metaClass, primaryKey, out var primaryKeyValues))
{
return BadRequest("Cannot convert the primaryKey to the correct type.");
}
if (!_dataCalculator.SupportsData(metaClass))
{
return BadRequest($"The class [{className}] is not persisted");
}
var metaObject = _dataAccess.GetObject<IDataObject>(DataSession, metaClass, primaryKeyValues);
if (metaObject == null)
{
return NotFound($"The metaObject for the primaryKey [{primaryKey}] nad the class [{className}] cannot be empty.");
}
}
- 1 回答
- 0 关注
- 103 浏览
添加回答
举报