add boostNotice function
This commit is contained in:
@@ -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.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user