private async Task <StudentLearningEventLog> MapToStudentLearningEventLog(
            LearningActivityEventModel model,
            Persistence.Models.LearningApp learningApp)
        {
            if (null == learningApp)
            {
                return(null);
            }

            // Map model to entity
            var entity = new StudentLearningEventLog
            {
                StudentElectronicMailAddress = model.IdentityElectronicMailAddress,
                LeaningAppUrl         = model.LeaningAppUrl,
                UTCStartDate          = model.UTCStartDateTime,
                UTCEndDate            = model.UTCEndDateTime,
                LearningAppIdentifier = learningApp.LearningAppIdentifier
                                        // TODO: Add other properties as needed.
            };

            await setStudentIds(entity);

            if (model.UTCEndDateTime != null)
            {
                entity.TimeSpent = (int?)(entity.UTCEndDate.Value - entity.UTCStartDate).TotalSeconds;
            }
            return(entity);
        }
        public async Task <StudentLearningEventLog> AddorUpdateAsync(StudentLearningEventLog model)
        {
            var id = _db.StudentLearningEventLogs
                     .Where(el =>
                            el.StudentElectronicMailAddress == model.StudentElectronicMailAddress &&
                            el.LeaningAppUrl == model.LeaningAppUrl &&
                            el.UTCStartDate == model.UTCStartDate
                            )
                     .Select(el => el.Id)
                     .FirstOrDefault();

            if (id != 0)
            {
                model.Id = id;
                _db.Attach(model);
                _db.Entry(model).State = EntityState.Modified;
            }
            else
            {
                _db.Add(model);
            }
            await _db.SaveChangesAsync();

            return(model);
        }
        public async Task <StudentLearningEventLog> AddAsync(StudentLearningEventLog model)
        {
            _db.Add(model);
            await _db.SaveChangesAsync();

            return(model);
        }
        public async Task SaveLearningActivityEventAsync(LearningActivityEventModel model)
        {
            var learningApp = await _learningAppQueries.GetWhitelistedApp(model.LeaningAppUrl);

            StudentLearningEventLog entity = await MapToStudentLearningEventLog(model, learningApp);

            await _studentLearningEventLogCommands.AddAsync(entity);
        }
        private async Task setStudentIds(StudentLearningEventLog entity)
        {
            var student = await _studentInformationQueries.GetStudentFromEmail(entity.StudentElectronicMailAddress);

            if (null == student)
            {
                return;
            }
            entity.StudentUSI      = student.StudentUSI;
            entity.StudentUniqueId = student.StudentUniqueId;
        }