消防培训系统服务器
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.

197 lines
6.8 KiB

using AX.FireTrainingSys.DTOs;
using AX.FireTrainingSys.Models;
using Mapster;
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.Threading.Tasks;
namespace AX.FireTrainingSys.Controllers.V1
{
/// <summary>
/// 消防力量控制器。
/// </summary>
[Produces("application/json")]
[Route("api/[controller]")]
[ApiVersion("1.0")]
[ApiController]
public class FireForcesController : ControllerBase
{
private readonly DriveDbContext dbContext;
public FireForcesController(DriveDbContext dbContext)
{
this.dbContext = dbContext;
}
/// <summary>
/// 获得指定课件的所有消防力量信息或者指定机构的所有消防力量信息。
/// </summary>
/// <param name="coursewareId">课件编号</param>
/// <param name="organizationId">机构编号</param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpGet]
public async Task<ActionResult<IEnumerable<FireForceInfo>>> Get([FromQuery, BindRequired]string coursewareId, [FromQuery]string organizationId)
{
if (string.IsNullOrEmpty(coursewareId))
return BadRequest($"{nameof(coursewareId)}:{coursewareId}");
IEnumerable<FireForceInfo> result = default;
if (string.IsNullOrEmpty(organizationId))
{
result = await dbContext.FireForces
.AsNoTracking()
.Where(e => e.CoursewareId == coursewareId)
.Select(e => e.ToDTO())
.ToListAsync();
}
else
{
result = await dbContext.FireForces
.AsNoTracking()
.Where(e => (e.OrganizationId == organizationId && e.CoursewareId == coursewareId))
.Select(e => e.ToDTO())
.ToListAsync();
}
return Ok(result);
}
/// <summary>
/// 创建或更新指定课件的单条消防力量。
/// </summary>
/// <param name="coursewareId">课件编号</param>
/// <param name="id">消防力量编号</param>
/// <param name="dto">要保存的消防力量数据</param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpPost("{id}")]
public async Task<Microsoft.AspNetCore.Mvc.ActionResult> PostOrPut([FromQuery, BindRequired]string coursewareId, string id, [FromBody] FireForceInfo dto)
{
if (string.IsNullOrEmpty(coursewareId))
return BadRequest($"{nameof(coursewareId)}:{coursewareId}");
if (string.IsNullOrEmpty(id))
return BadRequest(id);
if (id != dto.Id)
return BadRequest($"id: {id} is not equal to dto: {dto.Id}.");
var model = await dbContext.FireForces.FindAsync(coursewareId, id);
if (model == null)
{
model = dto.ToModel(coursewareId);
dbContext.FireForces.Add(model);
}
else
{
dto.MapTo(model);
}
await dbContext.SaveChangesAsync();
return Ok();
}
/// <summary>
/// 批量更新指定课件的消防力量信息。
/// </summary>
/// <param name="coursewareId">指定的课件编号</param>
/// <param name="list">要保存的内容</param>
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpPut]
public async Task<Microsoft.AspNetCore.Mvc.ActionResult> PutAll([FromQuery, BindRequired]string coursewareId, [FromBody]IEnumerable<FireForceInfo> list)
{
if (string.IsNullOrEmpty(coursewareId))
return BadRequest(coursewareId);
using (var transaction = dbContext.Database.BeginTransaction())
{
//依次更新信息
await foreach (var (model, dto) in AsAsyncEnumerable(coursewareId, list))
{
if (model == null)
return NotFound();
dto.MapTo(model);
}
await dbContext.SaveChangesAsync();
transaction.Commit();
}
return NoContent();
}
/// <summary>
/// 删除指定课件的单条消防力量信息。
/// </summary>
/// <param name="coursewareId">指定课件编号</param>
/// <param name="id">组织机构编号</param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status204NoContent)]
[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.FireForces.FindAsync(coursewareId, id);
if (model == null)
return NotFound();
dbContext.FireForces.Remove(model);
await dbContext.SaveChangesAsync();
return NoContent();
}
private async IAsyncEnumerable<(FireForce, FireForceInfo)> AsAsyncEnumerable(string coursewareId, IEnumerable<FireForceInfo> data)
{
foreach (var dto in data)
{
var model = await dbContext.FireForces.FindAsync(coursewareId, dto.Id);
yield return (model, dto);
}
}
}
internal static class FireForceExtensions
{
public static FireForce ToModel(this FireForceInfo dto, string coursewareId)
{
var model = dto.Adapt<FireForce>();
model.CoursewareId = coursewareId;
return model;
}
public static FireForceInfo ToDTO(this FireForce model)
{
var dto = model.Adapt<FireForceInfo>();
return dto;
}
public static void MapTo(this FireForceInfo dto, FireForce model)
{
dto.Adapt(model);
}
}
}