Commit bd5fdf7a authored by Ali's avatar Ali

add notification service

parent 05353643
......@@ -69,6 +69,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
......
package com.ali.notificationsvc.dtos;
import java.time.LocalDateTime;
public class CreateNotificationDto {
private int jobId;
private int companyId;
private int jobseekerId;
private String details;
private boolean isRead;
private boolean toCompany;
private LocalDateTime createdAt;
public CreateNotificationDto( int jobId, int companyId, int jobseekerId, String details, boolean isRead, boolean toCompany, LocalDateTime createdAt) {
this.jobId = jobId;
this.companyId = companyId;
this.jobseekerId = jobseekerId;
this.details = details;
this.isRead = isRead;
this.toCompany = toCompany;
this.createdAt = createdAt;
}
public CreateNotificationDto() {
}
}
package com.ali.notificationsvc.dtos;
import java.time.LocalDateTime;
public class NotificationDto {
private int id;
private int jobId;
private int companyId;
private int jobseekerId;
private String details;
private boolean isRead;
private boolean toCompany;
private LocalDateTime createdAt;
public NotificationDto(int id, int jobId, int companyId, int jobseekerId, String details, boolean isRead, boolean toCompany, LocalDateTime createdAt) {
this.id = id;
this.jobId = jobId;
this.companyId = companyId;
this.jobseekerId = jobseekerId;
this.details = details;
this.isRead = isRead;
this.toCompany = toCompany;
this.createdAt = createdAt;
}
public NotificationDto() {
}
}
......@@ -2,8 +2,16 @@ package com.ali.notificationsvc.repositories;
import com.ali.notificationsvc.models.Notification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface INotificationRepository extends JpaRepository<Notification, Integer> {
@Query("SELECT i FROM Notification i WHERE i.companyId = :companyId")
List<Notification> findNotificationsByCompanyId(@Param("companyId") int companyId);
@Query("SELECT i FROM Notification i WHERE i.jobseekerId = :jobseekerId")
List<Notification> findNotificationsByJobseekerId(@Param("jobseekerId") int jobseekerId);
}
package com.ali.notificationsvc.services;
import com.ali.notificationsvc.dtos.CreateNotificationDto;
import com.ali.notificationsvc.dtos.NotificationDto;
import com.ali.notificationsvc.status.RequestStatus;
import java.util.List;
public interface INotificationService {
public NotificationDto getById(int notificationId);
public List<NotificationDto> getAll();
public List<NotificationDto> getNotificationByJobseekerId(int jobseekerId);
public List<NotificationDto> getNotificationByCompanyId(int companyId);
public RequestStatus create(CreateNotificationDto cNotificationDto);
public RequestStatus update(NotificationDto notificationDto);
public RequestStatus delete(int notificationId);
public boolean existById(int notificationId);
}
package com.ali.notificationsvc.services;
import com.ali.notificationsvc.dtos.CreateNotificationDto;
import com.ali.notificationsvc.dtos.NotificationDto;
import com.ali.notificationsvc.models.Notification;
import com.ali.notificationsvc.repositories.INotificationRepository;
import com.ali.notificationsvc.status.RequestStatus;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class NotificationService implements INotificationService {
@Autowired
INotificationRepository iNotificationRepository;
private ModelMapper modelMapper = new ModelMapper();
NotificationService(INotificationRepository iNotificationRepository){
this.iNotificationRepository = iNotificationRepository;
}
@Override
public NotificationDto getById(int notificationId) {
NotificationDto notificationDto = new NotificationDto();
notificationDto = modelMapper.map(iNotificationRepository.getById(notificationId), NotificationDto.class);
return notificationDto;
}
@Override
public List<NotificationDto> getAll() {
List<Notification> notifications = iNotificationRepository.findAll();
List<NotificationDto> notificationDtos = new ArrayList<>();
for(Notification notification : notifications){
NotificationDto notificationDto = modelMapper.map(notification, NotificationDto.class);
notificationDtos.add(notificationDto);
}
return notificationDtos;
}
@Override
public List<NotificationDto> getNotificationByJobseekerId(int jobseekerId) {
List<Notification> notifications = iNotificationRepository.findNotificationsByJobseekerId(jobseekerId);
List<NotificationDto> notificationDtos = new ArrayList<>();
for(Notification notification : notifications){
NotificationDto notificationDto = modelMapper.map(notification, NotificationDto.class);
notificationDtos.add(notificationDto);
}
return notificationDtos;
}
@Override
public List<NotificationDto> getNotificationByCompanyId(int companyId) {
List<Notification> notifications = iNotificationRepository.findNotificationsByCompanyId(companyId);
List<NotificationDto> notificationDtos = new ArrayList<>();
for(Notification notification : notifications){
NotificationDto notificationDto = modelMapper.map(notification, NotificationDto.class);
notificationDtos.add(notificationDto);
}
return notificationDtos;
}
@Override
public RequestStatus create(CreateNotificationDto cNotificationDto) {
RequestStatus status = new RequestStatus();
try {
Notification notification = modelMapper.map(cNotificationDto, Notification.class);
iNotificationRepository.save(notification);
status.setStatus(true);
status.setDescribtion("created successfully");
return status;
}catch (Exception e){
status.setStatus(false);
status.setDescribtion(e.getMessage().toString());
return status;
}
}
@Override
public RequestStatus update(NotificationDto notificationDto) {
RequestStatus status = new RequestStatus();
try {
Notification notification = new Notification();
notification = modelMapper.map(notificationDto,Notification.class);
iNotificationRepository.save(notification);
status.setStatus(true);
status.setDescribtion("updated successfully");
return status;
}catch (Exception e){
status.setStatus(false);
status.setDescribtion("failed to update");
return status;
}
}
@Override
public RequestStatus delete(int notificationId) {
RequestStatus status = new RequestStatus();
if(iNotificationRepository.existsById(notificationId)){
iNotificationRepository.deleteById(notificationId);
status.setStatus(true);
status.setDescribtion("deleted successfully");
return status;
}else {
status.setStatus(false);
status.setDescribtion("failed to delete, cv does not exist");
return status;
}
}
@Override
public boolean existById(int notificationId) {
return iNotificationRepository.existsById(notificationId);
}
}
package com.ali.notificationsvc.status;
public class RequestStatus {
boolean status;
String describtion;
public RequestStatus(boolean status, String describtion) {
this.status = status;
this.describtion = describtion;
}
public RequestStatus() {
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public String getDescribtion() {
return describtion;
}
public void setDescribtion(String describtion) {
this.describtion = describtion;
}
}
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