Skip to content

Commit 23daf49

Browse files
authored
Merge pull request #8 from Azure-Samples/core3.1
upgrade to core 3.1
2 parents d74edae + f92a08f commit 23daf49

10 files changed

+150
-162
lines changed

Controllers/TodoController.cs

+63-53
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
using System.Collections.Generic;
21
using Microsoft.AspNetCore.Mvc;
3-
using TodoApi.Models;
2+
using Microsoft.AspNetCore.Mvc.Filters;
3+
using Microsoft.EntityFrameworkCore;
4+
using Newtonsoft.Json;
5+
using System.Collections.Generic;
46
using System.Linq;
57
using System.Net.Http;
6-
using Newtonsoft.Json;
7-
using Microsoft.AspNetCore.Mvc.Filters;
88
using System.Net.Http.Headers;
9+
using System.Threading.Tasks;
10+
using TodoApi.Models;
911

10-
#region TodoController
1112
namespace TodoApi.Controllers
1213
{
13-
[Route("api/[controller]")]
14+
[Route("api/[controller]")]
15+
[ApiController]
1416
public class TodoController : Controller
1517
{
1618
private readonly TodoContext _context;
17-
#endregion
1819

1920
public TodoController(TodoContext context)
2021
{
@@ -27,81 +28,90 @@ public TodoController(TodoContext context)
2728
}
2829
}
2930

30-
#region snippet_GetAll
31+
// GET: api/Todo
3132
[HttpGet]
32-
public IEnumerable<TodoItem> GetAll()
33+
public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItem()
3334
{
34-
return _context.TodoItems.ToList();
35+
return await _context.TodoItems.ToListAsync();
3536
}
3637

37-
#region snippet_GetByID
38-
[HttpGet("{id}", Name = "GetTodo")]
39-
public IActionResult GetById(long id)
38+
// GET: api/Todo/5
39+
[HttpGet("{id}")]
40+
public async Task<ActionResult<TodoItem>> GetTodoItem(long id)
4041
{
41-
var item = _context.TodoItems.FirstOrDefault(t => t.Id == id);
42-
if (item == null)
42+
var todoItem = await _context.TodoItems.FindAsync(id);
43+
44+
if (todoItem == null)
4345
{
4446
return NotFound();
4547
}
46-
return new ObjectResult(item);
47-
}
48-
#endregion
49-
#endregion
50-
#region snippet_Create
51-
[HttpPost]
52-
public IActionResult Create([FromBody] TodoItem item)
53-
{
54-
if (item == null)
55-
{
56-
return BadRequest();
57-
}
58-
59-
_context.TodoItems.Add(item);
60-
_context.SaveChanges();
6148

62-
return CreatedAtRoute("GetTodo", new { id = item.Id }, item);
49+
return todoItem;
6350
}
64-
#endregion
6551

66-
#region snippet_Update
52+
// PUT: api/Todo/5
53+
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
54+
// more details see https://aka.ms/RazorPagesCRUD.
6755
[HttpPut("{id}")]
68-
public IActionResult Update(long id, [FromBody] TodoItem item)
56+
public async Task<IActionResult> PutTodoItem(long id, TodoItem todoItem)
6957
{
70-
if (item == null || item.Id != id)
58+
if (id != todoItem.Id)
7159
{
7260
return BadRequest();
7361
}
7462

75-
var todo = _context.TodoItems.FirstOrDefault(t => t.Id == id);
76-
if (todo == null)
63+
_context.Entry(todoItem).State = EntityState.Modified;
64+
65+
try
7766
{
78-
return NotFound();
67+
await _context.SaveChangesAsync();
68+
}
69+
catch (DbUpdateConcurrencyException)
70+
{
71+
if (!TodoItemExists(id))
72+
{
73+
return NotFound();
74+
}
75+
else
76+
{
77+
throw;
78+
}
7979
}
8080

81-
todo.IsComplete = item.IsComplete;
82-
todo.Name = item.Name;
81+
return NoContent();
82+
}
8383

84-
_context.TodoItems.Update(todo);
85-
_context.SaveChanges();
86-
return new NoContentResult();
84+
// POST: api/Todo
85+
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
86+
// more details see https://aka.ms/RazorPagesCRUD.
87+
[HttpPost]
88+
public async Task<ActionResult<TodoItem>> PostTodoItem(TodoItem todoItem)
89+
{
90+
_context.TodoItems.Add(todoItem);
91+
await _context.SaveChangesAsync();
92+
93+
return CreatedAtAction("GetTodoItem", new { id = todoItem.Id }, todoItem);
8794
}
88-
#endregion
8995

90-
#region snippet_Delete
96+
// DELETE: api/Todo/5
9197
[HttpDelete("{id}")]
92-
public IActionResult Delete(long id)
98+
public async Task<ActionResult<TodoItem>> DeleteTodoItem(long id)
9399
{
94-
var todo = _context.TodoItems.FirstOrDefault(t => t.Id == id);
95-
if (todo == null)
100+
var todoItem = await _context.TodoItems.FindAsync(id);
101+
if (todoItem == null)
96102
{
97103
return NotFound();
98104
}
99105

100-
_context.TodoItems.Remove(todo);
101-
_context.SaveChanges();
102-
return new NoContentResult();
106+
_context.TodoItems.Remove(todoItem);
107+
await _context.SaveChangesAsync();
108+
109+
return todoItem;
110+
}
111+
112+
private bool TodoItemExists(long id)
113+
{
114+
return _context.TodoItems.Any(e => e.Id == id);
103115
}
104-
#endregion
105116
}
106117
}
107-

