消防培训系统服务器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
8.6 KiB

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
{
/// <summary>
/// 市政消火栓控制器。
/// </summary>
[Produces("application/json")]
[Route("api/[controller]")]
[ApiVersion("1.0")]
[ApiController]
public class MunicipalFireHydrantsController : ControllerBase
{
private readonly DriveDbContext dbContext;
public MunicipalFireHydrantsController(DriveDbContext dbContext)
{
this.dbContext = dbContext;
}
/// <summary>
/// 获得指定课件的所有市政消火栓信息。
/// </summary>
/// <param coursewareId="coursewareId">指定的课件编号</param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpGet]
public async Task<ActionResult<IEnumerable<string>>> GetAll([FromQuery, BindRequired]string coursewareId)
{
if (string.IsNullOrEmpty(coursewareId))
return BadRequest(coursewareId);
var models = await dbContext.MunicipalFireHydrants
.AsNoTracking()
.Where(e => e.CoursewareId == coursewareId)
.Select(e => e.Content)
.ToListAsync();
return Ok(models);
}
/// <summary>
/// 获得指定课件的单条市政消火栓信息。
/// </summary>
/// <param coursewareId="coursewareId">指定的课件编号</param>
/// <param coursewareId="id">指定的相关编号</param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{id}")]
public async Task<ActionResult<string>> Get([FromQuery, BindRequired]string coursewareId, string id)
{
if (string.IsNullOrEmpty(coursewareId))
return BadRequest(coursewareId);
var model = await dbContext.MunicipalFireHydrants.FindAsync(coursewareId, id);
if (model == null)
return NotFound(coursewareId);
return Ok(model.Content);
}
/// <summary>
/// 创建或更新指定课件的单条市政消火栓信息。
/// </summary>
/// <param coursewareId="coursewareId">指定的课件编号</param>
/// <param coursewareId="id">指定的相关编号</param>
/// <param coursewareId="content">要保存的内容</param>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpPost("{id}")]
public async Task<Microsoft.AspNetCore.Mvc.ActionResult> PostOrPut([FromQuery, BindRequired]string coursewareId, string id, [FromBody]string content)
{
if (string.IsNullOrEmpty(coursewareId))
return BadRequest(coursewareId);
var model = await dbContext.MunicipalFireHydrants.FindAsync(coursewareId, id);
if (model == null)
{
model = new MunicipalFireHydrant
{
CoursewareId = coursewareId,
Id = id,
Content = content
};
dbContext.MunicipalFireHydrants.Add(model);
}
else
{
model.Content = content;
}
await dbContext.SaveChangesAsync();
return Ok();
}
/// <summary>
/// 批量创建或更新指定课件的市政消火栓信息。
/// </summary>
/// <param coursewareId="coursewareId">指定的课件编号</param>
/// <param coursewareId="content">要保存的内容</param>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpPost]
public async Task<Microsoft.AspNetCore.Mvc.ActionResult> PostOrPutAll([FromQuery, BindRequired]string coursewareId, [FromBody]string content)
{
if (string.IsNullOrEmpty(coursewareId))
return BadRequest(coursewareId);
if (string.IsNullOrEmpty(content))
return BadRequest(content);
var data = JsonSerializer.Deserialize<IDictionary<string, string>>(content);
if (data == null)
return BadRequest(content);
using (var transaction = dbContext.Database.BeginTransaction())
{
//首先删除所有该单位的相关信息
var olds = await dbContext
.MunicipalFireHydrants
.Where(e => e.CoursewareId == coursewareId)
.ToListAsync();
dbContext.MunicipalFireHydrants.RemoveRange(olds);
//依次插入新信息
foreach (var item in data)
{
var model = new MunicipalFireHydrant
{
CoursewareId = coursewareId,
Id = item.Key,
Content = item.Value
};
dbContext.MunicipalFireHydrants.Add(model);
}
await dbContext.SaveChangesAsync();
transaction.Commit();
}
return Ok();
}
/// <summary>
/// 批量删除指定课件的市政消火栓信息。
/// </summary>
/// <param coursewareId="coursewareId">指定的课件编号</param>
/// <param coursewareId="content">要删除的内容</param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpDelete]
public async Task<Microsoft.AspNetCore.Mvc.ActionResult> DeleteAll([FromQuery, BindRequired]string coursewareId, [FromBody]string content)
{
if (string.IsNullOrEmpty(coursewareId))
return BadRequest(coursewareId);
if (string.IsNullOrEmpty(content))
return BadRequest(content);
var data = JsonSerializer.Deserialize<string[]>(content);
if (data == null)
return BadRequest(content);
using (var transaction = dbContext.Database.BeginTransaction())
{
await foreach (var (id, model) in AsAsyncEnumerable(coursewareId, data))
{
if (model == null)
return NotFound(id);
dbContext.MunicipalFireHydrants.Remove(model);
}
await dbContext.SaveChangesAsync();
transaction.Commit();
}
return NoContent();
}
/// <summary>
/// 删除指定课件的单条市政消火栓信息。
/// </summary>
/// <param coursewareId="coursewareId">指定的课件编号</param>
/// <param coursewareId="id">指定的相关编号</param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpDelete("{id}")]
public async Task<Microsoft.AspNetCore.Mvc.ActionResult> Delete([FromQuery, BindRequired]string coursewareId, string id)
{
if (string.IsNullOrEmpty(coursewareId))
return BadRequest(coursewareId);
var model = await dbContext.MunicipalFireHydrants.FindAsync(coursewareId, id);
if (model == null)
return NotFound(id);
dbContext.MunicipalFireHydrants.Remove(model);
await dbContext.SaveChangesAsync();
return NoContent();
}
private async IAsyncEnumerable<(string, MunicipalFireHydrant)> AsAsyncEnumerable(string coursewareId, string[] data)
{
foreach (var item in data)
{
var model = await dbContext.MunicipalFireHydrants.FindAsync(coursewareId, item);
yield return (item, model);
}
}
}
}