From 820d6eb99165ef78c1f20ec479c74b5eb4793057 Mon Sep 17 00:00:00 2001 From: gavindya Date: Sat, 16 Jun 2018 16:20:11 +0530 Subject: [PATCH 1/6] WebAPI with CRUD operations --- Controllers/CarController.cs | 143 ++++++++++++++++++++++++++++++++ Controllers/ValuesController.cs | 45 ++++++++++ Models/Car.cs | 12 +++ Models/CarContext.cs | 14 ++++ Program.cs | 24 ++++++ Properties/launchSettings.json | 30 +++++++ Startup.cs | 26 ++++++ appsettings.Development.json | 9 ++ appsettings.json | 8 ++ coding.csproj | 16 ++++ wwwroot/index.html | 76 +++++++++++++++++ wwwroot/site.js | 118 ++++++++++++++++++++++++++ 12 files changed, 521 insertions(+) create mode 100644 Controllers/CarController.cs create mode 100644 Controllers/ValuesController.cs create mode 100644 Models/Car.cs create mode 100644 Models/CarContext.cs create mode 100644 Program.cs create mode 100644 Properties/launchSettings.json create mode 100644 Startup.cs create mode 100644 appsettings.Development.json create mode 100644 appsettings.json create mode 100644 coding.csproj create mode 100644 wwwroot/index.html create mode 100644 wwwroot/site.js diff --git a/Controllers/CarController.cs b/Controllers/CarController.cs new file mode 100644 index 0000000..e1a203d --- /dev/null +++ b/Controllers/CarController.cs @@ -0,0 +1,143 @@ +using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; +using System.Linq; +using coding.Models; +using System; +using System.IO; +using Newtonsoft.Json; + +namespace coding.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class CarController : ControllerBase + { + private readonly CarContext _context; + private string _path = Environment.CurrentDirectory+"\\data.json"; + private void AddCarsToContext(){ + _context.Cars.Add(new Car { Manufacturer = "Ford",Make="Mustang",Model="GT",Year=2015 }); + _context.Cars.Add(new Car { Manufacturer = "Chevrolet",Make="Corvette",Model="Z06",Year=2017 }); + _context.Cars.Add(new Car { Manufacturer = "Dodge",Make="Challenger",Model="Hellcat",Year=2016 }); + _context.SaveChanges(); + } + private void WriteToJSONfile(){ + + try{ + // Create a file to write to. + using (StreamWriter file = System.IO.File.CreateText(_path)) + { + JsonSerializer serializer = new JsonSerializer(); + serializer.Serialize(file, _context.Cars.ToList()); + } + }catch(Exception e){ + Console.WriteLine("Exception information: {0}", e); + } + } + private List ReadFromJSONfile(){ + try{ + List carsList; + using (StreamReader file = System.IO.File.OpenText(_path)) + { + JsonSerializer serializer = new JsonSerializer(); + carsList = (List)serializer.Deserialize(file, typeof(List)); + } + foreach (var c in carsList) { + _context.Cars.Add(c); + } + _context.SaveChanges(); + }catch(Exception e){ + Console.WriteLine("Exception information: {0}", e); + //In case ofan exception, 3 cars still wold be added to the _context + AddCarsToContext(); + } + return _context.Cars.ToList(); + } + + public CarController(CarContext context) + { + _context = context; + + if (_context.Cars.Count() == 0) + { + if (!System.IO.File.Exists(_path)) + { + AddCarsToContext(); + WriteToJSONfile(); + } + else{ + Console.WriteLine("\n\ndata file exist\n\n"); + ReadFromJSONfile(); + } + } + } + + //Get all cars + [HttpGet] + public ActionResult> GetAll() + { + return _context.Cars.ToList(); + } + + //Get one car given the ID + [HttpGet("{id}", Name = "GetCar")] + public ActionResult GetById(long id) + { + var item = _context.Cars.Find(id); + if (item == null) + { + return NotFound(); + } + return item; + } + + //Create Car + [HttpPost] + public IActionResult Create(Car car) + { + _context.Cars.Add(car); + _context.SaveChanges(); + WriteToJSONfile(); + return CreatedAtRoute("GetCar", new { id = car.Id }, car); + } + + + //Edit Method + [HttpPut("{id}")] + public IActionResult Update(long id, Car item) + { + var car = _context.Cars.Find(id); + if (car == null) + { + return NotFound(); + } + + car.Manufacturer = item.Manufacturer; + car.Make = item.Make; + car.Manufacturer = item.Manufacturer; + car.Year = item.Year; + + _context.Cars.Update(car); + _context.SaveChanges(); + WriteToJSONfile(); + return NoContent(); + } + + //Delete Method + [HttpDelete("{id}")] + public IActionResult Delete(long id) + { + var car = _context.Cars.Find(id); + if (car == null) + { + return NotFound(); + } + + _context.Cars.Remove(car); + _context.SaveChanges(); + WriteToJSONfile(); + return NoContent(); + } + } + + +} \ No newline at end of file diff --git a/Controllers/ValuesController.cs b/Controllers/ValuesController.cs new file mode 100644 index 0000000..6e1bf7b --- /dev/null +++ b/Controllers/ValuesController.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; + +namespace coding.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class ValuesController : ControllerBase + { + // GET api/values + [HttpGet] + public ActionResult> Get() + { + return new string[] { "value1", "value2" }; + } + + // GET api/values/5 + [HttpGet("{id}")] + public ActionResult 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) + { + } + } +} diff --git a/Models/Car.cs b/Models/Car.cs new file mode 100644 index 0000000..520414d --- /dev/null +++ b/Models/Car.cs @@ -0,0 +1,12 @@ +namespace coding.Models +{ + public class Car + { + public long Id { get; set; } + public string Manufacturer { get; set; } + public string Make { get; set; } + public string Model { get; set; } + public int Year { get; set; } + + } +} \ No newline at end of file diff --git a/Models/CarContext.cs b/Models/CarContext.cs new file mode 100644 index 0000000..b00ef59 --- /dev/null +++ b/Models/CarContext.cs @@ -0,0 +1,14 @@ +using Microsoft.EntityFrameworkCore; + +namespace coding.Models +{ + public class CarContext : DbContext + { + public CarContext(DbContextOptions options) + : base(options) + { + } + + public DbSet Cars { get; set; } + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..5318d22 --- /dev/null +++ b/Program.cs @@ -0,0 +1,24 @@ +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 coding +{ + public class Program + { + public static void Main(string[] args) + { + CreateWebHostBuilder(args).Build().Run(); + } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + WebHost.CreateDefaultBuilder(args) + .UseStartup(); + } +} diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json new file mode 100644 index 0000000..cacc76f --- /dev/null +++ b/Properties/launchSettings.json @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:26620", + "sslPort": 44386 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "api/car", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "coding": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "api/car", + "applicationUrl": "https://localhost:5001;http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} \ No newline at end of file diff --git a/Startup.cs b/Startup.cs new file mode 100644 index 0000000..8d1db3f --- /dev/null +++ b/Startup.cs @@ -0,0 +1,26 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using coding.Models; + +namespace coding +{ + public class Startup + { + public void ConfigureServices(IServiceCollection services) + { + services.AddDbContext(opt => + opt.UseInMemoryDatabase("CarsList")); + services.AddMvc() + .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); + } + + public void Configure(IApplicationBuilder app) + { + app.UseDefaultFiles(); + app.UseStaticFiles(); + app.UseMvc(); + } + } +} \ No newline at end of file diff --git a/appsettings.Development.json b/appsettings.Development.json new file mode 100644 index 0000000..e203e94 --- /dev/null +++ b/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git a/appsettings.json b/appsettings.json new file mode 100644 index 0000000..def9159 --- /dev/null +++ b/appsettings.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/coding.csproj b/coding.csproj new file mode 100644 index 0000000..276e864 --- /dev/null +++ b/coding.csproj @@ -0,0 +1,16 @@ + + + + netcoreapp2.1 + + + + + + + + + + + + diff --git a/wwwroot/index.html b/wwwroot/index.html new file mode 100644 index 0000000..7e82eb5 --- /dev/null +++ b/wwwroot/index.html @@ -0,0 +1,76 @@ + + + + + Cars CRUD + + + +

Cars CRUD

+

Add

+
+ + + + + +
+ +
+

Edit

+
+ + + + + + + + +
+
+ +

+ + + + + + + + + + + +
ManufacturerMakeModelYear
+ + + + + \ No newline at end of file diff --git a/wwwroot/site.js b/wwwroot/site.js new file mode 100644 index 0000000..cdae09b --- /dev/null +++ b/wwwroot/site.js @@ -0,0 +1,118 @@ +const uri = 'api/car'; +let cars = null; +function getCount(data) { + const el = $('#counter'); + let name = 'to-do'; + if (data) { + if (data > 1) { + name = 'to-dos'; + } + el.text(data + ' ' + name); + } else { + el.html('No ' + name); + } +} + +$(document).ready(function () { + getData(); +}); + +function getData() { + $.ajax({ + type: 'GET', + url: uri, + success: function (data) { + $('#cars').empty(); + getCount(data.length); + $.each(data, function (key, car) { + // const checked = item.isComplete ? 'checked' : ''; + + $('' + + '' + car.manufacturer + '' + + '' + car.make + '' + + '' + car.model + '' + + '' + car.year + '' + + '' + + '' + + '').appendTo($('#cars')); + }); + + cars = data; + } + }); +} + +function addItem() { + const car = { + 'manufacturer': $('#add-manufacturer').val(), + 'make': $('#add-make').val(), + 'model': $('#add-model').val(), + 'year': $('#add-year').val(), + }; + + $.ajax({ + type: 'POST', + accepts: 'application/json', + url: uri, + contentType: 'application/json', + data: JSON.stringify(car), + error: function (jqXHR, textStatus, errorThrown) { + alert('here'); + }, + success: function (result) { + getData(); + $('#add-manufacturer').val(''); + } + }); +} + +function deleteItem(id) { + $.ajax({ + url: uri + '/' + id, + type: 'DELETE', + success: function (result) { + getData(); + } + }); +} + +function editItem(id) { + $.each(cars, function (key, car) { + if (car.id === id) { + $('#edit-manufacturer').val(car.manufacturer); + $('#edit-make').val(car.make); + $('#edit-model').val(car.model); + $('#edit-year').val(car.year); + $('#edit-id').val(car.id); + } + }); + $('#spoiler').css({ 'display': 'block' }); +} + +$('.my-form').on('submit', function () { + const car = { + 'manufacturer': $('#edit-manufacturer').val(), + 'make': $('#edit-make').val(), + 'model': $('#edit-model').val(), + 'year': $('#edit-year').val(), + 'id': $('#edit-id').val() + }; + + $.ajax({ + url: uri + '/' + $('#edit-id').val(), + type: 'PUT', + accepts: 'application/json', + contentType: 'application/json', + data: JSON.stringify(car), + success: function (result) { + getData(); + } + }); + + closeInput(); + return false; +}); + +function closeInput() { + $('#spoiler').css({ 'display': 'none' }); +} \ No newline at end of file From c2fb1bd3881ab6df9fbe1f694eea176ddce2469a Mon Sep 17 00:00:00 2001 From: gavindya Date: Sat, 16 Jun 2018 17:49:52 +0530 Subject: [PATCH 2/6] Update method modified --- Controllers/CarController.cs | 19 ++++++-- Controllers/ValuesController.cs | 80 ++++++++++++++++----------------- 2 files changed, 55 insertions(+), 44 deletions(-) diff --git a/Controllers/CarController.cs b/Controllers/CarController.cs index e1a203d..9d4066e 100644 --- a/Controllers/CarController.cs +++ b/Controllers/CarController.cs @@ -41,14 +41,14 @@ private List ReadFromJSONfile(){ JsonSerializer serializer = new JsonSerializer(); carsList = (List)serializer.Deserialize(file, typeof(List)); } - foreach (var c in carsList) { - _context.Cars.Add(c); + foreach (Car c in carsList) { + _context.Cars.Add(new Car { Id =c.Id,Manufacturer = c.Manufacturer,Make=c.Make,Model=c.Model,Year=c.Year }); } _context.SaveChanges(); }catch(Exception e){ Console.WriteLine("Exception information: {0}", e); //In case ofan exception, 3 cars still wold be added to the _context - AddCarsToContext(); + // AddCarsToContext(); } return _context.Cars.ToList(); } @@ -75,6 +75,7 @@ public CarController(CarContext context) [HttpGet] public ActionResult> GetAll() { + WriteToJSONfile(); return _context.Cars.ToList(); } @@ -94,6 +95,16 @@ public ActionResult GetById(long id) [HttpPost] public IActionResult Create(Car car) { + Console.WriteLine("\n\n\n\n NEW CAR ID = "+car.Id+"\n\n\n\n"); + if(car.Id == 0){ + car.Id=1; + } + Console.WriteLine("\n\n\n\n AFTER - NEW CAR ID = "+car.Id+"\n\n\n\n"); + var item = _context.Cars.Find(car.Id); + while(item!=null){ + car.Id = car.Id+1; + item = _context.Cars.Find(car.Id); + } _context.Cars.Add(car); _context.SaveChanges(); WriteToJSONfile(); @@ -113,7 +124,7 @@ public IActionResult Update(long id, Car item) car.Manufacturer = item.Manufacturer; car.Make = item.Make; - car.Manufacturer = item.Manufacturer; + car.Model = item.Model; car.Year = item.Year; _context.Cars.Update(car); diff --git a/Controllers/ValuesController.cs b/Controllers/ValuesController.cs index 6e1bf7b..b24612d 100644 --- a/Controllers/ValuesController.cs +++ b/Controllers/ValuesController.cs @@ -1,45 +1,45 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; +// using System; +// using System.Collections.Generic; +// using System.Linq; +// using System.Threading.Tasks; +// using Microsoft.AspNetCore.Mvc; -namespace coding.Controllers -{ - [Route("api/[controller]")] - [ApiController] - public class ValuesController : ControllerBase - { - // GET api/values - [HttpGet] - public ActionResult> Get() - { - return new string[] { "value1", "value2" }; - } +// namespace coding.Controllers +// { +// [Route("api/[controller]")] +// [ApiController] +// public class ValuesController : ControllerBase +// { +// // GET api/values +// [HttpGet] +// public ActionResult> Get() +// { +// return new string[] { "value1", "value2" }; +// } - // GET api/values/5 - [HttpGet("{id}")] - public ActionResult Get(int id) - { - return "value"; - } +// // GET api/values/5 +// [HttpGet("{id}")] +// public ActionResult Get(int id) +// { +// return "value"; +// } - // POST api/values - [HttpPost] - public void Post([FromBody] string 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) - { - } +// // 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) - { - } - } -} +// // DELETE api/values/5 +// [HttpDelete("{id}")] +// public void Delete(int id) +// { +// } +// } +// } From d36afe823b0a615e6654f3b7eb3523abf9bd8261 Mon Sep 17 00:00:00 2001 From: gavindya Date: Sat, 16 Jun 2018 18:05:19 +0530 Subject: [PATCH 3/6] WebAPI pRTIlly done --- Controllers/CarController.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Controllers/CarController.cs b/Controllers/CarController.cs index 9d4066e..47df20d 100644 --- a/Controllers/CarController.cs +++ b/Controllers/CarController.cs @@ -95,11 +95,9 @@ public ActionResult GetById(long id) [HttpPost] public IActionResult Create(Car car) { - Console.WriteLine("\n\n\n\n NEW CAR ID = "+car.Id+"\n\n\n\n"); if(car.Id == 0){ car.Id=1; } - Console.WriteLine("\n\n\n\n AFTER - NEW CAR ID = "+car.Id+"\n\n\n\n"); var item = _context.Cars.Find(car.Id); while(item!=null){ car.Id = car.Id+1; From c03f76c5e2a37cb0ad314871401bf5d02a31bd11 Mon Sep 17 00:00:00 2001 From: Yasith Jayawardana Date: Sun, 17 Jun 2018 01:13:08 +0530 Subject: [PATCH 4/6] fix integration with frontend --- Controllers/CarController.cs | 11 +++++--- Controllers/ValuesController.cs | 45 --------------------------------- Startup.cs | 7 +++++ coding.csproj | 1 + 4 files changed, 15 insertions(+), 49 deletions(-) delete mode 100644 Controllers/ValuesController.cs diff --git a/Controllers/CarController.cs b/Controllers/CarController.cs index 47df20d..f69339f 100644 --- a/Controllers/CarController.cs +++ b/Controllers/CarController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Cors; using System.Collections.Generic; using System.Linq; using coding.Models; @@ -10,6 +11,7 @@ namespace coding.Controllers { [Route("api/[controller]")] [ApiController] + [EnableCors("MyPolicy")] public class CarController : ControllerBase { private readonly CarContext _context; @@ -70,7 +72,7 @@ public CarController(CarContext context) } } } - + //localhost:5000/api/car //Get all cars [HttpGet] public ActionResult> GetAll() @@ -91,6 +93,7 @@ public ActionResult GetById(long id) return item; } + //localhost:5000/api/car //Create Car [HttpPost] public IActionResult Create(Car car) @@ -109,7 +112,7 @@ public IActionResult Create(Car car) return CreatedAtRoute("GetCar", new { id = car.Id }, car); } - + //localhost:5000/api/car/{id} //Edit Method [HttpPut("{id}")] public IActionResult Update(long id, Car item) @@ -119,7 +122,7 @@ public IActionResult Update(long id, Car item) { return NotFound(); } - + //send in raw-json body car.Manufacturer = item.Manufacturer; car.Make = item.Make; car.Model = item.Model; @@ -130,7 +133,7 @@ public IActionResult Update(long id, Car item) WriteToJSONfile(); return NoContent(); } - + //localhost:5000/api/car/{id} //Delete Method [HttpDelete("{id}")] public IActionResult Delete(long id) diff --git a/Controllers/ValuesController.cs b/Controllers/ValuesController.cs deleted file mode 100644 index b24612d..0000000 --- a/Controllers/ValuesController.cs +++ /dev/null @@ -1,45 +0,0 @@ -// using System; -// using System.Collections.Generic; -// using System.Linq; -// using System.Threading.Tasks; -// using Microsoft.AspNetCore.Mvc; - -// namespace coding.Controllers -// { -// [Route("api/[controller]")] -// [ApiController] -// public class ValuesController : ControllerBase -// { -// // GET api/values -// [HttpGet] -// public ActionResult> Get() -// { -// return new string[] { "value1", "value2" }; -// } - -// // GET api/values/5 -// [HttpGet("{id}")] -// public ActionResult 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) -// { -// } -// } -// } diff --git a/Startup.cs b/Startup.cs index 8d1db3f..1bf0ecf 100644 --- a/Startup.cs +++ b/Startup.cs @@ -14,6 +14,12 @@ public void ConfigureServices(IServiceCollection services) opt.UseInMemoryDatabase("CarsList")); services.AddMvc() .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); + services.AddCors(o => o.AddPolicy("MyPolicy", builder => + { + builder.AllowAnyOrigin() + .AllowAnyMethod() + .AllowAnyHeader(); + })); } public void Configure(IApplicationBuilder app) @@ -21,6 +27,7 @@ public void Configure(IApplicationBuilder app) app.UseDefaultFiles(); app.UseStaticFiles(); app.UseMvc(); + app.UseCors("MyPolicy"); } } } \ No newline at end of file diff --git a/coding.csproj b/coding.csproj index 276e864..cf8e03b 100644 --- a/coding.csproj +++ b/coding.csproj @@ -10,6 +10,7 @@ + From 8a28ef6d94a4e15b695c49044c17839cd399f3ac Mon Sep 17 00:00:00 2001 From: Gavindya Date: Sun, 17 Jun 2018 11:11:21 +0530 Subject: [PATCH 5/6] Update CarController to preserve cars order using Id --- Controllers/CarController.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Controllers/CarController.cs b/Controllers/CarController.cs index f69339f..8291f00 100644 --- a/Controllers/CarController.cs +++ b/Controllers/CarController.cs @@ -29,7 +29,8 @@ private void WriteToJSONfile(){ using (StreamWriter file = System.IO.File.CreateText(_path)) { JsonSerializer serializer = new JsonSerializer(); - serializer.Serialize(file, _context.Cars.ToList()); + //saves cars preserving the order (ID) of cars + serializer.Serialize(file, _context.Cars.OrderBy(s => s.Id).ToList()); } }catch(Exception e){ Console.WriteLine("Exception information: {0}", e); @@ -78,7 +79,8 @@ public CarController(CarContext context) public ActionResult> GetAll() { WriteToJSONfile(); - return _context.Cars.ToList(); + //returns the list of cars preserving the order (ID) of cars + return _context.Cars.OrderBy(s => s.Id).ToList(); } //Get one car given the ID @@ -152,4 +154,4 @@ public IActionResult Delete(long id) } -} \ No newline at end of file +} From bbf668cd104642c2b9724ee643dc1c820607756d Mon Sep 17 00:00:00 2001 From: Yasith Jayawardana Date: Sun, 17 Jun 2018 11:42:34 +0530 Subject: [PATCH 6/6] path problem fix --- .gitignore | 12 ++++++++++++ Controllers/CarController.cs | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f2bb5f2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +# Build +bin/ +obj/ + +# IDE +.vscode + +# MacOS +.DS_Store + +# Generated Data +data.json \ No newline at end of file diff --git a/Controllers/CarController.cs b/Controllers/CarController.cs index 8291f00..4df9ab6 100644 --- a/Controllers/CarController.cs +++ b/Controllers/CarController.cs @@ -15,7 +15,7 @@ namespace coding.Controllers public class CarController : ControllerBase { private readonly CarContext _context; - private string _path = Environment.CurrentDirectory+"\\data.json"; + private string _path = Path.Combine(Environment.CurrentDirectory, "data.json"); private void AddCarsToContext(){ _context.Cars.Add(new Car { Manufacturer = "Ford",Make="Mustang",Model="GT",Year=2015 }); _context.Cars.Add(new Car { Manufacturer = "Chevrolet",Make="Corvette",Model="Z06",Year=2017 });