Example #1
0
        public List <CodeListVM> GetCodes(CodeListDTO codeListDTO)
        {
            var currentUser = _userService.GetLoggedInUser();

            Expression <Func <Code, bool> > where = x => x.isDeleted == false && x.CreatedBy == currentUser.Id && (
                string.IsNullOrEmpty(codeListDTO.Search) || x.Title.ToLower().Contains(codeListDTO.Search.ToLower())
                );

            var programmingLanguages = _lookUpService.GetCodes(type: "programmingLanguage", limit: -1);
            var skipData             = (codeListDTO.Page - 1) * codeListDTO.Limit;
            var codes = _context.Codes.Where(where).Skip(skipData).Take(codeListDTO.Limit).ToList();
            // var userUsedHashTags = codes.Select(x=>x.Hashtags).Distinct();
            var result = (from code in codes
                          join programminLanguage in programmingLanguages
                          on code.LookupId equals programminLanguage.Id


                          select new CodeListVM()
            {
                Id = code.Id,
                CreatedDate = code.CreatedDate,
                UpdatedDate = code.UpdatedDate,
                Description = code.Description,
                Title = code.Title,
                Code = code.Body,
                ProgrammingLanguage = programminLanguage.Name,
                HashTags = this.getHashTagsByName(code.Hashtags)
            }

                          ).ToList();

            return(result);
        }
        public IActionResult GetCodes([FromQuery] CodeListDTO codeListDTO)
        {
            try
            {
                var result = _codeService.GetCodes(codeListDTO);
                return(Ok(result));
            }
            catch (AuthenticationException)
            {
                return(Forbid());
            }
            catch (ServiceException ex)
            {
                return(BadRequest(ex.Message));
            }

            catch (Exception ex)
            {
                return(BadRequest(ex.StackTrace));
            }
        }