Пример #1
0
        /// <summary>
        ///     提交个人作业。
        /// </summary>
        /// <param name="groupAssignment">个人作业。</param>
        /// <returns>服务结果。</returns>
        public async Task <ServiceResult> SubmitAsync(
            StudentAssignment studentAssignment)
        {
            var studentAssignmentToSubmit = new StudentAssignment {
                StudentID  = studentAssignment.StudentID,
                HomeworkID = studentAssignment.HomeworkID,
                Score      = studentAssignment.Score,
                Solution   = studentAssignment.Solution
            };

            var identifiedHttpMessageHandler =
                _identityService.GetIdentifiedHttpMessageHandler();

            using (var httpClient =
                       new HttpClient(identifiedHttpMessageHandler)) {
                var json =
                    JsonConvert.SerializeObject(studentAssignmentToSubmit);
                HttpResponseMessage response;
                try {
                    response = await httpClient.PutAsync(
                        App.ServerEndpoint + "/api/StudentAssignment/" +
                        studentAssignment.HomeworkID,
                        new StringContent(json, Encoding.UTF8,
                                          "application/json"));
                } catch (Exception e) {
                    return(new ServiceResult {
                        Status = ServiceResultStatus.Exception,
                        Message = e.Message
                    });
                }

                var serviceResult = new ServiceResult {
                    Status =
                        ServiceResultStatusHelper.FromHttpStatusCode(
                            response.StatusCode)
                };

                switch (response.StatusCode)
                {
                case HttpStatusCode.Unauthorized:
                case HttpStatusCode.Forbidden:
                    break;

                case HttpStatusCode.NoContent:
                    break;

                case HttpStatusCode.BadRequest:
                    serviceResult.Message =
                        await response.Content.ReadAsStringAsync();

                    break;

                default:
                    serviceResult.Message = response.ReasonPhrase;
                    break;
                }

                return(serviceResult);
            }
        }
Пример #2
0
        /// <summary>
        ///     登录。
        /// </summary>
        /// <returns>服务结果。</returns>
        public async Task <ServiceResult> LoginAsync()
        {
            var oidcClient = new OidcClient(CreateOidcClientOptions());

            LoginResult loginResult = null;

            try {
                loginResult = await oidcClient.LoginAsync(new LoginRequest());
            } catch (Exception e) {
                return(new ServiceResult {
                    Status = ServiceResultStatus.Exception, Message = e.Message
                });
            }

            if (!string.IsNullOrEmpty(loginResult.Error))
            {
                return new ServiceResult {
                           Status =
                               ServiceResultStatusHelper.FromHttpStatusCode(
                                   HttpStatusCode.BadRequest),
                           Message = loginResult.Error
                }
            }
            ;

            var refreshTokenHandler =
                (RefreshTokenHandler)loginResult.RefreshTokenHandler;

            _refreshToken = refreshTokenHandler.RefreshToken;
            _accessToken  = refreshTokenHandler.AccessToken;

            return(new ServiceResult {
                Status = ServiceResultStatus.OK
            });
        }
Пример #3
0
        /// <summary>
        ///     根据学号获得学生。
        /// </summary>
        /// <param name="studentId">学号。</param>
        /// <returns>服务结果。</returns>
        public async Task <ServiceResult <Student> > GetStudentByStudentIdAsync(
            string studentId)
        {
            var identifiedHttpMessageHandler =
                _identityService.GetIdentifiedHttpMessageHandler();

            using (var httpClient =
                       new HttpClient(identifiedHttpMessageHandler)) {
                HttpResponseMessage response;
                try {
                    response = await httpClient.GetAsync(
                        App.ServerEndpoint + "/api/Student?studentId=" +
                        HttpUtility.UrlEncode(studentId));
                } catch (Exception e) {
                    return(new ServiceResult <Student> {
                        Status = ServiceResultStatus.Exception,
                        Message = e.Message
                    });
                }

                var serviceResult = new ServiceResult <Student> {
                    Status =
                        ServiceResultStatusHelper.FromHttpStatusCode(
                            response.StatusCode)
                };

                switch (response.StatusCode)
                {
                case HttpStatusCode.Unauthorized:
                    break;

                case HttpStatusCode.OK:
                    var json = await response.Content.ReadAsStringAsync();

                    serviceResult.Result =
                        JsonConvert.DeserializeObject <Student>(json);
                    break;

                case HttpStatusCode.NotFound:
                    break;

                default:
                    serviceResult.Message = response.ReasonPhrase;
                    break;
                }

                return(serviceResult);
            }
        }
Пример #4
0
        /// <summary>
        ///     绑定账号。
        /// </summary>
        /// <param name="studentId">学号。</param>
        /// <returns>服务结果。</returns>
        public async Task <ServiceResult> BindAccountAsync(string studentId)
        {
            var identifiedHttpMessageHandler =
                _identityService.GetIdentifiedHttpMessageHandler();

            using (var httpClient =
                       new HttpClient(identifiedHttpMessageHandler)) {
                HttpResponseMessage response;
                try {
                    response = await httpClient.PutAsync(
                        App.ServerEndpoint + "/api/Student?studentId=" +
                        HttpUtility.UrlEncode(studentId),
                        new StringContent(""));
                } catch (Exception e) {
                    return(new ServiceResult {
                        Status = ServiceResultStatus.Exception,
                        Message = e.Message
                    });
                }

                var serviceResult = new ServiceResult {
                    Status =
                        ServiceResultStatusHelper.FromHttpStatusCode(
                            response.StatusCode)
                };

                switch (response.StatusCode)
                {
                case HttpStatusCode.Unauthorized:
                    break;

                case HttpStatusCode.NoContent:
                    break;

                case HttpStatusCode.BadRequest:
                    serviceResult.Message =
                        await response.Content.ReadAsStringAsync();

                    break;

                default:
                    serviceResult.Message = response.ReasonPhrase;
                    break;
                }

                return(serviceResult);
            }
        }
