public async Task <IActionResult> CreateGoal(int userId, GoalForCreationDto goalCreationDto) { try { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } // Validate the total weights of the objectives within an axis instance var axisInstanceIds = new List <int> { goalCreationDto.AxisInstanceId }; var goalsGroupedByAxisInstanceList = await GetAxisInstancesWithGoals(axisInstanceIds); if (IsTotalWeightOfObjectivesOver100(goalsGroupedByAxisInstanceList, goalCreationDto.Weight)) { return(BadRequest("Le poids total de vos objectifs est supérieur à 100%!")); } // Create a new goal var goal = _mapper.Map <Goal>(goalCreationDto); _repo.Goal.AddGoal(goal); await _repo.Goal.SaveAllAsync(); return(CreatedAtRoute("GetGoal", new { id = goal.Id }, goal)); } catch (Exception ex) { _logger.LogError($"Something went wrong inside CreateGoal endpoint: {ex.Message}"); return(StatusCode(500, "Internal server error")); } }
public ActionResult <GoalDto> CreateGoal(GoalForCreationDto goal) { var goalEntity = _mapper.Map <Entities.Goal>(goal); goalEntity.UserId = AuthUser.Id; _financialGoalsRepository.AddGoal(goalEntity); _financialGoalsRepository.Save(); var goalToReturn = _mapper.Map <GoalDto>(goalEntity); return(CreatedAtRoute("GetGoal", new { goalId = goalToReturn.Id }, goalToReturn)); }