예제 #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
                   ));
        }
        public async Task <HomeAnnounceForUserDto> UpdateForPublicAsync(HomeAnnounceForCreationDto creationDto, 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 checkFromRepo = await homeAnnounceDal.GetAsync(x => x.Id == creationDto.Id);

            if (checkFromRepo == null)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { NotFound = Messages.NotFound });
            }

            var mapForUpdate = mapper.Map(creationDto, checkFromRepo);

            mapForUpdate.Updated      = DateTime.Now;
            mapForUpdate.IsNew        = true;
            mapForUpdate.AnnounceType = "home";
            mapForUpdate.IsPublish    = false;
            mapForUpdate.Reject       = false;
            await homeAnnounceDal.Update(mapForUpdate);

            var spec = new HomeAnnounceByUserIdSpecification(userId, creationDto.Id);
            var getAnnounceWithUserFromRepo = await homeAnnounceDal.GetEntityWithSpecAsync(spec);

            return(mapper.Map <HomeAnnounce, HomeAnnounceForUserDto>(getAnnounceWithUserFromRepo));
        }
        public async Task <HomeAnnounceForUserDto> CreateForPublicAsync(HomeAnnounceForCreationDto creationDto, 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 checkByNameFromRepo = await homeAnnounceDal.GetAsync(x => x.Header.ToLower() == creationDto.Header.ToLower());

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

            var mapForCreate = mapper.Map <HomeAnnounce>(creationDto);
            var slideId      = Guid.NewGuid();

            mapForCreate.UserId            = claimId;
            mapForCreate.IsNew             = true;
            mapForCreate.IsPublish         = false;
            mapForCreate.Reject            = false;
            mapForCreate.SlideIntervalTime = 8;
            mapForCreate.PublishFinishDate = DateTime.Now;
            mapForCreate.PublishStartDate  = DateTime.Now;
            mapForCreate.AnnounceType      = "home";
            mapForCreate.SlideId           = slideId;
            mapForCreate.Created           = DateTime.Now;

            var createHomeAnnounce = await homeAnnounceDal.Add(mapForCreate);

            var spec = new HomeAnnounceByUserIdSpecification(userId, createHomeAnnounce.Id);

            var getAnnounceFromRepo = await homeAnnounceDal.GetEntityWithSpecAsync(spec);

            return(mapper.Map <HomeAnnounce, HomeAnnounceForUserDto>(getAnnounceFromRepo));
        }