-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathTripController.cs
More file actions
146 lines (132 loc) · 6.42 KB
/
TripController.cs
File metadata and controls
146 lines (132 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Net;
using System.Threading.Tasks;
using AutoMapper;
using Duber.Domain.Trip.Commands;
using OpenCqrs;
using OpenCqrs.Domain;
using Microsoft.ApplicationInsights.AspNetCore.Extensions;
using Microsoft.AspNetCore.Mvc;
using Action = Duber.Domain.Trip.Commands.Action;
using ViewModel = Duber.Trip.API.Application.Model;
namespace Duber.Trip.API.Controllers
{
[Route("api/v1/[controller]")]
public class TripController : Controller
{
private readonly IDispatcher _dispatcher;
private readonly IMapper _mapper;
private readonly IRepository<Domain.Trip.Model.Trip> _repository;
public TripController(IDispatcher dispatcher, IMapper mapper, IRepository<Domain.Trip.Model.Trip> repository)
{
_dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
_repository = repository ?? throw new ArgumentNullException(nameof(repository));
}
/// <summary>
/// Returns a trip that matches with the specified id.
/// </summary>
/// <param name="tripId"></param>
/// <returns>Returns a trip that matches with the specified id.</returns>
/// <response code="200">Returns a Trip object that matches with the specified id.</response>
[HttpGet("{tripId}")]
[ProducesResponseType(typeof(ViewModel.Trip), (int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
[ProducesResponseType((int)HttpStatusCode.InternalServerError)]
public async Task<IActionResult> GetTrip(Guid tripId)
{
var trip = await _repository.GetByIdAsync(tripId);
var tripViewModel = _mapper.Map<ViewModel.Trip>(trip);
if (tripViewModel == null)
return NotFound();
return Ok(tripViewModel);
}
/// <summary>
/// Creates a new trip.
/// </summary>
/// <param name="command"></param>
/// <returns>Returns the newly created trip identifier.</returns>
/// <response code="201">Returns the newly created trip identifier.</response>
[HttpPost]
[ProducesResponseType(typeof(Guid), (int)HttpStatusCode.Created)]
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
[ProducesResponseType((int)HttpStatusCode.InternalServerError)]
public async Task<IActionResult> CreateTrip([FromBody]ViewModel.CreateTripCommand command)
{
// BadRequest and InternalServerError could be throw in HttpGlobalExceptionFilter
var domainCommand = _mapper.Map<CreateTripCommand>(command);
domainCommand.AggregateRootId = Guid.NewGuid();
await _dispatcher.SendAsync(domainCommand);
return Created(HttpContext.Request.GetUri().AbsoluteUri, domainCommand.AggregateRootId);
}
/// <summary>
/// Accepts the specified trip.
/// </summary>
/// <param name="command"></param>
/// <returns></returns>
[HttpPut("accept")]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
[ProducesResponseType((int)HttpStatusCode.InternalServerError)]
public async Task<IActionResult> AcceptTrip([FromBody]ViewModel.UpdateTripCommand command)
{
// BadRequest and InternalServerError could be throw in HttpGlobalExceptionFilter, and also by ValidatorActionFilter due to the UpdateTripCommandValidator.
var domainCommand = _mapper.Map<UpdateTripCommand>(command);
domainCommand.Action = Action.Accept;
await _dispatcher.SendAsync(domainCommand);
return Ok();
}
/// <summary>
/// Starts the specified trip.
/// </summary>
/// <param name="command"></param>
/// <returns></returns>
[HttpPut("start")]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
[ProducesResponseType((int)HttpStatusCode.InternalServerError)]
public async Task<IActionResult> StartTrip([FromBody]ViewModel.UpdateTripCommand command)
{
// BadRequest and InternalServerError could be throw in HttpGlobalExceptionFilter, and also by ValidatorActionFilter due to the UpdateTripCommandValidator.
var domainCommand = _mapper.Map<UpdateTripCommand>(command);
domainCommand.Action = Action.Start;
await _dispatcher.SendAsync(domainCommand);
return Ok();
}
/// <summary>
/// Cancels the specified trip.
/// </summary>
/// <param name="command"></param>
/// <returns></returns>
[HttpPut("cancel")]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
[ProducesResponseType((int)HttpStatusCode.InternalServerError)]
public async Task<IActionResult> CancelTrip([FromBody]ViewModel.UpdateTripCommand command)
{
// BadRequest and InternalServerError could be throw in HttpGlobalExceptionFilter, and also by ValidatorActionFilter due to the UpdateTripCommandValidator.
var domainCommand = _mapper.Map<UpdateTripCommand>(command);
domainCommand.Action = Action.Cancel;
await _dispatcher.SendAsync(domainCommand);
return Ok();
}
/// <summary>
/// Updates the current location for the specified trip.
/// </summary>
/// <param name="command"></param>
/// <returns></returns>
[HttpPut("update")]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
[ProducesResponseType((int)HttpStatusCode.InternalServerError)]
public async Task<IActionResult> UpdateCurrentLocation([FromBody]ViewModel.UpdateCurrentLocationTripCommand command)
{
// BadRequest and InternalServerError could be throw in HttpGlobalExceptionFilter, and also by ValidatorActionFilter due to the UpdateTripCommandValidator.
var domainCommand = _mapper.Map<UpdateTripCommand>(command);
domainCommand.Action = Action.UpdateCurrentLocation;
await _dispatcher.SendAsync(domainCommand);
return Ok();
}
}
}