添加项目文件。
This commit is contained in:
79
XXCpzs.MobileAppService/Controllers/ItemController.cs
Normal file
79
XXCpzs.MobileAppService/Controllers/ItemController.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using XXCpzs.Models;
|
||||
|
||||
namespace XXCpzs.Controllers
|
||||
{
|
||||
[Route("api/item")]
|
||||
[ApiController]
|
||||
public class ItemController : ControllerBase
|
||||
{
|
||||
private readonly IItemRepository ItemRepository;
|
||||
|
||||
public ItemController(IItemRepository itemRepository)
|
||||
{
|
||||
ItemRepository = itemRepository;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public ActionResult<IEnumerable<Item>> List()
|
||||
{
|
||||
return ItemRepository.GetAll().ToList();
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public ActionResult<Item> GetItem(string id)
|
||||
{
|
||||
Item item = ItemRepository.Get(id);
|
||||
|
||||
if (item == null)
|
||||
return NotFound();
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public ActionResult<Item> Create([FromBody]Item item)
|
||||
{
|
||||
ItemRepository.Add(item);
|
||||
return CreatedAtAction(nameof(GetItem), new { item.Id }, item);
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public ActionResult Edit([FromBody] Item item)
|
||||
{
|
||||
try
|
||||
{
|
||||
ItemRepository.Update(item);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return BadRequest("Error while editing item");
|
||||
}
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public ActionResult Delete(string id)
|
||||
{
|
||||
Item item = ItemRepository.Remove(id);
|
||||
|
||||
if (item == null)
|
||||
return NotFound();
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
14
XXCpzs.MobileAppService/Models/IItemRepository.cs
Normal file
14
XXCpzs.MobileAppService/Models/IItemRepository.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace XXCpzs.Models
|
||||
{
|
||||
public interface IItemRepository
|
||||
{
|
||||
void Add(Item item);
|
||||
void Update(Item item);
|
||||
Item Remove(string key);
|
||||
Item Get(string id);
|
||||
IEnumerable<Item> GetAll();
|
||||
}
|
||||
}
|
||||
16
XXCpzs.MobileAppService/Models/Item.cs
Normal file
16
XXCpzs.MobileAppService/Models/Item.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace XXCpzs.Models
|
||||
{
|
||||
public class Item
|
||||
{
|
||||
public string Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Text { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Description { get; set; }
|
||||
}
|
||||
}
|
||||
47
XXCpzs.MobileAppService/Models/ItemRepository.cs
Normal file
47
XXCpzs.MobileAppService/Models/ItemRepository.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace XXCpzs.Models
|
||||
{
|
||||
public class ItemRepository : IItemRepository
|
||||
{
|
||||
private static ConcurrentDictionary<string, Item> items =
|
||||
new ConcurrentDictionary<string, Item>();
|
||||
|
||||
public ItemRepository()
|
||||
{
|
||||
Add(new Item { Id = Guid.NewGuid().ToString(), Text = "Item 1", Description = "This is an item description." });
|
||||
Add(new Item { Id = Guid.NewGuid().ToString(), Text = "Item 2", Description = "This is an item description." });
|
||||
Add(new Item { Id = Guid.NewGuid().ToString(), Text = "Item 3", Description = "This is an item description." });
|
||||
}
|
||||
|
||||
public IEnumerable<Item> GetAll()
|
||||
{
|
||||
return items.Values;
|
||||
}
|
||||
|
||||
public void Add(Item item)
|
||||
{
|
||||
item.Id = Guid.NewGuid().ToString();
|
||||
items[item.Id] = item;
|
||||
}
|
||||
|
||||
public Item Get(string id)
|
||||
{
|
||||
items.TryGetValue(id, out Item item);
|
||||
return item;
|
||||
}
|
||||
|
||||
public Item Remove(string id)
|
||||
{
|
||||
items.TryRemove(id, out Item item);
|
||||
return item;
|
||||
}
|
||||
|
||||
public void Update(Item item)
|
||||
{
|
||||
items[item.Id] = item;
|
||||
}
|
||||
}
|
||||
}
|
||||
22
XXCpzs.MobileAppService/Program.cs
Normal file
22
XXCpzs.MobileAppService/Program.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace XXCpzs.MobileAppService
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
}
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
{
|
||||
webBuilder.UseStartup<Startup>();
|
||||
});
|
||||
}
|
||||
}
|
||||
30
XXCpzs.MobileAppService/Properties/launchSettings.json
Normal file
30
XXCpzs.MobileAppService/Properties/launchSettings.json
Normal file
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:53100",
|
||||
"sslPort": 44300
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "api/item",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"XXCpzs.MobileAppService": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "api/item",
|
||||
"applicationUrl": "https://localhost:5001;http://localhost:5000",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
46
XXCpzs.MobileAppService/Startup.cs
Normal file
46
XXCpzs.MobileAppService/Startup.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using XXCpzs.Models;
|
||||
|
||||
namespace XXCpzs.MobileAppService
|
||||
{
|
||||
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.AddControllers();
|
||||
services.AddSingleton<IItemRepository, ItemRepository>();
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseRouting();
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapControllers();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
7
XXCpzs.MobileAppService/XXCpzs.MobileAppService.csproj
Normal file
7
XXCpzs.MobileAppService/XXCpzs.MobileAppService.csproj
Normal file
@@ -0,0 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
9
XXCpzs.MobileAppService/appsettings.Development.json
Normal file
9
XXCpzs.MobileAppService/appsettings.Development.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"System": "Information",
|
||||
"Microsoft": "Information"
|
||||
}
|
||||
}
|
||||
}
|
||||
15
XXCpzs.MobileAppService/appsettings.json
Normal file
15
XXCpzs.MobileAppService/appsettings.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
},
|
||||
"Console": {
|
||||
"LogLevel": {
|
||||
"Default": "Warning"
|
||||
}
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user