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.
130 lines
4.1 KiB
130 lines
4.1 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 TemplateFireForcesController : ControllerBase |
|
{ |
|
private readonly DriveDbContext dbContext; |
|
|
|
public TemplateFireForcesController(DriveDbContext dbContext) |
|
{ |
|
this.dbContext = dbContext; |
|
} |
|
|
|
/// <summary> |
|
/// 获得指定组织机构的所有消防力量信息。 |
|
/// </summary> |
|
/// <param name="organizationId">机构编号</param> |
|
/// <returns></returns> |
|
[ProducesResponseType(StatusCodes.Status200OK)] |
|
[ProducesResponseType(StatusCodes.Status400BadRequest)] |
|
[HttpGet] |
|
public async Task<ActionResult<IEnumerable<FireForceInfo>>> Get([FromQuery, BindRequired]string organizationId) |
|
{ |
|
if (string.IsNullOrEmpty(organizationId)) |
|
return BadRequest($"{nameof(organizationId)}:{organizationId}"); |
|
|
|
var result = await dbContext.TemplateFireForces |
|
.AsNoTracking() |
|
.Where(e => e.OrganizationId == organizationId) |
|
.Select(e => e.ToDTO()) |
|
.ToListAsync(); |
|
|
|
return Ok(result); |
|
} |
|
|
|
/// <summary> |
|
/// 创建或更新单条消防力量。 |
|
/// </summary> |
|
/// <param name="id">消防力量编号</param> |
|
/// <param name="dto">要保存的消防力量数据</param> |
|
/// <returns></returns> |
|
[ProducesResponseType(StatusCodes.Status201Created)] |
|
[ProducesResponseType(StatusCodes.Status400BadRequest)] |
|
[HttpPost("{id}")] |
|
public async Task<ActionResult<FireForceInfo>> PostOrPut(string id, [FromBody] FireForceInfo dto) |
|
{ |
|
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.TemplateFireForces.FindAsync(dto.Id); |
|
|
|
if (model == null) |
|
{ |
|
model = dto.ToModel(); |
|
dbContext.TemplateFireForces.Add(model); |
|
} |
|
else |
|
{ |
|
dto.MapTo(model); |
|
} |
|
|
|
await dbContext.SaveChangesAsync(); |
|
|
|
return Ok(); |
|
} |
|
|
|
/// <summary> |
|
/// 删除单条消防力量信息。 |
|
/// </summary> |
|
/// <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(string id) |
|
{ |
|
var model = await dbContext.TemplateFireForces.FindAsync(id); |
|
|
|
if (model == null) |
|
return NotFound(); |
|
|
|
dbContext.TemplateFireForces.Remove(model); |
|
|
|
await dbContext.SaveChangesAsync(); |
|
|
|
return NoContent(); |
|
} |
|
} |
|
|
|
internal static class TemplateFireForceExtensions |
|
{ |
|
public static TemplateFireForce ToModel(this FireForceInfo dto) |
|
{ |
|
var model = dto.Adapt<TemplateFireForce>(); |
|
return model; |
|
} |
|
|
|
public static FireForceInfo ToDTO(this TemplateFireForce model) |
|
{ |
|
var dto = model.Adapt<FireForceInfo>(); |
|
return dto; |
|
} |
|
|
|
public static void MapTo(this FireForceInfo dto, TemplateFireForce model) |
|
{ |
|
dto.Adapt(model); |
|
} |
|
} |
|
}
|
|
|