using IRaCIS.Application.Interfaces; using IRaCIS.Application.Contracts; using IRaCIS.Core.Infra.EFCore; using Microsoft.AspNetCore.Mvc; namespace IRaCIS.Application.Services { [ ApiExplorerSettings(GroupName = "Institution")] public class SiteService : BaseService, ISiteService { private readonly IRepository _siteRepository; private readonly IRepository _trialSiteUserRepository; public SiteService(IRepository siteRepository, IRepository trialSiteUserRepository) { _siteRepository = siteRepository; this._trialSiteUserRepository = trialSiteUserRepository; } /// 分页获取研究中心列表 [HttpPost] public async Task> GetSiteList(SiteQueryParam searchModel) { var siteQueryable = _siteRepository .WhereIf(!string.IsNullOrWhiteSpace(searchModel.SiteName), t => t.SiteName.Contains(searchModel.SiteName)) .ProjectTo(_mapper.ConfigurationProvider); return await siteQueryable.ToPagedListAsync(searchModel.PageIndex, searchModel.PageSize, string.IsNullOrWhiteSpace(searchModel.SortField) ? "SiteName" : searchModel.SortField, searchModel.Asc); } public async Task> GetAllSiteList() { return await _siteRepository.ProjectTo(_mapper.ConfigurationProvider).ToListAsync(); } /// 添加研究中心 public async Task AddOrUpdateSite(SiteCommand siteCommand) { var exp = new EntityVerifyExp() { VerifyExp = h => h.SiteName.Equals(siteCommand.SiteName)|| h.SiteCode.Equals(siteCommand.SiteCode), VerifyMsg = "A site with the same name or code already existed in the table. Please confirm." }; var site = await _siteRepository.InsertOrUpdateAsync(siteCommand, true, exp); return ResponseOutput.Ok(site.Id.ToString()); } /// 删除研究中心 [HttpDelete("{siteId:guid}")] public async Task DeleteSite(Guid siteId) { if (await _trialSiteUserRepository.AnyAsync(t => t.SiteId == siteId)) { return ResponseOutput.NotOk("This site has participated in the trial and couldn't be deleted."); } var success = await _siteRepository.BatchDeleteAsync(x => x.Id == siteId); return ResponseOutput.Result(success); } } }