Пример #1
0
        public async Task <IActionResult> Post([FromBody] UserDetailsDTO userDto)
        {
            if (userDto == null)
            {
                // return HTTP 400 badrequest as something is wrong
                return(BadRequest("User information formatted incorrectly."));
            }

            // Generate the 'user rewards' for this new 'user' ready for insertion to the DB so, that the user has a complete
            // list of rewards and challenges so, they can participate in reward and challenge completion.
            var rewards = await _rewardRepository.GetAllRewards();

            var userRewards = (List <UserReward>)GenerateUserRewards(rewards);

            // Create new user
            var user = new User()
            {
                Id                     = userDto.Id,
                First_name             = userDto.First_name,
                Last_name              = userDto.Last_name,
                Created_at             = DateTime.UtcNow,
                Email                  = userDto.Email,
                Level                  = userDto.Level,
                EcologicalMeasurements = new List <EcologicalMeasurement>(),
                LivingSpace            = userDto.LivingSpace,
                NumPeopleHousehold     = userDto.NumPeopleHousehold,
                CarMPG                 = userDto.CarMPG,
                ShareData              = userDto.ShareData,
                Country                = userDto.Country,
                UserRewards            = userRewards,
                GrantedRewards         = new List <Bin>()
            };

            // Save the new user to the DB
            var result = await _userRepository.CreateUser(user);

            if (result == 1)
            {
                // return HTTP 201 Created with user object in body of return and a 'location' header with URL of newly created object
                return(CreatedAtAction("Get", new { id = userDto.Id }, user));
            }
            else if (result == -10)
            {
                // return HTTP 409 Conflict as user already exists in DB
                return(Conflict("User with ID '" + userDto.Id + "' already exists.  Cannot create a duplicate."));
            }
            else
            {
                // return HTTP 400 badrequest as something is wrong
                return(BadRequest("An internal error occurred.  Please contact the system administrator."));
            }
        }
Пример #2
0
        public async Task <IActionResult> Get()
        {
            var result = await _rewardRepository.GetAllRewards();

            if (result != null && result.Count > 0)
            {
                return(Ok(result));
            }
            else
            {
                // return HTTP 404 as rewards cannot be found in DB
                return(NotFound("No Rewards are present in the DB."));
            }
        }
Пример #3
0
        public IEnumerable <Reward> GetAllRewards()
        {
            var allRewards = _rewardRepo.GetAllRewards();

            return(allRewards);
        }