Example #1
0
        public async Task <Pagination <HomeAnnounceForUserDto> > GetHomeAnnounceByUserIdAsync(HomeAnnounceParams queryParams, int userId)
        {
            var claimId = int.Parse(httpContextAccessor.HttpContext.User?.Claims?.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value);

            if (claimId != userId)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { AlreadyExist = Messages.OperationDenied });
            }

            var userFromRepo = await userDal.GetAsync(x => x.Id == userId);

            if (userFromRepo == null)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { AlreadyExist = Messages.UserNotFound });
            }

            var spec          = new HomeAnnounceByUserIdSpecification(queryParams, userId);
            var homeAnnounces = await homeAnnounceDal.ListEntityWithSpecAsync(spec);

            var countSpec = new HomeAnnounceByUserIdSpecification(userId);
            var totalItem = await homeAnnounceDal.CountAsync(countSpec);

            var data = mapper.Map <List <HomeAnnounce>, List <HomeAnnounceForUserDto> >(homeAnnounces);

            return(new Pagination <HomeAnnounceForUserDto>
                   (
                       queryParams.PageIndex,
                       queryParams.PageSize,
                       totalItem,
                       data
                   ));
        }
        //[SecuredOperation("Sudo,HomeAnnounces.List,HomeAnnounces.All", Priority = 1)]
        public async Task <Pagination <HomeAnnounceForReturnDto> > GetListAsync(HomeAnnounceParams queryParams)
        {
            var spec         = new HomeAnnounceWithPhotoAndUserSpecification(queryParams);
            var listFromRepo = await homeAnnounceDal.ListEntityWithSpecAsync(spec);

            var countSpec = new HomeAnnounceWithFilterForCountSpecification(queryParams);
            var totalItem = await homeAnnounceDal.CountAsync(countSpec);



            var data = mapper.Map <List <HomeAnnounce>, List <HomeAnnounceForReturnDto> >(listFromRepo);

            return(new Pagination <HomeAnnounceForReturnDto>
                   (
                       queryParams.PageIndex,
                       queryParams.PageSize,
                       totalItem,
                       data
                   ));
        }