public async Task <IActionResult> Put([FromBody] EcologicalMeasurementDTO dto)
        {
            if (dto == null)
            {
                return(BadRequest("Measurement information formatted incorrectly."));
            }

            var ecologicalMeasurement = new EcologicalMeasurement
            {
                Date_taken          = dto.Date_taken,
                EcologicalFootprint = dto.EcologicalFootprint,
                Transport           = dto.Transport,
                Diet        = dto.Diet,
                Electronics = dto.Electronics,
                Clothing    = dto.Clothing,
                Footwear    = dto.Footwear
            };

            var result = await _userRepository.UpdateEcologicalMeasurement(dto.UserId, ecologicalMeasurement);

            if (result == 1)
            {
                return(Ok());
            }
            else if (result == -8)
            {
                // return HTTP 404 as ecologicalMeasure with date cannot be found in DB
                return(NotFound("User with ID '" + dto.UserId + "' does not have ecological measure on " + dto.Date_taken));
            }
            else
            {
                // return HTTP 404 as user cannot be found in DB
                return(NotFound("User with ID '" + dto.UserId + "' does not exist."));
            }
        }
        public async Task <IActionResult> Post([FromBody] EcologicalMeasurementDTO dto)
        {
            if (dto == null)
            {
                return(BadRequest("Measurement information formatted incorrectly."));
            }

            var ecologicalMeasurement = new EcologicalMeasurement
            {
                Date_taken          = dto.Date_taken,
                EcologicalFootprint = dto.EcologicalFootprint,
                Transport           = dto.Transport,
                Diet        = dto.Diet,
                Electronics = dto.Electronics,
                Clothing    = dto.Clothing,
                Footwear    = dto.Footwear
            };

            int result = await _userRepository.AddEcologicalMeasurement(dto.UserId, ecologicalMeasurement);

            if (result == 1)
            {
                /***  HERE MIGHT CAL Collective_EF ***/

                // 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 = dto.UserId, date = dto.Date_taken }, ecologicalMeasurement));
            }
            else if (result == -7)
            {
                // return HTTP 409 as ecologicalMeasure with date already exists - conflict
                return(Conflict("User with ID '" + dto.UserId + "' already has an ecological measure on " + dto.Date_taken));
            }
            else if (result == -9)
            {
                // return HTTP 404 as user cannot be found in DB
                return(NotFound("User with ID '" + dto.UserId + "' does not exist."));
            }
            else
            {
                return(BadRequest("An internal error occurred.  Please contact the system administrator."));
            }
        }
Пример #3
0
        /*
         * Check if there has been any activity in the specified category in the specified amount of time so that
         * we can check early if the user is activly tracking this category while using PLNKTN.
         */
        private bool EcoMeasurementHasActivity(string _category, EcologicalMeasurement ecoMeasurement)
        {
            var _activityDetected = false;

            // Use reflection to dynamically get the correct 'category' and 'sub category' from the eco measurement
            // based on the text values held in the Challenge list entry.
            var _itemsCategory = ecoMeasurement.GetType().GetProperty(_category).GetValue(ecoMeasurement);

            // Create a dictionary to hold the list of property names and their values
            //var properties = new Dictionary<string, int>();
            // Iterate over the list of sub categories and add their property names and values to dict.
            foreach (var _tempProps in _itemsCategory.GetType().GetProperties())
            {
                if ((float)_tempProps.GetValue(_itemsCategory) > 0)
                {
                    _activityDetected = true;
                    break;
                }
            }

            return(_activityDetected);
        }