示例#1
0
        public async void UpdateStudent()
        {
            List <Student> students = await _context.Student.ToListAsync();

            // Create Debug Item to record event time
            Debug debug = new Debug
            {
                EventName = "Update XP points for " + students.Count.ToString() + " students",
                StartTime = DateTime.Now
            };

            try
            {
                foreach (Student student in students)
                {
                    student.Score = MicrosoftLearnUtil.GetXP(student.UserId);
                }
            }
            catch (Exception e)
            {
                debug.Exception = e.ToString();
            }
            finally
            {
                debug.EndTime = DateTime.Now;
                _context.Debug.Add(debug);
                await _context.SaveChangesAsync();
            }
        }
示例#2
0
        public async Task <ActionResult <Student> > PostStudent(NewStudent student)
        {
            // if (StudentExists(student.Username))
            // {
            //     return BadRequest("Student already exists in the database");
            // }
            try
            {
                string  userId     = MicrosoftLearnUtil.GetUserId(student.Username);
                Student newStudent = new Student
                {
                    FirstName  = student.FirstName,
                    LastName   = student.LastName,
                    School     = student.School,
                    Username   = student.Username,
                    Email      = student.Email,
                    CreateDate = DateTime.Now,
                    UserId     = userId,
                    Score      = MicrosoftLearnUtil.GetXP(userId)
                };

                _context.Student.Add(newStudent);
                await _context.SaveChangesAsync();

                return(CreatedAtAction("GetStudent", new { id = newStudent.Id }, newStudent));
            }
            catch (WebException)
            {
                return(BadRequest("Unable to find user, please check the username"));
            }
        }