Пример #1
0
        public bool UpdateCourse(Course courseToUpdate)
        {
            try
            {
                using (var context = new TrainingTrackerEntities())
                {
                    var courseEntity = context.Courses.FirstOrDefault(x => x.Id == courseToUpdate.Id);

                    if (courseEntity == null)
                    {
                        return(false);
                    }

                    courseEntity.Name        = courseToUpdate.Name;
                    courseEntity.Description = courseToUpdate.Description;
                    courseEntity.Icon        = courseToUpdate.Icon;
                    courseEntity.Duration    = courseToUpdate.Duration;
                    context.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                LogUtility.ErrorRoutine(ex);
                return(false);
            }
        }
Пример #2
0
        /// <summary>
        /// Add Course
        /// </summary>
        /// <param name="courseToAdd">Course data object</param>
        /// <returns>Course Id of the added course if successful otherwise 0 </returns>
        public int AddCourse(Course courseToAdd)
        {
            try
            {
                using (var context = new TrainingTrackerEntities())
                {
                    EntityFramework.Course newCourseEntity = ModelMapper.MapToCourseModel(courseToAdd);

                    context.Courses.Add(newCourseEntity);
                    context.SaveChanges();
                    return(newCourseEntity.Id);
                }
            }
            catch (Exception ex)
            {
                LogUtility.ErrorRoutine(ex);
                return(0);
            }
        }
Пример #3
0
        /// <summary>
        /// Map custom entity class Course object to EF generated class Course object
        /// </summary>
        /// <param name="objectToMap">Custom entity Course object</param>
        /// <returns>EF generated course object if inputted parameter objectToMap is not null otherwise returns null</returns>
        public EFModel.Course MapToEfCourseModel(CommonModel.Course objectToMap)
        {
            if (objectToMap == null)
            {
                return(null);
            }

            return(new EFModel.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
            });
        }
Пример #4
0
        /// <summary>
        ///  Private to class method to generate course
        /// </summary>
        /// <param name="courseId">Course Id</param>
        /// <param name="userId">User Id</param>
        /// <returns>success flag for the event</returns>
        internal bool GenerateCourseFeedback(int courseId, int userId)
        {
            Course course = LearningPathDataAccessor.GetCourseWithAllData(courseId, userId);

            Feedback feedback = new Feedback
            {
                AddedBy = new User {
                    UserId = Constants.AppBotUserId
                },
                AddedFor = new User {
                    UserId = userId
                },
                FeedbackType = new Common.Entity.FeedbackType {
                    FeedbackTypeId = (int)FeedbackType.Course
                },
                FeedbackText = UtilityFunctions.GenerateHtmlForCourseFeedback(course),
                Title        = course.Name
            };

            return(AddFeedback(feedback));
        }
Пример #5
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);
        }