using AX.FireTrainingSys.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
namespace AX.FireTrainingSys.Controllers.V1
{
///
/// 消防电梯控制器。
///
[Produces("application/json")]
[Route("api/[controller]")]
[ApiVersion("1.0")]
[ApiController]
public class TemplateFireLiftsController : ControllerBase
{
private readonly DriveDbContext dbContext;
public TemplateFireLiftsController(DriveDbContext dbContext)
{
this.dbContext = dbContext;
}
///
/// 获得指定单位的所有消防电梯信息。
///
/// 指定的单位名称
///
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpGet]
public async Task>> GetAll([FromQuery, BindRequired]string name)
{
if (string.IsNullOrEmpty(name))
return BadRequest(name);
var models = await dbContext.TemplateFireLifts
.AsNoTracking()
.Where(e => e.CompanyName == name)
.Select(e => e.Content)
.ToListAsync();
return Ok(models);
}
///
/// 获得指定单位的单条消防电梯信息。
///
/// 指定的单位名称
/// 指定的相关编号
///
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{id}")]
public async Task> Get([FromQuery, BindRequired]string name, string id)
{
if (string.IsNullOrEmpty(name))
return BadRequest(name);
var model = await dbContext.TemplateFireLifts.FindAsync(name, id);
if (model == null)
return NotFound(name);
return Ok(model.Content);
}
///
/// 创建或更新指定单位的单条消防电梯信息。
///
/// 指定的单位名称
/// 指定的相关编号
/// 要保存的内容
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpPost("{id}")]
public async Task PostOrPut([FromQuery, BindRequired]string name, string id, [FromBody]string content)
{
if (string.IsNullOrEmpty(name))
return BadRequest(name);
var model = await dbContext.TemplateFireLifts.FindAsync(name, id);
if (model == null)
{
model = new TemplateFireLift
{
CompanyName = name,
Id = id,
Content = content
};
dbContext.TemplateFireLifts.Add(model);
}
else
{
model.Content = content;
}
await dbContext.SaveChangesAsync();
return Ok();
}
///
/// 批量创建或更新指定单位的消防电梯信息。
///
/// 指定的单位名称
/// 要保存的内容
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpPost]
public async Task PostOrPutAll([FromQuery, BindRequired]string name, [FromBody]string content)
{
if (string.IsNullOrEmpty(name))
return BadRequest(name);
if (string.IsNullOrEmpty(content))
return BadRequest(content);
var data = JsonSerializer.Deserialize>(content);
if (data == null)
return BadRequest(content);
using (var transaction = dbContext.Database.BeginTransaction())
{
//首先删除所有该单位的相关信息
var olds = await dbContext
.TemplateFireLifts
.Where(e => e.CompanyName == name)
.ToListAsync();
dbContext.TemplateFireLifts.RemoveRange(olds);
//依次插入新信息
foreach (var item in data)
{
var model = new TemplateFireLift
{
CompanyName = name,
Id = item.Key,
Content = item.Value
};
dbContext.TemplateFireLifts.Add(model);
}
await dbContext.SaveChangesAsync();
transaction.Commit();
}
return Ok();
}
///
/// 批量删除指定单位的消防电梯信息。
///
/// 指定的单位名称
/// 要删除的内容
///
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpDelete]
public async Task DeleteAll([FromQuery, BindRequired]string name, [FromBody]string content)
{
if (string.IsNullOrEmpty(name))
return BadRequest(name);
if (string.IsNullOrEmpty(content))
return BadRequest(content);
var data = JsonSerializer.Deserialize(content);
if (data == null)
return BadRequest(content);
using (var transaction = dbContext.Database.BeginTransaction())
{
await foreach (var (id, model) in AsAsyncEnumerable(name, data))
{
if (model == null)
return NotFound(id);
dbContext.TemplateFireLifts.Remove(model);
}
await dbContext.SaveChangesAsync();
transaction.Commit();
}
return NoContent();
}
///
/// 删除指定单位的单条消防电梯信息。
///
/// 指定的单位名称
/// 指定的相关编号
///
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpDelete("{id}")]
public async Task Delete([FromQuery, BindRequired]string name, string id)
{
if (string.IsNullOrEmpty(name))
return BadRequest(name);
var model = await dbContext.TemplateFireLifts.FindAsync(name, id);
if (model == null)
return NotFound(id);
dbContext.TemplateFireLifts.Remove(model);
await dbContext.SaveChangesAsync();
return NoContent();
}
private async IAsyncEnumerable<(string, TemplateFireLift)> AsAsyncEnumerable(string name, string[] data)
{
foreach (var item in data)
{
var model = await dbContext.TemplateFireLifts.FindAsync(name, item);
yield return (item, model);
}
}
}
}