Esempio n. 1
0
        public bool Add(Courses request)
        {
            var exists = isExists(request);

            if (exists == true)
            {
                return(false);
            }

            var refCode = _encryptionService.GenerateRefCode();

            Courses model = new Courses();

            model.title            = request.title;
            model.code             = refCode;
            model.description      = request.description;
            model.status           = request.status;
            model.durationTime     = request.durationTime;
            model.durationType     = request.durationType;
            model.passingGrade     = request.passingGrade;
            model.capacity         = request.capacity;
            model.CourseLevel      = request.CourseLevel;
            model.CourseCategory   = request.CourseCategory;
            model.CourseType       = request.CourseType;
            model.CourseLanguage   = request.CourseLanguage;
            model.CourseTag        = request.CourseTag;
            model.CourseInstructor = request.CourseInstructor;
            model.createdAt        = DateTime.Now;

            _context.Courses.Add(model);
            Save();

            if (request.RelatedCourse != null && request.RelatedCourse.Count() > 0)
            {
                foreach (CourseRelatedDetails x in request.RelatedCourse)
                {
                    CourseRelatedDetails cr = new CourseRelatedDetails();
                    cr.courseId       = model.id;
                    cr.isPrerequisite = x.isPrerequisite;
                    _context.CourseRelated.Add(cr);
                    _context.SaveChanges();

                    CourseRelatedList rcl = new CourseRelatedList();
                    rcl.courseRelatedId = cr.id;
                    rcl.courseId        = x.relatedCourseId;
                    _context.CourseRelatedList.Add(rcl);
                    _context.SaveChanges();
                }
            }


            return(true);
        }
Esempio n. 2
0
        public async Task <IActionResult> Store([FromForm] CourseRequest request)
        {
            try
            {
                if (_userRepository.LogCurrentUser().canCreate == false)
                {
                    return(Unauthorized(_UnAuthorized));
                }

                Courses model = new Courses();
                model.title        = request.title;
                model.description  = request.description;
                model.status       = request.status;
                model.isPublished  = request.isPublished;
                model.durationTime = request.durationTime;
                model.durationType = request.durationType;
                model.passingGrade = request.passingGrade;
                model.capacity     = request.capacity;


                RequiredFields validation = new RequiredFields();
                validation.Courses = model;
                object validateFields = _validationService.ValidateRequest("Courses", validation);
                if (JsonConvert.SerializeObject(validateFields).Length > 2)
                {
                    return(BadRequest(validateFields));
                }

                var checkExists = _context.Courses.Where(x => x.title == request.title).Any();
                if (checkExists == true)
                {
                    return(BadRequest(new GenericResult {
                        Response = false, Message = request.title + " is already exists"
                    }));
                }

                var refCode = _encryptionService.GenerateRefCode();
                model.code           = refCode;
                model.CourseLevel    = request.CourseLevel;
                model.CourseCategory = request.CourseCategory;
                model.CourseType     = request.CourseType;
                model.CourseLanguage = request.CourseLanguage;
                model.CourseTag      = request.CourseTag;
                model.createdAt      = DateTime.Now;

                if (request.featureImage != null)
                {
                    if (request.featureImage.Length > 0)
                    {
                        Stream stream = request.featureImage.OpenReadStream();

                        var    path = Path.Combine(_hostingEnvironment.WebRootPath, _fileDirectory.virtualDirectory);
                        string courseImageFolder = String.Format("{0}\\Content\\Images\\Course", path);

                        if (!Directory.Exists(courseImageFolder))
                        {
                            Directory.CreateDirectory(courseImageFolder);
                        }

                        var id        = Guid.NewGuid();
                        var extension = Path.GetExtension(request.featureImage.FileName);
                        var fileName  = id.ToString() + extension.ToString().ToLower();


                        using (var zipStream = new FileStream(Path.Combine(courseImageFolder, fileName), FileMode.Create, FileAccess.Write))
                        {
                            request.featureImage.CopyTo(zipStream);
                        }
                        model.featureImage = fileName;
                    }
                }

                if (request.featureVideo != null)
                {
                    if (request.featureVideo.Length > 0)
                    {
                        Stream stream = request.featureVideo.OpenReadStream();

                        var    path = Path.Combine(_hostingEnvironment.WebRootPath, _fileDirectory.virtualDirectory);
                        string courseVideoFolder = String.Format("{0}\\Content\\Video\\Course", path);

                        if (!Directory.Exists(courseVideoFolder))
                        {
                            Directory.CreateDirectory(courseVideoFolder);
                        }

                        var id        = Guid.NewGuid();
                        var extension = Path.GetExtension(request.featureVideo.FileName);
                        var fileName  = id.ToString() + extension.ToString().ToLower();


                        using (var zipStream = new FileStream(Path.Combine(courseVideoFolder, fileName), FileMode.Create, FileAccess.Write))
                        {
                            request.featureVideo.CopyTo(zipStream);
                        }
                        model.featureVideo = fileName;
                    }
                }

                _context.Courses.Add(model);
                await _context.SaveChangesAsync();

                for (int i = 0; request.RelatedCourse.Count() > i; i++)
                {
                    CourseRelatedDetails crdModel = new CourseRelatedDetails();
                    crdModel.courseId       = model.id;
                    crdModel.isPrerequisite = request.RelatedCourse[i].isPrerequisite;
                    _context.CourseRelated.Add(crdModel);
                    await _context.SaveChangesAsync();

                    CourseRelatedList crlModel = new CourseRelatedList();
                    crlModel.courseRelatedId = crdModel.id;
                    crlModel.courseId        = request.RelatedCourse[i].relatedCourseId;
                    _context.CourseRelatedList.Add(crlModel);
                    await _context.SaveChangesAsync();
                }

                return(Ok(new GenericResult {
                    Response = true, Message = "Course " + request.title + " successfully created"
                }));
            } catch (Exception e) {
                return(BadRequest(e));
            }
        }
