Esempio n. 1
0
        public IActionResult GetAllBearTypes()
        {
            try
            {
                var bearTypes = _bearTypeRepository.GetAll();
                var result    = _mapper.Map <IEnumerable <BearTypeDto> >(bearTypes);

                return(Ok(result));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
                return(Problem("Failed to retrieve bear types from repository."));
            }
        }
Esempio n. 2
0
        private IMapper CreateMapper()
        {
            var bearTypes = _bearTypeRepository.GetAll().ToDictionary(key => key.BearTypeId);
            var genders   = _genderRepository.GetAll().ToDictionary(key => key.GenderId);

            var mapper = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <Bear, BearDto>().AfterMap((src, dest) =>
                {
                    dest.BearTypeName = bearTypes[dest.BearTypeId].Name;
                    dest.GenderName   = genders[dest.GenderId].Name;
                });
                cfg.CreateMap <BearDto, Bear>();
                cfg.CreateMap <BearCreateDto, Bear>();
                cfg.CreateMap <BearUpdateDto, Bear>();
            }).CreateMapper();

            return(mapper);
        }