2016-08-10 12 views
0

私はプロジェクトタスク用のAPIを作成しています。以下にリストされているTasksControllerがあります。私はWebApi.Halを使用してハイパーメディアを生成しており、サービスはhal+jsonhal+xmlmedia typesもサポートしています。HALでリンクされたリソースの関連する名前を取得する方法

以下はGETリクエストhttp://localhost:51910/api/tasks/1の現在の対応です。応答には、優先度のリンクのリストがありますが、回答には関連付けられた名前はありません(LowMediumHighなど)。

WebApi.HALを使用して、優先度の名前を取得するための最良のHALアプローチは何ですか?

注:優先度のリストは、今後拡張することができます。

enter image description here

優先

public class Priority 
{ 
    public int PriorityID { get; set; } 
    public string PriorityName { get; set; } 
    public string Revision { get; set; } 
    public DateTime ApprovalDate { get; set; } 
} 

コントローラ

public class TasksController : ApiController 
    { 
     // GET api/values/5 
     [HttpGet] 
     public TaskRepresentation Get(int id) 
     { 

      Task selectedTask = TasksHelper.GetTask(id); 
      TaskRepresentation taskRepresentation = new TaskRepresentation(selectedTask); 
      return taskRepresentation; 
     } 

     //PUT For Setting Priority 
     [HttpPut] 
     [Route("api/tasks/{taskID}/priorities/{priorityID}")] 
     public TaskRepresentation PutSetPriority(int taskID, int priorityID) 
     { 
      Task selectedTask = TasksHelper.GetTask(taskID); 

      Priority selectedPriority = null; 
      List<Priority> allPriorities = TasksPrioritiesHelper.GetAllPriorities(); 
      foreach (Priority p in allPriorities) 
      { 
       if (p.PriorityID == priorityID) 
       { 
        selectedPriority = p; 
       } 
      } 

      //Update Task 
      if (selectedPriority != null) 
      { 
       selectedTask.CurrentPriority = selectedPriority.PriorityName; 
      } 
      else 
      { 

       throw new Exception("Not available"); 
      } 


      TaskRepresentation taskRepresentation = new TaskRepresentation(selectedTask); 
      return taskRepresentation; 
     } 

     [HttpGet] 
     [Route("api/tasks/{taskID}/priorities/{priorityID}")] 
     public Priority Get(int taskID, int priorityID) 
     { 
      Priority selectedPriority = null; 
      List<Priority> allPriorities = TasksPrioritiesHelper.GetAllPriorities(); 
      foreach (Priority p in allPriorities) 
      { 
       if (p.PriorityID == priorityID) 
       { 
        selectedPriority = p; 
       } 
      } 

      return selectedPriority; 
     } 

    } 

HALジェネレーション関連するクラス

public static class LinkTemplates 
    { 

     public static class TaskLinks 
     { 
      public static Link TaskEntry { get { return new Link("self", "~/api/tasks/{taskID}"); } } 
      public static Link PriorityLink { get { return new Link("priorities", "~/api/tasks/{taskID}/priorities/{priorityID}"); } } 
     } 
    } 

public class TaskRepresentation : Representation 
    { 
     Task theTask; 

     public int TaskID{get{return theTask.TaskID;}} 
     public string TaskName{get{return theTask.Name;}} 
     public string CurrentPriority{get{return theTask.CurrentPriority;}} 
     public string Category{get{return theTask.Category;}} 

     public TaskRepresentation(Task t) 
     { 
      theTask = t; 
     } 


     public override string Rel 
     { 
      get { return LinkTemplates.TaskLinks.TaskEntry.Rel; } 
      set { } 
     } 

     public override string Href 
     { 
      get { return LinkTemplates.TaskLinks.TaskEntry.CreateLink(new { taskID = theTask.TaskID }).Href; } 
      set { } 
     } 


     protected override void CreateHypermedia() 
     { 
      foreach (Priority p in theTask.PossiblePriorities) 
      { 
       Links.Add(LinkTemplates.TaskLinks.PriorityLink.CreateLink(new { taskID = theTask.TaskID, priorityID = p.PriorityID })); 
      } 
     } 
    } 