Пример #5
0
        public async Task <ServiceResult <IEnumerable <Announcement> > > GetAsync()
        {
            var identifiedHttpMessageHandler =
                _identityService.GetIdentifiedHttpMessageHandler();

            using (var httpClient =
                       new HttpClient(identifiedHttpMessageHandler)) {
                HttpResponseMessage response;
                try {
                    response =
                        await httpClient.GetAsync(
                            App.ServerEndpoint + "/api/Announcement");
                } catch (Exception e) {
                    return(new ServiceResult <IEnumerable <Announcement> > {
                        Status = ServiceResultStatus.Exception,
                        Message = e.Message
                    });
                }

                var serviceResult = new ServiceResult <IEnumerable <Announcement> > {
                    Status =
                        ServiceResultStatusHelper.FromHttpStatusCode(
                            response.StatusCode)
                };

                switch (response.StatusCode)
                {
                case HttpStatusCode.Unauthorized:
                case HttpStatusCode.Forbidden:
                    break;

                case HttpStatusCode.OK:
                    var json = await response.Content.ReadAsStringAsync();

                    serviceResult.Result = JsonConvert
                                           .DeserializeObject <List <Announcement> >(json);
                    break;

                default:
                    serviceResult.Message = response.ReasonPhrase;
                    break;
                }

                return(serviceResult);
            }
        }
Пример #6
0
        /// <summary>
        ///     注销。
        /// </summary>
        public async Task <ServiceResult> SignOutAsync()
        {
            ServiceResult serviceResult;

            using (var httpClient = new HttpClient()) {
                HttpResponseMessage response;

                try {
                    response =
                        await httpClient.GetAsync(
                            App.ServerEndpoint + "/Account/Logout");
                } catch (Exception e) {
                    return(new ServiceResult {
                        Status = ServiceResultStatus.Exception,
                        Message = e.Message
                    });
                }

                serviceResult = new ServiceResult {
                    Status =
                        ServiceResultStatusHelper.FromHttpStatusCode(
                            response.StatusCode)
                };

                switch (response.StatusCode)
                {
                case HttpStatusCode.NoContent:
                    break;

                default:
                    serviceResult.Message = response.ReasonPhrase;
                    break;
                }
            }

            _refreshToken = "empty";
            _accessToken  = "empty";
            Save();

            _rootNavigationService.Navigate(typeof(LoginPage), null,
                                            NavigationTransition.EntranceNavigationTransition);

            return(serviceResult);
        }
        /// <summary>
        ///     提交组内自评/互评表。
        /// </summary>
        /// <param name="peerWorkGroupEvaluation">组内自评互评表。</param>
        /// <returns>服务结果。</returns>
        public async Task <ServiceResult> SubmitAsync(
            PeerWorkGroupEvaluation peerWorkGroupEvaluation)
        {
            var workGroupEvaluationToSubmit = new PeerWorkGroupEvaluation {
                ID        = peerWorkGroupEvaluation.ID,
                StudentID = peerWorkGroupEvaluation.StudentID,
                TargetID  = peerWorkGroupEvaluation.TargetID,
                Finished  = peerWorkGroupEvaluation.Finished,
                Q1        = peerWorkGroupEvaluation.Q1,
                Q2        = peerWorkGroupEvaluation.Q2,
                Q3        = peerWorkGroupEvaluation.Q3,
                Q4        = peerWorkGroupEvaluation.Q4,
                Q5        = peerWorkGroupEvaluation.Q5,
                Q6        = peerWorkGroupEvaluation.Q6,
                Q7        = peerWorkGroupEvaluation.Q7,
                Q8        = peerWorkGroupEvaluation.Q8, Q9 = peerWorkGroupEvaluation.Q9
            };

            var identifiedHttpMessageHandler =
                _identityService.GetIdentifiedHttpMessageHandler();

            using (var httpClient =
                       new HttpClient(identifiedHttpMessageHandler)) {
                var json =
                    JsonConvert.SerializeObject(workGroupEvaluationToSubmit);
                HttpResponseMessage response;
                try {
                    response = await httpClient.PutAsync(
                        App.ServerEndpoint + "/api/PeerWorkGroupEvaluation/" +
                        peerWorkGroupEvaluation.TargetID,
                        new StringContent(json, Encoding.UTF8,
                                          "application/json"));
                } catch (Exception e) {
                    return(new ServiceResult {
                        Status = ServiceResultStatus.Exception,
                        Message = e.Message
                    });
                }

                var serviceResult = new ServiceResult {
                    Status =
                        ServiceResultStatusHelper.FromHttpStatusCode(
                            response.StatusCode)
                };

                switch (response.StatusCode)
                {
                case HttpStatusCode.Unauthorized:
                case HttpStatusCode.Forbidden:
                    break;

                case HttpStatusCode.NoContent:
                    break;

                case HttpStatusCode.BadRequest:
                    serviceResult.Message =
                        await response.Content.ReadAsStringAsync();

                    break;

                default:
                    serviceResult.Message = response.ReasonPhrase;
                    break;
                }

                return(serviceResult);
            }
        }