/// <summary>
        /// Updates and index course data
        /// </summary>
        /// <param name="course">Learning course</param>
        /// <returns></returns>
        public LearningCourseDTO Update(LearningCourseDTO course)
        {
            var data = _learningCourseDb.Update(_mapper.Map <LearningCourseDTO, CourseDB>(course));

            //_lucene.IndexCourse(course);
            return(_mapper.Map <CourseDB, LearningCourseDTO>(data));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a single search index entry based on our data, and it will be reused by public methods which we'll add later
        /// </summary>
        /// <param name="courseData"></param>
        /// <param name="writer"></param>
        private void _addToLuceneIndex(LearningCourseDTO courseData, IndexWriter writer)
        {
            // remove older index entry
            if (courseData.Id != null)
            {
                var searchQuery = new TermQuery(new Term("Id", courseData.Id.ToString()));
                writer.DeleteDocuments(searchQuery);

                // add new index entry
                var doc = new Document();

                // add lucene fields mapped to db fields
                doc.Add(new Field("Id", courseData.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                doc.Add(new Field("Description", courseData.Description.ToString(), Field.Store.YES, Field.Index.ANALYZED));
                doc.Add(new Field("Name", courseData.Name.ToString(), Field.Store.YES, Field.Index.ANALYZED));
                doc.Add(new Field("Category", courseData.Category.ToString(), Field.Store.YES, Field.Index.ANALYZED));
                doc.Add(new Field("Cost", courseData.Cost.ToString(), Field.Store.YES, Field.Index.ANALYZED));
                //doc.Add(new Field("Complexity", courseData.Complexity.ToString(), Field.Store.YES, Field.Index.ANALYZED));
                //doc.Add(new Field("Language", courseData.Language.ToString(), Field.Store.YES, Field.Index.ANALYZED));
                doc.Add(new Field("AuthorId", courseData.AuthorId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));

                // add entry to index
                writer.AddDocument(doc);
            }
        }
Ejemplo n.º 3
0
        public IEnumerable <LearningCourseDTO> IndexCourse(LearningCourseDTO course)
        {
            var indexer = new LuceneIndexer();

            indexer.AddUpdateLuceneIndex(course);
            return(indexer.GetAllIndexRecords());
        }
        /// <summary>
        /// Updates and index course data async
        /// </summary>
        /// <param name="course">Learning course</param>
        /// <returns></returns>
        public async Task <Result <LearningCourseDTO> > UpdateAsync(LearningCourseDTO course)
        {
            var courseDb = _mapper.Map <LearningCourseDTO, CourseDB>(course);

            var result = await _learningCourseDb.UpdateAsync(courseDb);

            //_lucene.IndexCourse(course);
            return(result.IsSuccess ? Result <LearningCourseDTO> .Ok(_mapper.Map <LearningCourseDTO>(result.Data))
                : Result <LearningCourseDTO> .Fail <LearningCourseDTO>(result.Message));
        }
        /// <summary>
        /// Creates learning course
        /// </summary>
        /// <param name="course">Learning course</param>
        /// <returns></returns>
        public LearningCourseDTO AddNoIndex(LearningCourseDTO course)
        {
            var mapper = new MapperConfiguration(cfg => {
                cfg.CreateMap <CourseDB, LearningCourseDTO>()
                .ForMember(x => x.AuthorId, opt => opt.MapFrom(c => c.AuthorDBId))
                .ForMember(x => x.Category, opt => opt.MapFrom(c => c.CourseCategoryDBId))
                .ReverseMap()
                .ForPath(x => x.AuthorDBId, opt => opt.MapFrom(c => c.AuthorId))
                .ForPath(x => x.CourseCategoryDBId, opt => opt.MapFrom(c => c.Category));
                cfg.CreateMap <CourseItemDB, LearningCourseItemDTO>().ReverseMap();
            }).CreateMapper();
            var data = _learningCourseDb.Add(mapper.Map <LearningCourseDTO, CourseDB>(course));
            LearningCourseDTO LearningCourse = mapper.Map <CourseDB, LearningCourseDTO>(data);

            return(LearningCourse);
        }
        public async Task <IHttpActionResult> Update([FromBody, CustomizeValidator(RuleSet = "default,UpdateCourse")] LearningCourseDTO course)
        {
            if (course is null || !ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                var result = await _learningCourseService.UpdateAsync(course);

                return(result.IsError ? BadRequest(result.Message) : (IHttpActionResult)Ok(result.Data));
            }
            catch (InvalidOperationException ex)
            {
                return(InternalServerError(ex));
            }
        }
        public async Task <IHttpActionResult> Create([FromBody, CustomizeValidator] LearningCourseDTO course)
        {
            if (course is null || !ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var result = await _learningCourseService.AddAsync(course);

            if (result.IsError)
            {
                return(BadRequest(result.Message));
            }
            else
            {
                await _bus.SendAsync("IndexService", result.Data);

                return((IHttpActionResult)Ok(result.Data));
            }
        }
Ejemplo n.º 8
0
        public void add_course()
        {
            var DbService        = new CourseDbService(mockContext.Object);
            var mockLogicService = new Mock <LearningCourseService>();
            var service          = new LearningCourseService(mapper, DbService);

            LearningCourseDTO lc = new LearningCourseDTO
            {
                Name     = "Course",
                Id       = "33",
                Category = 2,
                Cost     = 10,
                Language = "EN"
            };
            //Act
            var x = service.AddNoIndex(lc);

            //Assert
            mockContext.Verify(m => m.SaveChanges(), Times.Once());
        }
        /// <summary>
        /// Creates learning course async
        /// </summary>
        /// <param name="course">Learning course</param>
        /// <returns></returns>
        public async Task <Result <LearningCourseDTO> > AddAsync(LearningCourseDTO course)
        {
            var mapper = new MapperConfiguration(cfg => {
                cfg.CreateMap <CourseDB, LearningCourseDTO>()
                .ForMember(x => x.AuthorId, opt => opt.MapFrom(c => c.AuthorDBId))
                .ForMember(x => x.Category, opt => opt.MapFrom(c => c.CourseCategoryDBId))
                .ReverseMap()
                .ForPath(x => x.AuthorDBId, opt => opt.MapFrom(c => c.AuthorId))
                .ForPath(x => x.CourseCategoryDBId, opt => opt.MapFrom(c => c.Category));
                cfg.CreateMap <CourseItemDB, LearningCourseItemDTO>().ReverseMap();
            }).CreateMapper();

            var courseDb = mapper.Map <LearningCourseDTO, CourseDB>(course);
            var result   = await _learningCourseDb.AddAsync(courseDb);

            //_lucene.IndexCourse(course);

            return(result.IsSuccess ? Result <LearningCourseDTO> .Ok(mapper.Map <LearningCourseDTO>(result.Data))
                : Result <LearningCourseDTO> .Fail <LearningCourseDTO>(result.Message));
        }
Ejemplo n.º 10
0
        public void update_course()
        {
            var DbService        = new CourseDbService(mockContext.Object);
            var mockLogicService = new Mock <LearningCourseService>();
            var service          = new LearningCourseService(mapper, DbService);

            LearningCourseDTO lc = new LearningCourseDTO
            {
                Name     = "Course",
                Id       = "123",
                Category = 2,
                Cost     = 10,
                Language = "EN"
            };
            //Act
            var x = service.UpdateNoIndex(lc);

            //Assert
            Assert.AreEqual(queries.First().Id, lc.Id);
            Assert.AreEqual(queries.Count(), queries.Count());
        }
Ejemplo n.º 11
0
        public IHttpActionResult IndexCourse([FromBody] LearningCourseDTO course)
        {
            var result = _searchService.IndexCourse(course);

            return(result == null?NotFound() : (IHttpActionResult)Ok(result));
        }
Ejemplo n.º 12
0
 /// <summary>
 /// method that will add a single record to search index
 /// </summary>
 /// <param name="sampleData"></param>
 public void AddUpdateLuceneIndex(LearningCourseDTO sampleData)
 {
     AddUpdateLuceneIndex(new List <LearningCourseDTO> {
         sampleData
     });
 }