Exemplo n.º 1
0
        public async Task <ActionResult <UserTaskReadDTO> > CreateTask(UserTaskCreateDTO taskCreateDTO)
        {
            try
            {
                string token = HttpContext.Request.Headers["authorization"].Single().Split(" ")[1];
                if (!await _repository.ValidateToken(token))
                {
                    return(Unauthorized());
                }

                string loggedUserId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                taskCreateDTO.userId = Int32.Parse(loggedUserId);

                if (!await _repository.IsAdvancedUser(taskCreateDTO.userId))
                {
                    taskCreateDTO.text = null;
                }

                var taskModel = _mapper.Map <UserTask>(taskCreateDTO);
                await _repository.CreateTask(taskModel);

                await _repository.SaveChanges();

                var taskReadDTO = _mapper.Map <UserTaskReadDTO>(taskModel);

                return(CreatedAtRoute(nameof(GetTaskById), new { id = taskReadDTO.id }, taskReadDTO));
            }
            catch (Exception)
            {
                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }
        }