Skip to content

Commit 2a553ba

Browse files
committed
Update to .NET 7
1 parent b3adab3 commit 2a553ba

26 files changed

Lines changed: 675 additions & 733 deletions

AspNetCoreServiceBusApi1/AspNetCoreServiceBusApi1.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>net7.0</TargetFramework>
55
<UserSecretsId>d0554286-1d65-4180-9043-bc78d9085329</UserSecretsId>
6+
<ImplicitUsings>enable</ImplicitUsings>
67
</PropertyGroup>
78

89
<ItemGroup>
Lines changed: 106 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,136 @@
1-
using System.Collections.Generic;
2-
using System.ComponentModel.DataAnnotations;
3-
using System.Linq;
4-
using System.Threading.Tasks;
1+
using System.ComponentModel.DataAnnotations;
52
using AspNetCoreServiceBusApi1.Model;
6-
using Microsoft.AspNetCore.Http;
73
using Microsoft.AspNetCore.Mvc;
84
using ServiceBusMessaging;
95

10-
namespace AspNetCoreServiceBusApi1.Controllers
6+
namespace AspNetCoreServiceBusApi1.Controllers;
7+
8+
[Route("api/[controller]")]
9+
[ApiController]
10+
public class PayloadController : Controller
1111
{
12-
[Route("api/[controller]")]
13-
[ApiController]
14-
public class PayloadController : Controller
12+
private ServiceBusSender _serviceBusSender;
13+
14+
public PayloadController(ServiceBusSender serviceBusSender)
1515
{
16-
private ServiceBusSender _serviceBusSender;
16+
_serviceBusSender = serviceBusSender;
17+
}
1718

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)
1933
{
20-
_serviceBusSender = serviceBusSender;
34+
return BadRequest();
2135
}
2236

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)
2640
{
27-
return Ok(data);
41+
return NotFound();
2842
}
2943

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))
3553
{
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);
4058

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);
4266

43-
if (result == null)
44-
{
45-
return NotFound();
46-
}
67+
return Ok(request);
68+
}
4769

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");
4981
}
5082

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
5589
{
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);
7394

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)
81108
{
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();
100110
}
101111

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))
110113
{
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");
133115
}
134116

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
136122
{
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();
140129
}
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+
};
141136
}

0 commit comments

Comments
 (0)