示例#1
0
        public IActionResult ReadAll([FromQuery] ReadAllPromptsDto readAllPromptsDto)
        {
            ReadAllPromptsResponseDto responseDto = null;

            try
            {
                if (_httpContextAccessor.GetCurrentUserId() != readAllPromptsDto.AuthorId)
                {
                    throw new UnauthorizedAccessException($"Requested authorId does not match authenticated authorId.");
                }

                var lastReadAll         = readAllPromptsDto.LastReadAll.HasValue ? readAllPromptsDto.LastReadAll.Value : new DateTime(1970, 1, 1);
                var lastReadAllResponse = DateTime.Now;
                var prompts             = _promptService.ReadAll(readAllPromptsDto.AuthorId, lastReadAll);

                responseDto = new ReadAllPromptsResponseDto
                {
                    Prompts     = prompts.Select(p => _mapper.Map <ReadPromptResponseDto>(p)).ToArray(),
                    LastReadAll = lastReadAllResponse,
                };
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Encountered exception while attempting to satisfy read all prompts.  Message: {ex.Message}");
                Console.WriteLine(ex.StackTrace);
                return(BadRequest(new ErrorResponseDto(ex)));
            }

            return(Ok(responseDto));
        }