demo20230512/Filter/ApiEditableAttribute.cs

52 lines
1.9 KiB
C#
Raw Permalink Normal View History

2023-05-12 10:20:28 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http.Filters;
using System.Net.Http;
using Weee.Models;
namespace Weee.Filter
{
/// <summary>
/// this attribute is designed for sheet data , to determine if a data should be editable
///
/// </summary>
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<SheetData>();
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);
}
}
}