using System; using System.Linq; using System.Threading.Tasks; using FMSAdmin.Data; using FMSAdmin.Models; using FMSAdmin.Entities; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Authorization; using FMSAdmin.Helpers; using System.Data; using System.IO; using OfficeOpenXml; using FMSAdmin.Services; namespace FMSAdmin.Controllers { [Authorize] [ApiController] [ApiVersion("1")] [Route("api/[controller]")] public class ManualHistoryController : Controller { private readonly ILogger _logger; private readonly FMSContext _context; private readonly ManualHistoryService _service; private readonly StorageHelper _storage; public ManualHistoryController( ILogger logger, FMSContext context, ManualHistoryService service, StorageHelper storage ) { _logger = logger; _context = context; _service = service; _storage = storage; } /// /// 셀렉트 공통 /// /// /// private dynamic _ListSelect(IQueryable query) { var list = query.Select(x => new { x.SiteId, x.ManualId, x.HistoryId, x.RevisionNo, x.FileId, CmFile = _storage.ParseFile(x.CmFile), x.Description, x.UpdatedDate, UpdateUserName = x.CmUser.Name, }); return list; } /// /// 목록 /// [HttpGet] public IActionResult List([FromQuery] PagingRequest req, [FromQuery] string siteId, [FromQuery] string ManualId) { //수정필요 var query = _FilterAndSort(req); query = query.Where(x => x.SiteId == Util.ToInt(siteId) && x.ManualId == Util.ToInt(ManualId)); var list = _ListSelect(query); var paging = PagingList.Pagination(list, req.page, req.limit); return Ok(paging); } [HttpDelete("{siteId}/{ManualId}/{historyId}")] public IActionResult Delete(int siteId, int ManualId, int historyId) { _service.Delete(siteId, ManualId, historyId); return Ok(); } // 검색 & 정렬 공통 private IQueryable _FilterAndSort(PagingRequest req) { var query = _service.GetAll(); // 기본 Entity 검색 query = query.Filter(req.conditions); // 기본 Entity 정렬 query = query.Sort(req.sort); return query; } /// /// 조회 /// [HttpGet("{siteId}/{ManualId}/{historyId}")] public IActionResult Get(int siteId, int ManualId, int historyId) { var data = _service.Get(siteId, ManualId, historyId); if (data.Count() == 0) { return NotFound("사이트 정보를 찾을 수 없습니다."); } var list = data.Select(x => new { x.SiteId, x.ManualId, x.HistoryId, x.RevisionNo, CmUser = new { x.CmUser.Name, }, CmFile = _storage.ParseFile(x.CmFile), x.Description }); return Ok(list.First()); } /// /// 조회 /// [HttpGet("{siteId}/{ManualId}")] public int GetNextRevisionNo(int siteId, int ManualId) { int nextRevisionNo = _service.GetRevisionNo(siteId, ManualId); return nextRevisionNo; } /// /// 등록 /// [HttpPut] public IActionResult Create([FromBody] FmsManualHistory data) { _logger.LogInformation("Create"); //_logger.LogInformation(data.Dump()); if (ModelState.IsValid) { _service.Create(data); } else { return BadRequest(ModelState); } return Ok(); } /// /// 수정 /// [HttpPost("{siteId}/{ManualId}/{historyId}")] public IActionResult Edit(int siteId, int ManualId, int historyId, [FromBody] FmsManualHistory data) { _logger.LogInformation("Edit"); _logger.LogInformation("id:" + historyId); _logger.LogInformation("siteId:" + siteId); //_logger.LogInformation(data.Dump()); if (historyId != data.HistoryId) { return BadRequest("잘못된 접근입니다."); } if (ModelState.IsValid) { data.UpdatedUserId = User.Identity.Name; _service.Edit(data); } else { return BadRequest(ModelState); } return Ok(); } /// /// 삭제 /// // [HttpDelete("{siteId}/{ManualId}/{historyId}")] // public IActionResult Delete(int siteId, int id) { // _service.Delete(id, siteId); // return Ok(); // } } }