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

140 lines
4.7 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 OrganizationsController : ControllerBase
{
private readonly DriveDbContext dbContext;
public OrganizationsController(DriveDbContext dbContext)
{
this.dbContext = dbContext;
}
/// <summary>
/// 获得指定课件的所有组织机构信息。
/// </summary>
/// <param name="coursewareId">指定的课件编号</param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpGet]
public async Task<ActionResult<IEnumerable<OrganizationInfo>>> GetAll([FromQuery, BindRequired]string coursewareId)
{
if (string.IsNullOrEmpty(coursewareId))
return BadRequest(coursewareId);
var models = await dbContext.Organizations
.AsNoTracking()
.Where(e => e.CoursewareId == coursewareId)
.OrderByDescending(e => e.Order)
.Select(e => e.ToDTO())
.ToListAsync();
return Ok(models);
}
/// <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] OrganizationInfo dto)
{
if (string.IsNullOrEmpty(coursewareId))
return BadRequest(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.Organizations.FindAsync(coursewareId, id);
if (model == null)
{
model = dto.ToModel(coursewareId);
dbContext.Organizations.Add(model);
}
else
{
dto.MapTo(model);
}
await dbContext.SaveChangesAsync();
return Ok();
}
/// <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.Organizations.FindAsync(coursewareId, id);
if (model == null)
return NotFound();
dbContext.Remove(model);
await dbContext.SaveChangesAsync();
return NoContent();
}
}
internal static class OrganizationExtensions
{
public static Organization ToModel(this OrganizationInfo dto, string coursewareId)
{
var model = dto.Adapt<Organization>();
model.CoursewareId = coursewareId;
return model;
}
public static OrganizationInfo ToDTO(this Organization model)
{
var dto = model.Adapt<OrganizationInfo>();
return dto;
}
public static void MapTo(this OrganizationInfo dto, Organization model)
{
dto.Adapt(model);
}
}
}