Skip to content

Commit

Permalink
Merge pull request #8 from Azure-Samples/core3.1
Browse files Browse the repository at this point in the history
upgrade to core 3.1
  • Loading branch information
cephalin authored Apr 29, 2020
2 parents d74edae + f92a08f commit 23daf49
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 162 deletions.
116 changes: 63 additions & 53 deletions Controllers/TodoController.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using TodoApi.Models;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using TodoApi.Models;

#region TodoController
namespace TodoApi.Controllers
{
[Route("api/[controller]")]
[Route("api/[controller]")]
[ApiController]
public class TodoController : Controller
{
private readonly TodoContext _context;
#endregion

public TodoController(TodoContext context)
{
Expand All @@ -27,81 +28,90 @@ public TodoController(TodoContext context)
}
}

#region snippet_GetAll
// GET: api/Todo
[HttpGet]
public IEnumerable<TodoItem> GetAll()
public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItem()
{
return _context.TodoItems.ToList();
return await _context.TodoItems.ToListAsync();
}

#region snippet_GetByID
[HttpGet("{id}", Name = "GetTodo")]
public IActionResult GetById(long id)
// GET: api/Todo/5
[HttpGet("{id}")]
public async Task<ActionResult<TodoItem>> GetTodoItem(long id)
{
var item = _context.TodoItems.FirstOrDefault(t => t.Id == id);
if (item == null)
var todoItem = await _context.TodoItems.FindAsync(id);

if (todoItem == null)
{
return NotFound();
}
return new ObjectResult(item);
}
#endregion
#endregion
#region snippet_Create
[HttpPost]
public IActionResult Create([FromBody] TodoItem item)
{
if (item == null)
{
return BadRequest();
}

_context.TodoItems.Add(item);
_context.SaveChanges();

return CreatedAtRoute("GetTodo", new { id = item.Id }, item);
return todoItem;
}
#endregion

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

var todo = _context.TodoItems.FirstOrDefault(t => t.Id == id);
if (todo == null)
_context.Entry(todoItem).State = EntityState.Modified;

try
{
return NotFound();
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!TodoItemExists(id))
{
return NotFound();
}
else
{
throw;
}
}

todo.IsComplete = item.IsComplete;
todo.Name = item.Name;
return NoContent();
}

_context.TodoItems.Update(todo);
_context.SaveChanges();
return new NoContentResult();
// POST: api/Todo
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
[HttpPost]
public async Task<ActionResult<TodoItem>> PostTodoItem(TodoItem todoItem)
{
_context.TodoItems.Add(todoItem);
await _context.SaveChangesAsync();

return CreatedAtAction("GetTodoItem", new { id = todoItem.Id }, todoItem);
}
#endregion

#region snippet_Delete
// DELETE: api/Todo/5
[HttpDelete("{id}")]
public IActionResult Delete(long id)
public async Task<ActionResult<TodoItem>> DeleteTodoItem(long id)
{
var todo = _context.TodoItems.FirstOrDefault(t => t.Id == id);
if (todo == null)
var todoItem = await _context.TodoItems.FindAsync(id);
if (todoItem == null)
{
return NotFound();
}

_context.TodoItems.Remove(todo);
_context.SaveChanges();
return new NoContentResult();
_context.TodoItems.Remove(todoItem);
await _context.SaveChangesAsync();

return todoItem;
}

private bool TodoItemExists(long id)
{
return _context.TodoItems.Any(e => e.Id == id);
}
#endregion
}
}

28 changes: 0 additions & 28 deletions Controllers/TodoController2.cs

This file was deleted.

41 changes: 0 additions & 41 deletions Controllers/ValuesController.cs

This file was deleted.

3 changes: 1 addition & 2 deletions Models/TodoContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ namespace TodoApi.Models
{
public class TodoContext : DbContext
{
public TodoContext(DbContextOptions<TodoContext> options)
public TodoContext (DbContextOptions<TodoContext> options)
: base(options)
{
}

public DbSet<TodoItem> TodoItems { get; set; }

}
}
21 changes: 10 additions & 11 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace TodoApi
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();

host.Run();
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
16 changes: 16 additions & 0 deletions Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true
},
"profiles": {
"TodoApi": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:5000"
}
}
}
49 changes: 39 additions & 10 deletions Startup.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,69 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using TodoApi.Models;
using Swashbuckle.AspNetCore.Swagger;

namespace TodoApi
{
public class Startup
{
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<TodoContext>(opt => opt.UseInMemoryDatabase("TodoList"));
services.AddMvc();
services.AddControllers();

// Register the Swagger generator, defining one or more Swagger documents
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});

services.AddDbContext<TodoContext>(options => options.UseInMemoryDatabase("TodoList"));
}

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

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

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

//app.UseHttpsRedirection();

app.UseDefaultFiles();

app.UseStaticFiles();

app.UseMvc();
app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
Loading

0 comments on commit 23daf49

Please sign in to comment.