Ejemplo n.º 1
0
        public async Task <Pagination <VehicleAnnounceForUserDto> > GetVehicleAnnounceByUserIdAsync(VehicleAnnounceParams 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 VehicleAnnounceByUserIdSpecification(queryParams, userId);
            var vehicleAnnounces = await vehicleAnnounceDal.ListEntityWithSpecAsync(spec);

            var countSpec = new VehicleAnnounceByUserIdSpecification(userId);
            var totalItem = await vehicleAnnounceDal.CountAsync(countSpec);

            var data = mapper.Map <List <VehicleAnnounce>, List <VehicleAnnounceForUserDto> >(vehicleAnnounces);

            return(new Pagination <VehicleAnnounceForUserDto>
                   (
                       queryParams.PageIndex,
                       queryParams.PageSize,
                       totalItem,
                       data
                   ));
        }
        public async Task <VehicleAnnounceForUserDto> UpdateForPublicAsync(VehicleAnnounceForCreationDto 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 vehicleAnnounceDal.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 = "Car";
            mapForUpdate.IsNew        = true;
            mapForUpdate.IsPublish    = false;
            mapForUpdate.Reject       = false;
            await vehicleAnnounceDal.Update(mapForUpdate);

            var spec        = new VehicleAnnounceByUserIdSpecification(userId, checkFromRepo.Id);
            var getWithUser = await vehicleAnnounceDal.GetEntityWithSpecAsync(spec);

            return(mapper.Map <VehicleAnnounce, VehicleAnnounceForUserDto>(getWithUser));
        }
        public async Task <VehicleAnnounceForUserDto> CreateForPublicAsync(VehicleAnnounceForCreationDto 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 vehicleAnnounceDal.GetAsync(x => x.Header.ToLower() == creationDto.Header.ToLower());

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

            var mapForCreate = mapper.Map <VehicleAnnounce>(creationDto);
            var slideId      = System.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.SlideId           = slideId;
            mapForCreate.Created           = DateTime.Now;
            mapForCreate.AnnounceType      = "Car";

            var createHomeAnnounce = await vehicleAnnounceDal.Add(mapForCreate);

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

            var getAnnounceFromRepo = await vehicleAnnounceDal.GetEntityWithSpecAsync(spec);

            return(mapper.Map <VehicleAnnounce, VehicleAnnounceForUserDto>(getAnnounceFromRepo));
        }