Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to run with .Net 6 #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,6 @@ __pycache__/
*.btm.cs
*.odx.cs
*.xsd.cs

# VS Code
.vscode
37 changes: 18 additions & 19 deletions Controllers/TodoController.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using TodoApi.Models;

namespace TodoApi.Controllers
{
[Route("api/[controller]")]
[Route("api/[controller]")]
[ApiController]
public class TodoController : Controller
{
Expand All @@ -20,25 +13,27 @@ public class TodoController : Controller
public TodoController(TodoContext context)
{
_context = context;

if (_context.TodoItems.Count() == 0)
{
_context.TodoItems.Add(new TodoItem { Name = "Item1" });
_context.SaveChanges();
}
}

// GET: api/Todo
[HttpGet]
public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItem()
public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItems()
{
if (_context.TodoItems == null)
{
return NotFound();
}
return await _context.TodoItems.ToListAsync();
}

// GET: api/Todo/5
[HttpGet("{id}")]
public async Task<ActionResult<TodoItem>> GetTodoItem(long id)
{
if (_context.TodoItems == null)
{
return NotFound();
}
var todoItem = await _context.TodoItems.FindAsync(id);

if (todoItem == null)
Expand Down Expand Up @@ -90,13 +85,17 @@ public async Task<ActionResult<TodoItem>> PostTodoItem(TodoItem todoItem)
_context.TodoItems.Add(todoItem);
await _context.SaveChangesAsync();

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

// DELETE: api/Todo/5
[HttpDelete("{id}")]
public async Task<ActionResult<TodoItem>> DeleteTodoItem(long id)
public async Task<IActionResult> DeleteTodoItem(long id)
{
if (_context.TodoItems == null)
{
return NotFound();
}
var todoItem = await _context.TodoItems.FindAsync(id);
if (todoItem == null)
{
Expand All @@ -106,12 +105,12 @@ public async Task<ActionResult<TodoItem>> DeleteTodoItem(long id)
_context.TodoItems.Remove(todoItem);
await _context.SaveChangesAsync();

return todoItem;
return NoContent();
}

private bool TodoItemExists(long id)
{
return _context.TodoItems.Any(e => e.Id == id);
return (_context.TodoItems?.Any(e => e.Id == id)).GetValueOrDefault(false);
}
}
}
4 changes: 2 additions & 2 deletions Models/TodoContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +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; }
public DbSet<TodoItem> TodoItems { get; set; } = null!;
}
}
2 changes: 1 addition & 1 deletion Models/TodoItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class TodoItem
{
public long Id { get; set; }
public string Name { get; set; }
public string? Name { get; set; }
public bool IsComplete { get; set; }
}
}
25 changes: 20 additions & 5 deletions Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
{
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:23810",
"sslPort": 44332
}
},
"profiles": {
"TodoApi": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "api/todo",
"applicationUrl": "https://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:5000"
}
}
}
}
}
40 changes: 13 additions & 27 deletions Startup.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using TodoApi.Models;

Expand All @@ -26,40 +21,31 @@ public void ConfigureServices(IServiceCollection services)
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
c.SwaggerDoc("v1", new OpenApiInfo { Title = "TodoApi", Version = "v1" });
});
services.AddDbContext<TodoContext>(options =>
{
options.UseInMemoryDatabase("TodoList");
});

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

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
public void Configure(IApplicationBuilder app, IWebHostEnvironment environment)
{
if (environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// 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.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});

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

//app.UseHttpsRedirection();

app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "TodoApi v1"));
app.UseDefaultFiles();

app.UseStaticFiles();

app.UseHttpsRedirection();
app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
Expand Down
18 changes: 9 additions & 9 deletions TodoApi.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="EntityFramework" Version="6.4.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.3">
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.8">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.4.0" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.8" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

</Project>
3 changes: 1 addition & 2 deletions appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
"Microsoft.AspNetCore": "Warning"
}
}
}
2 changes: 1 addition & 1 deletion appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
}
},
"AllowedHosts": "*"
}
}
2 changes: 1 addition & 1 deletion wwwroot/app/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ angular.module('todoApp', ['ngRoute'])
templateUrl: "/App/Views/TodoList.html",
}).otherwise({ redirectTo: "/Home" });

}]);
}]);
12 changes: 6 additions & 6 deletions wwwroot/app/scripts/todoListSvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ angular.module('todoApp')

return {
getItems : function(){
return $http.get(apiEndpoint + '/api/Todo');
return $http.get(apiEndpoint + '/api/todo');
},
getItem : function(id){
return $http.get(apiEndpoint + '/api/Todo/' + id);
return $http.get(apiEndpoint + '/api/todo/' + id);
},
postItem : function(item){
return $http.post(apiEndpoint + '/api/Todo', item);
return $http.post(apiEndpoint + '/api/todo', item);
},
putItem : function(item){
return $http.put(apiEndpoint + '/api/Todo/' + item.id, item);
return $http.put(apiEndpoint + '/api/todo/' + item.id, item);
},
deleteItem : function(id){
return $http({
method: 'DELETE',
url: apiEndpoint + '/api/Todo/' + id
url: apiEndpoint + '/api/todo/' + id
});
}
};
}]);
}]);