Example #1
0
        public async Task <Pagination <AnnounceForUserDto> > GetAnnounceByUserIdAsync(AnnounceParams 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 AnnounceByUserIdSpecification(queryParams, userId);
            var announces = await announceDal.ListEntityWithSpecAsync(spec);

            var countSpec = new AnnounceByUserIdSpecification(userId);
            var totalItem = await announceDal.CountAsync(countSpec);

            var data = mapper.Map <List <Announce>, List <AnnounceForUserDto> >(announces);

            return(new Pagination <AnnounceForUserDto>
                   (
                       queryParams.PageIndex,
                       queryParams.PageSize,
                       totalItem,
                       data
                   ));
        }
Example #2
0
        public async Task <AnnounceForUserDto> UpdateForPublicAsync(AnnounceForCreationDto updateDto, 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 announceDal.GetAsync(x => x.Id == updateDto.Id);

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

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

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

            var spec = new AnnounceByUserIdSpecification(userId, checkFromRepo.Id);
            var getAnnounceWithUserFromRepo = await announceDal.GetEntityWithSpecAsync(spec);

            return(mapper.Map <Announce, AnnounceForUserDto>(getAnnounceWithUserFromRepo));
        }
Example #3
0
        public async Task <AnnounceForUserDto> CreateForPublicAsync(AnnounceForCreationDto 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 announceDal.GetAsync(x => x.Header.ToLower() == creationDto.Header.ToLower());

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

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

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

            var createAnnounce = await announceDal.Add(mapForCreate);

            var spec = new AnnounceByUserIdSpecification(userId, createAnnounce.Id);

            var getAnnounceFromRepo = await announceDal.GetEntityWithSpecAsync(spec);

            return(mapper.Map <Announce, AnnounceForUserDto>(getAnnounceFromRepo));
        }