ASP.NET API 2


API! API!

この間作ったこれが↓味気なさすぎるのでもうちょっとやってみる。
https://qiita.com/manipulative/items/42743a176ab38bb5d16f

フォルダ構成を簡略的に示すとこんな感じ。

ReactToDoList.sln
├─DataBase.csproj***********************(データベースにアクセス)
│  │  App.config
│  │  ToDoListContext.cs
│  ├─Migrations
│  │      201805241946002_init.cs
│  └─Models
│        ToDo.cs
├─ReactToDoList.csproj******************(API)
│  │  Web.config
│  ├─Controllers
│  │      ReactToDoListController.cs
│  └─Models
│        ListModel.cs
└─ToDoList.csproj***********************(ビジネスロジック)
      ToDoList.cs
ReactToDoListController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Results;
using System.Web.UI.WebControls;
using ReactToDoList.Models;
using ToDoList;

namespace ReactToDoList.Controllers
{

    public class ReactToDoListController : ApiController
    {
        [HttpGet]
        public IHttpActionResult GetToDos()
        {
            //さすがにハイジャックはされたくなのでむき出しの配列では返さない。
            var toDoList = new ToDoList.ToDoList().GetToDos()
                .Select(t => new ToDo { id = t.Id, title = t.Title, content = t.Content})
                .ToList();

            return Json(new ListModel {UserList = toDoList }); 
        }

        [HttpPost]
        public IHttpActionResult Add([FromBody]ToDoRequest model)
        {
            var toDomanager = new ToDoList.ToDoList();
            toDomanager.AddToDo(model.title, model.content);
            return Json(new { Result = "success" });
        }

        [HttpPost]
        public IHttpActionResult Delete([FromBody]ToDoRequest model)
        {
            var toDomanager = new ToDoList.ToDoList();
            toDomanager.DeleteToDo(model.id);

            return Json(new { Result = "success" });
        }
    }
}
ListModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ReactToDoList.Models
{
    [Serializable]
    public class ListModel
    {
        public List<ToDo> UserList { get; set; }
    }

    [Serializable]
    public class ToDo
    {
        public long id { get; set; }
        public string title { get; set; }
        public string content { get; set; }
    }

    [Serializable]
    public class ToDoRequest
    {
        public long id { get; set; }
        public string title { get; set; }
        public string content { get; set; }
    }
}

ビジネスロジック

ToDoList.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using DataBase;
using DataBase.Models;

namespace ToDoList
{
    public class ToDoList
    {
        public List<ToDo> GetToDos()
        {
            using (var context = new ToDoListContext())
            {
                var result = context.ToDos.ToList();
                return result;
            }
        }

        public void AddToDo(string title, string content)
        {
            using (var context = new ToDoListContext())
            {
                context.ToDos.Add(new ToDo
                {
                    Title = title,
                    Content = content
                });

                context.SaveChanges();
            }
        }

        public void DeleteToDo(long id)
        {
            using (var context = new ToDoListContext())
            {
                context.ToDos.RemoveRange(context.ToDos.Where(t => t.Id == id));
                context.SaveChanges();
            }
        }
    }
}

Select()Where()が気になった人はこちら LINQ

データベースにアクセス

DBはEntityFrameworkです。
この記事で作ってます。EntityFramework

ToDoListContext.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using DataBase.Models;

namespace DataBase
{
    public class ToDoListContext : DbContext
    {
        public DbSet<ToDo> ToDos { get; set; }
    }
}
ToDo.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataBase.Models
{
    public class ToDo
    {
        [Key]
        public long Id { get; set; }

        [Required]
        [MaxLength(50)]
        [Index]
        public string Title { get; set; }

        public string Content { get; set; }
    }
}