iT邦幫忙

2022 iThome 鐵人賽

DAY 5
0

文章物件(Article class)像是這個部落格系統的核心,所以要先設計好物件裡面的屬性(property)之後,才知道說要上傳甚麼欄位,資料庫要儲存甚麼欄位,再思考是否要新增刪除修改等等功能。

目標:
1.建立Article物件
2.建立ArticleController (會先把完成的程式碼附上去,會出錯的地方可以先註解)

步驟:
1.建立物件 -> Article
https://ithelp.ithome.com.tw/upload/images/20220916/201258539yegRuMgzO.png

namespace EFBlog.Models
{
    public class Article
    {
        public long Id { get; set; }

        public string Title { get; set; } = string.Empty;

        public string ArticleContent { get; set; } = string.Empty;

        public bool IsDelete { get; set; }
    }
}

2.建立Controller 分別是新增、查詢、編輯。以下就是目前會用到的Controller。

namespace EFBlog.Controllers
{
    public class ArticleController : Controller
    {
        private readonly IArticleService _article;

        public ArticleController(IArticleService articleService)
        {
            _article = articleService;
        }

        [HttpGet("Article/{id}")]
        public async Task<IActionResult> Index(long id)
        {
            var model = await _article.GetArticle(id);
            var result = new ArticleViewModel();

            if (model is not null && model.Count > 0)
            {
                result = model.Select(x => new ArticleViewModel
                {
                    Id = x.Id,
                    ArticleContent = x.ArticleContent,
                    Title = x.Title
                }).First();

                return View(result);
            }

            return Redirect("/");
        }

        [Authorize]
        [HttpGet("CreateArticle")]
        public IActionResult CreateArticle()
        {
            return View();
        }

        [Authorize]
        [HttpPost("CreateArticle")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> CreateArticle(string Content)
        {
            await _article.CreateArticle(Content);
            return Redirect("/");
        }

        [HttpGet("GetArticleList")]
        public async Task<IActionResult> GetArticleList()
        {
            var model = await _article.GetUpdateArticle(null);
            var result = new List<UpdateArticleViewModel>();

            if (model is not null && model.Count > 0)
            {
                result = model.Select(x => new UpdateArticleViewModel
                {
                    Id = x.Id,
                    ArticleContent = x.ArticleContent,
                    Title = x.Title,
                    IsDelete = x.IsDelete
                }).ToList();
            }

            return View(result);
        }

        [HttpGet("GetArticle/{id}")]
        public async Task<IActionResult> GetArticle(long id)
        {
            var model = await _article.GetUpdateArticle(id);
            var result = new UpdateArticleViewModel();

            if (model is not null && model.Count > 0)
            {
                result = model.Select(x => new UpdateArticleViewModel
                {
                    Id = x.Id,
                    ArticleContent = x.ArticleContent,
                    Title = x.Title,
                    IsDelete = x.IsDelete
                }).First();

                return View(result);
            }

            return Redirect("/");
        }


        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> UpdateArticle(UpdateArticleViewModel model)
        {
            await _article.UpdateArticle(model);
            return RedirectToAction($"GetArticleList");
        }


        [Authorize]
        public async Task<IActionResult> Uploads(IFormFile upload)
        {
            var obj = await _article.UploadImage(upload);

            return Json(new
            {
                uploaded = obj.Uploaded,
                fileName = obj.FileName,
                url = obj.Url,
                error = new
                {
                    message = obj.Msg
                }
            });
        }
    }
}

筆記:
習慣上,我會先建好架構,會先寫好會用到那些功能,建立文章為例,我們一定會需要新增文章、編輯文章、查看文章、刪除文章(不一定),所以就先建立這些Controller。

刪除文章不一定的原因是,有時候設計上雖然是刪除文章,但實際上只是修改資料狀態而已,像是IsDelete這欄位就是註記,是否刪除文章。


上一篇
[Day 4] 簡單錯誤處理 Middleware 擷取錯誤(二)
下一篇
[Day 6] 建立發文頁面 View 及 asp.net core mvc 傳直到View的方法
系列文
Asp.Net Core 零基礎建立自己的Blog30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言