add boostNotice function

This commit is contained in:
Patryk
2025-05-15 20:29:37 +02:00
parent 3d205df038
commit 1d55f40753
3 changed files with 31 additions and 0 deletions

View File

@@ -106,4 +106,15 @@ public class NoticeController {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@PostMapping("/boost/{id}")
public ResponseEntity boostNotice(@PathVariable("id") long id) {
long clientId = 1L;
if (!noticeService.isNoticeOwnedByClient(id, clientId)) {
throw new EntityNotFoundException("Ogłoszenie nie istnieje lub nie należy do zalogowanego klienta.");
}
noticeService.boostNotice(id);
return ResponseEntity.ok("Ogłoszenie zostało pomyślnie wypromowane.");
}
}

View File

@@ -5,4 +5,7 @@ import _11.asktpk.artisanconnectbackend.entities.Notice;
import org.springframework.data.jpa.repository.JpaRepository;
public interface NoticeRepository extends JpaRepository<Notice, Long> {
boolean existsByIdNoticeAndClientId(long noticeId, long clientId);
}

View File

@@ -9,6 +9,7 @@ import _11.asktpk.artisanconnectbackend.dto.NoticeDTO;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@@ -119,4 +120,20 @@ public class NoticeService {
throw new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + id);
}
}
public boolean isNoticeOwnedByClient(long noticeId, long clientId) {
return noticeRepository.existsByIdNoticeAndClientId(noticeId, clientId);
}
public void boostNotice(long noticeId) {
Notice notice = noticeRepository.findById(noticeId)
.orElseThrow(() -> new EntityNotFoundException("Ogłoszenie o ID " + noticeId + " nie istnieje."));
notice.setPublishDate(LocalDateTime.now());
noticeRepository.save(notice);
}
}