Ejemplo n.º 1
0
 public GoalsController(AllGoalsQuery allGoalsQuery,
                        UserGoalsQuery userGoalsQuery,
                        GoalQuery goalQuery,
                        CreateGoalCommand createGoalCommand,
                        UpdateGoalCommand updateGoalCommand,
                        DeleteGoalCommand deleteGoalCommand,
                        CompleteGoalCommand completeGoalCommand)
 {
     this.allGoalsQuery       = allGoalsQuery;
     this.userGoalsQuery      = userGoalsQuery;
     this.goalQuery           = goalQuery;
     this.createGoalCommand   = createGoalCommand;
     this.updateGoalCommand   = updateGoalCommand;
     this.deleteGoalCommand   = deleteGoalCommand;
     this.completeGoalCommand = completeGoalCommand;
 }
Ejemplo n.º 2
0
        public async Task <IActionResult> CreateGoalAsync(CreateGoalCommand createGoalCommand)
        {
            if (!Guid.TryParse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value, out Guid userId))
            {
                return(Unauthorized());
            }

            if (userId != createGoalCommand.CreatedBy)
            {
                return(Forbid());
            }

            if (string.IsNullOrEmpty(createGoalCommand.Title))
            {
                return(BadRequest("The title cannot be empty"));
            }

            var createdGoal = await _mediator.Send(createGoalCommand);

            Response.Headers.Add("Access-Control-Allow-Headers", "Location");
            Response.Headers.Add("Access-Control-Expose-Headers", "Location");

            return(CreatedAtRoute(routeName: nameof(GetGoalAsync), routeValues: new { id = createdGoal }, value: null));
        }