using System; using System.Collections.Generic; using System.Linq; using System.Web.Http.Filters; using System.Net.Http; using Weee.Models; namespace Weee.Filter { /// /// this attribute is designed for sheet data , to determine if a data should be editable /// /// public class ApiEditableAttribute : ActionFilterAttribute { private string _variableName; private bool _isList; public ApiEditableAttribute(string variableName , bool isList) { _variableName = variableName; _isList = isList; } public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext) { if (actionContext.ActionArguments.Count > 0) { dynamic target = actionContext.ActionArguments[_variableName]; var holder = new List(); if (_isList) holder.AddRange(target); else holder.Add(target); if(holder.Count>0)//CFT-6 處理bug時報錯了 { int LCAID = holder.Select(x => x.LCAID).First(); var db = actionContext.Request.GetDependencyScope().GetService(typeof(Weee.DAL.WeeeDataContext)) as Weee.DAL.WeeeDataContext; var status = db.ProductLCAs.Where(x => x.ID == LCAID).Select(x => x.Status).SingleOrDefault(); if (status == LCAStatus.New || status == LCAStatus.Processing) { } else { var response = actionContext.Request.CreateErrorResponse(System.Net.HttpStatusCode.BadRequest, new Exception("error lca status is wrong")); actionContext.Response = response; } } } base.OnActionExecuting(actionContext); } } }