Beispiel #1
0
        /// <summary>
        /// Map the Course Model(EF generated) object to custom entity class Course object
        /// </summary>
        /// <param name="objectToMap">EF generated course model object which will be used for mapped</param>
        /// <returns>Mapped custom entity course object if inputted parameter objectToMap is not null otherwise returns null</returns>
        public CommonModel.Course MapFromEfCourseModel(EFModel.Course objectToMap)
        {
            if (objectToMap == null)
            {
                return(null);
            }

            return(new CommonModel.Course
            {
                Id = objectToMap.Id,
                Name = objectToMap.Name,
                Description = objectToMap.Description,
                Icon = objectToMap.Icon,
                AddedBy = objectToMap.AddedBy,
                IsActive = objectToMap.IsActive,
                IsPublished = objectToMap.IsPublished,
                Duration = objectToMap.Duration,
                CreatedOn = objectToMap.CreatedOn,
                CourseSubtopics = objectToMap.CourseSubtopics == null ? null : objectToMap.CourseSubtopics.Select(s =>
                {
                    var subtopic = MapFromEfCourseSubtopic(s);
                    return subtopic;
                }).ToList()
            });
        }
Beispiel #2
0
        public int AddOrUpdateCourse(CommonModel.Course courseData, int currentUserId)
        {
            // var data = ModelMapper.MapToEfCourseModel(courseData);
            var courseEntity = new EFModel.Course();

            if (courseData.Id == 0) // Add
            {
                courseEntity.Id          = 0;
                courseEntity.Name        = courseData.Name;
                courseEntity.Description = courseData.Description ?? "";
                courseEntity.Icon        = courseData.Icon ?? Constants.DefaultCourseIcon;
                courseEntity.Duration    = courseData.Duration;
                courseEntity.CreatedOn   = DateTime.Now;
                courseEntity.AddedBy     = currentUserId;
                courseEntity.IsActive    = true;
                courseEntity.IsPublished = false;

                UnitOfWork.CourseRepository.Add(courseEntity);
            }
            else    //Update
            {
                courseEntity = UnitOfWork.CourseRepository.Get(courseData.Id);
                if (courseEntity == null)
                {
                    return(0);
                }
                // ToDo : Try attach/entry for updating(save one trip to DB) or update only modified property
                // ToDo : Set IsPublished and IsActive
                courseEntity.Name        = courseData.Name;
                courseEntity.Icon        = courseData.Icon ?? Constants.DefaultCourseIcon;
                courseEntity.Description = courseData.Description ?? "";
                courseEntity.Duration    = courseData.Duration;
            }

            return(UnitOfWork.Commit() > 0 ? courseEntity.Id : 0);
        }