示例#1
0
        public async Task <ActionResult <UserTaskReadDTO> > GetTaskById(int id)
        {
            try
            {
                string token = HttpContext.Request.Headers["authorization"].Single().Split(" ")[1];
                if (!await _repository.ValidateToken(token))
                {
                    return(Unauthorized());
                }

                var taskItem = await _repository.GetTaskById(id);

                if (taskItem != null)
                {
                    int loggedUserId = Int32.Parse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
                    if (taskItem.userId == loggedUserId)
                    {
                        return(Ok(_mapper.Map <UserTaskReadDTO>(taskItem)));
                    }
                    else
                    {
                        return(Unauthorized());
                    }
                }
                return(NotFound());
            }
            catch (Exception)
            {
                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }
        }
        //Use Edit also for Create because the views are the exact same
        //To keep code DRY
        public ActionResult EditTask(int id)
        {
            Task task = new Task();

            if (id == 0)
            {
                task.Status = false;
            }
            else
            {
                task = _repository.GetTaskById(id);
            }

            return(View(Mapper.Map <TaskViewModel>(task)));
        }
        public IHttpActionResult DeleteTask(int id)
        {
            Task task = _repository.GetTaskById(id);

            if (task == null)
            {
                return(NotFound());
            }

            _repository.DeleteTask(task);

            return(Ok(task));
        }