-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
DevicesController.cs
167 lines (149 loc) · 6.14 KB
/
DevicesController.cs
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using DeviceManager.Api.ActionFilters;
using DeviceManager.Api.Model;
using DeviceManager.Api.Resources;
using DeviceManager.Api.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace DeviceManager.Api.Controllers
{
/// <summary>
/// CRUD operations of the Device
/// </summary>
[Route("api/v{version:apiVersion}/devices")]
public class DevicesController : BaseController<DeviceViewModel>
{
private readonly IDeviceService deviceService;
/// <summary>
/// Initializes a new instance of the <see cref="DevicesController"/> class.
/// </summary>
/// <param name="deviceService">The device service.</param>
/// <param name="sharedLocalizer">Global resource file</param>
public DevicesController(IDeviceService deviceService, IStringLocalizer<SharedResource> sharedLocalizer)
: base(sharedLocalizer)
{
this.deviceService = deviceService;
}
#region Entity Framework Core
/// <summary>
/// Gets the specified page.
/// </summary>
/// <param name="page">The page.</param>
/// <param name="pageSize">Size of the page.</param>
/// <returns></returns>
[HttpGet]
[SwaggerOperation("GetDevices")]
[ValidateActionParameters]
public IActionResult Get([FromQuery, Required]int page, [FromQuery, Required]int pageSize)
{
return new ObjectResult(deviceService.GetDevices(page, pageSize));
}
/// <summary>
/// Gets the specified device identifier.
/// </summary>
/// <param name="deviceId">The device identifier.</param>
/// <returns></returns>
[HttpGet("{deviceId}")]
[SwaggerOperation("GetDeviceById")]
[ValidateActionParameters]
public IActionResult GetDeviceById([FromRoute][Required]string deviceId)
{
return new ObjectResult(deviceService.GetDeviceById(Guid.Parse(deviceId)));
}
/// <summary>
/// Gets the specified device identifier in async await pattern.
/// </summary>
/// <param name="deviceId">The device identifier.</param>
/// <returns></returns>
[HttpGet("async/{deviceId}")]
[SwaggerOperation(nameof(GetDeviceByIdAsync))]
[ValidateActionParameters]
public async Task<IActionResult> GetDeviceByIdAsync([FromRoute][Required]string deviceId)
{
return new ObjectResult(await deviceService.GetDeviceByIdAsync(Guid.Parse(deviceId)));
}
/// <summary>
/// Gets the device by title.
/// </summary>
/// <param name="deviceTitle">The device title.</param>
/// <returns></returns>
[HttpGet("title/{deviceTitle}")]
[SwaggerOperation("GetDeviceByTitle")]
public IActionResult GetDeviceByTitle(string deviceTitle)
{
return new ObjectResult(deviceService.GetDeviceByTitle(deviceTitle));
}
/// <summary>
/// Posts the specified device view model.
/// </summary>
/// <param name="deviceViewModel">The device view model.</param>
/// <returns></returns>
[HttpPost]
[SwaggerOperation("CreateDevice")]
[SwaggerResponse(204, null, "Device was saved successfuly")]
[SwaggerResponse(400, null, "Error in saving the Device")]
public IActionResult Post([FromBody]DeviceViewModel deviceViewModel)
{
deviceService.CreateDevice(deviceViewModel);
return new OkResult();
}
/// <summary>
/// Puts the specified device identifier.
/// </summary>
/// <param name="deviceId">The device identifier.</param>
/// <param name="deviceViewModel">The device view model.</param>
/// <returns></returns>
[HttpPut("{deviceId}")]
[SwaggerOperation("UpdateDevice")]
public IActionResult Put([FromRoute]Guid deviceId, [FromBody]DeviceViewModel deviceViewModel)
{
deviceService.UpdateDevice(deviceId, deviceViewModel);
return new OkResult();
}
#endregion
#region Dapper
/// <summary>
/// Gets all records using dapper.
/// </summary>
/// <param name="page">The page.</param>
/// <param name="pageSize">Size of the page.</param>
/// <returns></returns>
[HttpGet("dapper")]
[SwaggerOperation("GetAllDevicesUsingDapper")]
[ValidateActionParameters]
public async Task<IActionResult> GetAllUsingDapper([FromQuery, Required]int page, [FromQuery, Required]int pageSize)
{
return new ObjectResult(await deviceService.GetDevicesUsingDapper(page, pageSize));
}
/// <summary>
/// Gets the specified device identifier in async await pattern using dapper.
/// </summary>
/// <param name="deviceId">The device identifier.</param>
/// <returns></returns>
[HttpGet("dapper/async/{deviceId}")]
[SwaggerOperation(nameof(GetDeviceByIdAsync))]
[ValidateActionParameters]
public async Task<IActionResult> GetDeviceByIdUsingDapperAsync([FromRoute][Required]string deviceId)
{
return new ObjectResult(await deviceService.GetDeviceByIdUsingDapperAsync(Guid.Parse(deviceId)));
}
/// <summary>
/// Posts the specified device view model and executeds query using dapper.
/// </summary>
/// <param name="deviceViewModel">The device view model.</param>
/// <returns></returns>
[HttpPost("dapper")]
[SwaggerOperation("CreateDeviceUsingDapper")]
[SwaggerResponse(204, null, "Device was saved successfuly")]
[SwaggerResponse(400, null, "Error in saving the Device")]
public async Task<IActionResult> PostUsingDapepr([FromBody]DeviceViewModel deviceViewModel)
{
await deviceService.CreateDeviceUsingDapperAsync(deviceViewModel);
return new OkResult();
}
#endregion
}
}