Esempio n. 3
0
        public bool DuplicateCourse(long id, string refCode, string title)
        {
            string sqlCourse = String.Format("INSERT INTO [course].[courses] (code, title, description, status, featureImage, featureVideo, isPublished, isVisible, durationTime, durationType, passingGrade, capacity, notifyInstructor, lmsProfile, publishDescription, createdAt, updatedAt) " +
                                             "SELECT CONCAT('','','{1}') code, CONCAT('', '', '{2}') title, description, status, featureImage, featureVideo, isPublished, isVisible, durationTime, durationType, passingGrade, capacity, notifyInstructor, lmsProfile, publishDescription, createdAt, updatedAt FROM [course].[courses] WHERE id = {0}", id, refCode, title);

            _context.Database.ExecuteSqlCommand(sqlCourse);

            var insertedCourse = GetByCode(refCode);


            if (insertedCourse.featureImage != null)
            {
                var    updateFeatureImage = _context.Courses.Where(x => x.id == insertedCourse.id).FirstOrDefault();
                var    path = Path.Combine(_hostingEnvironment.WebRootPath, _fileDirectory.virtualDirectory);
                string courseImageFolder = String.Format("{0}\\Content\\Images\\Course", path);

                if (!Directory.Exists(courseImageFolder))
                {
                    Directory.CreateDirectory(courseImageFolder);
                }

                var fileId    = Guid.NewGuid();
                var extension = Path.GetExtension(insertedCourse.featureImage);
                var fileName  = fileId.ToString() + extension.ToString().ToLower();

                System.IO.File.Copy(Path.Combine(courseImageFolder, insertedCourse.featureImage), Path.Combine(courseImageFolder, fileName), true);

                updateFeatureImage.featureImage = fileName;
                _context.SaveChanges();
            }

            if (insertedCourse.featureVideo != null)
            {
                var    updateFeatureVideo = _context.Courses.Where(x => x.id == insertedCourse.id).FirstOrDefault();
                var    path = Path.Combine(_hostingEnvironment.WebRootPath, _fileDirectory.virtualDirectory);
                string courseVideoFolder = String.Format("{0}\\Content\\Video\\Course", path);

                if (!Directory.Exists(courseVideoFolder))
                {
                    Directory.CreateDirectory(courseVideoFolder);
                }

                var fileIdVideo   = Guid.NewGuid();
                var extension     = Path.GetExtension(insertedCourse.featureVideo);
                var fileNameVideo = fileIdVideo.ToString() + extension.ToString().ToLower();

                System.IO.File.Copy(Path.Combine(courseVideoFolder, insertedCourse.featureVideo), Path.Combine(courseVideoFolder, fileNameVideo), true);

                updateFeatureVideo.featureVideo = fileNameVideo;
                _context.SaveChanges();
            }



            string sqlCourseLevel = String.Format("INSERT INTO [course].[course_level] (courseId, levelId, createdAt, updatedAt) " +
                                                  "SELECT CONCAT('','','{0}') AS courseId, levelId, createdAt, updatedAt FROM [course].[course_level] WHERE courseId = {1}", insertedCourse.id, id);

            _context.Database.ExecuteSqlCommand(sqlCourseLevel);

            string sqlCourseCategory = String.Format("INSERT INTO [course].[course_category] (courseId, categoryId, createdAt, updatedAt) " +
                                                     "SELECT CONCAT('','','{0}') AS courseId, categoryId, createdAt, updatedAt FROM [course].[course_category] WHERE courseId = {1}", insertedCourse.id, id);

            _context.Database.ExecuteSqlCommand(sqlCourseCategory);

            string sqlCourseType = String.Format("INSERT INTO [course].[course_type] (courseId, courseTypeId, createdAt, updatedAt) " +
                                                 "SELECT CONCAT('','','{0}') AS courseId, courseTypeId, createdAt, updatedAt FROM [course].[course_type] WHERE courseId = {1}", insertedCourse.id, id);

            _context.Database.ExecuteSqlCommand(sqlCourseType);

            string sqlCourseLanguage = String.Format("INSERT INTO [course].[course_language] (courseId, languageId, createdAt, updatedAt) " +
                                                     "SELECT CONCAT('','','{0}') AS courseId, languageId, createdAt, updatedAt FROM [course].[course_language] WHERE courseId = {1}", insertedCourse.id, id);

            _context.Database.ExecuteSqlCommand(sqlCourseLanguage);

            string sqlCourseTag = String.Format("INSERT INTO [course].[course_tags] (courseId, tagId, createdAt, updatedAt) " +
                                                "SELECT CONCAT('','','{0}') AS courseId, tagId, createdAt, updatedAt FROM [course].[course_tags] WHERE courseId = {1}", insertedCourse.id, id);

            _context.Database.ExecuteSqlCommand(sqlCourseTag);

            string sqlCourseInstructor = String.Format("INSERT INTO [course].[course_instructor] (courseId, userId, userGroupId, createdAt, updatedAt) " +
                                                       "SELECT CONCAT('','','{0}') AS courseId, userId, userGroupId, createdAt, updatedAt FROM [course].[course_instructor] WHERE courseId = {1}", insertedCourse.id, id);

            _context.Database.ExecuteSqlCommand(sqlCourseInstructor);

            string sqlCoursePrerequisite = String.Format("INSERT INTO [course].[course_prerequisite] (courseId, preRequisiteId, createdAt, updatedAt) " +
                                                         "SELECT CONCAT('','','{0}') AS courseId, preRequisiteId, createdAt, updatedAt FROM [course].[course_prerequisite] WHERE courseId = {1}", insertedCourse.id, id);

            _context.Database.ExecuteSqlCommand(sqlCoursePrerequisite);

            string _sql = String.Format("SELECT a.* FROM [course].[course_related] AS A " +
                                        "LEFT JOIN[course].[course_related_list] as b ON b.courseRelatedId = a.id " +
                                        "WHERE a.courseId = {0}", id);
            var crList = _context.CourseRelated.FromSqlRaw(_sql)
                         .Where(x => x.courseId == id)
                         .Include(x => x.courseRelated)
                         .Select(i => new
            {
                id             = i.id,
                courseId       = i.courseId,
                isPrerequisite = i.isPrerequisite,
                createdAt      = i.createdAt,
                updatedAt      = i.updatedAt,
                courseRelatedListCourseRelatedId = i.courseRelated.courseRelatedId,
                courseRelatedListCourseId        = i.courseRelated.courseId,
            })
                         .ToList();

            for (int x = 0; x < crList.Count(); x++)
            {
                CourseRelatedDetails crdModel = new CourseRelatedDetails();
                crdModel.courseId       = insertedCourse.id;
                crdModel.isPrerequisite = crList[x].isPrerequisite;
                _context.CourseRelated.Add(crdModel);
                _context.SaveChanges();

                CourseRelatedList crlModel = new CourseRelatedList();
                crlModel.courseRelatedId = crdModel.id;
                crlModel.courseId        = crList[x].courseRelatedListCourseId;
                _context.CourseRelatedList.Add(crlModel);
                _context.SaveChanges();
            }

            return(true);
        }