예제 #1
0
        private static void UserData()
        {
            UCSContext context = new UCSContext();

            string urlRequestToken = "https://usosapps.prz.edu.pl/services/users/user";

            string oauth_token        = HttpContext.Current.Session["oauth_token"].ToString();
            string oauth_token_secret = HttpContext.Current.Session["oauth_token_secret"].ToString();

            long     timestamp        = (long)(DateTime.Now - new DateTime(1970, 1, 1)).TotalSeconds;
            string   nonce            = Guid.NewGuid().ToString("N");
            string   parameters       = $"fields={WebUtility.UrlEncode("email|first_name|last_name")}&oauth_consumer_key=EmYdreSZDVA2AZGQTkDY&oauth_nonce={nonce}&oauth_signature_method=HMAC-SHA1&oauth_timestamp={timestamp}&oauth_token={oauth_token}&oauth_version=1.0";
            string   signatureContent = $"GET&{WebUtility.UrlEncode(urlRequestToken)}&{WebUtility.UrlEncode(parameters)}";
            HMACSHA1 sha1             = new HMACSHA1()
            {
                Key = Encoding.ASCII.GetBytes(WebUtility.UrlEncode("fNterDE3qrTHsxGrgz86yvHVUyrFngamMQR5wLTB") + "&" + WebUtility.HtmlEncode(oauth_token_secret))
            };
            string signature  = Convert.ToBase64String(sha1.ComputeHash(Encoding.ASCII.GetBytes(signatureContent)));
            string requestUrl = $"https://usosapps.prz.edu.pl/services/users/user?{parameters}&oauth_signature={WebUtility.UrlEncode(signature)}";

            using (HttpClient httpClient = new HttpClient())
            {
                HttpResponseMessage response = httpClient.GetAsync(requestUrl).Result;

                string responseData = response.Content.ReadAsStringAsync().Result;

                StudentResponseDTO studentData = JsonConvert.DeserializeObject <StudentResponseDTO>(responseData);

                Student studentDb = context.Students.SingleOrDefault(s => s.UserName == studentData.Email);
                if (studentDb == null)
                {
                    Student newStudentDb = new Student()
                    {
                        Guid         = Guid.NewGuid().ToString("N"),
                        FirstName    = studentData.FirstName,
                        LastName     = studentData.LastName,
                        UserName     = studentData.Email,
                        IsActive     = true,
                        AddedAt      = DateTime.Now,
                        LastActivity = DateTime.Now
                    };
                    context.Students.Add(newStudentDb);
                    context.SaveChanges();

                    HttpContext.Current.Session["ucs_student_guid"] = newStudentDb.Guid;
                }
                else
                {
                    studentDb.LastActivity = DateTime.Now;
                    context.SaveChanges();
                    HttpContext.Current.Session["ucs_student_guid"] = studentDb.Guid;
                }
            }
        }
        public CourseStudentResponseDTO connectWithUser(CourseStudentResponseDTO response)
        {
            StudentResponseDTO student = this._httpClientService.SendRequest <StudentResponseDTO>(HttpMethod.Get, "http://localhost:40001/api/users/students/" + response.student.uuid, new UserPrincipal(_httpContextAccessor.HttpContext).token).Result;

            if (student == null)
            {
                throw new EntityNotFoundException($"Student with uuid: {student.uuid} does not exist!", GeneralConsts.MICROSERVICE_NAME);
            }

            response.student = student;
            return(response);
        }
        public CourseStatisticsResponseDTO Get_Course_Satistics_Student_Uuid(string studentUuid)
        {
            try
            {
                StudentResponseDTO student = this._httpClientService.SendRequest <StudentResponseDTO>(HttpMethod.Get, "http://localhost:40001/api/users/students/" + studentUuid, new UserPrincipal(_httpContextAccessor.HttpContext).token).Result;
            }
            catch
            {
                throw new EntityNotFoundException("Student with uuid " + studentUuid + " doesn't exist", GeneralConsts.MICROSERVICE_NAME);
            }
            CourseStatistics stat = this._queryExecutor.Execute <CourseStatistics>(DatabaseConsts.USER_SCHEMA, this._sqlCommands.GET_COURSE_STATISTICS_STUDENT_UUID(studentUuid), this._modelMapper.MapToCourseStatistics);

            if (stat == null)
            {
                throw new EntityNotFoundException($"No data for student with uuid: " + studentUuid, GeneralConsts.MICROSERVICE_NAME);
            }
            return(this._autoMapper.Map <CourseStatisticsResponseDTO>(stat));
        }
        public List <CourseStudentMultipleResponseDTO> GetAllCoursesByStudentUuid(string studentUuid)
        {
            //provera da li postoji student
            try
            {
                StudentResponseDTO student = this._httpClientService.SendRequest <StudentResponseDTO>(HttpMethod.Get, "http://localhost:40001/api/users/students/" + studentUuid, new UserPrincipal(_httpContextAccessor.HttpContext).token).Result;
            }
            catch
            {
                throw new EntityNotFoundException("Student with uuid " + studentUuid + " doesn't exist", GeneralConsts.MICROSERVICE_NAME);
            }
            //ako student vrsi zahtev, moze da upise samo sebe
            UserPrincipal up = new UserPrincipal(_httpContextAccessor.HttpContext);

            if (up.role == RoleConsts.ROLE_STUDENT_ONLY)
            {
                checkStudent(studentUuid, up.uuid);
            }
            List <CourseStudent> courses = this._queryExecutor.Execute <List <CourseStudent> >(DatabaseConsts.USER_SCHEMA, this._sqlCommands.GET_ALL_COURSES_FROM_STUDENT(studentUuid), this._modelMapper.MapToCourseStudents);

            return(this._autoMapper.Map <List <CourseStudentMultipleResponseDTO> >(courses));
        }