コード例 #1
0
        public IActionResult GetAllAsync()
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(Unauthorized("You need to be logged in to perform this action"));
            }

            string mailAdress = User.Identity.Name;

            if (mailAdress == null)
            {
                return(BadRequest("User not found"));
            }

            var user = _userRepository.GetBy(mailAdress);

            if (user == null)
            {
                return(BadRequest("User not found"));
            }

            var all = _registrationRepository.GetAllFromUser(mailAdress);

            if (all == null || !all.Any())
            {
                return(NotFound("No registrations found"));
            }

            return(Ok(all));
        }
        public void GetAll_AtLeastOneRegistration_ReturnsAllRegistrations()
        {
            // Arrange 
            var user = DummyData.UserFaker.Generate();

            _sut.ControllerContext = FakeControllerContext.For(user);

            _userRepository.GetBy(user.Email).Returns(user);
            _registrationRepository.GetAllFromUser(user.Email).Returns(user.Registrations);

            //Act
            var result = _sut.GetAllAsync();

            // Assert 
            result.Should().BeOfType<OkObjectResult>()
                .Which.Value.Should().BeEquivalentTo(
                    user.Registrations,
                    options => options.Using(new EnumAsStringAssertionRule()) //treat enums as strings
                );
        }
コード例 #3
0
        public IActionResult UpdatePrivacy(string privacy)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(Unauthorized("You need to be logged in to perform this action"));
            }

            string mailAdress = User.Identity.Name;

            if (mailAdress == null)
            {
                return(BadRequest("User not found"));
            }

            var user = _userRepository.GetBy(mailAdress);

            if (user == null)
            {
                return(BadRequest("User not found"));
            }

            Privacy updatedPrivacy = Privacy.PRIVATE;

            Enum.TryParse(privacy, out updatedPrivacy);

            user.Privacy = updatedPrivacy;

            var last = _registrationRepository.GetLast(mailAdress);

            if (last != null)
            {
                last.Privacy = updatedPrivacy;
                _registrationRepository.Update(last, mailAdress);
                user.Registrations = _registrationRepository.GetAllFromUser(mailAdress);
            }

            // Update User
            _userRepository.Update(user);

            return(Ok(user.Privacy));
        }