public IActionResult ComposeForCreate(int statusCode, FacultyCreateDto facultyCreateDto) { switch (statusCode) { case -1: return(new BadRequestObjectResult("Invalid faculty name.")); case -2: return(new BadRequestObjectResult("Invalid number of places.")); case -3: return(new BadRequestObjectResult("Invalid number of budjet places.")); case -6: return(new BadRequestObjectResult("Invalid faculty number of places (can`t be greater than 255).")); case -7: return(new BadRequestObjectResult("Invalid faculty number of budget places (can`t be greater than 255).")); default: return(new CreatedAtRouteResult("GetFaculty", new { Id = statusCode }, facultyCreateDto)); } }
public async Task <int> AddAsync(FacultyCreateDto facultyCreateDto) { if (string.IsNullOrEmpty(facultyCreateDto.Name)) { _logger.LogWarn("Incorrect FacultyCreateModel. Create operation failed."); return(-1); } if (facultyCreateDto.NumberOfPlaces == 0) { _logger.LogWarn("Incorrect FacultyCreateModel. Create operation failed."); return(-2); } if (facultyCreateDto.NumberOfBudgetPlaces == 0) { _logger.LogWarn("Incorrect FacultyCreateModel. Create operation failed."); return(-3); } if (facultyCreateDto.NumberOfPlaces > 255) { _logger.LogWarn("Incorrect FacultyCreateModel. Create operation failed."); return(-6); } if (facultyCreateDto.NumberOfBudgetPlaces > 255) { _logger.LogWarn("Incorrect FacultyCreateModel. Create operation failed."); return(-7); } var faculty = _mapper.Map <Faculty>(facultyCreateDto); await _selectionCommitteeDataStorage.FacultyRepository.AddAsync(faculty); await _selectionCommitteeDataStorage.SaveChangesAsync(); _logger.LogInfo("AddAsync(FacultyCreateDto facultyCreateDto) from SelectionCommittee.BLL.Faculties.Services.FacultyService has been finished."); return(faculty.Id); }
public async Task <IActionResult> CreateFaculty([FromRoute] string campaignId, [FromRoute] string recruitmentId, [FromRoute] string openingId, [FromBody] FacultyCreateDto dto) { FacultyCreateDtoValidator validator = new FacultyCreateDtoValidator(); 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 role = User.Claims .Single(p => p.Type == ClaimTypes.Role).Value; var act = await _dbContext.RecruitmentCampaigns .Include(x => x.RecruitmentCampaignOpenings.Where(y => y.RecruitmentCampaignOpeningId == openingId)) .ThenInclude(x => x.Faculties) .Include(x => x.Company) .ThenInclude(x => x.Managers) .SingleOrDefaultAsync(x => x.CampaignId == campaignId && x.RecruitmentCampaignId == recruitmentId); var opening = act.RecruitmentCampaignOpenings.First(); #region 驗證 if (role == RoleType.Manager.ToString("G")) { if (!act.Company.Managers.Any(x => x.ApplicationUserId == userId)) { return(Problem(title: "禁止新增", detail: "非此活動邀請企業", statusCode: 403)); } } #endregion var entity = _mapper.Map <Faculty>(dto); opening.Faculties.Add(entity); _dbContext.RecruitmentCampaigns.Update(act); await _dbContext.SaveChangesAsync(); var routeValues = new { campaignId, recruitmentId, openingId, facultyId = entity.FacultyId }; var returnDto = _mapper.Map <FacultyDto>(entity); return(CreatedAtAction(nameof(Faculty), routeValues, returnDto)); } return(BadRequest(result.Errors)); }