using System; using System.Collections.Concurrent; using System.Collections.Generic; namespace Demo.Models { public class BookChapter { public Guid Id { get; set; } public int Number { get; set; } public string Title { get; set; } public int Pages { get; set; } } public interface IBookChaptersRepository { void Init(); IEnumerable<BookChapter> GetAll(); BookChapter Find(Guid id); void Add(BookChapter chapter); BookChapter Remove(Guid id); void Update(BookChapter chapter); } public class BookChaptersRepository : IBookChaptersRepository { private readonly ConcurrentDictionary<Guid, BookChapter> _chapters = new ConcurrentDictionary<Guid, BookChapter>(); public void Init() { Add(new BookChapter { Number = 1, Title = "Application Architectures", Pages = 35 }); Add(new BookChapter { Number = 2, Title = "Core C#", Pages = 42 }); } public IEnumerable<BookChapter> GetAll() => _chapters.Values; public BookChapter Find(Guid id) { BookChapter chapter; _chapters.TryGetValue(id, out chapter); return chapter; } public void Add(BookChapter chapter) { chapter.Id = Guid.NewGuid(); _chapters[chapter.Id] = chapter; } public BookChapter Remove(Guid id) { BookChapter chapter; _chapters.TryRemove(id, out chapter); return chapter; } public void Update(BookChapter chapter) { _chapters[chapter.Id] = chapter; } } }
using System; using System.Collections.Generic; using Demo.Models; using Microsoft.AspNetCore.Mvc; namespace Demo.Controllers { [Produces("application/json", ("application/xml"))] [Route("api/[controller]")] public class BookChaptersController : Controller { private readonly IBookChaptersRepository _repository; public BookChaptersController(IBookChaptersRepository bookChaptersRepository) { _repository = bookChaptersRepository; } // GET api/bookchapters [HttpGet] public IEnumerable<BookChapter> GetBookChapters() => _repository.GetAll(); // GET api/bookchapters/guid [HttpGet("{id}", Name = nameof(GetBookChapterById))] public IActionResult GetBookChapterById(Guid id) { BookChapter chapter = _repository.Find(id); if (chapter == null) { return NotFound(); } return new ObjectResult(chapter); } // POST api/bookchapters [HttpPost] public IActionResult PostBookChapter([FromBody]BookChapter chapter) { if (chapter == null) { return BadRequest(); } _repository.Add(chapter); return CreatedAtRoute(nameof(GetBookChapterById), new { id = chapter.Id }, chapter); } // PUT api/bookchapters [HttpPut("{id}")] public IActionResult PutBookChapter(Guid id, [FromBody]BookChapter chapter) { if (chapter == null || id != chapter.Id) { return BadRequest(); } if (_repository.Find(id) == null) { return NotFound(); } _repository.Update(chapter); return new NoContentResult(); } // DELETE api/bookchapters/id [HttpDelete("id")] public void Delete(Guid id) { _repository.Remove(id); } } }
using Demo.Models; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace Demo { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddMvc().AddXmlSerializerFormatters(); IBookChaptersRepository repos = new BookChaptersRepository(); repos.Init(); services.AddSingleton(repos); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(); } } }