-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleController.cs
More file actions
148 lines (130 loc) · 4.08 KB
/
Copy pathSampleController.cs
File metadata and controls
148 lines (130 loc) · 4.08 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
147
148
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SampleAPI.Models;
using Swashbuckle.AspNetCore.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SampleAPI.Controllers
{
[SwaggerTag("This is an example controller generated by ASP.NET Core 3.1")]
[ApiController]
// you can remove api from the url below
[Route("api/[controller]")]
public class SampleController : ControllerBase
{
private readonly ILogger<SampleController> _logger;
public SampleController(ILogger<SampleController> logger)
{
_logger = logger;
}
[HttpGet()]
[ProducesResponseType(StatusCodes.Status200OK)]
[SwaggerOperation(
OperationId = "GetSamples",
Summary = "Get all samples",
Description = "returns all samples"
)]
public ActionResult<IEnumerable<Sample>> GetSamples()
{
var rng = new Random();
List<Sample> samples = new List<Sample>();
samples.AddRange(Enumerable.Range(1, 5).Select(index => new Sample
{
CustomerId = 1,
FirstName = "FirstName",
LastName = "LastName"
})
.ToArray());
return samples;
}
[HttpGet("{id}")]
[Produces("application/json")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[SwaggerOperation(
OperationId = "GetSample",
Summary = "Get specific sample",
Description = "return a sample"
)]
public ActionResult<Sample> GetSample(int id)
{
if (id == 5)
{
return NotFound();
}
var sample = new Sample
{
CustomerId = id,
FirstName = "FirstName",
LastName = "LastName"
};
return sample;
}
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[SwaggerOperation(
OperationId = "PostSample",
Summary = "Create a sample",
Description = "Create a new Sample"
)]
public ActionResult PostSample([FromBody] Sample sample)
{
if (sample.CustomerId == 5)
{
return BadRequest();
}
if (sample.CustomerId == 6)
{
return NotFound();
}
return Ok();
}
[HttpPut("{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[SwaggerOperation(
OperationId = "PutSample",
Summary = "Update a sample",
Description = "Update an existing Sample"
)]
public ActionResult PutSample(int id, [FromBody] Sample sample)
{
if (id == 5)
{
return BadRequest();
}
if (id == 6)
{
return NotFound();
}
return Ok();
}
[HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[SwaggerOperation(
OperationId = "DeleteSample",
Summary = "Delete a sample",
Description = "Delete an existing Sample"
)]
public ActionResult DeleteSample(int id)
{
if (id == 5)
{
return BadRequest();
}
if (id == 6)
{
return NotFound();
}
return NoContent();
}
}
}