コード例 #1
0
        public IActionResult Post([FromBody] Assignment item)
        {
            try
            {
                if (item == null ||
                    !ModelState.IsValid ||
                    item.AssignmentId != 0 ||
                    item.Goal != null ||
                    item.GoalId == 0 ||
                    item.User != null ||
                    item.UserId != 0)
                {
                    return(BadRequest(ErrorCode.DataProvidedIsInvalid.ToString()));
                }

                if (!_goalRepository.DoesItemExist(item.GoalId))
                {
                    return(BadRequest(ErrorCode.DataProvidedIsInvalid.ToString()));
                }

                var userItem = _userRepository.Find(User.FindFirstValue(ClaimTypes.Email));
                if (userItem == null)
                {
                    return(NotFound(ErrorCode.ItemNotFound.ToString()));
                }

                item.UserId = userItem.UserId;

                _assignmentRepository.Insert(item);
            }
            catch (Exception)
            {
                return(BadRequest(ErrorCode.CouldNotCreateItem.ToString()));
            }

            return(Ok(item));
        }