Compare commits
3 Commits
cdd31fd6b7
...
initWishli
| Author | SHA1 | Date | |
|---|---|---|---|
| 3d205df038 | |||
|
|
5ccfc6ba2c | ||
|
|
f0e3a129d0 |
@@ -48,7 +48,6 @@ public class NoticeController {
|
||||
if (dto.getCategory() == null) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new NoticeAdditionDTO("Nie ma takiej kategorii"));
|
||||
}
|
||||
|
||||
dto.setPublishDate(java.time.LocalDateTime.now());
|
||||
|
||||
Long newNoticeId = noticeService.addNotice(dto);
|
||||
@@ -56,7 +55,6 @@ public class NoticeController {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(new NoticeAdditionDTO(newNoticeId ,"Dodano ogłoszenie."));
|
||||
}
|
||||
|
||||
|
||||
// TODO: zamiast dodawać tutaj pętlą, musi to robić NoticeService, trzeba zaimplementować odpowienią metodę
|
||||
@PostMapping("/bulk_add")
|
||||
public ResponseEntity<String> addNotices(@RequestBody List<NoticeDTO> notices_list) {
|
||||
|
||||
@@ -3,7 +3,6 @@ package _11.asktpk.artisanconnectbackend.controller;
|
||||
import _11.asktpk.artisanconnectbackend.dto.NoticeDTO;
|
||||
import _11.asktpk.artisanconnectbackend.dto.RequestResponseDTO;
|
||||
import _11.asktpk.artisanconnectbackend.dto.WishlistDTO;
|
||||
import _11.asktpk.artisanconnectbackend.entities.Wishlist;
|
||||
import _11.asktpk.artisanconnectbackend.service.ClientService;
|
||||
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
||||
import _11.asktpk.artisanconnectbackend.service.WishlistService;
|
||||
@@ -15,7 +14,6 @@ import java.util.List;
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/wishlist")
|
||||
public class WishlistController {
|
||||
|
||||
private final WishlistService wishlistService;
|
||||
private final ClientService clientService;
|
||||
private final NoticeService noticeService;
|
||||
@@ -30,7 +28,6 @@ public class WishlistController {
|
||||
public ResponseEntity<RequestResponseDTO> toggleWishlist(@RequestBody WishlistDTO wishlistDTO) {
|
||||
Long noticeId = wishlistDTO.getNoticeId();
|
||||
Long clientId = wishlistDTO.getClientId();
|
||||
|
||||
NoticeDTO noticeDTO = noticeService.getNoticeById(noticeId);
|
||||
if (noticeDTO == null) {
|
||||
return ResponseEntity.badRequest().body(new RequestResponseDTO("Notice not found"));
|
||||
@@ -47,21 +44,16 @@ public class WishlistController {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@GetMapping("/{clientId}")
|
||||
public ResponseEntity<List<WishlistDTO>> getWishlist(@PathVariable Long clientId) {
|
||||
List<WishlistDTO> wishlist = wishlistService.getWishlistForClientId(clientId);
|
||||
return ResponseEntity.ok(wishlist);
|
||||
}
|
||||
//
|
||||
// @GetMapping("/get/all")
|
||||
// public List<NoticeDTO> getAllNotices() {
|
||||
// return noticeService.getAllNotices();
|
||||
// @GetMapping("/{clientId}")
|
||||
// public ResponseEntity<List<WishlistDTO>> getWishlist(@PathVariable Long clientId) {
|
||||
// List<WishlistDTO> wishlist = wishlistService.getWishlistForClientId(clientId);
|
||||
// return ResponseEntity.ok(wishlist);
|
||||
// }
|
||||
@GetMapping("/")
|
||||
public List<NoticeDTO> getWishlistForClient() {
|
||||
Long clientId =1L;
|
||||
return wishlistService.getNoticesInWishlist(clientId);
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public List<NoticeDTO> getWishlistForClient() {
|
||||
// TODO: Replace with actual client ID from authentication context
|
||||
Long clientId = 1L;
|
||||
return wishlistService.getNoticesInWishlist(clientId);
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,4 @@ public interface WishlistRepository extends JpaRepository<Wishlist, Long> {
|
||||
List<Wishlist> findAllByClientId(Long clientId);
|
||||
|
||||
Optional<Wishlist> findByClientAndNotice(Client client, Notice notice);
|
||||
|
||||
Boolean existsByClientAndNotice(Client client, Notice notice);
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import _11.asktpk.artisanconnectbackend.entities.Notice;
|
||||
import _11.asktpk.artisanconnectbackend.repository.ClientRepository;
|
||||
import _11.asktpk.artisanconnectbackend.repository.NoticeRepository;
|
||||
import _11.asktpk.artisanconnectbackend.dto.NoticeDTO;
|
||||
import _11.asktpk.artisanconnectbackend.repository.WishlistRepository;
|
||||
//import _11.asktpk.artisanconnectbackend.service.WishlistService;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -18,12 +18,12 @@ public class NoticeService {
|
||||
|
||||
private final NoticeRepository noticeRepository;
|
||||
private final ClientRepository clientRepository;
|
||||
private final WishlistRepository wishlistRepository;
|
||||
private final WishlistService wishlistService;
|
||||
|
||||
public NoticeService(NoticeRepository noticeRepository, ClientRepository clientRepository,WishlistRepository wishlistRepository) {
|
||||
public NoticeService(NoticeRepository noticeRepository, ClientRepository clientRepository, WishlistService wishlistService) {
|
||||
this.noticeRepository = noticeRepository;
|
||||
this.clientRepository = clientRepository;
|
||||
this.wishlistRepository = wishlistRepository;//serwis zamiast repository
|
||||
this.wishlistService = wishlistService;
|
||||
}
|
||||
|
||||
public Notice fromDTO(NoticeDTO dto) {
|
||||
@@ -45,11 +45,12 @@ public class NoticeService {
|
||||
|
||||
private NoticeDTO toDTO(Notice notice) {
|
||||
NoticeDTO dto = new NoticeDTO();
|
||||
// TODO: To be updated using AuthService after implementing authentication.
|
||||
Optional<Client> client = clientRepository.findById(1L);
|
||||
boolean isWishlisted = false;
|
||||
if (client.isPresent()) {
|
||||
Client c = client.get();
|
||||
isWishlisted = wishlistRepository.existsByClientAndNotice(c,notice);
|
||||
isWishlisted = wishlistService.isWishlisted(c, notice);
|
||||
}
|
||||
dto.setNoticeId(notice.getIdNotice());
|
||||
dto.setTitle(notice.getTitle());
|
||||
@@ -77,10 +78,10 @@ public class NoticeService {
|
||||
.orElseThrow(() -> new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + id));
|
||||
return toDTO(notice);
|
||||
}
|
||||
|
||||
public Notice getNoticeByIdEntity(Long id) {
|
||||
Notice notice = noticeRepository.findById(id)
|
||||
return noticeRepository.findById(id)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + id));
|
||||
return notice;
|
||||
}
|
||||
|
||||
public Long addNotice(NoticeDTO dto) {
|
||||
|
||||
@@ -2,25 +2,26 @@ package _11.asktpk.artisanconnectbackend.service;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.dto.WishlistDTO;
|
||||
import _11.asktpk.artisanconnectbackend.dto.NoticeDTO;
|
||||
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
||||
import _11.asktpk.artisanconnectbackend.entities.Client;
|
||||
import _11.asktpk.artisanconnectbackend.entities.Notice;
|
||||
import _11.asktpk.artisanconnectbackend.entities.Wishlist;
|
||||
import _11.asktpk.artisanconnectbackend.repository.WishlistRepository;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class WishlistService {
|
||||
|
||||
private final WishlistRepository wishlistRepository;
|
||||
private final NoticeService noticeService;
|
||||
|
||||
public WishlistService(WishlistRepository wishlistRepository, NoticeService noticeService) {
|
||||
public WishlistService(WishlistRepository wishlistRepository, @Lazy NoticeService noticeService) {
|
||||
this.wishlistRepository = wishlistRepository;
|
||||
this.noticeService = noticeService;//tak robimy
|
||||
this.noticeService = noticeService;
|
||||
}
|
||||
|
||||
public List<WishlistDTO> getWishlistForClientId(Long clientId) {
|
||||
@@ -33,11 +34,7 @@ public class WishlistService {
|
||||
public boolean isWishlisted(Client client, Notice notice) {
|
||||
Optional<Wishlist> existingEntry = wishlistRepository.findByClientAndNotice(client, notice);
|
||||
|
||||
if (existingEntry.isPresent()) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
return existingEntry.isEmpty();
|
||||
}
|
||||
|
||||
public boolean toggleWishlist(Client client, Notice notice) {
|
||||
|
||||
Reference in New Issue
Block a user