public async Task <IActionResult> Post(string id, string region_name, [FromBody] Rgn project)
        {
            if (project == null)
            {
                return(BadRequest("project formatted incorrectly."));
            }

            int result = await _userRepository.AddUserGrantedReward(id, region_name, project, _appTotalTreesPlantedId);

            if (result == 1)
            {
                // return HTTP 201 Created with project object in body of return and a 'location' header with URL of newly created object
                return(CreatedAtAction("Get", new { id, region_name }, project));
            }
            else if (result == -7)
            {
                return(Ok("User already planted in this area. It is ok to plant again. Region count has been incremented"));
            }
            else if (result == -9)
            {
                // return HTTP 404 as user cannot be found in DB
                return(NotFound("User with ID '" + id + "' does not exist."));
            }
            else
            {
                return(BadRequest("An internal error occurred.  Please contact the system administrator."));
            }
        }
예제 #2
0
        public async Task <IActionResult> Put(string region_name, [FromBody] Rgn project)
        {
            if (region_name == null)
            {
                // return HTTP 400 badrequest as something is wrong
                return(BadRequest("Country information formatted incorrectly."));
            }

            var result = await _rewardRepository.ThrowTreeInBin(region_name, project);

            if (result == 1)
            {
                return(Ok());
            }
            else
            {
                return(NotFound("Country with name '" + region_name + "' does not exist."));
            }
        }
예제 #3
0
        public async Task <int> ThrowTreeInBin(string region_name, Rgn project)
        {
            using (var context = _dbConnection.Context())
            {
                try
                {
                    Bin region = await context.LoadAsync <Bin>(region_name);

                    if (region != null)
                    {
                        if (region.Projects != null)
                        {
                            region.Projects.Add(project);
                            await context.SaveAsync(region);

                            return(1);
                        }
                        else
                        {
                            region.Projects = new List <Rgn>();
                            region.Projects.Add(project);
                            await context.SaveAsync(region);

                            return(1);
                        }
                    }
                    else
                    {
                        // 404 - Country with specified name doesn't exist
                        return(-9);
                    }
                }
                catch (AmazonServiceException ase)
                {
                    Debug.WriteLine("Could not complete operation");
                    Debug.WriteLine("Error Message:  " + ase.Message);
                    Debug.WriteLine("HTTP Status:    " + ase.StatusCode);
                    Debug.WriteLine("AWS Error Code: " + ase.ErrorCode);
                    Debug.WriteLine("Error Type:     " + ase.ErrorType);
                    Debug.WriteLine("Request ID:     " + ase.RequestId);
                    return(-1);
                }
                catch (AmazonClientException ace)
                {
                    Debug.WriteLine("Internal error occurred communicating with DynamoDB");
                    Debug.WriteLine("Error Message:  " + ace.Message);
                    return(-1);
                }
                catch (NullReferenceException e)
                {
                    Debug.WriteLine("Context obj for DynamoDB set to null");
                    Debug.WriteLine("Error Message:  " + e.Message);
                    Debug.WriteLine("Inner Exception:  " + e.InnerException);
                    return(-1);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Internal error occurred communicating with DynamoDB");
                    Debug.WriteLine("Error Message:  " + e.Message);
                    Debug.WriteLine("Inner Exception:  " + e.InnerException);
                    return(-1);
                }
            }
        }