示例#1
0
        public async Task <IActionResult> AddGoal([FromBody] GoalRequestModel model)
        {
            try
            {
                var employee = await _employeeRepository.GetById(model.EmployeeId);

                if (employee == null)
                {
                    return(BadRequest("Employee does not exist"));
                }

                var subject = await _subjectRepository.GetById(model.SubjectId);

                if (subject == null)
                {
                    return(BadRequest("Subject does not exist"));
                }

                var goal = new Goal(model.EmployeeId, model.SubjectId);

                _repository.AddGoal(goal);
                await _repository.SaveChanges();

                return(Ok());
            }
            catch (Exception exception)
            {
                return(BadRequest(exception.Message));
            }
        }
示例#2
0
        public async Task <IActionResult> AddNewGoal([FromBody] CreateGoalRequestModel goalCreateRequestModel)
        {
            try
            {
                System.Threading.Thread.Sleep(1000);
                var goalExists = await _goalRepository.GoalExists(goalCreateRequestModel.GoalName);

                if (goalExists)
                {
                    return(BadRequest(new ApiResponseBadRequestResult()
                    {
                        ErrorMessage = $"Goal {goalCreateRequestModel.GoalName} already exists"
                    }));
                }

                var newGoal = await _goalRepository.AddGoal(_mapper.Map <Goal>(goalCreateRequestModel));

                return(Ok(new ApiResponseOKResult()
                {
                    Data = newGoal
                }));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failed to create new goal");
                return(StatusCode((int)HttpStatusCode.InternalServerError, new ApiResponseFailure()
                {
                    ErrorMessage = "Failed to create new goal"
                }));
            }
        }