public async Task <bool> RemoveProjectCaseAsync(int projectId) { if (projectId == 0) { throw new ArgumentNullException(nameof(projectId)); } try { ProjectCase projectCase = await this.context.ProjectCases.Where(p => p.Id == projectId).FirstOrDefaultAsync(); if (projectCase == null) { return(true); } if (!string.IsNullOrEmpty(projectCase.ImagePath) && File.Exists(projectCase.ImagePath)) { File.Delete(projectCase.ImagePath); } if (!string.IsNullOrEmpty(projectCase.DescriptionPath) && File.Exists(projectCase.DescriptionPath)) { File.Delete(projectCase.DescriptionPath); } this.context.ProjectCases.Remove(projectCase); return(await this.context.SaveChangesAsync() > 0); } catch { throw; } }
/// <summary> /// /// </summary> /// <param name="projectId"></param> /// <returns></returns> /// <exception cref="Exception">Project with id not found</exception> /// <exception cref="ArgumentNullException">projectId</exception> public async Task <ProjectCase> GetProjectCaseAsync(int projectId) { if (projectId == 0) { throw new ArgumentNullException(nameof(projectId)); } try { ProjectCase projectCase = await this.context.ProjectCases.Where(p => p.Id == projectId).FirstOrDefaultAsync(); if (projectCase == null) { throw new Exception($"Project with id {projectId} not found."); } await this.AttachProjectCaseFilesAsync(projectCase).ConfigureAwait(false); return(projectCase); } catch { throw; } }
/// <summary> /// /// </summary> /// <param name="updatedProject"></param> /// <returns></returns> /// <exception cref="Exception">Project with id not found</exception> /// <exception cref="ArgumentNullException">updatedProject</exception> public async Task <ProjectCase> UpdateProjectCaseAsync(ProjectCase updatedProject, string webRootFilePath) { updatedProject = updatedProject ?? throw new ArgumentNullException(nameof(updatedProject)); webRootFilePath = webRootFilePath ?? throw new ArgumentNullException(nameof(webRootFilePath)); try { ProjectCase projectCase = await this.context.ProjectCases.Where(p => p.Id == updatedProject.Id).FirstOrDefaultAsync(); if (projectCase == null) { throw new Exception($"Project with id {updatedProject.Id} not found."); } projectCase.ProjectName = updatedProject.ProjectName; if (!string.IsNullOrEmpty(projectCase.DescriptionPath)) { await this.SaveTextFileAsync(projectCase.DescriptionPath, updatedProject.Description).ConfigureAwait(false); } if (updatedProject.Image?.Length > 0) { updatedProject.ImageMimeType = updatedProject.ImageMimeType ?? throw new ArgumentNullException("ImageMimeType cannot be null."); if (!string.IsNullOrEmpty(projectCase.ImagePath) && File.Exists(projectCase.ImagePath)) { File.Delete(projectCase.ImagePath); } string folderPath = this.GetProjectFolderPath(projectCase); projectCase.Image = updatedProject.Image; projectCase = await this.SaveProjectImageAsync(projectCase, webRootFilePath, folderPath).ConfigureAwait(false); } if (this.context.Entry(projectCase).State == EntityState.Unchanged || await this.context.SaveChangesAsync() > 0) { return(projectCase); } return(null); } catch { throw; } }
public async Task <ProjectCaseModel> GetProjectCaseByIdAsync(int projectId) { try { ProjectCase entity = await this.repo.GetProjectCaseAsync(projectId).ConfigureAwait(false); if (entity == null) { return(null); } ProjectCaseModel model = new ProjectCaseModel(); model.ToModel(entity); return(model); } catch { throw; } }
/// <summary> /// /// </summary> /// <param name="projectCase"></param> /// <param name="fileFolderPath"></param> /// <returns>null if fail</returns> public async Task <ProjectCase> CreateProjectCaseAsync(ProjectCase projectCase, string webRootFilePath) { projectCase = projectCase ?? throw new ArgumentNullException(nameof(projectCase)); webRootFilePath = webRootFilePath ?? throw new ArgumentNullException(nameof(webRootFilePath)); try { projectCase = await this.SaveProjectFilesAsync(projectCase, webRootFilePath).ConfigureAwait(false); this.context.ProjectCases.Add(projectCase); if (await this.context.SaveChangesAsync() > 0) { return(projectCase); } return(null); } catch { throw; } }
public async Task <ProjectCaseModel> UpdateProjectCaseAsync(ProjectCaseModel model, string webRootFilePath) { model = model ?? throw new ArgumentNullException(nameof(model)); webRootFilePath = webRootFilePath ?? throw new ArgumentNullException(nameof(webRootFilePath)); try { ProjectCase entity = await this.repo.UpdateProjectCaseAsync(model.ToEntity(), webRootFilePath).ConfigureAwait(false); if (entity == null) { return(null); } ProjectCaseModel updatedModel = new ProjectCaseModel(); updatedModel.ToModel(entity); return(updatedModel); } catch { throw; } }
/// <summary> /// /// </summary> /// <param name="model"></param> /// <param name="fileFolderPath">Need folders {fileFolderPath}/Files/ProjectFiles</param> /// <returns></returns> public async Task <ProjectCaseModel> CreateProjectCaseAsync(ProjectCaseModel model, string fileFolderPath) { model = model ?? throw new ArgumentNullException(nameof(model)); fileFolderPath = fileFolderPath ?? throw new ArgumentNullException(nameof(fileFolderPath)); try { ProjectCase entity = await this.repo.CreateProjectCaseAsync(model.ToEntity(), fileFolderPath).ConfigureAwait(false); if (entity == null) { return(null); } ProjectCaseModel addedModel = new ProjectCaseModel(); addedModel.ToModel(entity); return(addedModel); } catch { throw; } }