添加项目文件。
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user