示例#1
0
        // Clean table Person of all its records
        static void CleanTablePerson()
        {
            var Db = new StreamAggregatorContext();

            Db.Persons.RemoveRange(Db.Persons);
            Db.SaveChanges();
        }
示例#2
0
        // Clean table Course of all its records
        static void CleanTableCourse()
        {
            var Db = new StreamAggregatorContext();

            Db.Courses.RemoveRange(Db.Courses);
            Db.SaveChanges();
        }
示例#3
0
        // Process/consume an event given in EvIt according to its event name, stream id and other arguments
        static void ConsumeEvent(EventStream EvIt)
        {
            var Db = new StreamAggregatorContext();

            switch (EvIt.Event.ToLower())
            {
            case "person_new":
                // Add new person to (local) data structure
                // Check first whether a person with same Id already exists
                var existingPerson = Db.Persons.FirstOrDefault(p => p.PersonId == EvIt.StreamId);
                if (existingPerson == null)
                {
                    // Person does not exist, so make sure name has been provided,
                    // and, if so, add new person to (local) data structure
                    if (EvIt.data.Name != "")
                    {
                        // Instantiate a new Person object, populate its properties..
                        var newPerson = new Person();
                        newPerson.PersonId = EvIt.StreamId;
                        newPerson.Name     = EvIt.data.Name;
                        // ..and add it to the data structure Persons
                        Db.Persons.Add(newPerson);
                        Db.SaveChanges();
                        // Log that "person_new" event was successfully processed
                        AddToLog(EvIt.EventId, EvIt.StreamId, $"Successfully processed event '{EvIt.Event}' to add person '{newPerson.Name}' with Id={newPerson.PersonId}.");
                    }
                    else
                    {
                        // Issue error that new person name has not been provided
                        AddToLog(EvIt.EventId, EvIt.StreamId, $"Could not process event '{EvIt.Event}' due to missing person name.");
                    }
                }
                else
                {
                    // Issue error that person already exists and cannot be replaced/overwritten
                    AddToLog(EvIt.EventId, EvIt.StreamId, $"Could not process event '{EvIt.Event}' due to person {EvIt.data.Name} already existing.");
                }
                break;

            case "course_new":
                // Add new course to (local) data structure
                // Check first, if course already exists
                var existingCourse = Db.Courses.FirstOrDefault(c => c.CourseId == EvIt.StreamId);
                if (existingCourse == null)
                {
                    // Course does not exist, so make sure course name has been provided, and if so,
                    // add it to (local) data structure
                    if (EvIt.data.Name != "")
                    {
                        // Instantiate a new Course object, populate its properties..
                        var newCourse = new Course();
                        newCourse.CourseId = EvIt.StreamId;
                        newCourse.Name     = EvIt.data.Name;
                        // ..and add it to the data structure Courses
                        Db.Courses.Add(newCourse);
                        Db.SaveChanges();
                        // Log that "course_new" event was successfully processed
                        AddToLog(EvIt.EventId, EvIt.StreamId, $"Successfully processed event '{EvIt.Event}' to create course '{EvIt.data.Name}'.");
                    }
                    else
                    {
                        // Issue error that course name has not been provided
                        AddToLog(EvIt.EventId, EvIt.StreamId, $"Could not process event '{EvIt.Event}' due to missing course name.");
                    }
                }
                else
                {
                    // Issue error that course already exists and cannot be replaced/overwritten
                    AddToLog(EvIt.EventId, EvIt.StreamId, $"Could not process event '{EvIt.Event}' due to course {EvIt.data.Name} already existing.");
                }
                break;

            case "course_enrolled":
                // Enroll existing person into existing course
                // Check whether course and person exist
                existingCourse = Db.Courses.FirstOrDefault(c => c.CourseId == EvIt.StreamId);
                if (existingCourse != null)
                {
                    existingPerson = Db.Persons.FirstOrDefault(p => p.PersonId == EvIt.data.PersonId);
                    if (existingPerson != null)
                    {
                        // Make sure that enrollment does not already exist
                        var existingCourseEnrolled = Db.CoursesEnrolled.FirstOrDefault(ce => ce.CourseId == EvIt.StreamId && ce.PersonId == EvIt.data.PersonId);
                        if (existingCourseEnrolled == null)
                        {
                            // Create a new CourseEnrollment object, populate all properties and save to data structure
                            var newCourseEnrolled = new CourseEnrolled();
                            newCourseEnrolled.CourseId = EvIt.StreamId;
                            newCourseEnrolled.PersonId = (int)EvIt.data.PersonId;
                            Db.CoursesEnrolled.Add(newCourseEnrolled);
                            Db.SaveChanges();
                            // Log that "course_enrolled" event was successfully processed
                            AddToLog(EvIt.EventId, EvIt.StreamId, $"Successfully processed event '{EvIt.Event}' to enroll person '{existingPerson.Name}' in course '{existingCourse.Name}'.");
                        }
                        else
                        {
                            // Issue error that course enrollment does already exist
                            AddToLog(EvIt.EventId, EvIt.StreamId, $"Could not process event '{EvIt.Event}' due to course enrollment for person '{existingPerson.Name}' in course '{existingCourse.Name}' already existing.");
                        }
                    }
                    else
                    {
                        // Issue error that person does not exist
                        AddToLog(EvIt.EventId, EvIt.StreamId, $"Could not process event '{EvIt.Event}' due to person {EvIt.data.PersonId} not existing.");
                    }
                }
                else
                {
                    // Issue error that course does not exist
                    AddToLog(EvIt.EventId, EvIt.StreamId, $"Could not process event '{EvIt.Event}' due to course {EvIt.StreamId} not existing.");
                }
                break;

            case "course_renamed":
                // Rename existing course
                // First, ensure course to rename exists
                existingCourse = Db.Courses.FirstOrDefault(c => c.CourseId == EvIt.StreamId);
                if (existingCourse != null)
                {
                    if (EvIt.data.Name != null)
                    {
                        // Course exists and can be renamed. Save new name to data structure.
                        string existingCourseName = existingCourse.Name;
                        existingCourse.Name = EvIt.data.Name;
                        Db.SaveChanges();
                        // Log that "course_renamed" event was successfully processed
                        AddToLog(EvIt.EventId, EvIt.StreamId, $"Successfully processed event '{EvIt.Event}' to rename course '{existingCourseName}' to '{EvIt.data.Name}'.");
                    }
                    else
                    {
                        // Issue error that new course name was not provided
                        AddToLog(EvIt.EventId, EvIt.StreamId, $"Could not process event '{EvIt.Event}' due to missing course name.");
                    }
                }
                else
                {
                    // Issue error that course to rename does not exist
                    AddToLog(EvIt.EventId, EvIt.StreamId, $"Could not process event '{EvIt.Event}' due to course {EvIt.StreamId} not existing.");
                }
                break;

            case "course_disenrolled":
                // Unenroll person from course
                // First, ensure enrollment record exists
                var existingCourseEnrolled2 = Db.CoursesEnrolled.FirstOrDefault(ce => ce.CourseId == EvIt.StreamId && ce.PersonId == EvIt.data.PersonId);
                if (existingCourseEnrolled2 != null)
                {
                    // Remove enrollment record from database
                    Db.CoursesEnrolled.Remove(existingCourseEnrolled2);
                    Db.SaveChanges();
                    // Log that "course_disenrolled" event was successfully processed
                    AddToLog(EvIt.EventId, EvIt.StreamId, $"Successfully processed event '{EvIt.Event}' to unenroll person {EvIt.data.PersonId} from course {EvIt.StreamId}.");
                }
                else
                {
                    // Issue error that course enrollment does not exist
                    AddToLog(EvIt.EventId, EvIt.StreamId, $"Could not process event '{EvIt.Event}' due to course enrollment for course {EvIt.StreamId} and person {EvIt.data.PersonId} not existing.");
                }
                break;

            case "person_renamed":
                // Rename existing person
                // First, ensure person exists, and new name has been provided; if so, overwrite name
                existingPerson = Db.Persons.FirstOrDefault(p => p.PersonId == EvIt.StreamId);
                if (existingPerson != null && EvIt.data.Name != null && EvIt.data.Name != "")
                {
                    // Save new name and save data to data structure
                    string existingPersonName = existingPerson.Name;
                    existingPerson.Name = EvIt.data.Name;
                    Db.SaveChanges();
                    // Log that "person_renamed" event was successfully processed
                    AddToLog(EvIt.EventId, EvIt.StreamId, $"Successfully processed event '{EvIt.Event}' to rename person '{existingPersonName}' to '{EvIt.data.Name}'.");
                }
                else
                {
                    // Person does not exist or name not provided: cannot rename, issue appropriate error
                    AddToLog(EvIt.EventId, EvIt.StreamId, $"Could not process event '{EvIt.Event}' due to person {EvIt.StreamId} not existing or person name not provided.");
                }
                break;

            default:
                // Log that event could not be processed, because it has no code handler
                AddToLog(EvIt.EventId, EvIt.StreamId, $"Could not process event '{EvIt.Event}' because no handler exists for it.");
                break;
            }
        }