Created
April 9, 2019 21:19
-
-
Save tugberkugurlu/127293e1e12f18004d3d4bbb2bf056ca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Concurrent; | |
using System.Collections.Generic; | |
using System.ComponentModel.DataAnnotations; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.DependencyInjection; | |
namespace _1_sample_web | |
{ | |
public class Startup | |
{ | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc(); | |
} | |
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | |
{ | |
app.UseMvcWithDefaultRoute(); | |
} | |
} | |
public class CarsController : Controller | |
{ | |
private static readonly CarsContext _carsCtx = new CarsContext(); | |
[HttpGet("cars")] | |
public IEnumerable<Car> Get() | |
{ | |
return _carsCtx.GetAll(); | |
} | |
[HttpGet("cars/{id}")] | |
public IActionResult GetCar(int id) | |
{ | |
var carTuple = _carsCtx.GetSingle(id); | |
if (!carTuple.Item1) | |
{ | |
return NotFound(); | |
} | |
return Ok(carTuple.Item2); | |
} | |
[HttpPost("cars/{id}")] | |
public IActionResult PostCar(Car car) | |
{ | |
var createdCar = _carsCtx.Add(car); | |
return CreatedAtAction(nameof(GetCar), | |
new { id = createdCar.Id }, | |
createdCar); | |
} | |
[HttpPut("cars/{id}")] | |
public IActionResult PutCar(int id, Car car) | |
{ | |
car.Id = id; | |
if (!_carsCtx.TryUpdate(car)) | |
{ | |
return NotFound(); | |
} | |
return Ok(car); | |
} | |
[HttpDelete("cars/{id}")] | |
public IActionResult DeleteCar(int id) | |
{ | |
if (!_carsCtx.TryRemove(id)) | |
{ | |
return NotFound(); | |
} | |
return NoContent(); | |
} | |
} | |
public class Car | |
{ | |
public int Id { get; set; } | |
[Required] | |
[StringLength(20)] | |
public string Make { get; set; } | |
[Required] | |
[StringLength(20)] | |
public string Model { get; set; } | |
public int Year { get; set; } | |
[Range(0, 500000)] | |
public float Price { get; set; } | |
} | |
public class CarsContext | |
{ | |
private int _nextId = 9; | |
private object _idLock = new object(); | |
private readonly ConcurrentDictionary<int, Car> _database = new ConcurrentDictionary<int, Car>(new HashSet<KeyValuePair<int, Car>> | |
{ | |
new KeyValuePair<int, Car>(1, new Car { Id = 1, Make = "Make1", Model = "Model1", Year = 2010, Price = 10732.2F }), | |
new KeyValuePair<int, Car>(2, new Car { Id = 2, Make = "Make2", Model = "Model2", Year = 2008, Price = 27233.1F }), | |
new KeyValuePair<int, Car>(3, new Car { Id = 3, Make = "Make3", Model = "Model1", Year = 2009, Price = 67437.0F }), | |
new KeyValuePair<int, Car>(4, new Car { Id = 4, Make = "Make4", Model = "Model3", Year = 2007, Price = 78984.2F }), | |
new KeyValuePair<int, Car>(5, new Car { Id = 5, Make = "Make5", Model = "Model1", Year = 1987, Price = 56200.89F }), | |
new KeyValuePair<int, Car>(6, new Car { Id = 6, Make = "Make6", Model = "Model4", Year = 1997, Price = 46003.2F }), | |
new KeyValuePair<int, Car>(7, new Car { Id = 7, Make = "Make7", Model = "Model5", Year = 2001, Price = 78355.92F }), | |
new KeyValuePair<int, Car>(8, new Car { Id = 8, Make = "Make8", Model = "Model1", Year = 2011, Price = 1823223.23F }) | |
}); | |
public IEnumerable<Car> GetAll() | |
{ | |
Thread.Sleep(500); | |
return _database.Values; | |
} | |
public IEnumerable<Car> Get(Func<Car, bool> predicate) | |
{ | |
Thread.Sleep(500); | |
return _database.Values.Where(predicate); | |
} | |
public Tuple<bool, Car> GetSingle(int id) | |
{ | |
Thread.Sleep(500); | |
Car car; | |
var doesExist = _database.TryGetValue(id, out car); | |
return new Tuple<bool, Car>(doesExist, car); | |
} | |
public Car GetSingle(Func<Car, bool> predicate) | |
{ | |
Thread.Sleep(500); | |
return _database.Values.FirstOrDefault(predicate); | |
} | |
public Car Add(Car car) | |
{ | |
Thread.Sleep(500); | |
lock(_idLock) | |
{ | |
car.Id = _nextId; | |
_database.TryAdd(car.Id, car); | |
_nextId++; | |
} | |
return car; | |
} | |
public bool TryRemove(int id) | |
{ | |
Thread.Sleep(500); | |
Car removedCar; | |
return _database.TryRemove(id, out removedCar); | |
} | |
public bool TryUpdate(Car car) | |
{ | |
Thread.Sleep(500); | |
Car oldCar; | |
if (_database.TryGetValue(car.Id, out oldCar)) { | |
return _database.TryUpdate(car.Id, car, oldCar); | |
} | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment