Beispiel #1
0
        public async Task Add_InvalidBoardMeeting_Returns_BadRequest()
        {
            //Arrange
            BoardMeetingInputDto x = null;

            //Act
            var result = await boardMeetingsController.CreateBoardMeeting(x);

            //Assert
            Assert.IsType <BadRequestResult>(result.Result);
        }
        public async Task <ActionResult <BoardMeetingViewDto> > CreateBoardMeeting([FromBody] BoardMeetingInputDto boardMeetingInputDto)
        {
            if (boardMeetingInputDto == null)
            {
                return(BadRequest());
            }

            //model state validation is not required due to the [ApiController] attribute automatically returning UnprocessableEntity (see startup.cs)
            //when model binding fails

            //fetch the user id from the JWT via HttpContext. Then get the user from the repository. This is to ensure that an authorized user
            //is calling the API with a valid user id
            var user = await _userRepository.GetUserAsync(User.GetUserId());

            if (user == null)
            {
                return(BadRequest(new { Error = "The user was not found in the system. Please try again with an authorized and valid user." }));
            }

            var boardMeetingToAdd = _mapper.Map <BoardMeeting>(boardMeetingInputDto); //map BoardMeetingInputDto to BoardMeeting

            boardMeetingToAdd.UserId = user.Id;                                       //set the user id as otherwise navigation property will be null

            if (boardMeetingInputDto.MeetingNotes != null)
            {
                var meetingMinuteToAdd = _mapper.Map <MeetingMinute>(boardMeetingInputDto.MeetingNotes); //map MeetingMinuteInputDto to MeetingMinute
                boardMeetingToAdd.MeetingNotes = meetingMinuteToAdd;                                     //set the user id as otherwise navigation property will be null
                meetingMinuteToAdd.UserId      = user.Id;                                                //add the MeetingMinute to the BoardMeeting
            }

            _boardMeetingRepository.AddBoardMeeting(boardMeetingToAdd);

            if (!await _boardMeetingRepository.SaveChangesAsync())
            {
                throw new Exception($"Error saving BoardMeeting {boardMeetingToAdd.Id} to the database");
            }

            var boardMeetingToReturn = _mapper.Map <BoardMeetingViewDto>(boardMeetingToAdd);

            return(CreatedAtRoute("GetBoardMeeting", new { id = boardMeetingToAdd.Id }, boardMeetingToReturn));
        }