Esempio n. 1
0
        public async Task <IActionResult> GetToy(int id)
        {
            var location = GetControllerActionNames();

            try
            {
                _logger.LogInfo($"{location}: Attempted Call for id: {id}");
                var toy = await _toyRepository.FindById(id);

                if (toy == null)
                {
                    _logger.LogWarn($"{location}: Failed to retrieve record with id: {id}");
                    return(NotFound());
                }
                var response = _mapper.Map <ToyDTO>(toy);
                if (!string.IsNullOrEmpty(response.Image))
                {
                    var imgPath = GetImagePath(toy.Image);
                    if (System.IO.File.Exists(imgPath))
                    {
                        byte[] imgBytes = System.IO.File.ReadAllBytes(imgPath);
                        response.File = Convert.ToBase64String(imgBytes);
                    }
                }

                _logger.LogInfo($"{location}: Successfully got record with id: {id}");
                return(Ok(response));
            }
            catch (Exception e)
            {
                return(internalError($"{location}: {e.Message} - {e.InnerException}"));
            }
        }