This commit is contained in:
Ying Zhao 2018-01-26 13:24:09 -08:00
Родитель 4635c331b8
Коммит dca50bdc42
2 изменённых файлов: 173 добавлений и 57 удалений

Просмотреть файл

@ -19,7 +19,6 @@ namespace Sia.Gateway.Controllers
{
private const string notFoundMessage = "Incident or event not found";
private readonly HubConnectionBuilder _hubConnectionBuilder;
public readonly RelationLinks _relationLinks;
public EventsController(IMediator mediator,
AzureActiveDirectoryAuthenticationInfo authConfig,
@ -28,27 +27,31 @@ namespace Sia.Gateway.Controllers
: base(mediator, authConfig, urlHelper)
{
_hubConnectionBuilder = hubConnectionBuilder;
_relationLinks = new RelationLinks();
}
public void CreateLinks(string id, string incidentId) {
_operationLinks.Single = new SingleOperationLinks()
{
Get = _urlHelper.Link(GetSingleRouteName, new { id }),
Post = _urlHelper.Link(PostSingleRouteName, new { })
//public void CreateLinks(string id, string incidentId)
//{
// var _operationLinks = new OperationLinks()
// {
// Single = new SingleOperationLinks()
// {
// Get = _urlHelper.Link(GetSingleRouteName, new { id }),
// Post = _urlHelper.Link(PostSingleRouteName, new { })
};
_operationLinks.Multiple = new MultipleOperationLinks()
{
Get = _urlHelper.Link(GetMultipleRouteName, new { })
};
_relationLinks.Parent = new RelatedParentLinks()
{
Incident = _urlHelper.Link(IncidentsController.GetSingleRouteName, new { id = incidentId })
};
}
// },
// Multiple = new MultipleOperationLinks()
// {
// Get = _urlHelper.Link(GetMultipleRouteName, new { })
// }
// };
// var _relationLinks = new RelationLinks()
// {
// Parent = new RelatedParentLinks()
// {
// Incident = _urlHelper.Link(IncidentsController.GetSingleRouteName, new { id = incidentId })
// }
// };
//}
public const string GetMultipleRouteName = "GetEvents";
[HttpGet(Name = GetMultipleRouteName)]
@ -58,9 +61,20 @@ namespace Sia.Gateway.Controllers
{
var result = await _mediator.Send(new GetEventsRequest(incidentId, pagination, filter, _authContext));
CreateLinks(null, incidentId.ToString());
//CreateLinks(null, incidentId.ToString());
//Response.Headers.AddLinksHeader(new FilteredLinksHeader(filter, pagination, _urlHelper, GetMultipleRouteName, _operationLinks, _relationLinks));
Response.Headers.AddLinksHeader(new FilteredLinksHeader(filter, pagination, _urlHelper, GetMultipleRouteName, new OperationLinks()
{
Single = new SingleOperationLinks()
{
Post = _urlHelper.Link(PostSingleRouteName, new { })
},
Multiple = new MultipleOperationLinks()
{
Get = _urlHelper.Link(GetMultipleRouteName, new { })
}
}, null));
Response.Headers.AddLinksHeader(new FilteredLinksHeader(filter, pagination, _urlHelper, GetMultipleRouteName, _operationLinks, _relationLinks));
return Ok(result);
}
@ -70,14 +84,35 @@ namespace Sia.Gateway.Controllers
{
var result = await _mediator.Send(new GetEventRequest(incidentId, id, _authContext));
CreateLinks(id.ToString(), incidentId.ToString());
Response.Headers.AddLinksHeader(new LinksHeader(null, _urlHelper, GetSingleRouteName, _operationLinks, _relationLinks));
//CreateLinks(id.ToString(), incidentId.ToString());
//Response.Headers.AddLinksHeader(new LinksHeader(null, _urlHelper, GetSingleRouteName, _operationLinks, _relationLinks));
Response.Headers.AddLinksHeader(new LinksHeader(null, _urlHelper, GetSingleRouteName,
new OperationLinks()
{
Single = new SingleOperationLinks()
{
Get = _urlHelper.Link(GetSingleRouteName, new { id }),
Post = _urlHelper.Link(PostSingleRouteName, new { })
},
Multiple = new MultipleOperationLinks()
{
Get = _urlHelper.Link(GetMultipleRouteName, new { })
}
},
new RelationLinks()
{
Parent = new RelatedParentLinks()
{
Incident = _urlHelper.Link(IncidentsController.GetSingleRouteName, new { id = incidentId })
}
}
));
if (result == null)
{
return NotFound(notFoundMessage);
}
return Ok(result);
}
@ -94,9 +129,30 @@ namespace Sia.Gateway.Controllers
var newUrl = _urlHelper.Link(GetSingleRouteName, new { id = result.Id });
CreateLinks(result.Id.ToString(), incidentId.ToString());
//CreateLinks(result.Id.ToString(), incidentId.ToString());
//Response.Headers.AddLinksHeader(new LinksHeader(null, _urlHelper, PostSingleRouteName, _operationLinks, _relationLinks));
Response.Headers.AddLinksHeader(new LinksHeader(null, _urlHelper, PostSingleRouteName,
new OperationLinks()
{
Single = new SingleOperationLinks()
{
Get = _urlHelper.Link(GetSingleRouteName, new { result.Id }),
Post = _urlHelper.Link(PostSingleRouteName, new { })
Response.Headers.AddLinksHeader(new LinksHeader(null, _urlHelper, PostSingleRouteName, _operationLinks, _relationLinks));
},
Multiple = new MultipleOperationLinks()
{
Get = _urlHelper.Link(GetMultipleRouteName, new { })
}
},
new RelationLinks()
{
Parent = new RelatedParentLinks()
{
Incident = _urlHelper.Link(IncidentsController.GetSingleRouteName, new { id = incidentId })
}
}
));
return Created(newUrl, result);
}

Просмотреть файл

@ -5,6 +5,7 @@ using Sia.Domain.ApiModels;
using Sia.Shared.Authentication;
using Sia.Gateway.Requests;
using System.Threading.Tasks;
using Sia.Data.Incidents.Filters;
using Sia.Shared.Controllers;
using Sia.Shared.Protocol;
@ -13,46 +14,71 @@ namespace Sia.Gateway.Controllers
[Route("[controller]")]
public class IncidentsController : BaseController
{
private RelationLinks _relationLinks;
public IncidentsController(IMediator mediator, AzureActiveDirectoryAuthenticationInfo authConfig, IUrlHelper urlHelper)
: base(mediator, authConfig, urlHelper)
{
}
public void CreateLinks(string id) {
_operationLinks.Single = new SingleOperationLinks()
{
Get = _urlHelper.Link(GetSingleRouteName, new {id}),
Post = _urlHelper.Link(PostSingleRouteName, new { })
};
_operationLinks.Multiple = new MultipleOperationLinks()
{
Get = _urlHelper.Link(GetMultipleRouteName, new { })
};
if (id != null)
{
_relationLinks = new RelationLinks()
{
Children = new RelatedChildLinks()
{
Events = _urlHelper.Link(EventsController.GetMultipleRouteName, new { incidentId = id })
}
};
}
}
//public void CreateLinks(string id)
//{
// var _operationLinks = new OperationLinks()
// {
// Single = new SingleOperationLinks()
// {
// Get = _urlHelper.Link(GetSingleRouteName, new { id }),
// Post = _urlHelper.Link(PostSingleRouteName, new { })
// },
// Multiple = new MultipleOperationLinks()
// {
// Get = _urlHelper.Link(GetMultipleRouteName, new { })
// }
// };
// if (id != null)
// {
// var _relationLinks = new RelationLinks()
// {
// Children = new RelatedChildLinks()
// {
// Events = _urlHelper.Link(EventsController.GetMultipleRouteName, new { incidentId = id })
// }
// };
// }
//}
public const string GetSingleRouteName = "GetIncident";
[HttpGet("{id}", Name = GetSingleRouteName)]
public async Task<IActionResult> Get(long id)
public async Task<IActionResult> Get(long id, [FromQuery]EventFilters filter)
{
var result = await _mediator.Send(new GetIncidentRequest(id, _authContext));
if (result == null)
{
return NotFound($"{nameof(Incident)} not found");
}
CreateLinks(id.ToString());
Response.Headers.AddLinksHeader(new LinksHeader(null, _urlHelper, "IncidentsController" + GetSingleRouteName, _operationLinks, _relationLinks));
//CreateLinks(id.ToString());
//Response.Headers.AddLinksHeader(new FilteredLinksHeader(filter, null, _urlHelper, "IncidentsController" + GetSingleRouteName, _operationLinks, _relationLinks));
Response.Headers.AddLinksHeader( new FilteredLinksHeader(
filter, null, _urlHelper, "IncidentsController" + GetSingleRouteName, new OperationLinks()
{
Single = new SingleOperationLinks()
{
Get = _urlHelper.Link(GetSingleRouteName, new { id }),
Post = _urlHelper.Link(PostSingleRouteName, new { })
},
Multiple = new MultipleOperationLinks()
{
Get = _urlHelper.Link(GetMultipleRouteName, new { })
}
}, new RelationLinks()
{
Children = new RelatedChildLinks()
{
Events = _urlHelper.Link(EventsController.GetMultipleRouteName, new { incidentId = id })
}
})
);
return Ok(result);
}
@ -65,8 +91,21 @@ namespace Sia.Gateway.Controllers
{
return NotFound($"{nameof(Incident)}s not found");
}
CreateLinks(null);
Response.Headers.AddLinksHeader(new LinksHeader(pagination, _urlHelper, "IncidentsController" + GetSingleRouteName, _operationLinks, _relationLinks));
//CreateLinks(null);
//Response.Headers.AddLinksHeader(new LinksHeader(pagination, _urlHelper, "IncidentsController" + GetSingleRouteName, _operationLinks, _relationLinks));
Response.Headers.AddLinksHeader( new LinksHeader(
pagination, _urlHelper, "IncidentsController" + GetSingleRouteName, new OperationLinks()
{
Single = new SingleOperationLinks()
{
Post = _urlHelper.Link(PostSingleRouteName, new { })
},
Multiple = new MultipleOperationLinks()
{
Get = _urlHelper.Link(GetMultipleRouteName, new { })
}
}, null));
return Ok(result);
}
@ -80,8 +119,29 @@ namespace Sia.Gateway.Controllers
return NotFound($"{nameof(Incident)} not found");
}
var newUrl = _urlHelper.Link(EventsController.GetMultipleRouteName, new { incidentId = result.Id });
CreateLinks(result.Id.ToString());
Response.Headers.AddLinksHeader(new LinksHeader(null, _urlHelper, PostSingleRouteName, _operationLinks, _relationLinks));
//CreateLinks(result.Id.ToString());
//Response.Headers.AddLinksHeader(new LinksHeader(null, _urlHelper, PostSingleRouteName, _operationLinks, _relationLinks));
Response.Headers.AddLinksHeader(
new LinksHeader(null, _urlHelper, PostSingleRouteName, new OperationLinks()
{
Single = new SingleOperationLinks()
{
Get = _urlHelper.Link(GetSingleRouteName, new { result.Id }),
Post = _urlHelper.Link(PostSingleRouteName, new { })
},
Multiple = new MultipleOperationLinks()
{
Get = _urlHelper.Link(GetMultipleRouteName, new { })
}
}, new RelationLinks()
{
Children = new RelatedChildLinks()
{
Events = _urlHelper.Link(EventsController.GetMultipleRouteName, new { incidentId = result.Id })
}
}
));
return Created(newUrl, result);
}