private IHttpActionResult GetErrorResult(RepoResponse result)
        {
            if (result == null)
            {
                return(InternalServerError());
            }

            if (!result.succeded)
            {
                if (result.errors != null)
                {
                    foreach (string error in result.errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                }

                if (ModelState.IsValid)
                {
                    // No ModelState errors are available to send, so just return an empty BadRequest.
                    return(BadRequest());
                }

                return(BadRequest(ModelState));
            }

            return(null);
        }
Exemple #2
0
        public async Task <RepoResponse <bool> > UpdateAsync(CheckDupAddSql model, int updateBy)
        {
            var pars = GetParams(model, ignoreKey: new string[]
            {
                nameof(model.CreatedBy),
                nameof(model.CreatedTime),
                nameof(model.UpdatedTime),
                nameof(model.UpdatedBy),
            });

            pars.Add(nameof(model.UpdatedBy), updateBy);
            try
            {
                using (var con = GetConnection())
                {
                    await con.ExecuteAsync("sp_UpdateCustomer_v2", pars, commandType : CommandType.StoredProcedure);

                    return(RepoResponse <bool> .Create(true));
                }
            }
            catch (Exception e)
            {
                return(RepoResponse <bool> .Create(false, GetException(e)));
            }
        }
Exemple #3
0
        /// <summary>
        /// Implements the logic after the response for Github's GraphQL API.
        /// </summary>
        /// <returns><c>true</c>, if to run a request again, <c>false</c> otherwise.</returns>
        /// <param name="httpResponse">Http response.</param>
        /// <param name="repoResponse">Repo response.</param>
        public bool AfterResponse(HttpResponseMessage httpResponse, RepoResponse repoResponse)
        {
            if (repoResponse is PullRequestResponse)
            {
                PullRequestResponse prResponse = (PullRequestResponse)repoResponse;
                // If a failure with the response, always exit cleanly.
                if (!httpResponse.IsSuccessStatusCode)
                {
                    throw new HttpRequestException("Failed HTTP request to Github's v4/GraphQL API: " + httpResponse.StatusCode + " - " + httpResponse.Content.ReadAsStringAsync().Result);
                }

                // Add the result - if there's a key, then we're using Repo-specific requests
                if (string.IsNullOrEmpty(_currentKey))
                {
                    var prResults = JsonConvert.DeserializeObject <gh.GraphQLPullRequest>((httpResponse.Content.ReadAsStringAsync()).Result);
                    prResponse.PullRequests.AddRange(ParseGraphQLResponse(prResults));
                }
                else
                {
                    var prResults = JsonConvert.DeserializeObject <gh.GraphQLRepoSpecificPullRequest>((httpResponse.Content.ReadAsStringAsync()).Result);
                    prResponse.PullRequests.AddRange(ParseRepoGraphQLResponse(prResults));
                }

                // No pagination, then we're done
                if (string.IsNullOrEmpty(_nextCursor))
                {
                    return(false);
                }
                // Otherwise keep going
                return(true);
            }

            return(false);
        }
 protected BaseResponse <T> ToResponse <T>(RepoResponse <T> result)
 {
     if (!result.success)
     {
         return(new BaseResponse <T>(result.error, result.data, result.success));
     }
     return(new BaseResponse <T>(null, result.data, true));
 }
        /// <summary>
        /// Implements the logic after the response for Github's REST API.
        /// </summary>
        /// <returns><c>true</c>, if to run a request again, <c>false</c> otherwise.</returns>
        /// <param name="httpResponse">Http response.</param>
        /// <param name="repoResponse">Repo response.</param>
        public bool AfterResponse(HttpResponseMessage httpResponse, RepoResponse repoResponse)
        {
            if (repoResponse is PullRequestResponse)
            {
                PullRequestResponse prResponse = (PullRequestResponse)repoResponse;
                // If a failure with the response, throw an exception so it gets caught and logged.
                if (!httpResponse.IsSuccessStatusCode)
                {
                    throw new HttpRequestException("Failed HTTP request to Github's v3/REST API: " + httpResponse.StatusCode + " - " + httpResponse.Content.ReadAsStringAsync().Result);
                }

                // If the URI contains 'orgs' we must have been getting the list of repos
                if (httpResponse.RequestMessage.RequestUri.AbsoluteUri.Contains("/orgs/"))
                {
                    var repoResults = JsonConvert.DeserializeObject <List <Dictionary <string, object> > >((httpResponse.Content.ReadAsStringAsync()).Result);
                    foreach (Dictionary <string, object> repo in repoResults)
                    {
                        if (repo.ContainsKey("name"))
                        {
                            _repoList.Add((string)repo["name"]);
                        }
                    }

                    return(_makeAnotherRequest);
                }

                // Add the result (right now the GH REST response "is" our internal data model, otherwise other conversion would be necessary)
                var prResults = JsonConvert.DeserializeObject <List <gh.PullRequest> >((httpResponse.Content.ReadAsStringAsync()).Result);
                prResponse.PullRequests.AddRange(ConvertGithubPullRequests(prResults));

                var nextLink = ReturnNextLinkPage(httpResponse.Headers);
                if (string.IsNullOrEmpty(nextLink))
                {
                    // Remove the first one (this is the one we've been working on
                    _repoList.RemoveAt(0);
                    // Reset pagination
                    _nextPage = null;
                    if (_repoList.Count < 1)
                    {
                        return(false);
                    }
                }
                else
                {
                    _nextPage           = new Uri(nextLink);
                    _makeAnotherRequest = true;
                }
                return(_makeAnotherRequest);
            }

            return(false);
        }
Exemple #6
0
        public bool AfterResponse(HttpResponseMessage httpResponse, RepoResponse repoResponse)
        {
            if (repoResponse.RepoResponseType == "PullRequestResponse")
            {
                PullRequestResponse prResponse = (PullRequestResponse)repoResponse;
                if (PRResponseToReturn != null)
                {
                    prResponse.PullRequests = PRResponseToReturn.PullRequests;
                }
            }

            return(RepeatResponse);
        }
        public async Task <IHttpActionResult> Register(UserModel userModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            RepoResponse result = await this.repoService.RegisterUser(userModel);

            IHttpActionResult errorResult = GetErrorResult(result);

            if (errorResult != null)
            {
                return(errorResult);
            }

            return(Ok());
        }
Exemple #8
0
        public object CreateRepoTemplate(string repo, string projectName)
        {
            RepoResponse repoInstance = new RepoResponse();
            APIServicecs ApiObject;
            string       Url = "https://api.github.com/repos/ashwin9627/NewRepo987"; //RepoCreated";

            ApiObject = new APIServicecs(Session["PAT"].ToString());
            var    userDetails = JsonConvert.DeserializeObject <UserDetails>(ApiObject.ApiService("https://api.github.com/user"));
            string ownername   = userDetails.login;//"ashwin9627";//;

            //string Url = "https://api.github.com/repos/"+ ownername + "/"+repo;     //RepoCreated";
            ApiObject = new APIServicecs(Session["PAT"].ToString());
            string RequestBody = System.IO.File.ReadAllText(System.Web.HttpContext.Current.Server.MapPath("~") + @"\Jsons\RepoTemplate.json");

            RequestBody = RequestBody.Replace("$name$", projectName);
            RequestBody = RequestBody.Replace("$owner$", ownername);
            string responsestring = ApiObject.ApiService1(Url, "POST", RequestBody, "");

            repoInstance = JsonConvert.DeserializeObject <RepoResponse>(responsestring);
            string output = JsonConvert.SerializeObject(repoInstance);

            return(output);
        }
Exemple #9
0
        public async Task <RepoResponse <TeacherReturnDto> > LoginTeacher(TeacherLoginDto loginCreds)
        {
            var teacher = await AuthenticateTeacher(loginCreds);

            if (teacher == null)
            {
                var failResponse = new RepoResponse <TeacherReturnDto>()
                {
                    IsSuccess = false,
                    Message   = _config["ErrorCodes:login"]
                };
                return(failResponse);
            }

            if (teacher.RegisterStatus != RegistrationStatus.Approved)
            {
                return(new RepoResponse <TeacherReturnDto>()
                {
                    IsSuccess = false,
                    Message = _config["ErrorCodes:reg_status"],
                    Content = teacher
                });
            }

            var token = GenerateToken(teacher.Id, teacher.Email, "teacher");

            var response = new RepoResponse <TeacherReturnDto>()
            {
                Content   = teacher,
                IsSuccess = true,
                // Message = token (token based auth)
                Message = _config["SuccessCodes:login"]
            };

            return(response);
        }
Exemple #10
0
        public async Task <RepoResponse <StudentReturnDto> > LoginStudent(StudentLoginDto loginCreds)
        {
            var student = await AuthenticateStudent(loginCreds);

            if (student == null)
            {
                var failResponse = new RepoResponse <StudentReturnDto>()
                {
                    IsSuccess = false,
                    Message   = _config["ErrorCodes:login"]
                };
                return(failResponse);
            }

            if (student.RegisterStatus != RegistrationStatus.Approved)
            {
                return(new RepoResponse <StudentReturnDto>()
                {
                    IsSuccess = false,
                    Message = _config["ErrorCodes:reg_status"],
                    Content = student
                });
            }

            var token = GenerateToken(student.Id, student.Email, "student");

            var response = new RepoResponse <StudentReturnDto>()
            {
                IsSuccess = true,
                Content   = student,
                //Message = token (token based auth)
                Message = _config["SuccessCodes:login"]
            };

            return(response);
        }
Exemple #11
0
        public async Task <RepoResponse <int> > CreateAsync(CheckDupAddSql model, int createdBy)
        {
            try
            {
                using (var con = GetConnection())
                {
                    var pars = GetParams(model, nameof(model.Id), ignoreKey: new string[] {
                        nameof(model.CreatedTime),
                        nameof(model.UpdatedTime),
                        nameof(model.UpdatedBy),
                        nameof(model.CreatedBy)
                    });
                    pars.Add("CreatedBy", createdBy);

                    await con.ExecuteAsync("sp_InsertCustomer_v2", pars, commandType : CommandType.StoredProcedure);

                    return(RepoResponse <int> .Create(pars.Get <int>(nameof(model.Id))));
                }
            }
            catch (Exception e)
            {
                return(RepoResponse <int> .Create(0, GetException(e)));
            }
        }
Exemple #12
0
        public static T Resolve <T, TObject>(this ResolveFieldContext <TObject> field, RepoResponse <T> response)
            where T : class
        {
            // Checking the errors
            if (response.Errors?.Count() > 0)
            {
                // Looping them
                foreach (var e in response.Errors)
                {
                    // Adding them
                    field.Errors.Add(new ExecutionError(e));
                }
            }

            // Returing the data
            return(response.Data);
        }