Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jared Mahan committed Apr 28, 2018
1 parent a8ae191 commit 86ffcca
Show file tree
Hide file tree
Showing 13 changed files with 342 additions and 1 deletion.
46 changes: 46 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.1/core-api.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart",
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}"
},
"osx": {
"command": "open"
},
"linux": {
"command": "xdg-open"
}
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
,]
}
15 changes: 15 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/core-api.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
91 changes: 91 additions & 0 deletions Controllers/EmployeeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Linq;
using CoreApi.Models;
using Microsoft.AspNetCore.Mvc;

namespace CoreApi.Controllers {
[Route ("api/employee")]
[ApiController]
public class EmployeeController : Controller {
private readonly CoreApiContext _context;

public EmployeeController (CoreApiContext context) {
_context = context;
if (_context.Employees.Count () == 0) {
// Add Some Initial Data since we are using In-Memory
_context.Add (new Employee {
FirstName = "Jared",
LastName = "Mahan",
Birthday = new DateTime (1987, 3, 30)
});
_context.Add (new Employee {
FirstName = "Heather",
LastName = "Mahan",
Birthday = new DateTime (1987, 12, 28)
});
_context.Add (new Employee {
FirstName = "Jase",
LastName = "Mahan",
Birthday = new DateTime (2014, 2, 20)
});
_context.Add (new Employee {
FirstName = "Caden",
LastName = "Mahan",
Birthday = new DateTime (2009, 5, 21)
});
_context.SaveChanges ();
}
}

[HttpGet]
public ActionResult<IEnumerable<Employee>> GetAll () {
return _context.Employees;
}

[HttpGet ("{id}", Name = "GetEmployee")]
public ActionResult<Employee> GetById (long id) {
var item = _context.Employees.Find (id);
if (item == null) {
return NotFound ();
}
return item;
}

[HttpPost]
public IActionResult Create (Employee employee) {
_context.Add (employee);
_context.SaveChanges ();

return CreatedAtRoute ("GetEmployee", new { id = employee.Id }, employee);
}

[HttpPut ("{id}")]
public IActionResult Update (long id, Employee employee) {
var result = _context.Employees.Find (id);
if (result == null) {
return NotFound ();
}

result.FirstName = employee.FirstName;
result.LastName = employee.LastName;
result.Birthday = employee.Birthday;

_context.Employees.Update (result);
_context.SaveChanges ();
return NoContent ();
}

[HttpDelete ("{id}")]
public IActionResult Delete (long id) {
var employee = _context.Employees.Find (id);
if (employee == null) {
return NotFound ();
}

_context.Employees.Remove (employee);
_context.SaveChanges ();
return NoContent ();
}
}
}
44 changes: 44 additions & 0 deletions Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace CoreApi.Controllers
{
[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}

// GET api/values/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}

// POST api/values
[HttpPost]
public void Post([FromBody]string value)
{
}

// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody]string value)
{
}

// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}
8 changes: 8 additions & 0 deletions Models/CoreApiContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Microsoft.EntityFrameworkCore;

namespace CoreApi.Models {
public class CoreApiContext : DbContext {
public CoreApiContext (DbContextOptions<CoreApiContext> options) : base (options) { }
public DbSet<Employee> Employees { get; set; }
}
}
9 changes: 9 additions & 0 deletions Models/Employee.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace CoreApi.Models {
public class Employee: Entity<long> {
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime Birthday { get; set; }
}
}
5 changes: 5 additions & 0 deletions Models/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace CoreApi.Models {
public abstract class Entity<T> {
public T Id { get; set; }
}
}
25 changes: 25 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace core_api
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# simpleEmployeeApi
# Simple Employee API
This is a simple .NET Core 2.1 API that utilizes an in-memory database via the Entity Framework DbContext
- Also setup to utilize swagger (Open API) so you can easily test actions
52 changes: 52 additions & 0 deletions Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoreApi.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.Swagger;

namespace core_api {
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<CoreApiContext> (opt => opt.UseInMemoryDatabase ("Employee"));
services.AddMvc ();

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

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure (IApplicationBuilder app, IHostingEnvironment env) {
if (env.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");
});

app.UseMvc ();
}
}
}
10 changes: 10 additions & 0 deletions appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
15 changes: 15 additions & 0 deletions appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
}
}
19 changes: 19 additions & 0 deletions core-api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0-preview2-final" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.0-preview2-final" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.0-preview2-final" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="2.4.0" />
</ItemGroup>


</Project>

0 comments on commit 86ffcca

Please sign in to comment.