答えて

0

HAL仕様が記載されていますtitle - これは要件を満たします。

以下は更新された応答です。

{ 
    "TaskID": 1, 
    "TaskName": "Task1", 
    "CurrentPriority": "Medium", 
    "Category": "IT", 
    "_links": { 
    "self": { 
     "href": "/api/tasks/1" 
    }, 
    "priorities": [ 
     { 
     "href": "/api/tasks/1/priorities/101", 
     "title": "Low" 
     }, 
     { 
     "href": "/api/tasks/1/priorities/103", 
     "title": "High" 
     }, 
     { 
     "href": "/api/tasks/1/priorities/104", 
     "title": "Critical" 
     } 
    ] 
    } 
} 

WebAPI.HALは変更

protected override void CreateHypermedia() 
     { 
      foreach (Priority p in theTask.PossiblePriorities) 
      { 
       Link lnk = LinkTemplates.TaskLinks.PriorityLink.CreateLink(new { taskID = theTask.TaskID, priorityID = p.PriorityID }); 
       lnk.Title = p.PriorityName; 
       Links.Add(lnk); 
      } 
     } 

コード

public static class LinkTemplates 
    { 

     public static class TaskLinks 
     { 
      public static Link TaskEntry { get { return new Link("self", "~/api/tasks/{taskID}"); } } 
      //public static Link PriorityLink { get { return new Link("priorities", "~/api/tasks/{taskID}/priorities/{priorityID}"); } } 
      public static Link PriorityLink 
      { 
       get 
       { 
        Link l = new Link("priorities", "~/api/tasks/{taskID}/priorities/{priorityID}"); 
        return l; 
       } 
      } 
     } 
    } 


    public class TasksController : ApiController 
    { 
     // GET api/values/5 
     [HttpGet] 
     public TaskRepresentation Get(int id) 
     { 

      Task selectedTask = TasksHelper.GetTask(id); 
      TaskRepresentation taskRepresentation = new TaskRepresentation(selectedTask); 
      return taskRepresentation; 
     } 

     //PUT For Setting Priority 
     [HttpPut] 
     [Route("api/tasks/{taskID}/priorities/{priorityID}")] 
     public TaskRepresentation PutSetPriority(int taskID, int priorityID) 
     { 
      Task selectedTask = TasksHelper.GetTask(taskID); 

      Priority selectedPriority = null; 
      List<Priority> allPriorities = TasksPrioritiesHelper.GetAllPriorities(); 
      foreach (Priority p in allPriorities) 
      { 
       if (p.PriorityID == priorityID) 
       { 
        selectedPriority = p; 
       } 
      } 

      //Update Task 
      if (selectedPriority != null) 
      { 
       selectedTask.CurrentPriority = selectedPriority.PriorityName; 
      } 
      else 
      { 

       throw new Exception("Not available"); 
      } 


      TaskRepresentation taskRepresentation = new TaskRepresentation(selectedTask); 
      return taskRepresentation; 
     } 

     [HttpGet] 
     [Route("api/tasks/{taskID}/priorities/{priorityID}")] 
     public Priority Get(int taskID, int priorityID) 
     { 
      Priority selectedPriority = null; 
      List<Priority> allPriorities = TasksPrioritiesHelper.GetAllPriorities(); 
      foreach (Priority p in allPriorities) 
      { 
       if (p.PriorityID == priorityID) 
       { 
        selectedPriority = p; 
       } 
      } 

      return selectedPriority; 
     } 

    } 

    public class TaskRepresentation : Representation 
    { 
     Task theTask; 

     public int TaskID{get{return theTask.TaskID;}} 
     public string TaskName{get{return theTask.Name;}} 
     public string CurrentPriority{get{return theTask.CurrentPriority;}} 
     public string Category{get{return theTask.Category;}} 

     public TaskRepresentation(Task t) 
     { 
      theTask = t; 
     } 


     public override string Rel 
     { 
      get { return LinkTemplates.TaskLinks.TaskEntry.Rel; } 
      set { } 
     } 

     public override string Href 
     { 
      get { return LinkTemplates.TaskLinks.TaskEntry.CreateLink(new { taskID = theTask.TaskID }).Href; } 
      set { } 
     } 


     protected override void CreateHypermedia() 
     { 
      foreach (Priority p in theTask.PossiblePriorities) 
      { 
       Link lnk = LinkTemplates.TaskLinks.PriorityLink.CreateLink(new { taskID = theTask.TaskID, priorityID = p.PriorityID }); 
       lnk.Title = p.PriorityName; 
       Links.Add(lnk); 
      } 
     } 
    } 

    public class Task 
    { 
     public string Name { get; set; } 
     public int TaskID { get; set; } 
     public string Category { get; set; } 
     public string CurrentPriority { get; set; } 
     public List<Priority> PossiblePriorities { get; set; } 
    } 
    public class Priority 
    { 
     public int PriorityID { get; set; } 
     public string PriorityName { get; set; } 
     public string Revision { get; set; } 
     public DateTime ApprovalDate { get; set; } 
    } 

    public static class TasksPrioritiesHelper 
    { 

     public static List<Priority> GetAllPriorities() 
     { 
      List<Priority> possiblePriorities = new List<Priority>(); 
      Priority pLow = new Priority { PriorityID = 101, PriorityName = "Low" }; 
      Priority pMedium = new Priority { PriorityID = 102, PriorityName = "Medium" }; 
      Priority pHigh = new Priority { PriorityID = 103, PriorityName = "High" }; 
      Priority pCritical = new Priority { PriorityID = 104, PriorityName = "Critical" }; 

      possiblePriorities.Add(pLow); 
      possiblePriorities.Add(pMedium); 
      possiblePriorities.Add(pHigh); 
      possiblePriorities.Add(pCritical); 

      return possiblePriorities; 
     } 

     public static List<Priority> GetAdministrativePriorities() 
     { 
      List<Priority> possiblePriorities = new List<Priority>(); 
      Priority pLow = new Priority { PriorityID = 101, PriorityName = "Low" }; 
      Priority pHigh = new Priority { PriorityID = 103, PriorityName = "High" }; 

      possiblePriorities.Add(pLow); 
      possiblePriorities.Add(pHigh); 

      return possiblePriorities; 
     } 


     public static List<Priority> GetPossiblePrioritiesForTask(Task t) 
     { 
      List<Priority> possibleTaskPriorities = new List<Priority>(); 


      if (String.Equals(t.Category, "IT")) 
      { 
       possibleTaskPriorities = GetAllPriorities(); 
      } 
      else 
      { 
       possibleTaskPriorities = GetAdministrativePriorities(); 
      } 

      Priority currentTaskPriority = null; 
      foreach(Priority p in possibleTaskPriorities) 
      { 
       if(String.Equals(t.CurrentPriority,p.PriorityName)) 
       { 
        currentTaskPriority=p; 
       } 
      } 

      if(currentTaskPriority!=null) 
      { 
       possibleTaskPriorities.Remove(currentTaskPriority); 
      } 


      return possibleTaskPriorities; 
     } 


    } 
    public static class TasksHelper 
    { 
     public static Task GetTask(int id) 
     { 
      Task selectedTask = null; 
      List<Task> tasks = GetAllTasks(); 

      foreach (Task t in tasks) 
      { 
       if(t.TaskID==id) 
       { 
        selectedTask = t; 
       } 
      } 
      return selectedTask; 
     } 


     public static List<Task> GetAllTasks() 
     { 

      List<Task> tasks; 
      tasks = new List<Task>(); 

      Task t1 = new Task{Category = "IT",CurrentPriority = "Medium",Name = "Task1",TaskID = 1}; 
      t1.PossiblePriorities = TasksPrioritiesHelper.GetPossiblePrioritiesForTask(t1); 

      tasks.Add(t1); 

      return tasks; 
     } 


    } 
関連する問題