diff --git a/Bremen_ESG.csproj b/Bremen_ESG.csproj index 32ab90d..f37e981 100644 --- a/Bremen_ESG.csproj +++ b/Bremen_ESG.csproj @@ -19,6 +19,7 @@ + diff --git a/Controllers/ApiController.cs b/Controllers/ApiController.cs index ad1175a..522539b 100644 --- a/Controllers/ApiController.cs +++ b/Controllers/ApiController.cs @@ -49,12 +49,34 @@ namespace Bremen_ESG.Controllers private readonly IHttpContextAccessor _httpContextAccessor; private readonly IWebHostEnvironment _hostingEnvironment; + DbConn dbConn = new DbConn(); + SqlConnection conn = new SqlConnection(GlobalClass.appsettings("ConnectionStrings:SQLConnectionString")); + public ApiController(IHttpContextAccessor httpContextAccessor, IWebHostEnvironment webHostEnvironment) { this._httpContextAccessor = httpContextAccessor; this._hostingEnvironment = webHostEnvironment; } + + [Route("news_list")] + public async Task News_List(IFormCollection obj) { + newResult ret = new newResult(); + + List newsList = conn.Query("select * from news order by news_sn desc").ToList(); + + ret.news_num = newsList.Count; + + foreach (news objNew in newsList) + { + newsDetial objDetial = new newsDetial(objNew); + ret.news_list.Add(objDetial); + } + + ret.ret = "yes"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + [HttpPost] [Route("esg_message")] public async Task Esg_Message(IFormCollection obj) { @@ -116,6 +138,15 @@ namespace Bremen_ESG.Controllers return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } + public class newResult + { + public string ret = "no"; + public string err_code = "0000"; + public string message = ""; + public int news_num = 0; + public List news_list = new List(); + } + public class result { public string ret = "no"; diff --git a/Controllers/BackEndApiController.cs b/Controllers/BackEndApiController.cs index ba227a4..2447120 100644 --- a/Controllers/BackEndApiController.cs +++ b/Controllers/BackEndApiController.cs @@ -21,6 +21,252 @@ namespace Bremen_ESG.Controllers this._httpContextAccessor = httpContextAccessor; } + [Route("newsAddEditDelGet")] + public ActionResult NewsAddEditDelGet(IFormCollection obj) { + newDetialResult ret = new newDetialResult(); + + authToken token = new authToken(this._httpContextAccessor); + + if (token.user_isLogin == false) + { + HttpContext.Response.Cookies.Delete("token_key"); + ret.ret = "no"; + ret.err_code = "9999"; + ret.message = "非登入狀態!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + DbConn dbConn = new DbConn(); + SqlConnection conn = dbConn.sqlConnection(); + + string news_uid = obj["news_uid"].ToString(); + string news_title = obj["news_title"].ToString(); + string news_subtitle = obj["news_subtitle"].ToString(); + string news_date = obj["news_date"].ToString(); + string news_mainPhoto = obj["news_mainPhoto"].ToString(); + string news_content = obj["news_content"].ToString(); + string TagsStr = obj["news_tags"].ToString().TrimEnd(','); + string photoArrayJson = obj["photoArrayJson"].ToString().TrimEnd(','); + + string method = obj["method"].ToString(); + + + + if (method == "get") + { + news newObj = conn.QueryFirstOrDefault("select * from news where news_uid = @news_uid", new { news_uid = news_uid }); + + if (newObj == null) { + ret.ret = "no"; + ret.err_code = "1009"; + ret.message = "無此news_uid資料!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + ret.data = new newsDetial(newObj); + ret.ret = "yes"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + if (method == "") + { + ret.ret = "no"; + ret.err_code = "0001"; + ret.message = "無method參數!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + if (method == "del") { + conn.Execute("delete photo where news_uid = @news_uid", new { news_uid = news_uid }); + conn.Execute("delete tag where news_uid = @news_uid", new { news_uid = news_uid }); + conn.Execute("delete news where news_uid = @news_uid", new { news_uid = news_uid }); + + ret.ret = "yes"; + + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + string err_msg = ""; + + if (news_title == "") + { + err_msg += "無標題!\n"; + } + + if (news_subtitle == "") + { + err_msg += "無副標題\n"; + } + + if (news_content == "") + { + err_msg += "無內文\n"; + } + + if (news_date == "") + { + err_msg += "無發布日期\n"; + } + + if (err_msg != "") + { + ret.ret = "no"; + ret.err_code = "0001"; + ret.message = err_msg; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + if (method == "edit") { + if (news_uid == "") { + ret.ret = "no"; + ret.err_code = "0002"; + ret.message = "無 news_uid"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + news objNew = conn.QueryFirstOrDefault("select * from news where news_uid = @news_uid", new { news_uid = news_uid }); + + if (objNew == null) { + ret.ret = "no"; + ret.err_code = "0003"; + ret.message = "無此 news_uid資料"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + dynamic photoJsonObj; + + try + { + photoJsonObj = JsonConvert.DeserializeObject(photoArrayJson); + } + catch (Exception ex) + { + ret.ret = "no"; + ret.err_code = "0003"; + ret.message = "photo json error" + ex.Message; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + conn.Execute("delete tag where news_uid = @news_uid", new { news_uid = news_uid }); + conn.Execute("delete photo where news_uid = @news_uid", new { news_uid = news_uid }); + + string[] newsTagArr = TagsStr.Split(","); + List newsTags = new List(); + foreach (string tag in newsTagArr) + { + tags tagData = conn.QueryFirstOrDefault("select * from tags where tag_uid = @tag_uid", new { tag_uid = tag }); + + if (tagData != null) + { + tag newTag = new tag(); + newTag.tag_uid = tagData.tag_uid; + newTag.news_uid = news_uid; + newTag.tag_text = tagData.tag_text; + newsTags.Add(newTag); + } + } + + List photos = new List(); + + foreach (dynamic item in photoJsonObj) + { + photo photoObj = new photo(); + + photoObj.photo_uid = GlobalClass.CreateRandomCode(12); + photoObj.news_uid = news_uid; + photoObj.photo_title = item.photo_title; + photoObj.photo_path = item.photo_path; + + + photos.Add(photoObj); + } + + objNew.news_title = news_title; + objNew.news_date = news_date; + objNew.news_subtitle = news_subtitle; + objNew.news_content = news_content; + objNew.news_mainPhoto = news_mainPhoto; + objNew.news_modifydate = DateTime.Now; + + conn.Update(objNew); + conn.Insert(photos); + conn.Insert(newsTags); + + ret.ret = "yes"; + ret.data = new newsDetial(objNew); + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + if (method == "add") + { + news_uid = "news_" + GlobalClass.CreateRandomCode(8); + + string[] newsTagArr = TagsStr.Split(","); + List newsTags = new List(); + foreach (string tag in newsTagArr) + { + tags tagData = conn.QueryFirstOrDefault("select * from tags where tag_uid = @tag_uid", new { tag_uid = tag }); + + if (tagData != null) + { + tag newTag = new tag(); + newTag.tag_uid = tagData.tag_uid; + newTag.news_uid = news_uid; + newTag.tag_text = tagData.tag_text; + newsTags.Add(newTag); + } + } + + dynamic photoJsonObj; + + try + { + photoJsonObj = JsonConvert.DeserializeObject(photoArrayJson); + } + catch (Exception ex) + { + ret.ret = "no"; + ret.err_code = "0003"; + ret.message = "photo json error" + ex.Message; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + List photos = new List(); + + foreach (dynamic item in photoJsonObj) + { + photo photoObj = new photo(); + + photoObj.photo_uid = GlobalClass.CreateRandomCode(12); + photoObj.news_uid= news_uid; + photoObj.photo_title = item.photo_title; + photoObj.photo_path = item.photo_path; + + + photos.Add(photoObj); + } + + news objNew = new news(); + + objNew.news_uid = news_uid; + objNew.news_title = news_title; + objNew.news_date = news_date; + objNew.news_subtitle = news_subtitle; + objNew.news_content = news_content; + objNew.news_mainPhoto = news_mainPhoto; + + conn.Insert(objNew); + conn.Insert(photos); + conn.Insert(newsTags); + + ret.ret = "yes"; + ret.data = new newsDetial(objNew); + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + [Route("updateTags")] public ActionResult UpdateTags(IFormCollection obj) { @@ -96,6 +342,84 @@ namespace Bremen_ESG.Controllers return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } + [Route("subPhotoUpload")] + [RequestFormLimits(MultipartBodyLengthLimit = int.MaxValue)] + [RequestSizeLimit(int.MaxValue)] + public ActionResult SubPhotoUpload([FromForm(Name = "subPhoto")] IFormFile file) + { + authToken token = new authToken(this._httpContextAccessor); + if (token.user_isLogin == false) + { + List files = new List(); + + errFile newFile = new errFile(); + newFile.name = ""; + newFile.size = 0; + newFile.error = "尚未登入"; + + files.Add(newFile); + + fileResult obj = new fileResult(); + + obj.files = files; + + return Content(JsonConvert.SerializeObject(files), "application/json;charset=utf-8"); + } + + + string originFileName = file.FileName; + string newFileName = "subPhoto_" + GlobalClass.CreateRandomCode(8) + Path.GetExtension(originFileName); + string fullPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/upload/sub/" + newFileName); + try + { + using (var stream = new FileStream(fullPath, FileMode.Create)) + { + file.CopyTo(stream); + } + + List files = new List(); + + uploadFile newFile = new uploadFile(); + + newFile.name = originFileName; + newFile.url = "/upload/sub/" + newFileName; + newFile.size = file.Length; + newFile.thumbnailUrl = "/upload/sub/" + newFileName; + newFile.deleteUrl = "/upload/sub/" + newFileName; + + + + files.Add(newFile); + + fileResult obj = new fileResult(); + + obj.files = files; + + + + + + return Content(JsonConvert.SerializeObject(obj), "application/json;charset=utf-8"); + } + catch (Exception ex) + { + List files = new List(); + + errFile newFile = new errFile(); + newFile.name = originFileName; + newFile.size = file.Length; + newFile.error = ex.Message; + + files.Add(newFile); + + fileResult obj = new fileResult(); + + obj.files = files; + + return Content(JsonConvert.SerializeObject(files), "application/json;charset=utf-8"); + } + } + [Route("mainPhotoUpload")] [RequestFormLimits(MultipartBodyLengthLimit = int.MaxValue)] [RequestSizeLimit(int.MaxValue)] @@ -250,6 +574,14 @@ namespace Bremen_ESG.Controllers } + public class newDetialResult + { + public string ret = "no"; + public string err_code = "0000"; + public string message = ""; + public newsDetial data = new newsDetial(); + } + public class fileResult { public object files = new object(); diff --git a/Models/newsDetial.cs b/Models/newsDetial.cs new file mode 100644 index 0000000..7f17503 --- /dev/null +++ b/Models/newsDetial.cs @@ -0,0 +1,33 @@ +using Dapper; +using Dapper.Contrib.Extensions; +using NPOI.SS.Formula.Functions; +using System.Data.SqlClient; +using static DbTableClass; + +public class newsDetial: news +{ + DbConn dbConn = new DbConn(); + SqlConnection conn = new SqlConnection(GlobalClass.appsettings("ConnectionStrings:SQLConnectionString")); + + public List tags = new List(); + public List photos = new List(); + public newsDetial() { + + } + + public newsDetial(news obj) { + Type newsType = obj.GetType(); + + foreach (var prop in newsType.GetProperties()) + { + string propName = prop.Name; + var valueProperty = newsType.GetProperty(propName); + object propValue = valueProperty.GetValue(obj, null); + + this.GetType().GetProperty(propName).SetValue(this, propValue); + } + + tags = conn.Query("select * from tag where news_uid = @news_uid", new { news_uid = this.news_uid }).ToList(); + photos = conn.Query("select * from photo where news_uid = @news_uid", new { news_uid = this.news_uid }).ToList(); + } +} diff --git a/Views/BackEnd/NewsList.cshtml b/Views/BackEnd/NewsList.cshtml index 7c71574..c8b3750 100644 --- a/Views/BackEnd/NewsList.cshtml +++ b/Views/BackEnd/NewsList.cshtml @@ -8,6 +8,7 @@ + @@ -94,7 +95,7 @@
-
Change photo
+
Change photo

主圖片

@@ -115,8 +116,12 @@
- - + + +
+ +
+
@@ -160,7 +165,59 @@
+ + + + + + +
+