Beispiel #1
0
        public async Async.Task <IActionResult> UpdateTask([FromBody] Task task, [FromHeader(Name = "Authorization")] string token)
        {
            logger.LogTrace("updating task {task}", task.ID);

            // Simple check if entity is present
            if (task.ID == 0)
            {
                return(BadRequest());
            }

            User loggedInUser = cache.GetUserByToken(token);

            Task original = context.Tasks.Include(t => t.Initiator).Include(t => t.Assignees).FirstOrDefault(x => x.ID == task.ID);

            // Check if user is allowed to edit task
            if (UserIsNotAllowedToEditTask(loggedInUser, original))
            {
                return(BadRequest("Only assignees or the initiator are allowed to change the task"));
            }

            if (original.Done != task.Done)
            {
                SetTaskTo(loggedInUser.ID, task.ID, task.Done);
            }

            context.Tasks.Update(task);

            context.SaveChanges();

            task = context.Tasks.Include(x => x.Assignees).ThenInclude(x => x.User).Include(x => x.Initiator).FirstOrDefault(x => x.ID == task.ID);

            // Send update message, but ignore errors
            try
            {
                await botService.SendTaskUpdatedMessage(task, loggedInUser);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Error sending Message to currently logged in user with token {token}", token);
            }

            return(Ok(task));
        }
 public User GetUserByToken([FromHeader(Name = "Authorization")] string token)
 {
     return(cache.GetUserByToken(token));
 }