Controllers/TodoController2.cs

-28
This file was deleted.

Controllers/ValuesController.cs

-41
This file was deleted.

Models/TodoContext.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ namespace TodoApi.Models
44
{
55
public class TodoContext : DbContext
66
{
7-
public TodoContext(DbContextOptions<TodoContext> options)
7+
public TodoContext (DbContextOptions<TodoContext> options)
88
: base(options)
99
{
1010
}
1111

1212
public DbSet<TodoItem> TodoItems { get; set; }
13-
1413
}
1514
}

Program.cs

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
using Microsoft.AspNetCore.Builder;
2-
using Microsoft.AspNetCore.Hosting;
3-
using System.IO;
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Hosting;
43

54
namespace TodoApi
65
{
76
public class Program
87
{
98
public static void Main(string[] args)
109
{
11-
var host = new WebHostBuilder()
12-
.UseKestrel()
13-
.UseContentRoot(Directory.GetCurrentDirectory())
14-
.UseIISIntegration()
15-
.UseStartup<Startup>()
16-
.Build();
17-
18-
host.Run();
10+
CreateHostBuilder(args).Build().Run();
1911
}
12+
13+
public static IHostBuilder CreateHostBuilder(string[] args) =>
14+
Host.CreateDefaultBuilder(args)
15+
.ConfigureWebHostDefaults(webBuilder =>
16+
{
17+
webBuilder.UseStartup<Startup>();
18+
});
2019
}
2120
}

Properties/launchSettings.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true
5+
},
6+
"profiles": {
7+
"TodoApi": {
8+
"commandName": "Project",
9+
"launchBrowser": true,
10+
"environmentVariables": {
11+
"ASPNETCORE_ENVIRONMENT": "Development"
12+
},
13+
"applicationUrl": "http://localhost:5000"
14+
}
15+
}
16+
}

Startup.cs

+39-10
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,69 @@
11
using Microsoft.AspNetCore.Builder;
2-
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.Extensions.Configuration;
34
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.Hosting;
6+
using Microsoft.EntityFrameworkCore;
7+
using Microsoft.OpenApi.Models;
48
using TodoApi.Models;
5-
using Swashbuckle.AspNetCore.Swagger;
69

710
namespace TodoApi
811
{
912
public class Startup
10-
{
13+
{
14+
public Startup(IConfiguration configuration)
15+
{
16+
Configuration = configuration;
17+
}
18+
19+
public IConfiguration Configuration { get; }
20+
21+
// This method gets called by the runtime. Use this method to add services to the container.
1122
public void ConfigureServices(IServiceCollection services)
1223
{
13-
services.AddDbContext<TodoContext>(opt => opt.UseInMemoryDatabase("TodoList"));
14-
services.AddMvc();
24+
services.AddControllers();
1525

16-
// Register the Swagger generator, defining one or more Swagger documents
26+
// Register the Swagger generator, defining 1 or more Swagger documents
1727
services.AddSwaggerGen(c =>
1828
{
19-
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
29+
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
2030
});
31+
32+
services.AddDbContext<TodoContext>(options => options.UseInMemoryDatabase("TodoList"));
2133
}
2234

23-
public void Configure(IApplicationBuilder app)
35+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
36+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
2437
{
2538
// Enable middleware to serve generated Swagger as a JSON endpoint.
2639
app.UseSwagger();
2740

28-
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
41+
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
42+
// specifying the Swagger JSON endpoint.
2943
app.UseSwaggerUI(c =>
3044
{
3145
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
3246
});
3347

48+
if (env.IsDevelopment())
49+
{
50+
app.UseDeveloperExceptionPage();
51+
}
52+
53+
//app.UseHttpsRedirection();
54+
3455
app.UseDefaultFiles();
56+
3557
app.UseStaticFiles();
3658

37-
app.UseMvc();
59+
app.UseRouting();
60+
61+
app.UseAuthorization();
62+
63+
app.UseEndpoints(endpoints =>
64+
{
65+
endpoints.MapControllers();
66+
});
3867
}
3968
}
4069
}

0 commit comments

Comments
 (0)