/// <summary>
        /// Shows all courses by search request and search variant from the database.
        /// </summary>
        /// <param name="searchRequest"></param>
        /// <param name="variant"></param>
        /// <returns></returns>
        public async Task <IEnumerable <CourseInfo> > GetSearchCourses(string searchRequest, SearchVariant variant)
        {
            IEnumerable <CourseInfo> coursesInfo = null;

            if (variant == SearchVariant.ByName)
            {
                try
                {
                    var allCourses = await _courseRepository.GetAllAsync();

                    var courses = allCourses.Where(c => c.Name.Contains(searchRequest));
                    coursesInfo = _mapper.Map <IEnumerable <CourseDb>, IEnumerable <CourseInfo> >(courses);
                }
                catch (KeyNotFoundException)
                {
                    coursesInfo = null;
                }
            }
            else if (variant == SearchVariant.ByTag)
            {
                try
                {
                    var tag = _tegRepository.GetAll().FirstOrDefault(c => c.Content.Equals(searchRequest));
                    if (tag == null)
                    {
                        tag.TagId   = Guid.NewGuid().ToString();
                        tag.Content = searchRequest;
                    }

                    var courses = await _tegRepository.GetCoursesAsync(tag);

                    coursesInfo = _mapper.Map <IEnumerable <CourseDb>, IEnumerable <CourseInfo> >(courses);
                }
                catch (KeyNotFoundException)
                {
                    coursesInfo = null;
                }
            }
            else if (variant == SearchVariant.ByAuthor)
            {
                try
                {
                    var author = _authorRepository.GetAllAsync().GetAwaiter().GetResult().FirstOrDefault(c => c.Lastname.Equals(searchRequest));

                    var courses = await _courseRepository.GetByAuthorAsync(author);

                    coursesInfo = _mapper.Map <IEnumerable <CourseDb>, IEnumerable <CourseInfo> >(courses);
                }
                catch (KeyNotFoundException)
                {
                    coursesInfo = null;
                }
            }

            return(coursesInfo);
        }
Exemple #2
0
 protected string SearchVariantValue(SearchVariant searchVariant)
 {
     return((searchVariant == SearchVariant.None) ? "" : JsonConvert.SerializeObject(searchVariant).Trim('\"'));
 }