Commit d38482db authored by Ali's avatar Ali

add some endpoints to notification api

parent 845ffe2e
...@@ -47,6 +47,16 @@ public class NotificationController { ...@@ -47,6 +47,16 @@ public class NotificationController {
boolean isExist = notificationService.existById(notificationId); boolean isExist = notificationService.existById(notificationId);
return ResponseEntity.ok(isExist); return ResponseEntity.ok(isExist);
} }
@GetMapping("getUnReadNotificationNumForJobseeker/{id}")
public ResponseEntity<Integer> getUnReadNotificationNumForJobseeker(@PathVariable("id") int jobseekerId) {
int num = notificationService.getUnReadNotificationsNumberByJobseekerId(jobseekerId);
return ResponseEntity.ok(num);
}
@GetMapping("getUnReadNotificationNumForCompany/{id}")
public ResponseEntity<Integer> getUnReadNotificationNumForCompany(@PathVariable("id") int companyId) {
int num = notificationService.getUnReadNotificationsNumberByCompanyId(companyId);
return ResponseEntity.ok(num);
}
@PostMapping("create") @PostMapping("create")
public ResponseEntity<RequestStatus> createNotification(@RequestBody CreateNotificationDto createNotificationDto) { public ResponseEntity<RequestStatus> createNotification(@RequestBody CreateNotificationDto createNotificationDto) {
......
...@@ -10,9 +10,14 @@ import java.util.List; ...@@ -10,9 +10,14 @@ import java.util.List;
@Repository @Repository
public interface INotificationRepository extends JpaRepository<Notification, Integer> { public interface INotificationRepository extends JpaRepository<Notification, Integer> {
@Query("SELECT i FROM Notification i WHERE i.companyId = :companyId") @Query("SELECT i FROM Notification i WHERE i.companyId = :companyId AND i.toCompany = true")
List<Notification> findNotificationsByCompanyId(@Param("companyId") int companyId); List<Notification> findNotificationsByCompanyId(@Param("companyId") int companyId);
@Query("SELECT i FROM Notification i WHERE i.jobseekerId = :jobseekerId") @Query("SELECT i FROM Notification i WHERE i.jobseekerId = :jobseekerId AND i.toCompany = false")
List<Notification> findNotificationsByJobseekerId(@Param("jobseekerId") int jobseekerId); List<Notification> findNotificationsByJobseekerId(@Param("jobseekerId") int jobseekerId);
@Query("SELECT i FROM Notification i WHERE i.jobseekerId = :jobseekerId AND i.isRead = false AND i.toCompany = false")
List<Notification> findUnreadNotificationsByJobseekerId(@Param("jobseekerId") int jobseekerId);
@Query("SELECT i FROM Notification i WHERE i.companyId = :companyId AND i.isRead = false AND i.toCompany = true")
List<Notification> findUnreadNotificationsByCompanyId(@Param("companyId") int companyId);
} }
...@@ -15,4 +15,6 @@ public interface INotificationService { ...@@ -15,4 +15,6 @@ public interface INotificationService {
public RequestStatus update(NotificationDto notificationDto); public RequestStatus update(NotificationDto notificationDto);
public RequestStatus delete(int notificationId); public RequestStatus delete(int notificationId);
public boolean existById(int notificationId); public boolean existById(int notificationId);
public int getUnReadNotificationsNumberByJobseekerId(int jobseekerId);
public int getUnReadNotificationsNumberByCompanyId(int companyId);
} }
...@@ -46,9 +46,11 @@ public class NotificationService implements INotificationService { ...@@ -46,9 +46,11 @@ public class NotificationService implements INotificationService {
List<Notification> notifications = iNotificationRepository.findNotificationsByJobseekerId(jobseekerId); List<Notification> notifications = iNotificationRepository.findNotificationsByJobseekerId(jobseekerId);
List<NotificationDto> notificationDtos = new ArrayList<>(); List<NotificationDto> notificationDtos = new ArrayList<>();
for(Notification notification : notifications){ for(Notification notification : notifications){
notification.setRead(true);
NotificationDto notificationDto = modelMapper.map(notification, NotificationDto.class); NotificationDto notificationDto = modelMapper.map(notification, NotificationDto.class);
notificationDtos.add(notificationDto); notificationDtos.add(notificationDto);
} }
iNotificationRepository.saveAll(notifications);
return notificationDtos; return notificationDtos;
} }
...@@ -57,9 +59,11 @@ public class NotificationService implements INotificationService { ...@@ -57,9 +59,11 @@ public class NotificationService implements INotificationService {
List<Notification> notifications = iNotificationRepository.findNotificationsByCompanyId(companyId); List<Notification> notifications = iNotificationRepository.findNotificationsByCompanyId(companyId);
List<NotificationDto> notificationDtos = new ArrayList<>(); List<NotificationDto> notificationDtos = new ArrayList<>();
for(Notification notification : notifications){ for(Notification notification : notifications){
notification.setRead(true);
NotificationDto notificationDto = modelMapper.map(notification, NotificationDto.class); NotificationDto notificationDto = modelMapper.map(notification, NotificationDto.class);
notificationDtos.add(notificationDto); notificationDtos.add(notificationDto);
} }
iNotificationRepository.saveAll(notifications);
return notificationDtos; return notificationDtos;
} }
...@@ -119,6 +123,17 @@ public class NotificationService implements INotificationService { ...@@ -119,6 +123,17 @@ public class NotificationService implements INotificationService {
public boolean existById(int notificationId) { public boolean existById(int notificationId) {
return iNotificationRepository.existsById(notificationId); return iNotificationRepository.existsById(notificationId);
} }
@Override
public int getUnReadNotificationsNumberByJobseekerId(int jobseekerId) {
return iNotificationRepository.findUnreadNotificationsByJobseekerId(jobseekerId).size();
}
@Override
public int getUnReadNotificationsNumberByCompanyId(int companyId) {
return iNotificationRepository.findUnreadNotificationsByCompanyId(companyId).size();
}
private void ignoreIdMapping(ModelMapper modelMapper){ private void ignoreIdMapping(ModelMapper modelMapper){
modelMapper.getConfiguration() modelMapper.getConfiguration()
.setPropertyCondition(Conditions.isNotNull()) .setPropertyCondition(Conditions.isNotNull())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment