public async Task <IActionResult> OnPostDeleteTemplateVersion() { // TODO: Error handling TemplateVersionTbl?version = (await _templateVersionTbl.Get(x => x.Id.Equals(DeleteTemplateVersion.VersionId) && x.TemplateId.Equals(DeleteTemplateVersion.TemplateId) && x.Template !.ProjectId.Equals(DeleteTemplateVersion.ProjectId))) .FirstOrDefault(); if (version == null) { throw new NullReferenceException(); } await _templateVersionTbl.Delete(DeleteTemplateVersion.VersionId); await _templateTbl.UpdateFromQuery(x => x.Id.Equals(MarkAsActive.TemplateId), _ => new TemplateTbl { DateModified = DateTime.Now }); await _projectTbl.UpdateFromQuery(x => x.Id.Equals(MarkAsActive.ProjectId), _ => new ProjectTbl { DateModified = DateTime.Now }); TempData["toastStatus"] = "success"; TempData["toastMessage"] = "Template version deleted"; TempData["scrollToId"] = $"template-{DeleteTemplateVersion.TemplateId}"; return(RedirectToPage("/Project/Details", new { id = DeleteTemplateVersion.ProjectId })); }
public async Task <IActionResult> DeleteTemplateVersion(long id, long versionId) { var template = await templateRepository.GetByIdAsync(id); if (template == null) { return(JsonNotFound($"Template ID {id} is not found.")); } var templateVersion = await templateVersionRepository .GetAll() .SingleOrDefaultAsync(x => x.Id == versionId && x.TemplateId == id); if (templateVersion == null) { return(JsonNotFound($"TemplateVersion (Id {id} VersionId {versionId}) is not found.")); } if (await experimentRepository.ExistsAsync(x => x.TemplateId == id && x.TemplateVersionId == versionId, true)) { return(JsonConflict($"TemplateVersion (Id {id} VersionId {versionId}) has been used by experiment.")); } if (await experimentPreprocessRepository.ExistsAsync(x => x.TemplateId == id && x.TemplateVersionId == versionId, true)) { return(JsonConflict($"TemplateVersion (Id {id} VersionId {versionId}) has been used by experiment preprocess.")); } // 最新バージョンを削除する場合 if (template.LatestVersion == templateVersion.Version) { var templateVersionsByTemplateId = templateVersionRepository .GetAll() .Where(x => x.TemplateId == id && x != templateVersion) .DefaultIfEmpty(); template.LatestVersion = templateVersionsByTemplateId?.Max(x => x.Version) ?? 0; templateRepository.Update(template); } templateVersionRepository.Delete(templateVersion); unitOfWork.Commit(); return(JsonNoContent()); }