348 lines
11 KiB
C#
348 lines
11 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;
|
|
using static TreeView;
|
|
using DocumentFormat.OpenXml.Office2010.Excel;
|
|
|
|
[ezAuthorize]
|
|
public class newsController : BaseApiController
|
|
{
|
|
// GET api/<controller>
|
|
public IEnumerable<Model.news> Get()
|
|
{
|
|
var list = _db.news.ToList();
|
|
if (list == null) throw new HttpResponseException(HttpStatusCode.NotFound);
|
|
return list;
|
|
}
|
|
|
|
// GET api/<controller>/5
|
|
public Model.news Get(int id)
|
|
{
|
|
var item = _db.news.Where(q => q.num == id).FirstOrDefault();
|
|
if (item == null) throw new HttpResponseException(HttpStatusCode.NotFound);
|
|
return item;
|
|
}
|
|
|
|
// GET api/<controller>/5
|
|
public IEnumerable<Model.news> GetPage(int page)
|
|
{
|
|
var news = _db.news.Where(q => q.num < 10).ToList();
|
|
return news;
|
|
}
|
|
|
|
// POST api/<controller>
|
|
public void Post([FromBody] string value)
|
|
{
|
|
}
|
|
|
|
// PUT api/<controller>/5
|
|
public void Put(int id, [FromBody] string value)
|
|
{
|
|
}
|
|
|
|
// DELETE api/<controller>/5
|
|
public void Delete(int id)
|
|
{
|
|
var prod = _db.news.AsEnumerable().Where(q => q.num == id).FirstOrDefault(); //刪除該筆資料
|
|
if (prod != null)
|
|
{
|
|
//刪除檔案
|
|
var prod2 = _db.news_files.AsEnumerable().Where(q => q.news_id == id).ToList();
|
|
if (prod2.Count > 0)
|
|
{
|
|
foreach (var item in prod2)
|
|
{
|
|
if (!string.IsNullOrEmpty(item.pic1))
|
|
{
|
|
//api裡不可以用MyWeb
|
|
publicFun publicFun = new publicFun();
|
|
publicFun.DeleteFile(Model.news.Dir + "/" + item.pic1);
|
|
}
|
|
}
|
|
|
|
//查詢結果全部刪除
|
|
_db.news_files.RemoveRange(prod2);
|
|
}
|
|
|
|
_db.news.Remove(prod);
|
|
_db.SaveChanges(); //執行
|
|
|
|
Model.admin_log admin_log = new Model.admin_log();
|
|
MyWeb.admin admin = new MyWeb.admin();//api裡不可以用MyWeb
|
|
if (admin.isLoign())
|
|
{
|
|
admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.News, (int)Model.admin_log.Status.Delete, prod.subject);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
[HttpDelete]
|
|
[Route("api/news/Delete/{nums}")]
|
|
public void Delete(string nums)
|
|
{
|
|
if (!string.IsNullOrEmpty(nums))
|
|
{
|
|
var getDelItem = nums.TrimEnd(',').Split(',').Select(s => int.Parse(s));
|
|
|
|
var prod = _db.news.AsEnumerable().Where(q => getDelItem.Contains(q.num)).ToList();
|
|
if (prod.Count() > 0)
|
|
{
|
|
var prod2 = _db.news_files.AsEnumerable().Where(q => getDelItem.Contains(q.news_id)).ToList();
|
|
if (prod2.Count > 0)
|
|
{
|
|
publicFun publicFun = new publicFun();
|
|
foreach (var item in prod2)
|
|
{
|
|
if (!string.IsNullOrEmpty(item.pic1))
|
|
{
|
|
//api裡不可以用MyWeb
|
|
//MyWeb.fileSystem fileSystem = new MyWeb.fileSystem();
|
|
//fileSystem.Delete(Model.news.Dir + "/" + item.pic1);
|
|
publicFun.DeleteFile(Model.news.Dir + "/" + item.pic1);
|
|
}
|
|
}
|
|
|
|
//查詢結果全部刪除
|
|
_db.news_files.RemoveRange(prod2);
|
|
}
|
|
|
|
_db.news.RemoveRange(prod);
|
|
_db.SaveChanges();
|
|
|
|
|
|
Model.admin_log admin_log = new Model.admin_log();
|
|
MyWeb.admin admin = new MyWeb.admin();//api裡不可以用MyWeb
|
|
if (admin.isLoign())
|
|
{
|
|
//List<string> x = new List<string>();
|
|
//prod.ForEach(item =>
|
|
//{
|
|
// x.Add(item.subject);
|
|
//});
|
|
admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.News, (int)Model.admin_log.Status.Delete, admin_log.LogViewBtn(prod.Select(x=>x.subject).ToList()));
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
[HttpPost]
|
|
[Route("api/news/GetList")]
|
|
public IHttpActionResult GetList([FromBody] Model.ViewModel.news q, int page, int pageSize = 10,
|
|
string sortBy = "", bool sortDesc = false, bool top = false)
|
|
{
|
|
var qry = _db.news.AsEnumerable();
|
|
|
|
if (!string.IsNullOrEmpty(q.subject))
|
|
qry = qry.Where(o => o.subject.Contains(q.subject.Trim()));
|
|
|
|
if (q.kind.HasValue)//搜尋要包含其分類底下所有分類
|
|
{
|
|
var _subKinds = new TreeView().subKinds(_db.news_kind.Select(o => new TreeItem()
|
|
{
|
|
num = o.num,
|
|
root = o.root,
|
|
}).ToList(), q.kind.Value);
|
|
|
|
qry = qry.Where(o => o.kind == q.kind.Value || _subKinds.Any(s => s == o.kind));
|
|
}
|
|
|
|
if (q.selltime1.HasValue)
|
|
qry = qry.Where(o => o.selltime1 >= q.selltime1.Value);
|
|
if (q.selltime2.HasValue)
|
|
qry = qry.Where(o => o.selltime2 < Convert.ToDateTime(q.selltime2.Value).AddDays(1));
|
|
if (q.uptime1.HasValue)
|
|
qry = qry.Where(o => o.uptime >= q.uptime1.Value);
|
|
if (q.uptime2.HasValue)
|
|
qry = qry.Where(o => o.uptime < Convert.ToDateTime(q.uptime2.Value).AddDays(1));
|
|
if (q.activity_num.HasValue)
|
|
qry = qry.Where(o => o.activity_num == q.activity_num.Value);
|
|
if (!string.IsNullOrEmpty(q.activity_numTxt))
|
|
qry = qry.Where(o => o.activity_num.HasValue && o.activity.subject.Contains(q.activity_numTxt.Trim()));
|
|
|
|
if (!string.IsNullOrEmpty(q.status))
|
|
qry = qry.Where(o => o.status==q.status);
|
|
|
|
if (!top)
|
|
{
|
|
if (sortBy.Equals("subject"))
|
|
{
|
|
if (sortDesc)
|
|
qry = qry.OrderByDescending(o => o.subject);
|
|
else
|
|
qry = qry.OrderBy(o => o.subject);
|
|
}
|
|
else if (sortBy.Equals("kind"))
|
|
{
|
|
if (sortDesc)
|
|
qry = qry.OrderByDescending(o => o.kind);
|
|
else
|
|
qry = qry.OrderBy(o => o.kind);
|
|
}
|
|
else if (sortBy.Equals("status"))
|
|
{
|
|
if (sortDesc)
|
|
qry = qry.OrderByDescending(o => o.status);
|
|
else
|
|
qry = qry.OrderBy(o => o.status);
|
|
}
|
|
else if (sortBy.Equals("author"))
|
|
{
|
|
if (sortDesc)
|
|
qry = qry.OrderByDescending(o => o.author);
|
|
else
|
|
qry = qry.OrderBy(o => o.author);
|
|
}
|
|
else if (sortBy.Equals("selltime1"))
|
|
{
|
|
if (sortDesc)
|
|
qry = qry.OrderByDescending(o => o.selltime1);
|
|
else
|
|
qry = qry.OrderBy(o => o.selltime1);
|
|
}
|
|
else if (sortBy.Equals("selltime2"))
|
|
{
|
|
if (sortDesc)
|
|
qry = qry.OrderByDescending(o => o.selltime2);
|
|
else
|
|
qry = qry.OrderBy(o => o.selltime2);
|
|
}
|
|
else if (sortBy.Equals("uptime"))
|
|
{
|
|
if (sortDesc)
|
|
qry = qry.OrderByDescending(o => o.uptime);
|
|
else
|
|
qry = qry.OrderBy(o => o.uptime);
|
|
}
|
|
else if (sortBy.Equals("topping"))
|
|
{
|
|
if (sortDesc)
|
|
qry = qry.OrderBy(o => o.topping);
|
|
else
|
|
qry = qry.OrderByDescending(o => o.topping);
|
|
}
|
|
else
|
|
qry = qry.OrderByDescending(o => o.num);
|
|
}
|
|
else
|
|
{
|
|
if (sortBy.Equals("subject"))
|
|
{
|
|
if (sortDesc)
|
|
qry = qry.OrderByDescending(o => o.topping).ThenByDescending(o => o.subject);
|
|
else
|
|
qry = qry.OrderByDescending(o => o.topping).ThenBy(o => o.subject);
|
|
}
|
|
else if (sortBy.Equals("kind"))
|
|
{
|
|
if (sortDesc)
|
|
qry = qry.OrderByDescending(o => o.topping).ThenByDescending(o => o.kind);
|
|
else
|
|
qry = qry.OrderByDescending(o => o.topping).ThenBy(o => o.kind);
|
|
}
|
|
else if (sortBy.Equals("uptime"))
|
|
{
|
|
if (sortDesc)
|
|
qry = qry.OrderByDescending(o => o.topping).ThenByDescending(o => o.uptime);
|
|
else
|
|
qry = qry.OrderByDescending(o => o.topping).ThenBy(o => o.uptime);
|
|
}
|
|
else
|
|
qry = qry.OrderByDescending(o => o.topping).ThenByDescending(o => o.num);
|
|
}
|
|
|
|
var ret = new
|
|
{
|
|
list = qry.ToPagedList(page, pageSize).Select(x => new
|
|
{
|
|
num = x.num,
|
|
subject = x.subject,
|
|
kind = x.kind,
|
|
kindTxt = x.kind.HasValue? x.news_kind.kind :"",
|
|
kindsTxt = x.kind.HasValue ? new TreeView().kindText(_db.news_kind.Select(o => new TreeItem()
|
|
{
|
|
kind = o.kind,
|
|
num = o.num,
|
|
root = o.root,
|
|
}).ToList(), x.kind ) :"",
|
|
status = x.status,
|
|
author = x.author,
|
|
authorName = x.author.HasValue? x.admin.u_name :"",
|
|
selltime1 = x.selltime1,
|
|
selltime2 = x.selltime2,
|
|
uptime = x.uptime,
|
|
topping = x.topping
|
|
|
|
}),
|
|
count = qry.Count()
|
|
|
|
};
|
|
|
|
|
|
if (ret.list == null) throw new HttpResponseException(HttpStatusCode.NotFound);
|
|
return Ok(ret);
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("api/news/GetKindList")]
|
|
public IHttpActionResult GetKindList([FromBody] Model.ViewModel.news_kind q, int page, int pageSize = 10,
|
|
string sortBy = "", bool sortDesc = false)
|
|
{
|
|
|
|
|
|
var qry = _db.news_kind.AsEnumerable();
|
|
|
|
if (!string.IsNullOrEmpty(q.kind))
|
|
qry = qry.Where(o => o.kind.Contains(q.kind));
|
|
|
|
|
|
if (q.inTime)
|
|
qry = qry.Where(o => o.status=="Y");
|
|
|
|
|
|
//qry = qry.OrderBy(o=>o.root).ThenBy(o => o.kind);
|
|
|
|
|
|
var qry2 = new TreeView().get_data2(qry.Select(o => new TreeItem()
|
|
{
|
|
kind = o.kind,
|
|
num = o.num,
|
|
root = o.root,
|
|
range = o.range,
|
|
}).OrderBy(x => x.root).ThenBy(x => x.kind).ToList(), 0, 0);
|
|
|
|
|
|
|
|
var ret = new
|
|
{
|
|
list = qry2.ToPagedList(page, pageSize).Select(x => new
|
|
{
|
|
num = x.num,
|
|
kind = new TreeView().RptDash(x.Level) + x.kind,
|
|
|
|
}),
|
|
count = qry.Count()
|
|
};
|
|
|
|
|
|
if (ret.list == null) throw new HttpResponseException(HttpStatusCode.NotFound);
|
|
return Ok(ret);
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
http://localhost:53442/api/news
|
|
http://localhost:53442/api/news?id=1
|
|
http://localhost:53442/api/news/1
|
|
http://localhost:53442/api/news/2
|
|
|
|
*/ |