public void Handle(EditedRatingEvent @event) { using (var db = new DisciturContext()) { // get read-model Ids (ID-maps) int lessonId = _identityMapper.GetModelId <Lesson>(@event.Id); int ratingtId = _identityMapper.GetModelId <LessonRating>(@event.RatingId); Lesson lesson = db.Lessons.Find(lessonId); LessonRating rating = db.LessonRatings.Find(ratingtId); rating.Rating = @event.Rating; rating.Content = @event.Content; rating.LastModifDate = @event.Date; rating.Vers++; // Persist changes db.Entry(rating).State = EntityState.Modified; lesson.Rate = CalculateAverageRating(rating); // TODO: is it correct to update readModel "devops fields" of lesson in this case? UpdateLessonArchFields(lesson, rating.LastModifUser, @event.Date, @event.Version); // Persist changes db.Entry(lesson).State = EntityState.Modified; db.SaveChanges(); } }
public void Handle(AddedNewRatingEvent @event) { using (var db = new DisciturContext()) { // get read-model Ids (ID-maps) int lessonId = _identityMapper.GetModelId <Lesson>(@event.Id); int userId = _identityMapper.GetModelId <User>(@event.UserId); // get involved read-model entities User author = db.Users.Find(userId); Lesson lesson = db.Lessons.Find(lessonId); // Create new Read-Model Lesson's Comment LessonRating rating = new LessonRating(); rating.LessonId = lessonId; rating.UserId = userId; rating.Author = author; rating.Rating = @event.Rating; rating.Content = @event.Content ?? string.Empty; rating.CreationDate = @event.Date; rating.LastModifDate = @event.Date; rating.LastModifUser = author.UserName; rating.Vers = 1; rating.RecordState = Constants.RECORD_STATE_ACTIVE; // Add new Lesson's Rating db.LessonRatings.Add(rating); //// Update Lesson Rating with Average of Ratings on the same Lesson //IQueryable<int> prevRatings = db.LessonRatings // .Where(r => r.LessonId.Equals(rating.LessonId) && // r.RecordState.Equals(Constants.RECORD_STATE_ACTIVE)) // .Select(r => r.Rating); //List<int> ratingsList = prevRatings.ToList(); //ratingsList.Add(rating.Rating); //lesson.Rate = Math.Max((int)Math.Round(ratingsList.Average()), 1); lesson.Rate = CalculateAverageRating(rating); // Update Lesson's version // TODO: is it correct to update readModel "devops fields" of lesson in this case? UpdateLessonArchFields(lesson, author.UserName, @event.Date, @event.Version); // Persist changes db.Entry(lesson).State = EntityState.Modified; db.SaveChanges(); // Map new IDs // NOTE: Comment is NOT and AR, but it's mapped with it's own Id-map for compatibility with existant read-model _identityMapper.Map <LessonRating>(rating.Id, @event.RatingId); } }
private int CalculateAverageRating(LessonRating rating) { using (var db = new DisciturContext()) { // Calculate Lesson Rating with Average of Ratings on the same Lesson IQueryable <int> prevRatings = db.LessonRatings .Where(r => r.LessonId.Equals(rating.LessonId) && !r.Id.Equals(rating.Id) && r.RecordState.Equals(Constants.RECORD_STATE_ACTIVE)) .Select(r => r.Rating); List <int> ratingsList = prevRatings.ToList(); if (rating.RecordState.Equals(Constants.RECORD_STATE_ACTIVE)) { ratingsList.Add(rating.Rating); } return(ratingsList.Count > 0 ? Math.Max((int)Math.Round(ratingsList.Average()), 1) : 0); } }
protected List <int> UseModelForBest5Prediction(MLContext mlContext, ITransformer model) { var top5List = new List <int> { 0, 0, 0, 0, 0, 0 }; var top5ListScore = new List <int> { 0, 0, 0, 0, 0, 0 }; var numberofLessons = new Lesson(); var predictionEngine = mlContext.Model.CreatePredictionEngine <LessonRating, LessonRatingPrediction>(model); for (var i = 0; i < numberofLessons.GetLengthOfDB(); i++) { var testInput = new LessonRating { userId = Convert.ToInt64(Session["id"]), lessonId = i }; var lessonRatingPrediction = predictionEngine.Predict(testInput); var lowestIndex = IndexOfMin(top5ListScore); if (lessonRatingPrediction.Score > top5ListScore[lowestIndex]) { top5List[lowestIndex] = i; top5ListScore[lowestIndex] = (int)lessonRatingPrediction.Score; } } top5List.Sort(); System.Diagnostics.Debug.WriteLine("HERE ARE THE TOP 5"); for (var j = 0; j < 6; j++) { System.Diagnostics.Debug.WriteLine(top5List[j] + " " + Environment.NewLine); } return(top5List); }
public void Handle(LessonMementoPropagatedEvent @event) { using (var db = new DisciturContext()) { int itemId = _identityMapper.GetModelId <Lesson>(@event.Memento.Id); if (itemId.Equals(0)) { int userId = _identityMapper.GetModelId <User>(@event.Memento.AuthorId); User _user = db.Users.Find(userId); Lesson lesson = new Lesson(); lesson.Title = @event.Memento.Title; lesson.Discipline = @event.Memento.Discipline; lesson.School = @event.Memento.School; lesson.Classroom = @event.Memento.Classroom; lesson.Author = _user; lesson.UserId = _user.UserId; lesson.Content = @event.Memento.Content; lesson.Conclusion = @event.Memento.Conclusion; lesson.PublishDate = @event.Memento.CreationDate; lesson.CreationDate = @event.Memento.CreationDate; UpdateLessonArchFields(lesson, _user.UserName, @event.Memento.CreationDate, @event.Memento.Version); //lesson.LastModifDate = @event.CreationDate; //lesson.LastModifUser = _user.UserName; //lesson.Vers = 1; lesson.RecordState = Constants.RECORD_STATE_ACTIVE; // Create FeedBacks Collection @event.Memento.FeedBacks.ToList() .ForEach(feedback => { LessonFeedback fb = new LessonFeedback() { Feedback = feedback.Feedback, Nature = feedback.Nature, LessonFeedbackGuid = feedback.Id }; lesson.FeedBacks.Add(fb); }); // Create Tags Collection @event.Memento.Tags.ToList() .ForEach(tag => { LessonTag t = new LessonTag() { LessonTagName = tag.LessonTagName }; lesson.Tags.Add(t); }); db.Lessons.Add(lesson); db.SaveChanges(); //Ids mapping _identityMapper.Map <Lesson>(lesson.LessonId, @event.Memento.Id); if (lesson.FeedBacks.Count > 0) { lesson.FeedBacks.ToList() .ForEach(feedback => _identityMapper.Map <LessonFeedback>(feedback.LessonFeedbackId, feedback.LessonFeedbackGuid)); } // Create Ratings Collection List <int> ratingsList = new List <int>(); @event.Memento.Ratings.ToList() .ForEach(r => { int authorId = _identityMapper.GetModelId <User>(r.UserId); User author = db.Users.Find(userId); ratingsList.Add(r.Rating); LessonRating rating = new LessonRating() { LessonId = lesson.LessonId, UserId = authorId, Author = author, Rating = r.Rating, Content = r.Content ?? string.Empty, CreationDate = r.CreationDate, LastModifDate = r.LastModifDate, LastModifUser = r.LastModifUser, Vers = r.Vers, RecordState = Constants.RECORD_STATE_ACTIVE }; // Add new Lesson's Rating db.LessonRatings.Add(rating); db.SaveChanges(); _identityMapper.Map <LessonRating>(rating.Id, r.Id); }); lesson.Rate = ratingsList.Count > 0 ? Math.Max((int)Math.Round(ratingsList.Average()), 1) : 0; // Create Comments Collection @event.Memento.Comments.ToList() .ForEach(c => { // get read-model Ids (ID-maps) int _userId = _identityMapper.GetModelId <User>(c.AuthorId); int?parentId = c.ParentId == null ? null : (int?)_identityMapper.GetModelId <LessonComment>(c.ParentId.Value); // get involved read-model entities User author = db.Users.Find(userId); // Create new Read-Model Lesson's Comment LessonComment comment = new LessonComment() { LessonId = lesson.LessonId, Content = c.Content, CreationDate = c.CreationDate, Date = c.Date, LastModifDate = c.Date, Level = c.Level, ParentId = parentId, Vers = c.Vers, RecordState = Constants.RECORD_STATE_ACTIVE, UserId = _userId, Author = author, LastModifUser = author.UserName }; db.LessonComments.Add(comment); db.SaveChanges(); // Map new IDs // NOTE: Comment is NOT and AR, but it's mapped with it's own Id-map for compatibility with existant read-model _identityMapper.Map <LessonComment>(comment.Id, c.Id); }); // Persist changes db.Entry(lesson).State = EntityState.Modified; db.SaveChanges(); } // otherwise it could be used for maintenance purposes } }