/
drobash
/
AdBoard_service
Обзор
Документация
Войти
/
drobash
/
AdBoard_service
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/org/example/service/AdvertisementService.java
124 строки
5 KB
dushesp
Доделаны остальные страницы во фронтенде
16 апр 2025, 00:37
16 апр 2025, 00:37
f4dee66
Код
Авторство
О чём код?
package org.example.service; import lombok.extern.slf4j.Slf4j; import org.example.dto.request.AdvertisementRequestDto; import org.example.dto.response.AdvertisementResponseDto; import org.example.exception.AdvertisementNotFoundException; import org.example.mapper.AdvertisementMapper; import org.example.model.Advertisement; import org.example.model.AdvertisementStatus; import org.example.repository.AdvertisementRepository; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.util.List; @Service @RequiredArgsConstructor @Slf4j public class AdvertisementService { private final AdvertisementRepository repository; private final AdvertisementMapper mapper; @Transactional public AdvertisementResponseDto create(AdvertisementRequestDto dto) { log.info("Creating advertisement from DTO: {}", dto); Advertisement ad = mapper.toEntity(dto); ad.setStatus(AdvertisementStatus.DRAFT); log.info("Mapped entity before save: {}", ad); Advertisement savedAd = repository.save(ad); log.info("Saved entity: {}", savedAd); return mapper.toDto(savedAd); } @Transactional public AdvertisementResponseDto edit(Long id, AdvertisementRequestDto dto) { log.info("Editing advertisement ID: {} with data: {}", id, dto); Advertisement existing = findByIdOrThrow(id); // Обновляем только разрешенные поля existing.setTitle(dto.getTitle()); existing.setDescription(dto.getDescription()); existing.setPrice(dto.getPrice()); existing.setCategory(dto.getCategory()); Advertisement updatedAd = repository.save(existing); return mapper.toDto(updatedAd); } @Transactional public void deleteAd(Long id) { log.info("Deleting advertisement ID: {}", id); if (!repository.existsById(id)) { throw new AdvertisementNotFoundException(id); } repository.deleteById(id); } @Transactional public AdvertisementResponseDto publish(Long id) { log.info("Publishing advertisement ID: {}", id); Advertisement ad = findByIdOrThrow(id); ad.setStatus(AdvertisementStatus.PUBLISHED); ad.setPublishedAt(LocalDateTime.now()); Advertisement publishedAd = repository.save(ad); return mapper.toDto(publishedAd); } @Transactional public AdvertisementResponseDto archiveAd(Long id) { log.info("Archiving advertisement ID: {}", id); Advertisement ad = findByIdOrThrow(id); ad.setStatus(AdvertisementStatus.ARCHIVED); Advertisement archivedAd = repository.save(ad); return mapper.toDto(archivedAd); } @Transactional(readOnly = true) public AdvertisementResponseDto getAdvertisementById(Long id) { log.info("Fetching advertisement by ID: {}", id); return mapper.toDto(findByIdOrThrow(id)); } @Transactional(readOnly = true) public Page<AdvertisementResponseDto> getPublishedAdvertisementSt(Pageable pageable) { log.info("Fetching published advertisements"); return repository.findByStatus(AdvertisementStatus.PUBLISHED, pageable) .map(mapper::toDto); } @Transactional(readOnly = true) public List<AdvertisementResponseDto> getAdvertisementBySellerId(Long sellerId) { log.info("Fetching advertisements for seller ID: {}", sellerId); return repository.findBySellerId(sellerId).stream() .map(mapper::toDto) .toList(); } @Transactional(readOnly = true) public Page<AdvertisementResponseDto> searchAdvertisements(String keyword, Pageable pageable) { log.info("Searching advertisements with keyword: {}", keyword); return repository.findByTitleContainingIgnoreCaseOrDescriptionContainingIgnoreCase(keyword, keyword, pageable) .map(mapper::toDto); } // Existing methods @Transactional(readOnly = true) public List<AdvertisementResponseDto> getAll() { log.debug("Fetching all advertisements"); return repository.findAll().stream() .map(mapper::toDto) .toList(); } private Advertisement findByIdOrThrow(Long id) { return repository.findById(id) .orElseThrow(() -> new AdvertisementNotFoundException(id)); } }