using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; using Chruch.Net.Controllers; using Church.Net.DAL.EF; using Church.Net.Entity; namespace Chruch.Net.Areas.dashboard.Controllers { public class WhoIsSpiesController : Controller { private ChurchNetContext db = new ChurchNetContext(); // GET: dashboard/WhoIsSpies public ActionResult Index() { return View(db.WhoIsSpy.ToList()); } // GET: dashboard/WhoIsSpies/Details/5 public ActionResult Details(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } WhoIsSpy whoIsSpy = db.WhoIsSpy.Find(id); if (whoIsSpy == null) { return HttpNotFound(); } return View(whoIsSpy); } // GET: dashboard/WhoIsSpies/Create public ActionResult Create() { return View(); } // POST: dashboard/WhoIsSpies/Create // 若要免於過量張貼攻擊,請啟用想要繫結的特定屬性,如需 // 詳細資訊,請參閱 https://go.microsoft.com/fwlink/?LinkId=317598。 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(WhoIsSpy whoIsSpy) { if (ModelState.IsValid) { db.WhoIsSpy.Add(whoIsSpy); db.SaveChanges(); SaveImageFromRequest("Answer1Image", Server.MapPath("~/Images/IceBreak/WhoIsSpy/"), whoIsSpy.Answer1Image); SaveImageFromRequest("Answer2Image", Server.MapPath("~/Images/IceBreak/WhoIsSpy/"), whoIsSpy.Answer2Image); return RedirectToAction("Index"); } return View(whoIsSpy); } // GET: dashboard/WhoIsSpies/Edit/5 public ActionResult Edit(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } WhoIsSpy whoIsSpy = db.WhoIsSpy.Find(id); if (whoIsSpy == null) { return HttpNotFound(); } return View(whoIsSpy); } // POST: dashboard/WhoIsSpies/Edit/5 // 若要免於過量張貼攻擊,請啟用想要繫結的特定屬性,如需 // 詳細資訊,請參閱 https://go.microsoft.com/fwlink/?LinkId=317598。 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([Bind(Include = "Id,Answer1Cht,Answer1Chs,Answer1En,Answer2Cht,Answer2Chs,Answer2En,Answer1Image,Answer2Image")] WhoIsSpy whoIsSpy) { if (ModelState.IsValid) { db.Entry(whoIsSpy).State = EntityState.Modified; db.SaveChanges(); SaveImageFromRequest("Answer1Image", Server.MapPath("~/Images/IceBreak/WhoIsSpy/"), whoIsSpy.Answer1Image); SaveImageFromRequest("Answer2Image", Server.MapPath("~/Images/IceBreak/WhoIsSpy/"), whoIsSpy.Answer2Image); return RedirectToAction("Index"); } return View(whoIsSpy); } // GET: dashboard/WhoIsSpies/Delete/5 public ActionResult Delete(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } WhoIsSpy whoIsSpy = db.WhoIsSpy.Find(id); if (whoIsSpy == null) { return HttpNotFound(); } return View(whoIsSpy); } // POST: dashboard/WhoIsSpies/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(int id) { WhoIsSpy whoIsSpy = db.WhoIsSpy.Find(id); db.WhoIsSpy.Remove(whoIsSpy); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } private void SaveImageFromRequest(string requestParameterName, string filePath,string newFileName) { var file = HttpContext.Request.Files[requestParameterName]; bool hasFile = file != null && !string.IsNullOrEmpty(file.FileName); //string newFileName = hasFile ? $"{model.Id}{file.FileName.Substring(file.FileName.LastIndexOf("."))}" : ""; Directory.CreateDirectory(filePath); if (hasFile) { Church.Net.Utility.Image.SaveThumbnail(new Bitmap(file.InputStream), filePath + "\\" + newFileName, 200, 200); } } } }