public bool UpdateLostThingRecord(LostThingsRecord Record, bool IsAdd = false) { using (var tr = context.Database.BeginTransaction()) { try { if (IsAdd) { context.LostThingsRecord.Add(Record); } else { context.LostThingsRecord.Update(Record); } context.SaveChanges(); tr.Commit(); return(true); } catch (Exception e) { ErrorHandler.FormatError <ThingOperator>(logger, e); tr.Rollback(); return(false); } } }
public CommonResponse PublishLostThingRecord(LostThingsRecord record) { if (record == null) { return(new CommonResponse() { StatusCode = 1505 }); } if (thing.UpdateLostThingRecord(record, true)) { PushClient.NotifyNewPublsih(); return(new CommonResponse() { StatusCode = 0 }); } else { return(new CommonResponse() { StatusCode = 1503 }); } }
public static LostThingsRecord MergeLostThingsRecord(this LostThingsRecord origin, LostThingsRecord changed) { var CanModifyProperties = typeof(LostThingsRecord) .GetProperties() .Where(x => !x.CustomAttributes.Any(y => y.AttributeType == typeof(ShouldNotModifyAttribute))); foreach (var prop in CanModifyProperties) { prop.SetValue(origin, prop.GetValue(changed)); } return(origin); }
public CommonResponse UpdateLostThingRecord(LostThingsRecord ChangedRecord) { if (ChangedRecord == null) { return(new CommonResponse() { StatusCode = 1504 }); } // 首先检查待更新的对象在数据库中是否存在旧对象 var RecordGUID = ChangedRecord.Id; var OldRecord = thing.GetLostThingsRecord(RecordGUID); if (OldRecord == null) { return(new CommonResponse() { StatusCode = 1501 }); } else { // 然后检查新对象是否修改了不应该修改的Field。 bool UnchangedValidation = ThingHelper.EnsureNotModifyFieldsUnchanged(OldRecord, ChangedRecord); if (!UnchangedValidation) { return(new CommonResponse() { StatusCode = 1502 }); } else { // 校验成功之后,合并新旧对象的Fields,向数据库中写入。 OldRecord.MergeLostThingsRecord(ChangedRecord); if (thing.UpdateLostThingRecord(OldRecord)) { return new CommonResponse() { StatusCode = 0 } } ; else { return new CommonResponse() { StatusCode = 1503 } }; } } }
public static bool EnsureNotModifyFieldsUnchanged(LostThingsRecord Old, LostThingsRecord New) { var CannotModifyProperties = typeof(LostThingsRecord) .GetProperties() .Where(x => x.CustomAttributes.Any(y => y.AttributeType == typeof(ShouldNotModifyAttribute))); foreach (var prop in CannotModifyProperties) { Type FieldType = prop.GetType(); var OldField = prop.GetValue(Old); var NewField = prop.GetValue(New); if (!OldField.Equals(NewField)) { return(false); } } return(true); }
public JsonResult UpdateLostThingInfo([FromBody] LostThingsRecord record) { return(new JsonResult(thingServices.UpdateLostThingRecord(record))); }
public JsonResult PublishLostThing([FromBody] LostThingsRecord record) { return(new JsonResult(thingServices.PublishLostThingRecord(record))); }