コード例 #1
0
        public void Post_ReturnsCreatedMember()
        {
            var member = new MemberDetailDto
            {
                Id        = 1,
                FirstName = "Player",
                LastName  = "One",
                Sports    = new List <SportDto>
                {
                    new SportDto
                    {
                        Id   = 1,
                        Name = "Squash"
                    }
                }
            };

            _memberServiceMock.Setup(x => x.AddMember(It.IsAny <Member>()))
            .Callback <Member>(m => m.Id = member.Id);

            var actionResult = _membersController.PostMember(member);

            var objectResult      = Assert.IsType <CreatedAtActionResult>(actionResult.Result);
            var objectResultValue = Assert.IsAssignableFrom <MemberDto>(objectResult.Value);

            Assert.Equal(member.Id, objectResultValue.Id);
            Assert.Equal(member.FirstName, objectResultValue.FirstName);
            Assert.Equal(member.LastName, objectResultValue.LastName);
        }
コード例 #2
0
        public void Put_ReturnsNoContent()
        {
            var member = new MemberDetailDto
            {
                Id        = 1,
                FirstName = "Player",
                LastName  = "One",
                Sports    = new List <SportDto>
                {
                    new SportDto
                    {
                        Id   = 1,
                        Name = "Squash"
                    }
                }
            };

            _memberServiceMock.Setup(x => x.GetMemberById(It.IsAny <int>()))
            .Returns(new Member());
            _memberServiceMock.Setup(x => x.UpdateMember(It.IsAny <Member>()));

            var actionResult = _membersController.PutMember(member.Id, member);

            Assert.IsType <NoContentResult>(actionResult);
        }
コード例 #3
0
        public ActionResult PutMember(int id, MemberDetailDto memberDetailDto)
        {
            if (id != memberDetailDto.Id)
            {
                return(BadRequest());
            }

            var _member = _memberService.GetMemberById(id);

            if (_member == null)
            {
                return(NotFound());
            }

            var member = new Member
            {
                Id           = memberDetailDto.Id,
                FirstName    = memberDetailDto.FirstName,
                LastName     = memberDetailDto.LastName,
                MemberSports = memberDetailDto.Sports.Select(s => new MemberSport
                {
                    SportId = s.Id
                }).ToList()
            };

            _memberService.UpdateMember(member);

            return(NoContent());
        }
コード例 #4
0
        public void Put_ReturnsBadRequest_WhenIdsDoNotMatch()
        {
            var member = new MemberDetailDto
            {
                Id        = 1,
                FirstName = "Player",
                LastName  = "One",
                Sports    = new List <SportDto>
                {
                    new SportDto
                    {
                        Id   = 1,
                        Name = "Squash"
                    }
                }
            };

            var actionResult = _membersController.PutMember(2, member);

            Assert.IsType <BadRequestResult>(actionResult);
        }
コード例 #5
0
        public ActionResult <Member> PostMember(MemberDetailDto memberDetailDto)
        {
            var member = new Member
            {
                FirstName    = memberDetailDto.FirstName,
                LastName     = memberDetailDto.LastName,
                MemberSports = memberDetailDto.Sports.Select(s => new MemberSport
                {
                    SportId = s.Id
                }).ToList()
            };

            _memberService.AddMember(member);

            var memberDto = new MemberDto()
            {
                Id        = member.Id,
                FirstName = member.FirstName,
                LastName  = member.LastName
            };

            return(CreatedAtAction("GetMember", new { id = member.Id }, memberDto));
        }
コード例 #6
0
        public ActionResult <MemberDetailDto> GetMember(int id)
        {
            var member = _memberService.GetMemberById(id);

            if (member == null)
            {
                return(NotFound());
            }

            var memberModel = new MemberDetailDto
            {
                Id        = member.Id,
                FirstName = member.FirstName,
                LastName  = member.LastName,
                Sports    = member.MemberSports.Select(ms => new SportDto
                {
                    Id   = ms.SportId,
                    Name = ms.Sport.Name
                }).ToList()
            };

            return(Ok(memberModel));
        }