Example #1
0
        public async Task <ActionResult <ProfessionDto> > CreateProfession(Guid academyId, [FromBody] ProfessionAddDto professionAddDto)
        {
            if (!await _academyRepository.AcademyExistAsync(academyId))
            {
                return(NotFound());
            }
            //ApiController在遇到professionAddDto为空时可以自动返回400错误
            var profession = _mapper.Map <Profession>(professionAddDto);

            profession.AcademyId = academyId;
            //添加导航属性
            profession.Academy = await _academyRepository.GetAcademyAsync(academyId);

            _professionRepository.AddProfession(profession);//只是被添加到DbContext里
            await _professionRepository.SaveAsync();

            var professionDto = _mapper.Map <ProfessionDto>(profession);

            return(CreatedAtRoute(nameof(GetProfession), new { professionId = profession.ProfessionId }, professionDto));
        }