步骤4.实施Web API

现在该进行实际的REST API实施了。转到Startup.cs并启用MVC路由(如果尚未启用):

启动文件

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext(options =>
options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”)));
}

//The method is called by the runtime. Use it to configure HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc();

}

添加控制器

创建Controllers文件夹并创建三个空的API Controller:一个用于Tasks,另一个用于Links,另一个用于整个数据集:

dhtmlxGantt实施Web API_mvc

任务控制器

让我们为Tasks创建一个控制器。它将为甘特任务定义基本的CRUD操作。

这个怎么运作:

在GET请求中,任务是从数据库中加载的,输出是任务的数据传输对象;
在PUT / POST请求中,任务作为WebAPITask类来自客户端。它们在dhtmlxGantt中以这种方式表示。因此,您应该将它们转换为EntityFramework(任务类)的数据模型的格式。之后,可以将更改保存在DatabaseContext中。
控制器/ TaskController.cs
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using DHX.Gantt.Models;

namespace DHX.Gantt.Controllers
{
[Produces(“application/json”)]
[Route(“api/task”)]
public class TaskController : Controller
{
private readonly GanttContext _context;
public TaskController(GanttContext context)
{
_context = context;
}

    // GET api/task
[HttpGet]
public IEnumerable<WebApiTask> Get()
{
return _context.Tasks
.ToList()
.Select(t => (WebApiTask)t);
}

// GET api/task/5
[HttpGet("{id}")]
public WebApiTask Get(int id)
{
return (WebApiTask)_context
.Tasks
.Find(id);
}

// POST api/task
[HttpPost]
public ObjectResult Post(WebApiTask apiTask)
{
var newTask = (Task)apiTask;

_context.Tasks.Add(newTask);
_context.SaveChanges();

return Ok(new
{
tid = newTask.Id,
action = "inserted"
});
}

// PUT api/task/5
[HttpPut("{id}")]
public ObjectResult Put(int id, WebApiTask apiTask)
{
var updatedTask = (Task)apiTask;
var dbTask = _context.Tasks.Find(id);
dbTask.Text = updatedTask.Text;
dbTask.StartDate = updatedTask.StartDate;
dbTask.Duration = updatedTask.Duration;
dbTask.ParentId = updatedTask.ParentId;
dbTask.Progress = updatedTask.Progress;
dbTask.Type = updatedTask.Type;

_context.SaveChanges();

return Ok(new
{
action = "updated"
});
}

// DELETE api/task/5
[HttpDelete("{id}")]
public ObjectResult DeleteTask(int id)
{
var task = _context.Tasks.Find(id);
if (task != null)
{
_context.Tasks.Remove(task);
_context.SaveChanges();
}

return Ok(new
{
action = "deleted"
});
}
}

}

链接控制器

接下来,您应该为Links创建一个控制器:
控制器/LinkController.cs
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using DHX.Gantt.Models;

namespace DHX.Gantt.Controllers
{
[Produces(“application/json”)]
[Route(“api/link”)]
public class LinkController : Controller
{
private readonly GanttContext _context;
public LinkController(GanttContext context)
{
_context = context;
}

    // GET api/Link
[HttpGet]
public IEnumerable<WebApiLink> Get()
{
return _context.Links
.ToList()
.Select(t => (WebApiLink)t);
}

// GET api/Link/5
[HttpGet("{id}")]
public WebApiLink Get(int id)
{
return (WebApiLink)_context
.Links
.Find(id);
}

// POST api/Link
[HttpPost]
public ObjectResult Post(WebApiLink apiLink)
{
var newLink = (Link)apiLink;

_context.Links.Add(newLink);
_context.SaveChanges();

return Ok(new
{
tid = newLink.Id,
action = "inserted"
});
}

// PUT api/Link/5
[HttpPut("{id}")]
public ObjectResult Put(int id, WebApiLink apiLink)
{
var updatedLink = (Link)apiLink;
updatedLink.Id = id;
_context.Entry(updatedLink).State = EntityState.Modified;


_context.SaveChanges();

return Ok(new
{
action = "updated"
});
}

// DELETE api/Link/5
[HttpDelete("{id}")]
public ObjectResult DeleteLink(int id)
{
var Link = _context.Links.Find(id);
if (Link != null)
{
_context.Links.Remove(Link);
_context.SaveChanges();
}

return Ok(new
{
action = "deleted"
});
}
}

}

数据控制器

最后,您需要为数据操作创建一个控制器:
控制器/DataController.cs
using System.Collections.Generic;
using System.Linq;

using Microsoft.AspNetCore.Mvc;
using DHX.Gantt.Models;

namespace DHX.Gantt.Controllers
{
[Produces(“application/json”)]
[Route(“api/data”)]
public class DataController : Controller
{
private readonly GanttContext _context;
public DataController(GanttContext context)
{
_context = context;
}

    // GET api/data
[HttpGet]
public object Get()
{
return new
{
data = _context.Tasks.ToList().Select(t => (WebApiTask)t),
links = _context.Links.ToList().Select(l => (WebApiLink)l)

};
}

}

}

都准备好了。您可以运行该应用程序,并查看完整的Gantt。

dhtmlxGantt实施Web API_android_02

是否想尝试DHTMLX Gantt来构建自己的Salesforce应用?访问我们的GitHub存储库,您可以在其中找到Salesforce的Gantt组件的完整源代码,并按照我们的视频指南中的步骤进行操作。