public AdvertisementViewModel GetById(int id)
        {
            var adv           = _advertisementRepository.FindById(id);
            var advertisement = Mapper.Map <Advertisement, AdvertisementViewModel>(adv);

            return(advertisement);
        }
示例#2
0
        public async Task <Get.Response> Get(Get.Request request, CancellationToken cancellationToken)
        {
            var ad = await _repository.FindById(request.Id, cancellationToken);

            if (ad == null)
            {
                throw new AdNotFoundException(request.Id);
            }

            if (ad.Status == Domain.Advertisement.Statuses.Closed)
            {
                throw new AdNotFoundException(request.Id);
            }

            return(new Get.Response
            {
                Title = ad.Title,
                Description = ad.Description,
                Status = ad.Status.ToString(),
                Price = ad.Price,
                Cover = ad.Cover,
                CreatedDate = ad.CreatedDate,
                Location = ad.Location,
                GeoLat = ad.GeoLat,
                GeoLon = ad.GeoLon,
                Images = ad.Images.Select(i => new Get.Response.ImageResponse
                {
                    Id = i.Id,
                    ImageUrl = S3Url + i.Name
                }),

                Category = new Get.Response.CategoryResponse
                {
                    Id = ad.Category.Id,
                    Name = ad.Category.Name
                },

                Owner = new Get.Response.OwnerResponse
                {
                    Id = ad.OwnerUser.Id,
                    Username = ad.OwnerUser.Username,
                    Name = ad.OwnerUser.Name,
                    Phone = ad.OwnerUser.Phone,
                    Lastname = ad.OwnerUser.LastName,
                    Avatar = ad.OwnerUser.Avatar
                }
            });
        }
示例#3
0
        public async Task CreateChat(Save.Request request, CancellationToken cancellationToken)
        {
            var advertisement = await _advertisementRepository.FindById(request.AdvertisementId, cancellationToken);

            if (advertisement == null)
            {
                throw new AdNotFoundException(request.AdvertisementId);
            }

            var userId = await _identityService.GetCurrentUserId(cancellationToken);

            var chatDuplicate = await _chatRepository.FindWhere(c => c.Advertisement.Id == advertisement.Id && c.BuyerId == userId, cancellationToken);

            if (chatDuplicate != null)
            {
                throw new ChatDuplicateException($"Чат с id {chatDuplicate.Id} уже создан");
            }

            if (userId == advertisement.OwnerId)
            {
                throw new ChatException($"Попытка создать чат с самим собой, пользователь с id {userId}, является владельцем объявления");
            }

            var chat = new Domain.Chat
            {
                Advertisement = advertisement,
                BuyerId       = userId,
                CreatedDate   = DateTime.UtcNow,
            };

            await _chatRepository.Save(chat, cancellationToken);
        }
示例#4
0
        public async Task <CreateFavorite.Response> AddToFavorite(CreateFavorite.Request request, CancellationToken cancellationToken)
        {
            var userId = await _identityService.GetCurrentUserId(cancellationToken);

            if (string.IsNullOrEmpty(userId))
            {
                throw new UserNotFoundException("Пользователь не найден");
            }

            var advertisement = await _advertisementRepository.FindById(request.AdvertisementId, cancellationToken);

            if (advertisement == null)
            {
                throw new AdvertisementNotFoundException($"Объявление с Id = {request.AdvertisementId} не было найдено");
            }

            var advertisementInFavoriteCurrentUser = await _repository.FindWhere(a => a.UserId == userId && a.AdvertisementId == request.AdvertisementId, cancellationToken);

            if (advertisementInFavoriteCurrentUser != null)
            {
                throw new DuplicateFavoriteException($"Объявление с id = {request.AdvertisementId} уже содержится в избранных пользователя");
            }

            var favorite = new Domain.Favorite
            {
                AdvertisementId = advertisement.Id,
                UserId          = userId,
                CreatedDate     = DateTime.UtcNow
            };

            await _repository.Save(favorite, cancellationToken);

            return(new CreateFavorite.Response
            {
                Id = favorite.Id
            });
        }