첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ TestDB.sql

CREATE TABLE dbo.Task
(
    ID           INT           NOT NULL PRIMARY KEY IDENTITY(1, 1) -- ID
   ,Title        NVARCHAR(140) NOT NULL                            -- 제목
   ,IsCompleted  BIT                                               -- 완료 여부
   ,CreateDate   DATETIME      DEFAULT(GETDATE())                  -- 생성일
)
GO

 

▶ TaskModel.cs

using System;

namespace TestProject
{
    /// <summary>
    /// 작업 모델
    /// </summary>
    public class TaskModel
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region ID - ID

        /// <summary>
        /// ID
        /// </summary>
        public int ID { get; set; }

        #endregion
        #region 제목 - Title

        /// <summary>
        /// 제목
        /// </summary>
        public string Title { get; set; }

        #endregion
        #region 완료 여부 - IsCompleted

        /// <summary>
        /// 완료 여부
        /// </summary>
        public bool IsCompleted { get; set; }

        #endregion
        #region 생성일 - CreateDate

        /// <summary>
        /// 생성일
        /// </summary>
        public DateTime CreateDate { get; set; }

        #endregion
    }
}

 

▶ TaskRepository.cs

using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

using Dapper;

namespace TestProject
{
    /// <summary>
    /// 작업 저장소
    /// </summary>
    public class TaskRepository
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// DB 연결
        /// </summary>
        private IDbConnection connection = new SqlConnection
        (
            ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString
        );

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 추가하기 - Add(task)

        /// <summary>
        /// 추가하기
        /// </summary>
        /// <param name="task">작업</param>
        public void Add(TaskModel task)
        {
            string sql = @"
INSERT INTO dbo.Task
(
    Title
   ,IsCompleted
)
VALUES
(
    @Title
   ,CAST('false' AS BIT)
)
";

            this.connection.Execute(sql, task);
        }

        #endregion
        #region 리스트 구하기 - GetList()

        /// <summary>
        /// 리스트 구하기
        /// </summary>
        /// <returns>리스트</returns>
        public List<TaskModel> GetList()
        {
            return this.connection.Query<TaskModel>("SELECT * FROM dbo.Task ORDER BY ID DESC").ToList();
        }

        #endregion
        #region 작업 완료하기 - CompleteTask(id)

        /// <summary>
        /// 작업 완료하기
        /// </summary>
        /// <param name="id">ID</param>
        public void CompleteTask(int id)
        {
            this.connection.Execute("UPDATE dbo.Task SET IsCompleted = ~IsCompleted WHERE ID = @ID", new { id });
        }

        #endregion
    }
}

 

▶ TaskController.cs

using System.Web.Mvc;

namespace TestProject.Controllers
{
    /// <summary>
    /// 작업 컨트롤러
    /// </summary>
    public class TaskController : Controller
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 태스크 저장소
        /// </summary>
        private TaskRepository repository = new TaskRepository();

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 인덱스 페이지 처리하기 - Index()

        /// <summary>
        /// 인덱스 페이지 처리하기
        /// </summary>
        /// <returns>액션 결과</returns>
        public ActionResult Index()
        {
            var resultList = this.repository.GetList();

            return View(resultList);
        }

        #endregion
        #region 생성 페이지 처리하기 - Create()

        /// <summary>
        /// 생성 페이지 처리하기
        /// </summary>
        /// <returns>액션 결과</returns>
        public ActionResult Create()
        {
            return View();
        }

        #endregion
        #region 생성 처리 페이지 처리하기 - ProcessCreate(title)

        /// <summary>
        /// 생성 처리 페이지 처리하기
        /// </summary>
        /// <param name="title">제목</param>
        /// <returns>액션 결과</returns>
        [HttpPost]
        public ActionResult ProcessCreate(string title)
        {
            this.repository.Add(new TaskModel { Title = title });

            return RedirectToAction("Index");
        }

        #endregion
        #region 작업 완료 페이지 처리하기 - CompleteTask(id)

        /// <summary>
        /// 작업 완료 페이지 처리하기
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>액션 결과</returns>
        public ActionResult CompleteTask(int id)
        {
            this.repository.CompleteTask(id);

            return RedirectToAction("Index");
        }

        #endregion
    }
}

 

▶ Create.cshtml

@{
    ViewBag.Title = "Create";
}
<h2>작업 등록</h2>
<form action="/Task/ProcessCreate" method="post">
    제목 : <input type="text" name="title" value="" /><br />
    <input type="submit" value="등록" />
</form>

 

▶ Index.cshtml

@model List<TestProject.TaskModel>
@{
    ViewBag.Title = "Index";
}
<h2>작업 리스트</h2>
<table class="table table-bordered table-condensed table-striped table-hover">
    <tr>
        <th>ID</th>
        <th>제목</th>
        <th>생성일</th>
        <th>완료</th>
    </tr>
    @foreach(TaskModel task in Model)
    {
        <tr>
            <td>@task.ID</td>
            <td>
                @if(@task.IsCompleted)
                {
                    <s>@task.Title</s>
                }
                else
                {
                    <text>@task.Title</text>
                }
            </td>
            <td>@task.CreateDate.ToShortDateString()</td>
            <td>
                <a href="/Task/CompleteTask/@task.ID.ToString()">
                    @if(task.IsCompleted)
                    {
                        <text>완료 취소하기</text>
                    }
                    else
                    {
                        @:완료하기
                    }
                </a>
            </td>
        </tr>
    }
</table>
<a href="/Task/Create">등록</a>
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요