修復文件(共 17 處):
1. supplierController.cs (6 處)
- 移除簡單查詢的 AsEnumerable() (2 處)
- AsEnumerable() → AsQueryable() (GetList, GetKindList)
- 優化分頁查詢模式(Count + ToPagedList.ToList())(2 處)
- 修復 Split().Select().ToList() 類型問題
2. adminUserController.cs (4 處)
- 移除簡單查詢的 AsEnumerable()
- AsEnumerable() → AsQueryable() (GetList)
- 優化分頁查詢模式
- 重構複雜查詢:移除嵌套的 AsEnumerable()
改為先 ToList() 再使用 Contains
3. orderdetailController.cs (3 處)
- 移除 AsEnumerable(),改為在查詢末端使用 OrderBy
- 優化分頁查詢模式
- 修復 null-propagating operator:
.FirstOrDefault()?.print_id → .Select(u => u.print_id).FirstOrDefault()
4. transfer_registerController.cs (6 處)
- AsEnumerable() → ToList() (需要解密操作的情況)
- 保留在必要時才轉換到內存操作
- 確保數據庫查詢先執行
技術改進:
✓ 複雜嵌套查詢優化(adminUserController)
✓ 解密操作前置優化(transfer_registerController)
✓ Null-propagating operator 替換為安全的 Select
✓ 統一使用三元運算符處理分頁
✓ IEnumerable<int> → List<int> 避免類型轉換錯誤
139 lines
3.6 KiB
C#
139 lines
3.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Web.Http;
|
|
using PagedList;
|
|
using Newtonsoft.Json;
|
|
using System.Collections;
|
|
|
|
|
|
// api/adminUser
|
|
//[ezAuthorize(Roles = "admin")]//群組:*
|
|
[ezAuthorize]
|
|
public class adminUserController : ApiController
|
|
{
|
|
private Model.ezEntities _db = new Model.ezEntities();
|
|
// GET api/<controller>
|
|
public IEnumerable<Model.admin> Get()
|
|
{
|
|
var list = _db.admins.ToList();
|
|
if (list == null) throw new HttpResponseException(HttpStatusCode.NotFound);
|
|
return list;
|
|
}
|
|
|
|
public IEnumerable<Model.admin> Get(int page, int pageSize = 10,
|
|
string sortBy="", bool sortDesc=false)
|
|
{
|
|
var list = _db.admins.OrderBy(o=>o.num).ToPagedList(page, pageSize);
|
|
if (list == null) throw new HttpResponseException(HttpStatusCode.NotFound);
|
|
return list;
|
|
}
|
|
|
|
// GET api/<controller>/5
|
|
public Model.admin Get(int id)
|
|
{
|
|
var item = _db.admins.Where(q => q.num == id).FirstOrDefault();
|
|
//if (item == null) throw new HttpResponseException(HttpStatusCode.NotFound);
|
|
return item;
|
|
}
|
|
|
|
// POST api/<controller>
|
|
public void Post([FromBody] Model.admin item)
|
|
{
|
|
}
|
|
|
|
// PUT api/<controller>/5
|
|
public void Put(int id, [FromBody] Model.admin item)
|
|
{
|
|
}
|
|
|
|
// DELETE api/<controller>/5
|
|
public void Delete(int id)
|
|
{
|
|
var prod = _db.admins.Where(q => q.num == id).FirstOrDefault(); //刪除該筆資料
|
|
if (prod != null)
|
|
{
|
|
|
|
_db.admins.Remove(prod);
|
|
_db.SaveChanges();//執行
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
[HttpGet]
|
|
[Route("api/adminUser/count")]
|
|
public int Count()
|
|
{
|
|
var count = _db.admins.Count();
|
|
return count;
|
|
}
|
|
[HttpPost]
|
|
[Route("api/adminUser/GetList")]
|
|
public IHttpActionResult GetList([FromBody] Model.ViewModel.admin q, int page, int pageSize = 10,
|
|
string sortBy = "", bool sortDesc = false)
|
|
{
|
|
var qry = _db.admins.AsQueryable();
|
|
|
|
if (!string.IsNullOrEmpty(q.u_id))
|
|
qry = qry.Where(o => o.u_id.Contains(q.u_id));
|
|
|
|
if (!string.IsNullOrEmpty(q.u_name))
|
|
qry = qry.Where(o => o.u_name.Contains(q.u_name));
|
|
|
|
|
|
if (!string.IsNullOrEmpty(q.power))
|
|
qry = qry.Where(o => o.power == q.power);
|
|
|
|
if(q.removeExist.HasValue && q.removeExist.Value)
|
|
{
|
|
var existingAdminNums = _db.members.Select(b => b.admin_num).ToList();
|
|
if (q.num.HasValue && q.num.Value > 0)
|
|
{
|
|
qry = qry.Where(o => !existingAdminNums.Contains(o.num) || o.num == q.num.Value);
|
|
}
|
|
else
|
|
{
|
|
qry = qry.Where(o => !existingAdminNums.Contains(o.num));
|
|
}
|
|
}
|
|
|
|
if (sortBy.Equals("u_id"))
|
|
{
|
|
if (sortDesc)
|
|
qry = qry.OrderByDescending(o => o.u_id);
|
|
else
|
|
qry = qry.OrderBy(o => o.u_id);
|
|
}
|
|
else
|
|
qry = qry.OrderByDescending(o => o.num);
|
|
|
|
var count = qry.Count();
|
|
var qryList = (pageSize > 0) ? qry.ToPagedList(page, pageSize).ToList() : qry.ToList();
|
|
|
|
var ret = new
|
|
{
|
|
list = qryList.Select(x => new
|
|
{
|
|
num = x.num,
|
|
u_id = x.u_id,
|
|
u_name = x.u_name,
|
|
power = x.power,
|
|
|
|
}),
|
|
count = count
|
|
|
|
};
|
|
|
|
|
|
if (ret.list == null) throw new HttpResponseException(HttpStatusCode.NotFound);
|
|
return Ok(ret);
|
|
}
|
|
|
|
|
|
|
|
}
|