public virtual void Patch(string id, IDictionary <string, object> data) { if (string.IsNullOrWhiteSpace(id) || data == null) { throw HttpResponseExceptionUtility.BadRequest("Missing the id argument."); } var count = 0; var parts = id.Split('-'); switch (parts.Length) { case 1: count = this.DataService.Update <string>(data, parts[0], this.GetScope()); break; case 2: count = this.DataService.Update <string, string>(data, parts[0], parts[1], this.GetScope()); break; case 3: count = this.DataService.Update <string, string, string>(data, parts[0], parts[1], parts[2], this.GetScope()); break; default: throw HttpResponseExceptionUtility.BadRequest("The parts of id argument too many."); } if (count < 1) { throw new HttpResponseException(System.Net.HttpStatusCode.NotFound); } }
public virtual object Search(string keyword, [FromUri] Paging paging = null) { if (string.IsNullOrWhiteSpace(keyword)) { throw HttpResponseExceptionUtility.BadRequest("Missing keyword for search."); } return(this.DataService.Search(keyword, this.GetScope(), paging)); }
public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken) { var routeData = actionContext.RequestContext.RouteData; object value; //从当前请求的路由数据中获取指定的键值,获取失败则抛出异常 if (!routeData.Values.TryGetValue(this.Attribute.Key, out value)) { return(TaskHelper.FromError(HttpResponseExceptionUtility.BadRequest(string.Format("Resolve '{0}' route data failed for the '{1}' parameter.", this.Attribute.Key, this.Descriptor.ParameterName)))); } //获取指定键值切分之后的部位顺序 var ordinal = this.GetOrdinal(actionContext); //如果获取的切分部位顺序为正数则必须进行切片处理 if (ordinal >= 0 && value != null) { var parts = Utility.Slice(value.ToString()); if (ordinal < parts.Length) { value = parts[ordinal]; } else { value = this.Descriptor.DefaultValue; } } //进行类型转换处理 if (this.Attribute.ConverterType == null) { object targetValue; if (Zongsoft.Common.Convert.TryConvertValue(value, this.Descriptor.ParameterType, out targetValue)) { value = targetValue; } else { value = this.Descriptor.DefaultValue; } } else { var converter = (TypeConverter)System.Activator.CreateInstance(this.Attribute.ConverterType); if (converter.CanConvertTo(this.Descriptor.ParameterType)) { value = converter.ConvertTo(value, this.Descriptor.ParameterType); } } return(Task.Run(() => this.SetValue(actionContext, value))); }
public virtual void Put(TModel model) { if (model == null || (!this.ModelState.IsValid)) { throw HttpResponseExceptionUtility.BadRequest(this.ModelState); } if (this.DataService.Update(model, this.GetScope()) < 1) { throw new HttpResponseException(System.Net.HttpStatusCode.NotFound); } }
public virtual TModel Post(TModel model) { if (model == null || (!this.ModelState.IsValid)) { throw HttpResponseExceptionUtility.BadRequest(this.ModelState); } if (this.DataService.Insert(model, this.GetScope()) > 0) { return(model); } throw new HttpResponseException(System.Net.HttpStatusCode.Conflict); }
public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken) { var routeData = actionContext.RequestContext.RouteData; object value; //从当前请求的路由数据中获取指定的键值,获取失败则抛出异常 if (!routeData.Values.TryGetValue(this.Attribute.Key, out value)) { return(TaskHelper.FromError(HttpResponseExceptionUtility.BadRequest(string.Format("Resolve '{0}' route data failed for the '{1}' parameter.", this.Attribute.Key, this.Descriptor.ParameterName)))); } //获取指定键值切分之后的部位顺序 var ordinal = this.GetOrdinal(actionContext); //如果获取的切分部位顺序为正数则必须进行切片处理 if (ordinal >= 0 && value != null) { var parts = Utility.Slice(value.ToString()); if (ordinal < parts.Length) { value = parts[ordinal]; } else { value = this.Descriptor.DefaultValue; } } //进行类型转换处理 if (this.Attribute.ConverterType == null) { object targetValue; if (value != null && value is string text && text.Length > 0 && (this.Descriptor.ParameterType.IsArray || Zongsoft.Common.TypeExtension.IsCollection(this.Descriptor.ParameterType))) { var parts = text.Split(','); var elementType = Zongsoft.Common.TypeExtension.GetElementType(this.Descriptor.ParameterType); var array = Array.CreateInstance(elementType, parts.Length); for (int i = 0; i < parts.Length; i++) { if (Zongsoft.Common.Convert.TryConvertValue(parts[i], elementType, out var element)) { array.SetValue(element, i); } else { actionContext.ModelState.AddModelError(this.Descriptor.ParameterName, $"The specified '{parts[i]}' is an invalid element value and cannot be converted to '{elementType.Name}' type."); } } value = array; } else if (Zongsoft.Common.Convert.TryConvertValue(value, this.Descriptor.ParameterType, out targetValue)) { value = targetValue; } else { value = this.Descriptor.DefaultValue; } }
public virtual void Delete(string id) { if (string.IsNullOrWhiteSpace(id)) { throw HttpResponseExceptionUtility.BadRequest("Missing the id argument."); } string[] parts; var entries = id.Split('|'); if (entries != null && entries.Length > 1) { int count = 0; using (var transaction = new Zongsoft.Transactions.Transaction()) { foreach (var entry in entries) { parts = entry.Split('-'); switch (parts.Length) { case 1: count += this.DataService.Delete <string>(parts[0]); break; case 2: count += this.DataService.Delete <string, string>(parts[0], parts[1]); break; case 3: count += this.DataService.Delete <string, string, string>(parts[0], parts[1], parts[2]); break; default: throw HttpResponseExceptionUtility.BadRequest("The parts of id argument too many."); } } transaction.Commit(); } return; } parts = id.Split('-'); var succeed = false; switch (parts.Length) { case 1: succeed = this.DataService.Delete <string>(parts[0]) > 0; break; case 2: succeed = this.DataService.Delete <string, string>(parts[0], parts[1]) > 0; break; case 3: succeed = this.DataService.Delete <string, string, string>(parts[0], parts[1], parts[2]) > 0; break; default: throw HttpResponseExceptionUtility.BadRequest("The parts of id argument too many."); } if (!succeed) { throw new HttpResponseException(System.Net.HttpStatusCode.NotFound); } }
public virtual object Get(string id = null, [FromUri] string key = null, [FromUri] Paging paging = null) { object result = null; if (string.IsNullOrWhiteSpace(id)) { if (string.IsNullOrWhiteSpace(key)) { return(this.DataService.Select(null, this.GetScope(), paging)); } result = this.DataService.Search(key, this.GetScope(), paging); if (result == null) { throw new HttpResponseException(System.Net.HttpStatusCode.NotFound); } return(result); } //如果同时指定了id和key参数,则抛出无效的请求异常(即400错误) if (key != null) { throw HttpResponseExceptionUtility.BadRequest("Cannot specify the 'id' and 'key' argument at the same time."); } var parts = this.Slice(id); switch (parts.Length) { case 1: //如果只指定了一个参数并且参数包含冒号,则直接返回搜索操作结果 if (parts[0].Contains(":")) { result = this.DataService.Search(parts[0], this.GetScope(), paging); if (result == null) { throw new HttpResponseException(System.Net.HttpStatusCode.NotFound); } return(result); } result = this.DataService.Get <string>(parts[0], this.GetScope(), paging); break; case 2: result = this.DataService.Get <string, string>(parts[0], parts[1], this.GetScope(), paging); break; case 3: result = this.DataService.Get <string, string, string>(parts[0], parts[1], parts[2], this.GetScope(), paging); break; default: throw new HttpResponseException(System.Net.HttpStatusCode.BadRequest); } //如果结果对象为空则抛出404异常 if (result == null) { throw new HttpResponseException(System.Net.HttpStatusCode.NotFound); } return(result); }