public async Task <ActionResult <FileDto> > StudentCreateVerifyFile([FromForm] FileCreateDto dto)
        {
            FileCreateDtoValidator validator = new FileCreateDtoValidator();
            ValidationResult       result    = await validator.ValidateAsync(dto);

            if (result.IsValid)
            {
                var userId = User.Claims
                             .Single(p => p.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").Value;
                var student = await _dbContext.Students
                              .Include(x => x.StudentVerify)
                              .Include(x => x.StudentVerify.StudentVerifyFiles)
                              .SingleOrDefaultAsync(x => x.ApplicationUserId == userId);

                if (student.StudentConfirmed)
                {
                    return(Problem(title: "禁止修改", detail: "在校生已驗證", statusCode: 403));
                }
                var entity = _mapper.Map <StudentVerifyFile>(dto);

                #region 處理檔案

                await using (var transaction = await _dbContext.Database.BeginTransactionAsync())
                {
                    try
                    {
                        using (var stream = new FileStream(entity.Path, FileMode.Create))
                        {
                            await dto.FileData.CopyToAsync(stream);
                        }
                        student.StudentVerify.StudentVerifyFiles.Add(entity);
                        _dbContext.Students.Update(student);
                        await _dbContext.SaveChangesAsync();

                        await transaction.CommitAsync();
                    }
                    catch (IOException)
                    {
                        await transaction.RollbackAsync();

                        throw;
                    }
                    catch (DbUpdateException)
                    {
                        System.IO.File.Delete(entity.Path);
                        await transaction.RollbackAsync();

                        throw;
                    }
                }

                #endregion

                var returnDto   = _mapper.Map <FileDto>(entity);
                var routeValues = new { fileId = returnDto.Id };
                return(CreatedAtAction(nameof(StudentVerifyFile), routeValues, returnDto));
            }
            return(BadRequest(result.Errors));
        }
        public async Task <IActionResult> CreatePhoto([FromForm] FileCreateDto dto)
        {
            FileCreateDtoValidator validator = new FileCreateDtoValidator();
            ValidationResult       result    = await validator.ValidateAsync(dto);

            if (result.IsValid)
            {
                var userId = User.Claims
                             .Single(p => p.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").Value;
                var user = await _dbContext.Users
                           .Include(x => x.UserPhoto)
                           .SingleOrDefaultAsync(x => x.Id == userId);

                if (user.UserPhoto != null)
                {
                    return(Problem(title: "禁止修改", detail: "使用者照片已存在", statusCode: 403));
                }

                var entity = _mapper.Map(dto, user.UserPhoto);

                #region 處理檔案

                await using (var transaction = await _dbContext.Database.BeginTransactionAsync())
                {
                    try
                    {
                        using (var stream = new FileStream(entity.Path, FileMode.Create))
                        {
                            await dto.FileData.CopyToAsync(stream);
                        }
                        user.UserPhoto = entity;
                        _dbContext.Users.Update(user);
                        await _dbContext.SaveChangesAsync();

                        await transaction.CommitAsync();
                    }
                    catch (IOException)
                    {
                        await transaction.RollbackAsync();

                        throw;
                    }
                    catch (DbUpdateException)
                    {
                        System.IO.File.Delete(entity.Path);
                        await transaction.RollbackAsync();

                        throw;
                    }
                }

                #endregion

                var returnDto = _mapper.Map <FileDto>(user.UserPhoto);
                return(CreatedAtAction(nameof(Photo), null, returnDto));
            }
            return(BadRequest(result.Errors));
        }
        public async Task <IActionResult> CreateRecruitmentFile([FromRoute] string campaignId, [FromRoute] string recruitmentId, [FromForm] FileCreateDto dto)
        {
            FileCreateDtoValidator validator = new FileCreateDtoValidator();
            ValidationResult       result    = await validator.ValidateAsync(dto);

            if (result.IsValid)
            {
                var userId = User.Claims
                             .Single(p => p.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").Value;
                var administrator = await _dbContext.Administrators
                                    .AsNoTracking()
                                    .SingleOrDefaultAsync(x => x.ApplicationUserId == userId);

                #region 驗證

                if (!administrator.AdministratorConfirmed)
                {
                    return(Problem(title: "禁止修改", detail: "管理員尚未驗證", statusCode: 403));
                }

                #endregion

                var act = await _dbContext.RecruitmentCampaigns
                          .Include(x => x.RecruitmentCampaignFiles)
                          .SingleOrDefaultAsync(x => x.CampaignId == campaignId && x.RecruitmentCampaignId == recruitmentId);

                if (act == null)
                {
                    return(NotFound());
                }

                var entity = _mapper.Map <RecruitmentCampaignFile>(dto);

                #region 處理檔案

                await using (var transaction = await _dbContext.Database.BeginTransactionAsync())
                {
                    try
                    {
                        using (var stream = new FileStream(entity.Path, FileMode.Create))
                        {
                            await dto.FileData.CopyToAsync(stream);
                        }
                        act.RecruitmentCampaignFiles.Add(entity);
                        _dbContext.RecruitmentCampaigns.Update(act);
                        await _dbContext.SaveChangesAsync();

                        await transaction.CommitAsync();
                    }
                    catch (IOException)
                    {
                        await transaction.RollbackAsync();

                        throw;
                    }
                    catch (DbUpdateException)
                    {
                        System.IO.File.Delete(entity.Path);
                        await transaction.RollbackAsync();

                        throw;
                    }
                }

                #endregion

                var routeValues = new { campaignId = act.CampaignId, recruitmentId = act.RecruitmentCampaignId, fileId = entity.RecruitmentCampaignFileId };
                var returnDto   = _mapper.Map <FileDto>(entity);
                return(CreatedAtAction(nameof(RecruitmentFile), routeValues, returnDto));
            }
            return(BadRequest(result.Errors));
        }
        public async Task <IActionResult> CreateCompanyLogo([FromRoute] string companyId, [FromForm] FileCreateDto dto)
        {
            FileCreateDtoValidator validator = new FileCreateDtoValidator();
            ValidationResult       result    = await validator.ValidateAsync(dto);

            if (result.IsValid)
            {
                var userId = User.Claims
                             .Single(p => p.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").Value;
                var company = await _dbContext.Companies
                              .Include(x => x.CompanyLogo)
                              .Include(x => x.Managers)
                              .SingleOrDefaultAsync(x => x.CompanyId == companyId);

                if (company == null)
                {
                    return(NotFound());
                }
                var manager = company.Managers
                              .SingleOrDefault(x => x.ApplicationUserId == userId);
                if (manager == null)
                {
                    return(Problem(title: "禁止修改", detail: "非該公司管理者", statusCode: 403));
                }
                if (!manager.ManagerConfirmed)
                {
                    return(Problem(title: "禁止修改", detail: "企業使用者尚未驗證", statusCode: 403));
                }

                var entity = _mapper.Map(dto, company.CompanyLogo);

                #region 處理檔案

                await using (var transaction = await _dbContext.Database.BeginTransactionAsync())
                {
                    try
                    {
                        using (var stream = new FileStream(entity.Path, FileMode.Create))
                        {
                            await dto.FileData.CopyToAsync(stream);
                        }
                        company.CompanyLogo = entity;
                        _dbContext.Companies.Update(company);
                        await _dbContext.SaveChangesAsync();

                        await transaction.CommitAsync();
                    }
                    catch (IOException)
                    {
                        await transaction.RollbackAsync();

                        throw;
                    }
                    catch (DbUpdateException)
                    {
                        System.IO.File.Delete(entity.Path);
                        await transaction.RollbackAsync();

                        throw;
                    }
                }

                #endregion

                var routeValues = new { companyId = entity.CompanyId };
                var returnDto   = _mapper.Map <FileDto>(entity);
                return(CreatedAtAction(nameof(CompanyLogo), routeValues, returnDto));
            }
            return(BadRequest(result.Errors));
        }