|
1 | | -using System.Collections.Generic; |
2 | | -using System.ComponentModel.DataAnnotations; |
3 | | -using System.Linq; |
4 | | -using System.Threading.Tasks; |
| 1 | +using System.ComponentModel.DataAnnotations; |
5 | 2 | using AspNetCoreServiceBusApi1.Model; |
6 | | -using Microsoft.AspNetCore.Http; |
7 | 3 | using Microsoft.AspNetCore.Mvc; |
8 | 4 | using ServiceBusMessaging; |
9 | 5 |
|
10 | | -namespace AspNetCoreServiceBusApi1.Controllers |
| 6 | +namespace AspNetCoreServiceBusApi1.Controllers; |
| 7 | + |
| 8 | +[Route("api/[controller]")] |
| 9 | +[ApiController] |
| 10 | +public class PayloadController : Controller |
11 | 11 | { |
12 | | - [Route("api/[controller]")] |
13 | | - [ApiController] |
14 | | - public class PayloadController : Controller |
| 12 | + private ServiceBusSender _serviceBusSender; |
| 13 | + |
| 14 | + public PayloadController(ServiceBusSender serviceBusSender) |
15 | 15 | { |
16 | | - private ServiceBusSender _serviceBusSender; |
| 16 | + _serviceBusSender = serviceBusSender; |
| 17 | + } |
17 | 18 |
|
18 | | - public PayloadController(ServiceBusSender serviceBusSender) |
| 19 | + [HttpGet] |
| 20 | + [ProducesResponseType(StatusCodes.Status200OK)] |
| 21 | + public ActionResult<List<Payload>> Get() |
| 22 | + { |
| 23 | + return Ok(data); |
| 24 | + } |
| 25 | + |
| 26 | + [HttpGet("{id}")] |
| 27 | + [ProducesResponseType(StatusCodes.Status200OK)] |
| 28 | + [ProducesResponseType(StatusCodes.Status400BadRequest)] |
| 29 | + [ProducesResponseType(StatusCodes.Status404NotFound)] |
| 30 | + public ActionResult<Payload> Get(int id) |
| 31 | + { |
| 32 | + if (id == 0) |
19 | 33 | { |
20 | | - _serviceBusSender = serviceBusSender; |
| 34 | + return BadRequest(); |
21 | 35 | } |
22 | 36 |
|
23 | | - [HttpGet] |
24 | | - [ProducesResponseType(StatusCodes.Status200OK)] |
25 | | - public ActionResult<List<Payload>> Get() |
| 37 | + var result = data.Where(d => d.Id == id); |
| 38 | + |
| 39 | + if (result == null) |
26 | 40 | { |
27 | | - return Ok(data); |
| 41 | + return NotFound(); |
28 | 42 | } |
29 | 43 |
|
30 | | - [HttpGet("{id}")] |
31 | | - [ProducesResponseType(StatusCodes.Status200OK)] |
32 | | - [ProducesResponseType(StatusCodes.Status400BadRequest)] |
33 | | - [ProducesResponseType(StatusCodes.Status404NotFound)] |
34 | | - public ActionResult<Payload> Get(int id) |
| 44 | + return Ok(result); |
| 45 | + } |
| 46 | + |
| 47 | + [HttpPost] |
| 48 | + [ProducesResponseType(typeof(Payload), StatusCodes.Status200OK)] |
| 49 | + [ProducesResponseType(typeof(Payload), StatusCodes.Status409Conflict)] |
| 50 | + public async Task<IActionResult> Create([FromBody][Required] Payload request) |
| 51 | + { |
| 52 | + if (data.Any(d => d.Id == request.Id)) |
35 | 53 | { |
36 | | - if (id == 0) |
37 | | - { |
38 | | - return BadRequest(); |
39 | | - } |
| 54 | + return Conflict($"data with id {request.Id} already exists"); |
| 55 | + } |
| 56 | + |
| 57 | + data.Add(request); |
40 | 58 |
|
41 | | - var result = data.Where(d => d.Id == id); |
| 59 | + // Send this to the bus for the other services |
| 60 | + await _serviceBusSender.SendMessage(new MyPayload |
| 61 | + { |
| 62 | + Goals = request.Goals, |
| 63 | + Name = request.Name, |
| 64 | + Delete = false |
| 65 | + }).ConfigureAwait(false); |
42 | 66 |
|
43 | | - if (result == null) |
44 | | - { |
45 | | - return NotFound(); |
46 | | - } |
| 67 | + return Ok(request); |
| 68 | + } |
47 | 69 |
|
48 | | - return Ok(result); |
| 70 | + [HttpPut] |
| 71 | + [Route("{id}")] |
| 72 | + [ProducesResponseType(typeof(Payload), StatusCodes.Status200OK)] |
| 73 | + [ProducesResponseType(StatusCodes.Status400BadRequest)] |
| 74 | + [ProducesResponseType(typeof(string), StatusCodes.Status404NotFound)] |
| 75 | + [ProducesDefaultResponseType] |
| 76 | + public async Task<IActionResult> Update(int id, [FromBody][Required] Payload request) |
| 77 | + { |
| 78 | + if (!data.Any(d => d.Id == request.Id)) |
| 79 | + { |
| 80 | + return NotFound($"data with id {id} does not exist"); |
49 | 81 | } |
50 | 82 |
|
51 | | - [HttpPost] |
52 | | - [ProducesResponseType(typeof(Payload), StatusCodes.Status200OK)] |
53 | | - [ProducesResponseType(typeof(Payload), StatusCodes.Status409Conflict)] |
54 | | - public async Task<IActionResult> Create([FromBody][Required] Payload request) |
| 83 | + var item = data.First(d => d.Id == id); |
| 84 | + item.Name = request.Name; |
| 85 | + item.Goals = request.Goals; |
| 86 | + |
| 87 | + // Send this to the bus for the other services |
| 88 | + await _serviceBusSender.SendMessage(new MyPayload |
55 | 89 | { |
56 | | - if (data.Any(d => d.Id == request.Id)) |
57 | | - { |
58 | | - return Conflict($"data with id {request.Id} already exists"); |
59 | | - } |
60 | | - |
61 | | - data.Add(request); |
62 | | - |
63 | | - // Send this to the bus for the other services |
64 | | - await _serviceBusSender.SendMessage(new MyPayload |
65 | | - { |
66 | | - Goals = request.Goals, |
67 | | - Name = request.Name, |
68 | | - Delete = false |
69 | | - }).ConfigureAwait(false); |
70 | | - |
71 | | - return Ok(request); |
72 | | - } |
| 90 | + Goals = request.Goals, |
| 91 | + Name = request.Name, |
| 92 | + Delete = false |
| 93 | + }).ConfigureAwait(false); |
73 | 94 |
|
74 | | - [HttpPut] |
75 | | - [Route("{id}")] |
76 | | - [ProducesResponseType(typeof(Payload), StatusCodes.Status200OK)] |
77 | | - [ProducesResponseType(StatusCodes.Status400BadRequest)] |
78 | | - [ProducesResponseType(typeof(string), StatusCodes.Status404NotFound)] |
79 | | - [ProducesDefaultResponseType] |
80 | | - public async Task<IActionResult> Update(int id, [FromBody][Required] Payload request) |
| 95 | + return Ok(request); |
| 96 | + } |
| 97 | + |
| 98 | + [HttpDelete] |
| 99 | + [Route("{id}")] |
| 100 | + [ProducesResponseType(StatusCodes.Status200OK)] |
| 101 | + [ProducesResponseType(StatusCodes.Status404NotFound)] |
| 102 | + [ProducesResponseType(StatusCodes.Status409Conflict)] |
| 103 | + [ProducesResponseType(StatusCodes.Status400BadRequest)] |
| 104 | + [ProducesDefaultResponseType] |
| 105 | + public async Task<IActionResult> Delete([FromRoute] int id) |
| 106 | + { |
| 107 | + if (id == 0) |
81 | 108 | { |
82 | | - if (!data.Any(d => d.Id == request.Id)) |
83 | | - { |
84 | | - return NotFound($"data with id {id} does not exist"); |
85 | | - } |
86 | | - |
87 | | - var item = data.First(d => d.Id == id); |
88 | | - item.Name = request.Name; |
89 | | - item.Goals = request.Goals; |
90 | | - |
91 | | - // Send this to the bus for the other services |
92 | | - await _serviceBusSender.SendMessage(new MyPayload |
93 | | - { |
94 | | - Goals = request.Goals, |
95 | | - Name = request.Name, |
96 | | - Delete = false |
97 | | - }).ConfigureAwait(false); |
98 | | - |
99 | | - return Ok(request); |
| 109 | + return BadRequest(); |
100 | 110 | } |
101 | 111 |
|
102 | | - [HttpDelete] |
103 | | - [Route("{id}")] |
104 | | - [ProducesResponseType(StatusCodes.Status200OK)] |
105 | | - [ProducesResponseType(StatusCodes.Status404NotFound)] |
106 | | - [ProducesResponseType(StatusCodes.Status409Conflict)] |
107 | | - [ProducesResponseType(StatusCodes.Status400BadRequest)] |
108 | | - [ProducesDefaultResponseType] |
109 | | - public async Task<IActionResult> Delete([FromRoute] int id) |
| 112 | + if (!data.Any(d => d.Id == id)) |
110 | 113 | { |
111 | | - if (id == 0) |
112 | | - { |
113 | | - return BadRequest(); |
114 | | - } |
115 | | - |
116 | | - if (!data.Any(d => d.Id == id)) |
117 | | - { |
118 | | - return NotFound($"data with id {id} does not exist"); |
119 | | - } |
120 | | - |
121 | | - var item = data.First(d => d.Id == id); |
122 | | - data.Remove(item); |
123 | | - |
124 | | - // Send this to the bus for the other services |
125 | | - await _serviceBusSender.SendMessage(new MyPayload |
126 | | - { |
127 | | - Goals = item.Goals, |
128 | | - Name = item.Name, |
129 | | - Delete = true |
130 | | - }).ConfigureAwait(false); |
131 | | - |
132 | | - return Ok(); |
| 114 | + return NotFound($"data with id {id} does not exist"); |
133 | 115 | } |
134 | 116 |
|
135 | | - private static readonly List<Payload> data = new List<Payload> |
| 117 | + var item = data.First(d => d.Id == id); |
| 118 | + data.Remove(item); |
| 119 | + |
| 120 | + // Send this to the bus for the other services |
| 121 | + await _serviceBusSender.SendMessage(new MyPayload |
136 | 122 | { |
137 | | - new Payload{ Id=1, Goals=3, Name="wow"}, |
138 | | - new Payload{ Id=2, Goals=4, Name="not so bad"}, |
139 | | - }; |
| 123 | + Goals = item.Goals, |
| 124 | + Name = item.Name, |
| 125 | + Delete = true |
| 126 | + }).ConfigureAwait(false); |
| 127 | + |
| 128 | + return Ok(); |
140 | 129 | } |
| 130 | + |
| 131 | + private static readonly List<Payload> data = new List<Payload> |
| 132 | + { |
| 133 | + new Payload{ Id=1, Goals=3, Name="wow"}, |
| 134 | + new Payload{ Id=2, Goals=4, Name="not so bad"}, |
| 135 | + }; |
141 | 136 | } |
0 commit comments