Example #1
0
        public async Task <ActionResult <ProfessionDto> > UpdateProfession(Guid professionId, ProfessionUpdateDto professionUpdateDto)
        {
            if (!await _professionRepository.ProfessionExistAsync(professionId))
            {
                return(NotFound());
            }

            var profession = await _professionRepository.GetProfessionAsync(professionId);

            if (profession == null)
            {
                var professionToAdd = _mapper.Map <Profession>(professionUpdateDto);
                professionToAdd.ProfessionId = professionId;

                _professionRepository.AddProfession(professionToAdd);

                await _professionRepository.SaveAsync();

                var professionDtoNew = _mapper.Map <ProfessionDto>(professionToAdd);
                return(CreatedAtRoute(nameof(GetProfession), new { professionId = profession.ProfessionId }, professionDtoNew));
            }

            _mapper.Map(professionUpdateDto, profession);

            _professionRepository.UpdateProfession(profession);

            await _professionRepository.SaveAsync();

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

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