ASP.NET CORE WEB API DEMO 01

2021-02-09 21:21

阅读:721

标签:form   action   adr   ini   XML   request   enc   asp   service   

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 GetAll();
        BookChapter Find(Guid id);
        void Add(BookChapter chapter);
        BookChapter Remove(Guid id);
        void Update(BookChapter chapter);
    }

    public class BookChaptersRepository : IBookChaptersRepository
    {
        private readonly ConcurrentDictionary _chapters =
            new ConcurrentDictionary();

        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 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 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();
        }
    }
}

 

ASP.NET CORE WEB API DEMO 01

标签:form   action   adr   ini   XML   request   enc   asp   service   

原文地址:https://www.cnblogs.com/xiaowangzhi/p/8542968.html


评论


亲,登录后才可以留言!